I am developing a nagios/collectd check for time checking on each server. I have done bash script to get info from Collectd WSP files and check them against the nagios server time (date +%s
). Collectd sends the date +%s
value after 60 seconds has past from the last sending. The problem I am facing is how can I make the check command calculate that if the value is out of sync maximum 60 seconds then everything is OK, if its out of sync over 60 seconds then its critical.
The script:
Time collecting on nagios server and collectd WSP files that other servers send. Each command returns a value in epoch ($time
is taken from nagios server and $stime
is taken from collectd WSP files):
time=`date +%s`
stime=`whisper-fetch --from=$(expr $(date +%s) - 60) --until=$(date +%s) $WSP_PATH | cut -c1-10`
Critical IF statement. If this $stime
does not match to $time
got from nagios server then it sends critical. I want this to be, if nagios servers time value is out of sync over 60 seconds compared to WSP output value then its critical else its OK.
if [ "$stime" -ne "$time" ] ; then
echo CRITICAL: Server time is out of sync!
exit $STATE_CRITICAL
fi
OK statement:
echo OK: Server time is in sync
exit $STATE_OK
Subtract the two times from each other and check if the result is greater than 60. It appears that
bash
doesn't have an operator to compute absolute value, so you have to do the comparison both ways.