#!/usr/bin/perl -w

# check_timestamp.pl Copyright (c) 2008 CARNet <riss@carnet.hr>
#
# Checks the time on the remote machine using ICMP Timestamp packets.
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA

use strict;
use Getopt::Long;
use POSIX;
use lib "/usr/lib/nagios/plugins";
use utils qw (%ERRORS &support);

my $name = 'TIMESTAMP';
my $hostname;
my $warn_offset = 5;
my $crit_offset = 60;
my $want_help = 0;
my $verbose = 0;

Getopt::Long::Configure(qw(bundling no_ignore_case));
my $res = GetOptions(
    "help|h" => \$want_help,
    "hostname|H=s" => \$hostname,
    "warning-offset|w=i" => \$warn_offset,
    "critical-offset|c=i" => \$crit_offset,
    "verbose|v+" => \$verbose,
);

if ($want_help or !$res) {
    print_help();
    exit $ERRORS{$res ? 'OK' : 'UNKNOWN'};
}

if (!$hostname) { # required parameter
    print "Hostname not set.\n\n";
    print_help();
    exit $ERRORS{'UNKNOWN'};
}

# get the time offset from the remote node
my $command = "/usr/bin/sing -c 1 -tstamp $hostname 2>&1";
print "CMD: $command\n" if $verbose >= 2;
my $output = `$command` || '';
print map({ "Output: $_\n" } split(/\n/, $output)) if $verbose >= 3;
nagios_exit(UNKNOWN => "Command failed ("
                       . (WIFEXITED($?) ? "status " . WEXITSTATUS($?)
                                        : "signal " . WTERMSIG($?)) . ")")
    if $?;

# extract offset from output
my ($offset) = $output =~ m/ diff=-?(\d+)$/m;
nagios_exit(UNKNOWN => "Cannot parse output") if !defined($offset);
my $offset_sec = $offset/1000;

# check offset
my $status = 'OK';
if ($offset_sec >= $crit_offset) {
    $status = 'CRITICAL'; 
} elsif ($offset_sec >= $warn_offset) {
    $status = 'WARNING';
}

# report findings
nagios_exit($status => sprintf("%g second time difference|offset=%.3fs;%.3f;%.3f;0",
    $offset_sec, $offset_sec, $warn_offset, $crit_offset)
);

# output the result and exit plugin style
sub nagios_exit {
    my ($result, $msg) = @_;

    print "$name $result - $msg\n";
    exit $ERRORS{$result};
}

sub print_help {
    print <<EOH;
check_timestamp 0.1
Copyright (c) 2008 CARNet <riss\@carnet.hr>

This plugin checks the time on the remote machine using ICMP Timestamp packets.


Usage:
  check_timestamp -H <host_address> [-w offset] [-c offset]

Options:
 -h, --help
    Print detailed help screen
 -H, --hostname=ADDRESS
    Host name or IP address
 -w, --warning-offset=INTEGER
   Time difference (sec.) necessary to result in a warning status
 -c, --critical-offset=INTEGER
   Time difference (sec.) necessary to result in a critical status
 -v, --verbose
   Show details for command-line debugging (Nagios may truncate output)

EOH

    support();
}

# vim:sw=4:ts=4:et
