#!/usr/bin/perl -w 
# nagios: -epn
############################## check_snmp_time.pl #################
my $Version='1.1';
# Date    : Dec 08 2010
# Purpose : Nagios plugin to check the time on a server using SNMP.\n";
# Author  : Karl Bolingbroke, 2007
# Updated : Frank Migge (support at frank4dd dot com)
# Help    : http://www.frank4dd.com/howto
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
#################################################################
#
# Help : ./check_snmp_time.pl -h
#
# This plugin queries the remote systems time through SNMP and compares
# it against the local time on the Nagios server. This identifies systems
# with no correct time set and sends alarms if the time is off to far.
# HOST-RESOURCES-MIB::hrSystemDate.0 used here returns 8 or 11 byte octets.
# SNMP translation needs to be switched off and we need to convert the
# received SNMP data into readable strings.
#
# snmpget example data on Windows 2003
# susie112:~ > snmpget -v 1 -c SECro 192.168.100.21 1.3.6.1.2.1.25.1.2.0
# HOST-RESOURCES-MIB::hrSystemDate.0 = STRING: 2010-12-10,14:27:36.5
# example data on Linux 2.6
# susie112:~ > snmpget -v 1 -c SECro 192.168.103.32 1.3.6.1.2.1.25.1.2.0
# HOST-RESOURCES-MIB::hrSystemDate.0 = STRING: 2010-12-10,14:27:44.0,+9:0
# example data on AIX 6.1
# susie112:~ > snmpget -v 1 -c SECro 192.168.98.109 1.3.6.1.2.1.25.1.2.0
# HOST-RESOURCES-MIB::hrSystemDate.0 = STRING: 2010-12-10,14:27:59.0


use strict;
use Net::SNMP 5.0;
use Getopt::Long;
use Date::Format;
use Time::Local;

# Nagios specific

my $TIMEOUT = 15;
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);

# HOST-RESOURCES-MIB::hrSystemDate.0 OID
my $remote_time_oid   = '1.3.6.1.2.1.25.1.2.0';

# Globals
my $o_host = 	undef; 		# hostname
my $o_community = undef; 	# community
my $o_port = 	161; 		# port
my $o_help=	undef; 		# wan't some help ?
my $o_verb=	undef;		# verbose mode
my $o_version=	undef;		# print version
# End compatibility
my $o_tzoff=	0;		# remote TZ offset in mins
my $o_warn=	undef;		# warning level in seconds
my $o_crit=	undef;		# critical level in seconds
my $o_timeout=  undef; 		# Timeout (Default 5)
my $o_perf=     undef;          # Output performance data
my $o_version2= undef;          # use snmp v2c
# SNMPv3 specific
my $o_login=	undef;		# Login for snmpv3
my $o_passwd=	undef;		# Pass for snmpv3
my $v3protocols=undef;	        # V3 protocol list.
my $o_authproto='md5';		# Auth protocol
my $o_privproto='des';		# Priv protocol
my $o_privpass= undef;		# priv password

# functions

sub p_version { print "check_snmp_time version : $Version\n"; }

sub print_usage {
    print "Usage: $0 [-v] -H <host> -C <snmp_community> [-2] | (-l login -x passwd [-X pass -L <authp>,<privp>])  [-p <port>] -w <warn level> -c <crit level> [-f] [-t <timeout>] [-V]\n";
}

sub isnnum { # Return true if arg is not a number
  my $num = shift;
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$|^-(\d+\.?\d*)|(^-\.\d+)$/ ) { return 0 ;}
  return 1;
}

sub help {
   print "\nRemote SNMP System Time Monitor for Nagios version ",$Version,"\n";
   print "GPL licence, (c) 2007 Karl Bolingbroke, update (c)2010 Frank Migge\n\n";
   print_usage();
   print <<EOT;

This plugin queries the remote systems time through SNMP and compares it against the local time on the Nagios server. This identifies systems with no correct time set and sends alarms if the time is off to far.

-v, --verbose
   print extra debugging information 
-h, --help
   print this help message
-H, --hostname=HOST
   name or IP address of host to check
-C, --community=COMMUNITY NAME
   community name for the host's SNMP agent (implies v1 protocol)
-2, --v2c
   Use snmp v2c
-l, --login=LOGIN ; -x, --passwd=PASSWD
   Login and auth password for snmpv3 authentication 
   If no priv password exists, implies AuthNoPriv 
-X, --privpass=PASSWD
   Priv password for snmpv3 (AuthPriv protocol)
-L, --protocols=<authproto>,<privproto>
   <authproto> : Authentication protocol (md5|sha : default md5)
   <privproto> : Priv protocole (des|aes : default des) 
-P, --port=PORT
   SNMP port (Default 161)
-o, --tzoffset=MINS
   the remote systems timezone offset to the Nagios server, in minutes
-w, --warn=INTEGER
   warning level for time difference in seconds
-c, --crit=INTEGER
   critical level for time difference in seconds
-f, --perfparse
   Perfparse compatible output
-t, --timeout=INTEGER
   timeout for SNMP in seconds (Default: 5)
-V, --version
   prints version number
EOT
}

# For verbose output
sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }

sub check_options {
    Getopt::Long:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      