I am trying to run a check of a local certificate file - not doing an SSL/HTTPS/443 check on an active port. The check script in the remote machine is using openssl to validate the certificate lifespan. Below is the script and it works as expected when run as root locally in the target machine.
Code: Select all
#!/bin/bash
CERT="/cert/location/cert.pem"
#WARN_DAYS="$1"
#CRIT_DAYS="$2"
#Nagios wants args...
while getopts ":w:c:" opt; do
case $opt in
w)
WARN_DAYS=$OPTARG
;;
c)
CRIT_DAYS=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 3
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 3
;;
esac
done
# Calculate when cert expires
DAYS_LEFT=$(( ($(/usr/bin/date -d "$(/usr/bin/openssl x509 -inform pem -in $CERT -enddate -noout | /usr/bin/cut -d= -f 2)" +%s) - $(/usr/bin/date +%s)) / 86400 ))
# Check expiration and output
[ $DAYS_LEFT -le $CRIT_DAYS ] && { echo "CRITICAL: Certificate expires in $DAYS_LEFT days!"; exit 2; }
[ $DAYS_LEFT -le $WARN_DAYS ] && { echo "WARNING: Certificate expires in $DAYS_LEFT days!"; exit 1; }
echo "OK: Certificate is valid for $DAYS_LEFT more days."
exit 0
Code: Select all
./check_cert.sh -w 178 -c 7
WARNING: Certificate expires in 177 days!
Code: Select all
/usr/bin/ssh -x nagios-user@$HOSTADDRESS$ "/var/lib/nagios-user/check_cert.sh -w $ARG1$ -c $ARG2$" 2>/dev/null
Code: Select all
$ /usr/bin/ssh -x nagios-user@hostname "/var/lib/nagios-user/check_cert.sh -w 178 -c 7" 2>/dev/null
CRITICAL: Certificate expires in 0 days!
Below are permissions on the check script:
Code: Select all
-rwxr-xr-x. 1 nagios-user nagios-user 909 Sep 28 09:51 check_cert.sh