#!/usr/bin/perl -wT
#
#  check_nwstat.pl: Nagios plugin that uses Jim Drews' nwstat.pl for
#     MRTG instead of emulating it.  For use particularly with Cliff
#     Woolley's mrtgext.pl Unix companion to Drews' MRTGEXT.NLM, where
#     mrtgext.pl can contain custom commands that check_nwstat won't recognize,
#     though this also does its best to perfectly emulate the C version
#     of check_nwstat.
#


######################################################################
#  Configuration
######################################################################

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :thresholds);

use vars qw($opt_H $opt_v $opt_w $opt_c $opt_t $opt_url $PROGNAME);

use utils qw(%ERRORS);

my $var_opt = new Plugin::Parameter(-name => "variable", -flags => [ 'v', 'variable' ],
				    -optional => "no", -valueoptional => "no", -type => "VARIABLE",
				    -description => "Variable to check. Valid variables include: LOAD1 = 1 minute average CPU load, LOAD5 = 5 minute average CPU load, LOAD15 = 15 minutes average CPU load, CONNS = number of currently licensed connections, VPF<vol> = percent free space on volume <vol>, VKF<vol> = KB of free space on volume <vol>, LTCH = percent long term cache hits, CBUFF = current number of cache buffers, CDBUFF = current number of dirty cache buffers, LRUM = LRU sitting time in minutes");
$w_opt->description("Threshold for value necessary to result in a warning status");
$c_opt->description("Threshold for value necessary to resul tin a critical status");
$t_opt->flags([ 't', 'to', 'timeout' ]);
my $url_opt = new Plugin::Parameter(-name => "url", -flags => [ 'url' ],
				    -optional => "yes", -valueoptional => "no", -type => "URL",
				    -description => "URL to use in output as a hyperlink. Useful to link to a page with more details or history for this variable (e.g. a MRTG page)");
my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1 $',
			-copyright => "2000 Cliff Woolley, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "This plugin attempts to contact the MRTGEXT NLM running on a Novell server to gather the requested system information.",
			-longcomment => "Notes: This plugin requres that the MRTGEXT.NLM file distributed with James Drews' MRTG extension for NetWare (available from http://www.engr.wisc.edu/~drews/mrtg/) be loaded on the Novell servers you wish to check. Critical thresholds should be lower than warning thresholds when the following variables are checked: VPF, VKF, LTCH, CBUFF, and LRUM.",
			-parameterlists => [ [ $H_opt, $var_opt, $w_opt, $c_opt, $t_opt, $url_opt ], $h_opts, $V_opts ]);

my $nwstatcmd = "/apps/mrtg/helpers/nwstat.pl";

$plugin->init();

my $cmd1     = "";
my $cmd2     = "ZERO";
my $backward = 0;
my $desc     = "";
my $okstr    = "OK";
my $probstr  = "Problem";
my $result   = "";
my @CMD;

my $status   = 'OK';


######################################################################
#  Main program
######################################################################

# translate table for compatability with
# check_nwstat (C version)
SWITCH: for ($opt_v) {
           /^LOAD(1|5|15)$/
                       && do { $desc = "Load <status> - Up <cmd2>, ".
                                       "$1-min load average = <cmd0>%";
                               $cmd1 = "UTIL$1";  last; };
           /^CONNS$/   && do { $desc = "Conns <status>: ".
                                       "<cmd0> current connections";
                               $cmd1 = "CONNECT"; last; };
           /^CDBUFF$/  && do { $desc = "Dirty cache buffers = <cmd0>";
                               $cmd1 = "S3";      last; };
           /^LTCH$/    && do { $desc = "Long term cache hits = <cmd0>%";
                               $cmd1 = "S1";
                               $backward = 1;     last; };
           /^CBUFF$/   && do { $desc = "Total cache buffers = <cmd0>";
                               $cmd1 = "S2";
                               $backward = 1;     last; };
           /^LRUM$/    && do { $desc = "LRU sitting time = <cmd0> minutes";
                               $cmd1 = "S5";
                               $backward = 1;     last; };
           /^VPF(.*)$/ && do { $desc = "<status><int(cmd0/1024)> MB ".
                                       "(<result>%) free on volume $1";
                               $okstr = ""; $probstr = "Only ";
                               $cmd1 = "VKF$1";
                               $cmd2 = "VKS$1";
                               $backward = 1;     last; };
           /^VKF/      && do { $desc = "<status><cmd0> KB free on volume $1";
                               $okstr = ""; $probstr = "Only ";
                               $cmd1 = "$::opt_v";
                               $backward = 1;     last; };
           /^$/        && die "Nothing to check!";
           $desc = "<status>: <cmd0>";
           $cmd1 = "$::opt_v";
        }


# begin timeout period, run the check
$plugin->start_timeout($opt_t, "Connection timed out");

$ENV{PATH} = "";
$ENV{BASH_ENV} = "";

open  ( CMD, "$nwstatcmd $opt_H $cmd1 $cmd2|" ) || die "Couldn't execute nwstat";
@CMD = <CMD>;
close ( CMD );

$plugin->stop_timeout();

for (@CMD) { chomp; }

# for any variables that manipulate the results instead of
# just using <cmd0> directly, do that manipulation here into <result>
SWITCH: for ($opt_v) {
           /^VPF/       && do { $result=int(("$CMD[0]"/"$CMD[1]")*100); last; };
           $result = "$CMD[0]";
        }

if ("$result" == -1) {
   $status = "UNKNOWN";
   $desc = "Server returned \"variable unknown\"";
} elsif ("$result" == -2) {
   $status = "CRITICAL";
   $desc = "Connection failed";
}

if (defined($opt_c) && $status == 'OK') {
   if ($backward) {
      ("$result" <= "$opt_c") && ( $status = "CRITICAL" );
   } else {
      ("$result" >= "$opt_c") && ( $status = "CRITICAL" );
   }
}
if (defined($opt_w) && $status == 'OK') {
   if ($backward) {
      ("$result" <= "$opt_w") && ( $status = "WARNING" );
   } else {
      ("$result" >= "$opt_w") && ( $status = "WARNING" );
   }
}

$desc =~ s/<status>/($status == 'OK')?"$okstr":"$probstr"/eg;
$desc =~ s/<([^>]*)cmd([0-3])([^>]*)>/eval("$1\"$CMD[$2]\"$3")/eg;
$desc =~ s/<result>/"$result"/eg;

if (defined($opt_url)) {
   print "$PROGNAME $status: <A HREF=\"$opt_url\">$desc</A>\n";
} else {
   print "$PROGNAME $status: $desc\n";
}
exit $ERRORS{status};
