Languages

Nagios time check using SNMP

When you would like to retrieve the remotely configured time using SNMP and compare it to see how accurate the time is, here is a script to help you out.

This setup does not specifically require NTP to be running on the hosts that are checked, it just requires that the time is correct. Virtual machines for example are advised to have the appropriate "tools" installed to synchronize time. NTP is not desirable for virtual machines.

(Parts of the script are borrowed from http://spielwiese.la-evento.com/xelasblog/archives/27-SNMP-hrSystemDate.0-Datumsformat-anpassen.html)

This is the graph that is created:

The script:

#!/bin/sh

# Nagios plugin to report time difference as received via SNMP compared to the local time.# Make sure the machine this script runs on (poller/nagios host) is using NTP.

checkvariables() { # This function checks if all collected input is correct. if [ ! "$host" ] ; then echo "Please specify a hostname or IP address." echo usage fi if [ ! "$community" ] ; then # The public community is used when a user did not enter a community. community="public" fi if [ ! "$version" ] ; then # Version 2c is used when a user did not enter a version. version="2c" fi if [ ! "$critical" ] ; then critical="120" fi if [ ! "$warning" ] ; then warning="60" fi}

# If the calculated difference is negative, make it positive again for comparison. difference=$(($rdate_s - $ldate_s)) if [ "$difference" -lt 0 ] ; then positivedifference=$(($difference*-1)) else positivedifference=$difference fi

I'm sorry but the idea behind this nagios check is terribly wrong:
1)-the time between the moment you get the SNMP response and the moment you calculate the difference is not taken in account, therefore, the value of the variable "difference" could never be correct.
2)-if the monitoring host's clock is not correct or drifting (e.g. ntp connectivity issue), all your services checks will be in error
3)-this is bash, you cannot relay on an interpreted script for high res. time calculations
If the goal is to be "accurate" I would definitly not recommend this in production environments.
I would advice to check the NTP sync, precision of your hosts rather than using the monitor's clock as reference.