#!/usr/bin/perl -w
#
# Description:
# this plugin calculate the difference between the last logged MRTG value and a another one in the past and send alerts to Nagios should this difference be greater than threshold values.
#
# Usage:
# check_mrtg_diff.pl [-v] -l <log_file> -w <warning_threshold> -c <critical_threshold> [-V]
# Description:
#
# Licence:
# 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author: Cyrille Bollu cyrille.bollu@tiscali.net
# Last Changed: $LastChangedDate: 200R6-04-12 12:50:14 +0100 (Wen, 12 Apr 2006) $
# Revision:  $Rev: 1 $

use strict;
use Getopt::Long;
use Date::Manip;

use utils qw(%ERRORS);

# Globals

my $Version='0.1';

my $o_log=      undef;          # Log file
my $o_crit=     undef;          # critical threshold
my $o_warn=     undef;          # warning threshold
my $o_help=     undef;          # wan't some help ?
my $o_verb=     undef;          # verbose mode
my $o_version=  undef;          # print version

my $now=	undef;		# Actual value
my $ref=	undef;		# Reference
my $diff=	undef;		# Absolute difference between actual and reference
my $date=	undef;		# to read date from log file
my $logv=	undef;		# to read value from log file

# functions

sub p_version { print "check_mrtg_diff.pl version : $Version\n"; }

sub print_usage {
    print "Usage: $0 [-v] -l <log_file> -w <warning_threshold> -c <critical_threshold> [-V]\n";
}

sub help {
   print "\nNagios plugin to monitor variation in MRTG monitored data\n";
   print "(c)2006 - Author: Cyrille Bollu\n\n";
   print_usage();
   print <<EOT;
-v, --verbose
   print extra debugging information (including interface list on the system)
-h, --help
   print this help message
-l, --log_file
   name of the log file to analyse
-w, --warning
   warning threshold
-c, --critical
   critical threshold
-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::Configure ("bundling");
    GetOptions(
        'v'     => \$o_verb,            'verbose'       => \$o_verb,
        'h'     => \$o_help,            'help'          => \$o_help,
        'l:s'   => \$o_log,             'log_file:s'    => \$o_log,
        'c:i'     => \$o_crit,          'critical:i'    => \$o_crit,
        'w:i'     => \$o_warn,          'warning:i'     => \$o_warn,
        'V'     => \$o_version,         'version'       => \$o_version,
    );
    if (defined ($o_help) ) { help(); exit $ERRORS{"UNKNOWN"}};
    if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}};
    if ( ! defined($o_log) ) # check log file and filter
        { print "No log file defined!\n";print_usage(); exit $ERRORS{"UNKNOWN"}}
    if ( ! defined($o_warn) ) # check warning threshold and filter
        { print "No warning threshold defined!\n";print_usage(); exit $ERRORS{"UNKNOWN"}}
    if ( ! defined($o_crit) ) # check critical threshold and filter
        { print "No critical threshold defined!\n";print_usage(); exit $ERRORS{"UNKNOWN"}}

}

sub get_values {
    open(LOG,$o_log) or exit $ERRORS{"UNKNOWN"};

    while (<LOG>) {
        s/(\d*) (\d*) \d* \d* \d*/$1 $2/s;
        ($date,$logv) = split;
        # Get the actual value
        if ($. == 2) {
            $now = $logv;
            verb("Actual value: $now");
        } 
        if ($date == &UnixDate("today 00:00:00","%s")) {
            # Get the reference and exit
            $ref = $logv;
            verb("Reference: $ref");
	    close(LOG);
            last
        }
    }
}
    
########## MAIN #######

check_options();

get_values();

# Analyse result and send result to Nagios
$diff = $now-$ref;
verb("Difference: $diff");
if ($diff<$o_crit) {
    print "disk space decreased in more than $o_crit bytes";
    exit $ERRORS{"CRITICAL"};
}
if ($diff<$o_warn) {
    print "disk space decreased in more than $o_warn bytes";
    exit $ERRORS{"WARNING"};
}
if ($diff>$o_warn) {
    print "disk space changed by $diff bytes";
    exit $ERRORS{"OK"};
}
