Code

plugins from Patrick Greenwell
[nagiosplug.git] / contrib / check_nwstat.pl
1 #!/usr/bin/perl
2 #
3 #  check_nwstat.pl: Nagios plugin that uses Jim Drews' nwstat.pl for
4 #     MRTG instead of emulating it.  For use particularly with Cliff
5 #     Woolley's mrtgext.pl Unix companion to Drews' MRTGEXT.NLM, where
6 #     mrtgext.pl can contain custom commands that check_nwstat won't recognize,
7 #     though this also does its best to perfectly emulate the C version
8 #     of check_nwstat.
9 #
12 ######################################################################
13 #  Configuration
14 ######################################################################
16 $nwstatcmd = "/apps/mrtg/helpers/nwstat.pl";
18 use Getopt::Long;
20 $::host      = shift || &usage(%ERROR);
21 $::opt_v     = undef;
22 $::opt_wv    = undef;
23 $::opt_cv    = undef;
24 $::opt_to    = 10;
25 $::opt_url   = undef;
27 GetOptions (qw(v=s wv=i cv=i to=i url=s)) || &usage(%ERROR);
29 my $cmd1     = "";
30 my $cmd2     = "ZERO";
31 my $backward = 0;
32 my $desc     = "";
33 my $okstr    = "OK";
34 my $probstr  = "Problem";
35 my $result   = "";
36 my @CMD;
37 my %ERROR    = ("UNKNOWN"   => -1,
38                 "OK"        =>  0,
39                 "WARNING"   =>  1,
40                 "CRITICAL"  =>  2);
41 my $status   = $ERROR{"OK"};
44 ######################################################################
45 #  Main program
46 ######################################################################
48 $SIG{'ALRM'} = sub { 
49     print "Connection timed out\n";
50     exit $ERROR{"CRITICAL"};
51 };
53 # translate table for compatability with
54 # check_nwstat (C version)
55 SWITCH: for ($::opt_v) {
56            /^LOAD(1|5|15)$/
57                        && do { $desc = "Load <status> - Up <cmd2>, ".
58                                        "$1-min load average = <cmd0>%";
59                                $cmd1 = "UTIL$1";  last; };
60            /^CONNS$/   && do { $desc = "Conns <status>: ".
61                                        "<cmd0> current connections";
62                                $cmd1 = "CONNECT"; last; };
63            /^CDBUFF$/  && do { $desc = "Dirty cache buffers = <cmd0>";
64                                $cmd1 = "S3";      last; };
65            /^LTCH$/    && do { $desc = "Long term cache hits = <cmd0>%";
66                                $cmd1 = "S1";
67                                $backward = 1;     last; };
68            /^CBUFF$/   && do { $desc = "Total cache buffers = <cmd0>";
69                                $cmd1 = "S2";
70                                $backward = 1;     last; };
71            /^LRUM$/    && do { $desc = "LRU sitting time = <cmd0> minutes";
72                                $cmd1 = "S5";
73                                $backward = 1;     last; };
74            /^VPF(.*)$/ && do { $desc = "<status><int(cmd0/1024)> MB ".
75                                        "(<result>%) free on volume $1";
76                                $okstr = ""; $probstr = "Only ";
77                                $cmd1 = "VKF$1";
78                                $cmd2 = "VKS$1";
79                                $backward = 1;     last; };
80            /^VKF/      && do { $desc = "<status><cmd0> KB free on volume $1";
81                                $okstr = ""; $probstr = "Only ";
82                                $cmd1 = "$::opt_v";
83                                $backward = 1;     last; };
84            /^$/        && die "Nothing to check!";
85            $desc = "<status>: <cmd0>";
86            $cmd1 = "$::opt_v";
87         }
90 # begin timeout period, run the check
91 alarm($::opt_to);
92 open  ( CMD, "$nwstatcmd $host $cmd1 $cmd2|" ) || die "Couldn't execute nwstat";
93 @CMD = <CMD>;
94 close ( CMD );
95 alarm(0);
97 for (@CMD) { chomp; }
99 # for any variables that manipulate the results instead of
100 # just using <cmd0> directly, do that manipulation here into <result>
101 SWITCH: for ($::opt_v) {
102            /^VPF/       && do { $result=int(("$CMD[0]"/"$CMD[1]")*100); last; };
103            $result = "$CMD[0]";
104         }
106 if ("$result" == -1) {
107    $status = $ERROR{"UNKNOWN"};
108    $desc = "Server returned \"variable unknown\"";
109 } elsif ("$result" == -2) {
110    $status = $ERROR{"CRITICAL"};
111    $desc = "Connection failed";
114 if (defined($::opt_cv) && $status == $ERROR{"OK"}) {
115    if ($backward) {
116       ("$result" <= "$::opt_cv") && ( $status = $ERROR{"CRITICAL"} );
117    } else {
118       ("$result" >= "$::opt_cv") && ( $status = $ERROR{"CRITICAL"} );
119    }
121 if (defined($::opt_wv) && $status == $ERROR{"OK"}) {
122    if ($backward) {
123       ("$result" <= "$::opt_wv") && ( $status = $ERROR{"WARNING"} );
124    } else {
125       ("$result" >= "$::opt_wv") && ( $status = $ERROR{"WARNING"} );
126    }
129 $desc =~ s/<status>/($status == $ERROR{"OK"})?"$okstr":"$probstr"/eg;
130 $desc =~ s/<([^>]*)cmd([0-3])([^>]*)>/eval("$1\"$CMD[$2]\"$3")/eg;
131 $desc =~ s/<result>/"$result"/eg;
133 if (defined($::opt_url)) {
134    print "<A HREF=\"$::opt_url\">$desc</A>\n";
135 } else {
136    print "$desc\n";
138 exit $status;
141 ######################################################################
142 #  Subroutines
143 ######################################################################
145 sub usage {
147     %ERROR = shift;
149     print <<EOF
150 check_nwstat.pl plugin for Nagios
151 by Cliff Woolley, (c) 2000
153 Usage: ./check_nwstat.pl <host_address> [-v variable] [-wv warn_value] [-cv crit_value] [-to to_sec] [-url url_value]
155 Options:
156  [variable]   = Variable to check.  Valid variables include:
157                   LOAD1    = 1 minute average CPU load
158                   LOAD5    = 5 minute average CPU load
159                   LOAD15   = 15 minute average CPU load
160                   CONNS    = number of currently licensed connections
161                   VPF<vol> = percent free space on volume <vol>
162                   VKF<vol> = KB of free space on volume <vol>
163                   LTCH     = percent long term cache hits
164                   CBUFF    = current number of cache buffers
165                   CDBUFF   = current number of dirty cache buffers
166                   LRUM     = LRU sitting time in minutes
167  [warn_value] = Threshold for value necessary to result in a warning status
168  [crit_value] = Threshold for value necessary to result in a critical status
169  [to_sec]     = Number of secs before connection times out - default is 10 sec
170  [url_value]  = URL to use in output as a hyperlink.  Useful to link to a page
171                 with more details or history for this variable (ie an MRTG page)
173 This plugin attempts to contact the MRTGEXT NLM running on a Novell server
174 to gather the requested system information.
176 Notes:
177  - This plugin requres that the MRTGEXT.NLM file distributed with
178    James Drews' MRTG extension for NetWare (available from
179    http://www.engr.wisc.edu/~drews/mrtg/) be loaded on the Novell
180    servers you wish to check.
181  - Critical thresholds should be lower than warning thresholds when
182    the following variables are checked: VPF, VKF, LTCH, CBUFF, and LRUM.
183 EOF
186     exit $ERROR{"UNKNOWN"};