check_nwstat.pl 6.5 KB

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