check_hprsc.pl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/perl -wT
  2. #
  3. # Copyright (c) 2000 Hugo Gayosso
  4. #
  5. # Description:
  6. # Nagios plug-in that monitors the resources on an HP-UX machine
  7. # by querying the SNMP daemon
  8. #
  9. # License: General Public License (GPL)
  10. # http://www.gnu.org/copyleft/gpl.txt
  11. #
  12. # ChangeLog
  13. #
  14. # Requirements: Perl 5.005 or higher
  15. # Variable initialization
  16. $ENV{'PATH'}="";
  17. $ENV{'ENV'}="";
  18. $ENV{'BASH_ENV'}="";
  19. if (-e "/usr/bin/snmpwalk") {
  20. $snmpwalk = "/usr/bin/snmpwalk";
  21. } elsif (-e "/usr/local/bin/snmpwalk") {
  22. $snmpwalk = "/usr/local/snmpwalk";
  23. }
  24. # HP-UX SNMP OIDs
  25. $filesystemID1_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.1";
  26. $mounted_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.3";
  27. $totalspace_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.4";
  28. $freespace_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.6";
  29. $path_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.10";
  30. $cpu_5min_OID = ".1.3.6.1.4.1.11.2.3.1.1.4";
  31. use Getopt::Long;
  32. GetOptions( "check-filesystem" => \$chk_fs,
  33. "show-filesystems" => \$show_fs,
  34. "check-filesystemID" => \$chk_fsid,
  35. "check-cpu" => \$chk_cpu,
  36. "host=s" => \$target_host,
  37. "community=s" => \$target_community,
  38. "filesystemID1=i" => \$fsid1_opt,
  39. "filesystem=s" => \$fs_opt,
  40. "warning=i" => \$warning_opt,
  41. "critical=i" => \$critical_opt);
  42. if ($chk_fs) {
  43. walk_data($snmpwalk, $target_host, $target_community, $mounted_OID );
  44. walk_data($snmpwalk, $target_host, $target_community, $totalspace_OID );
  45. walk_data($snmpwalk, $target_host, $target_community, $freespace_OID ); check_filesystem($fs_opt, $warning_opt, $critical_opt);
  46. } elsif ($show_fs) {
  47. walk_data($snmpwalk, $target_host, $target_community, $filesystemID1_OID);
  48. walk_data($snmpwalk, $target_host, $target_community, $mounted_OID );
  49. walk_data($snmpwalk, $target_host, $target_community, $path_OID);
  50. show_filesystem();
  51. } elsif ($chk_fsid){
  52. $totalspace_fsID_OID = "$totalspace_OID.$fsid1_opt";
  53. $freespace_fsID_OID = "$freespace_OID.$fsid1_opt";
  54. walk_data($snmpwalk, $target_host, $target_community, $totalspace_fsID_OID);
  55. walk_data($snmpwalk, $target_host, $target_community, $freespace_fsID_OID);
  56. check_filesystemID1($fsid1_opt, $warning_opt, $critical_opt);
  57. } elsif ($chk_cpu) {
  58. get_cpu_load($snmpwalk, $target_host, $target_community, $cpu_5min_OID);
  59. check_cpu_5min($cpu, $warning_opt, $critical_opt);
  60. } else {
  61. print "\n\nUsage:\n";
  62. print "Checking 5-min CPU Load:\n";
  63. print " $0 --check-cpu -warning <threshold> --critical <threshold> --host <yourhost> --community <SNMP community>\n\n";
  64. print "Checking local filesystem mounted on a host:\n";
  65. print " $0 --show-filesystems --host <hostname> --community <SNMP community>\n\n";
  66. print "Checking by filesystem name:\n";
  67. print " $0 --check-filesystem --filesystem </dev/vg00/lvol1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community>\n\n";
  68. print "Checking by filesystem ID:\n";
  69. print " $0 --check-filesystemID --filesystemID <filesystemID1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community>\n\n";
  70. }
  71. sub get_cpu_load {
  72. my ($snmpwalk, $target_host, $target_community, $OID) = @_;
  73. die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
  74. if ($pid) { # parent
  75. while (<SNMPWALK>) {
  76. my @snmpdata = split(/:/,$_);
  77. $cpu = $snmpdata[1]/100;
  78. }
  79. close(SNMPWALK) or warn "kid exited $?";
  80. } else { # child
  81. exec($snmpwalk,$target_host,$target_community,$OID) or die "can't exec program: $!";
  82. }
  83. }
  84. sub walk_data {
  85. #This function queries the SNMP daemon for the specific OID
  86. my ($snmpwalk, $target_host, $target_community, $OID) = @_;
  87. die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
  88. if ($pid) { # parent
  89. while (<SNMPWALK>) {
  90. $output = $_;
  91. sort_walk_data($output);
  92. }
  93. close(SNMPWALK) or warn "kid exited $?";
  94. } else { # child
  95. exec($snmpwalk,$target_host,$target_community,$OID) or die "can't exec program: $!";
  96. }
  97. }
  98. sub sort_walk_data {
  99. my ($snmp_data) = @_;
  100. @fields = split(/\./,$snmp_data);
  101. $item = $fields[8];
  102. $filesystemID1 = $fields[9];
  103. @fields2 = split(/=/,$fields[10]);
  104. # $filesystemID2 = $fields2[0];
  105. $value = $fields2[1];
  106. chomp($value);
  107. if ($value =~ /"/) {
  108. @fields3 = split(/"/,$value);
  109. $value = $fields3[1];
  110. }
  111. if ($item == 3) {
  112. $mounted{$filesystemID1} = "$value";
  113. } elsif ($item == 4) {
  114. $totalspace{$filesystemID1} = "$value";
  115. } elsif ($item == 6) {
  116. $freespace{$filesystemID1} = "$value";
  117. } elsif ($item == 10) {
  118. $filesystempath{$filesystemID1} = "$value";
  119. }
  120. }
  121. sub show_filesystem {
  122. print "\n\nfilesystemID1\tmounted filesystem\tfilesystem path\n";
  123. foreach $element (keys %mounted) {
  124. print "$element\t$mounted{$element}\t\t$filesystempath{$element}\n";
  125. }
  126. print "\n\n";
  127. }
  128. sub check_filesystem {
  129. # Warning = percentage of used space >= $warning and < $critical
  130. # Critical = percentage of used space > $warning and >= $critical
  131. # OK = percentage of used space < $warning and < $critical
  132. my ($mounted_filesystem, $warning, $critical) = @_;
  133. foreach $element (keys %mounted) {
  134. if ($mounted{$element} eq $mounted_filesystem) {
  135. my $warning_result = $totalspace{$element}*(100-$warning)/100;
  136. my $critical_result = $totalspace{$element}*(100-$critical)/100;
  137. my $result_percent = $freespace{$element}*100/$totalspace{$element};
  138. if (($freespace{$element} <= $warning_result) && ($freespace{$element} > $critical_result)) {
  139. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  140. exit 1;
  141. } elsif ($freespace{$element} <= $critical_result) {
  142. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  143. exit 2;
  144. } else {
  145. printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  146. exit 0;
  147. }
  148. }
  149. }
  150. print "$mounted_filesystem doesn't exist in $target_host\n\n";
  151. exit -1;
  152. }
  153. sub check_filesystemID1{
  154. # Warning = percentage of used space >= $warning and < $critical
  155. # Critical = percentage of used space > $warning and >= $critical
  156. # OK = percentage of used space < $warning and < $critical
  157. my ($fsid1, $warning, $critical) = @_;
  158. foreach $element (keys %totalspace) {
  159. if ($element eq $fsid1) {
  160. my $warning_result = $totalspace{$element}*(100-$warning)/100;
  161. my $critical_result = $totalspace{$element}*(100-$critical)/100;
  162. my $result_percent = $freespace{$element}*100/$totalspace{$element};
  163. if (($freespace{$element} <= $warning_result) && ($freespace{$element} >= $critical_result)) {
  164. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  165. exit 1;
  166. } elsif ($freespace{$element} <= $critical_result) {
  167. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  168. exit 2;
  169. } else {
  170. printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  171. exit 0;
  172. }
  173. }
  174. }
  175. print "$fsid1 doesn't exist in $target_host\n\n";
  176. exit -1;
  177. }
  178. sub check_cpu_5min {
  179. my ($cpu, $warn, $crit) = @_;
  180. if ($cpu >= $crit) {
  181. print "Critical- 5-min load: $cpu\n";
  182. exit 2;
  183. } elsif ($cpu >= $warn) {
  184. print "Warning - 5-min load: $cpu\n";
  185. exit 1;
  186. } else {
  187. print "Load ok - 5-min load: $cpu\n";
  188. exit 0;
  189. }
  190. }