HEX
Server: LiteSpeed
System: Linux s3604.bom1.stableserver.net 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
User: dmstechonline (1480)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //lib64/nagios/plugins/check_a2_le_certs.sh.saltmaster1
#!/bin/bash

le_cert_dirs=(le_certs le_certs_dev le_certs_ernie supercp_le_certs)
le_epoch_time=$(date +%s)
le_hours_min=600 # 600 hours = 25 days

exit_code=0

for cert_dir in "${le_cert_dirs[@]}"; do 
  le_certs=$(ls -1 /var/cache/"${cert_dir}" | grep pem | grep -v privkey)
  for cert in ${le_certs}; do 
    cert_path="/var/cache/${cert_dir}/${cert}"
    if [ -e "$cert_path" ]; then
      le_expiry_epoch=$(date -d "$(openssl x509 -enddate -noout -in "$cert_path" | cut -d= -f2)" +%s)
      le_life=$(( le_expiry_epoch - le_epoch_time ))
      le_life_hours=$(( le_life / 3600 ))
      
      if [ "$le_life_hours" -lt "$le_hours_min" ]; then
        echo "CRITICAL - ${cert_path} expires in ${le_life_hours} hours | lifetime=${le_life_hours}h;${le_hours_min};0;0"
        exit_code=2
      else
        echo "OK - ${cert_path} expires in ${le_life_hours} hours | lifetime=${le_life_hours}h;${le_hours_min};0;0"
      fi
    else
      echo "UNKNOWN - Certificate ${cert_path} not found"
      exit_code=3
    fi
  done
done

exit $exit_code