check_hprsc.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/bin/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. "protocol:s" => \$proto_opt,
  41. "warning=i" => \$warning_opt,
  42. "critical=i" => \$critical_opt);
  43. $proto_opt = 1
  44. unless $proto_opt == 1 ||
  45. $proto_opt == '2c' ||
  46. $proto_opt == 3;
  47. if ($chk_fs) {
  48. walk_data($snmpwalk, $target_host, $target_community, $mounted_OID,$proto_opt );
  49. walk_data($snmpwalk, $target_host, $target_community, $totalspace_OID,$proto_opt );
  50. walk_data($snmpwalk, $target_host, $target_community, $freespace_OID,$proto_opt ); check_filesystem($fs_opt, $warning_opt, $critical_opt);
  51. } elsif ($show_fs) {
  52. walk_data($snmpwalk, $target_host, $target_community, $filesystemID1_OID,$proto_opt);
  53. walk_data($snmpwalk, $target_host, $target_community, $mounted_OID,$proto_opt );
  54. walk_data($snmpwalk, $target_host, $target_community, $path_OID,$proto_opt);
  55. show_filesystem();
  56. } elsif ($chk_fsid){
  57. $totalspace_fsID_OID = "$totalspace_OID.$fsid1_opt";
  58. $freespace_fsID_OID = "$freespace_OID.$fsid1_opt";
  59. walk_data($snmpwalk, $target_host, $target_community, $totalspace_fsID_OID,$proto_opt);
  60. walk_data($snmpwalk, $target_host, $target_community, $freespace_fsID_OID,$proto_opt);
  61. check_filesystemID1($fsid1_opt, $warning_opt, $critical_opt);
  62. } elsif ($chk_cpu) {
  63. get_cpu_load($snmpwalk, $target_host, $target_community, $cpu_5min_OID,$proto_opt);
  64. check_cpu_5min($cpu, $warning_opt, $critical_opt);
  65. } else {
  66. print "\n\nUsage:\n";
  67. print "Checking 5-min CPU Load:\n";
  68. print " $0 --check-cpu -warning <threshold> --critical <threshold> --host <yourhost> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
  69. print "Checking local filesystem mounted on a host:\n";
  70. print " $0 --show-filesystems --host <hostname> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
  71. print "Checking by filesystem name:\n";
  72. print " $0 --check-filesystem --filesystem </dev/vg00/lvol1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
  73. print "Checking by filesystem ID:\n";
  74. print " $0 --check-filesystemID --filesystemID <filesystemID1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
  75. }
  76. sub get_cpu_load {
  77. my ($snmpwalk, $target_host, $target_community, $OID,$vers) = @_;
  78. die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
  79. if ($pid) { # parent
  80. while (<SNMPWALK>) {
  81. my @snmpdata = split(/:/,$_);
  82. $cpu = $snmpdata[1]/100;
  83. }
  84. close(SNMPWALK) or warn "kid exited $?";
  85. } else { # child
  86. exec($snmpwalk,'-c',$target_community,'-v',$vers,$target_host,$OID) or die "can't exec program: $!";
  87. }
  88. }
  89. sub walk_data {
  90. #This function queries the SNMP daemon for the specific OID
  91. my ($snmpwalk, $target_host, $target_community, $OID,$vers) = @_;
  92. die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
  93. if ($pid) { # parent
  94. while (<SNMPWALK>) {
  95. $output = $_;
  96. sort_walk_data($output);
  97. }
  98. close(SNMPWALK) or warn "kid exited $?";
  99. } else { # child
  100. exec($snmpwalk,'-c',$target_community,'-v',$vers,$target_host,$OID) or die "can't exec program: $!";
  101. }
  102. }
  103. sub sort_walk_data {
  104. my ($snmp_data) = @_;
  105. @fields = split(/\./,$snmp_data);
  106. $item = $fields[8];
  107. $filesystemID1 = $fields[9];
  108. @fields2 = split(/=/,$fields[10]);
  109. # $filesystemID2 = $fields2[0];
  110. $value = $fields2[1];
  111. chomp($value);
  112. if ($value =~ /"/) {
  113. @fields3 = split(/"/,$value);
  114. $value = $fields3[1];
  115. }
  116. if ($item == 3) {
  117. $mounted{$filesystemID1} = "$value";
  118. } elsif ($item == 4) {
  119. $totalspace{$filesystemID1} = "$value";
  120. } elsif ($item == 6) {
  121. $freespace{$filesystemID1} = "$value";
  122. } elsif ($item == 10) {
  123. $filesystempath{$filesystemID1} = "$value";
  124. }
  125. }
  126. sub show_filesystem {
  127. print "\n\nfilesystemID1\tmounted filesystem\tfilesystem path\n";
  128. foreach $element (keys %mounted) {
  129. print "$element\t$mounted{$element}\t\t$filesystempath{$element}\n";
  130. }
  131. print "\n\n";
  132. }
  133. sub check_filesystem {
  134. # Warning = percentage of used space >= $warning and < $critical
  135. # Critical = percentage of used space > $warning and >= $critical
  136. # OK = percentage of used space < $warning and < $critical
  137. my ($mounted_filesystem, $warning, $critical) = @_;
  138. foreach $element (keys %mounted) {
  139. if ($mounted{$element} eq $mounted_filesystem) {
  140. my $warning_result = $totalspace{$element}*(100-$warning)/100;
  141. my $critical_result = $totalspace{$element}*(100-$critical)/100;
  142. my $result_percent = $freespace{$element}*100/$totalspace{$element};
  143. if (($freespace{$element} <= $warning_result) && ($freespace{$element} > $critical_result)) {
  144. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  145. exit 1;
  146. } elsif ($freespace{$element} <= $critical_result) {
  147. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  148. exit 2;
  149. } else {
  150. printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  151. exit 0;
  152. }
  153. }
  154. }
  155. print "$mounted_filesystem doesn't exist in $target_host\n\n";
  156. exit -1;
  157. }
  158. sub check_filesystemID1{
  159. # Warning = percentage of used space >= $warning and < $critical
  160. # Critical = percentage of used space > $warning and >= $critical
  161. # OK = percentage of used space < $warning and < $critical
  162. my ($fsid1, $warning, $critical) = @_;
  163. foreach $element (keys %totalspace) {
  164. if ($element eq $fsid1) {
  165. my $warning_result = $totalspace{$element}*(100-$warning)/100;
  166. my $critical_result = $totalspace{$element}*(100-$critical)/100;
  167. my $result_percent = $freespace{$element}*100/$totalspace{$element};
  168. if (($freespace{$element} <= $warning_result) && ($freespace{$element} >= $critical_result)) {
  169. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  170. exit 1;
  171. } elsif ($freespace{$element} <= $critical_result) {
  172. printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  173. exit 2;
  174. } else {
  175. printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
  176. exit 0;
  177. }
  178. }
  179. }
  180. print "$fsid1 doesn't exist in $target_host\n\n";
  181. exit -1;
  182. }
  183. sub check_cpu_5min {
  184. my ($cpu, $warn, $crit) = @_;
  185. if ($cpu >= $crit) {
  186. print "Critical- 5-min load: $cpu\n";
  187. exit 2;
  188. } elsif ($cpu >= $warn) {
  189. print "Warning - 5-min load: $cpu\n";
  190. exit 1;
  191. } else {
  192. print "Load ok - 5-min load: $cpu\n";
  193. exit 0;
  194. }
  195. }