#!/usr/bin/perl
#
# Nagios check script to check data in cricket rrd files
# Written by Dan Rich (drich@employees.org)
#
# usage: 
#    check_cricket_data -R rrdfile -i dsindex -c crit -w warn 
#
# Read a cricket-generated rrd file and return the status of the queried
# data
#
# License: GPL
# $Id: check_cricket_data,v 1.2 2003/06/05 15:27:20 drich Exp $

use strict;
use Getopt::Long;
use IPC::Open3;
use Symbol;
use vars qw($opt_V $opt_h $opt_H $opt_c $opt_w $opt_R $opt_i $verbose $host $state $PROGNAME);
use lib "/var/nagios/libexec" ;
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);

# rrdtool path
my $rrdtool = "/var/cricket/bin/rrdtool";

$PROGNAME = "check_cricket_data";

Getopt::Long::Configure('bundling');
GetOptions
        ("V"   => \$opt_V,   "version"    => \$opt_V,
         "c=s" => \$opt_c,   "critical=s" => \$opt_c,
         "w=s" => \$opt_w,   "warning=s"  => \$opt_w,
         "h"   => \$opt_h,   "help"       => \$opt_h,
	 "R=s" => \$opt_R,   "rrdfile=s"  => \$opt_R,
	 "i=s" => \$opt_i,   "index=s"    => \$opt_i,
         "v"   => \$verbose, "verbose"    => \$verbose);

if ($opt_V) {
        print_revision($PROGNAME,'$Revision: 1.2 $');
        exit $ERRORS{'OK'};
}

if ($opt_h) {print_help(); exit $ERRORS{'OK'};}

unless ($opt_c and $opt_w and $opt_R and $opt_i) {
  &print_usage;
  exit $ERRORS{'UNKNOWN'};
}

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
        print "No Answer from Client\n";
        exit 2;
};
alarm($TIMEOUT);


# Set default return
my $status = $ERRORS{"UNKNOWN"};

# Get data from rrd file
my $RDR = gensym();
my $ERR = gensym();
my $WTR = gensym();

# Get the timestamp of the last data in the rrd file
#print "$rrdtool last $opt_R\n";
open3($WTR, $RDR, $ERR, "$rrdtool last $opt_R");
close($WTR);
my $last = <$RDR>;
chomp($last);
if ($last <= 0) {
  printf "CRITICAL: ". <$ERR>;
  exit $ERRORS{'CRITICAL'};
}
close($RDR);
close($ERR);

# Get data
#print "$rrdtool fetch $opt_R AVERAGE -s $last -e $last\n";
open3($WTR, $RDR, $ERR, "$rrdtool fetch $opt_R AVERAGE -s $last -e $last");
close($WTR);

my @data;
while (<$RDR>) {
  chomp;
  my ($timestamp,$data) = split(/:/,$_,2);
  next unless ($timestamp =~ /^\d+$/);		# We only want data line
  @data = split(/\s+/,$data);
  last;
}
close($RDR);
close($ERR);

# Convert from exp format to integer
my $result =  $data[$opt_i];
if ($result =~ /\d+/) {
  #print $result ."\n";
  my ($int,$exp) = split(/e/,$result);
  #print "$int * 10^$exp\n";
  $result = $int * 10**$exp;
  #print $data[$opt_i] ." - $result\n";
}

# Check for warning/crit
if ( $result  > $opt_c ) {
  print "CRITICAL: rrdtool returned ". $result ."\n";
  exit $ERRORS{'CRITICAL'};
} elsif ( $result  > $opt_w or $result !~ /\d+/) {
  print "WARNING: rrdtool returned ". $result ."\n";
  exit $ERRORS{'WARNING'};
} else {
  print "rrdtool returned ". $result ."\n";
  exit $ERRORS{'OK'};
}

sub print_usage () {
        print "Usage:
   $PROGNAME [--verbose]
   $PROGNAME --help
   $PROGNAME --version
   $PROGNAME -R rrdfile -i dsindex -c critical -w warning
";
}

sub print_help () {
        print_revision($PROGNAME,'$Revision: 1.2 $');
        print "Copyright (c) 2002 Daniel Rich

Read data from an rrd file and return the result

";
        print_usage();
        print "
-v, --verbose
   Print some extra debugging information (not advised for normal operation)
-V, --version
   Show version and license information
-h, --help
   Show this help screen
-w, --warning=INTEGER
   load / performance level at which a warning message will be gererated.
-c, --critical=INTEGER
   load / performance level at which a critical message will be gererated.
-R, --rrdfile=FILE
   RRD file to read
-i, --index=INTEGER
   index of data set to check

";
        support();
}

