#!/usr/bin/perl
 
###  check_rrd.pl
 
# a Nagios plugin for querying values in rrd databases using the Nagios::Plugin modules.
 
# Originally by Olivier Jan based on Nagios::Plugin example, ojan at expertise-online dot net
# February 27 2008

# Version 1.2 2008-04-01 : --label option added, cleanup and various bugs fix
# Version 1.1 2008-03-01 : --unit option added

 
# $Id: check_rrd 1539 2008-02-27 21:48:22Z ojan $
 
##############################################################################
# prologue
use strict;
use warnings;

use Nagios::Plugin ;
 
use vars qw($VERSION $PROGNAME  $verbose $warn $critical $timeout $rrd $dsname $result $unit $label);
'$Revision: 1539 $' =~ /^.*(\d+.\d+) \$$/;  # Use The Revision from RCS/CVS/Subversion
$VERSION = $1;
 
# get the base name of this script
use File::Basename;
$PROGNAME = basename($0);
 
 
##############################################################################
# define and get the command line options.
#   see the command line option guidelines at
#   http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS
 
 
# Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory)
my $p = Nagios::Plugin->new(
    usage => "Usage: %s [ -v|--verbose ]  [-H <host>] [-t <timeout>]
    [ -F|--rrd = <rrd database file and path> ]
    [ -D|--dsname=<ds name in rrd> ]
    [ -c|--critical=<critical threshold> ]
    [ -w|--warning=<warning threshold> ]
    [ -u|--unit=<unit value> ]
    [ -l|--label=<unit value> ]",
    version => $VERSION,
    blurb => 'This plugin queries values in rrd databases. It is written in Perl using the Nagios::Plugin modules.  It accepts path and name of the rrd database to query',
 
        extra => "
 
THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max'
(or 'max'). If specified '\@min:max', a warning status will be generated
if the count *is* inside the specified range.
 
See more threshold examples at http
  : // nagiosplug
  . sourceforge
  . net / developer-guidelines
  . html    #THRESHOLDFORMAT
 
  Examples:
 
  $PROGNAME -w 10 -c 18 Returns a warning
  if the resulting number is greater than 10,
  or a critical error
  if it is greater than 18.
 
  $PROGNAME -w 10 : -c 4 : Returns a warning
  if the resulting number is less than 10,
  or a critical error
  if it is less than 4.
 
  "
);
 
 
# Define and document the valid command line options
# usage, help, version, timeout and verbose are defined by default.
 
$p->add_arg(
        spec => 'warning|w=s',
        help =>
qq{-w, --warning=INTEGER:INTEGER
   Minimum and maximum number of allowable result, outside of which a
   warning will be generated.  If omitted, no warning is generated.},
 
#       required => 1,
#       default => 10,
);
 
$p->add_arg(
        spec => 'critical|c=s',
        help =>
qq{-c, --critical=INTEGER:INTEGER
   Minimum and maximum number of allowable result, outside of
   which a critical will be generated. If omitted, no critical is generated.},
);
 
$p->add_arg(
        spec => 'unit|u=s',
        help =>
qq{-u, --unit=STRING
   Optionally specifies a unit to append to the value queried.},
);

$p->add_arg(
        spec => 'label|l=s',
        help =>
qq{-l, --unit=STRING
   Optionally specifies a label to preppend to the value queried.},
);
 
$p->add_arg(
        spec => 'rrd|F=s',
        required => 1,
        help =>
qq{-F, --rrd=STRING
   Specify the path and name of the rrd base to query.},
);
 
$p->add_arg(
        spec => 'dsname|D=s',
        required => 1,
        help =>
qq{-D, --dsname=STRING
   Specify the ds name to check in the rrd database.},
);
 
# Parse arguments and process standard ones (e.g. usage, help, version)
$p->getopts;
 
 
# perform sanity checking on command line options
#if ( (defined $p->opts->warning) && ($p->opts->warning < 0 || $p->opts->warning > 20) )  {
#    $p->nagios_die( " invalid number supplied for the -r option " );
#}
 
unless ( defined $p->opts->rrd || defined $p->opts->dsname ) {
        $p->nagios_die( " you didn't supply the required arguments " );
}
 
 
 
##############################################################################
# check stuff.
 
# THIS is where you'd do your actual checking to get a real value for $result
#  don't forget to timeout after $p->opts->timeout seconds, if applicable.
my $result;
my $ds_name;
my $unit_chosen;
my $label_chosen;
my $rrd_file;
    $ds_name = $p->opts->dsname;
    $rrd_file = $p->opts->rrd;
    $unit_chosen = "";
    $label_chosen = "";
if ( (defined $p->opts->unit) )  {
    $unit_chosen = $p->opts->unit;
}
if ( (defined $p->opts->label) )  {
    $label_chosen = $p->opts->label;
}

    $result = `/usr/bin/rrdpoller get $rrd_file $ds_name`;
    print " using supplied $rrd_file and $ds_name from command line \n " if $p->opts->verbose;

##############################################################################
# check the result against the defined warning and critical thresholds,
# output the result and exit
chomp $result;

$p->nagios_exit(
         return_code => $p->check_threshold($result),
         message => "$label_chosen: $result $unit_chosen"
);
