1
0

check_cpu_usage 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/perl
  2. # Author: Martin Fuerstenau, Oce Printing Systems
  3. # martin.fuerstenau_at_oce.com or Martin.fuerstenau_at_nagiossw.org
  4. #
  5. # Date: 14 Apr 2011
  6. #
  7. #
  8. # Purpose and features of the program:
  9. #
  10. # - Get the CPU Usage for Windows, Solaris and Linux servers.
  11. #
  12. use strict;
  13. use Getopt::Long;
  14. use File::Basename;
  15. my $TIMEOUT = 30;
  16. $SIG{ALRM} = sub { print "ERROR: No response\n"; exit 3; };
  17. alarm($TIMEOUT);
  18. #--- Start presets and declarations ----------------------------------------------
  19. my $version = '1.0'; # The program version
  20. my $get_version; # Switch to display the program version
  21. my $progname = basename($0); # The name of the program
  22. my $warning; # The warning threshold
  23. my $warning_def=80; # The default warning threshold
  24. my $critical; # The critical threshold
  25. my $critical_def=90; # The default critical threshold
  26. my $result; # The result from the snmpwalk
  27. my @result; # The splitted result from the snmpwalk
  28. my $host; # Host to check
  29. my $help; # Switch to display the help function
  30. my $community; # Contains the SNMP community string
  31. my $community_def="public"; # Contains the SNMP default community string
  32. my $NoA; # Number of Arguments handled over to the program
  33. # -1 means no arguments which will cause an error
  34. my $cpu_usage=""; # The usage per CPU
  35. my $avg_usage; # The usage for all CPUs
  36. my $perf_usage; # The usage for all
  37. my $noperfdata; # If set no performance data will be delivered
  38. my $NoC; # Number of CPUs
  39. my $snmpversion; # SNMP version
  40. my $snmpversion_def=1; # SNMP version default
  41. my $timeout; # SNMP timeout
  42. my $timeout_def=60; # SNMP timeout default
  43. sub usage();
  44. sub help ();
  45. #--- End presets -----------------------------------------------------------------
  46. # First we have to fix the number of arguments
  47. $NoA=$#ARGV;
  48. Getopt::Long::Configure('bundling');
  49. GetOptions
  50. ("H=s" => \$host, "hostname=s" => \$host,
  51. "C=s" => \$community, "community=s" => \$community,
  52. "w=s" => \$warning, "warning=s" => \$warning,
  53. "c=s" => \$critical, "critical=s" => \$critical,
  54. "t=s" => \$timeout, "timeout=s" => \$timeout,
  55. "V" => \$get_version, "version" => \$get_version,
  56. "v=s" => \$snmpversion, "snmpversion=s" => \$snmpversion,
  57. "n" => \$noperfdata, "noperfdata" => \$noperfdata,
  58. "h" => \$help, "help" => \$help);
  59. if ($get_version)
  60. {
  61. print "$progname version: $version\n";
  62. exit 0;
  63. }
  64. if ($help)
  65. {
  66. help();
  67. exit 0;
  68. }
  69. # Right number of arguments (therefore noa :-)) )
  70. if ( $NoA == -1 )
  71. {
  72. usage();
  73. exit 1;
  74. }
  75. if (!$warning)
  76. {
  77. $warning=$warning_def;
  78. }
  79. if (!$critical)
  80. {
  81. $critical=$critical_def;
  82. }
  83. if (!$timeout)
  84. {
  85. $timeout=$timeout_def;
  86. }
  87. if (!$host)
  88. {
  89. print "Host name/address not specified\n\n";
  90. usage();
  91. exit 1;
  92. }
  93. if (!$snmpversion)
  94. {
  95. $snmpversion=$snmpversion_def;
  96. }
  97. else
  98. {
  99. if ( $snmpversion ne "1" )
  100. {
  101. if ( $snmpversion ne "2c" )
  102. {
  103. print "\nWrong SNMP version submitted. Only version 1 of 2c is allowed.\n\n";
  104. exit 1;
  105. }
  106. }
  107. }
  108. if (!$community)
  109. {
  110. $community = $community_def;
  111. print "No community string supplied - using default $community_def\n";
  112. }
  113. $result =`snmpwalk -v $snmpversion -t $timeout -c $community $host 1.3.6.1.2.1.25.3.3.1.2`;
  114. if ( $result )
  115. {
  116. @result = split (/\n/,$result);
  117. $NoC=0;
  118. foreach ( @result )
  119. {
  120. s/HOST-RESOURCES-MIB::hrProcessorLoad.\d+ = INTEGER://g;
  121. $avg_usage+=$_;
  122. $cpu_usage=$cpu_usage.", CPU-$NoC:$_%".";".$warning.";".$critical;
  123. $NoC++;
  124. }
  125. $avg_usage = $avg_usage / $NoC;
  126. $avg_usage = sprintf("%.0f",$avg_usage);
  127. $perf_usage = $cpu_usage;
  128. $cpu_usage =~ s/;$warning;$critical//g;
  129. $perf_usage =~ s/, / /g;
  130. $perf_usage =~ s/: /=/g;
  131. $perf_usage = "CPU-AVG=".$avg_usage."%".";".$warning.";".$critical.$perf_usage;
  132. if ( $avg_usage < $warning )
  133. {
  134. if (!$noperfdata)
  135. {
  136. print "OK: Average CPU usage: $avg_usage%$cpu_usage|$perf_usage";
  137. exit 0;
  138. }
  139. else
  140. {
  141. print "OK: Average CPU usage: $avg_usage%$cpu_usage";
  142. exit 0;
  143. }
  144. }
  145. else
  146. {
  147. if ( $avg_usage >= $warning )
  148. {
  149. if ( $avg_usage < $critical )
  150. {
  151. if (!$noperfdata)
  152. {
  153. print "WARNING: Average CPU usage: $avg_usage%$cpu_usage|$perf_usage";
  154. exit 1;
  155. }
  156. else
  157. {
  158. print "WARNING: Average CPU usage: $avg_usage%$cpu_usage";
  159. exit 1;
  160. }
  161. }
  162. else
  163. {
  164. if (!$noperfdata)
  165. {
  166. print "CRITICAL: Average CPU usage: $avg_usage%$cpu_usage|$perf_usage";
  167. exit 2;
  168. }
  169. else
  170. {
  171. print "CRITICAL: Average CPU usage: $avg_usage%$cpu_usage";
  172. exit 2;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. else
  179. {
  180. print "Unknown: No response\n";
  181. exit 3;
  182. }
  183. # ---- Subroutines -------------------------------------------------------
  184. sub usage()
  185. {
  186. print "Usage:\n";
  187. print "$progname -H <host>|--hostname=<host> [-C <community>|--community=<community>] ";
  188. print "[-v <1|2c>|--snmpversion=<1|2c>] [-t <timeout>|--timeout=<timeout>] [-n|--noperfdata] ";
  189. print "[-w <threshold>|--warning=<threshold>] [-c <threshold>|--critical=<threshold>]\n\n";
  190. print "or \n\n";
  191. print "$progname -h|--help\n\n";
  192. print "or \n\n";
  193. print "$progname -V|--version\n";
  194. }
  195. sub help ()
  196. {
  197. usage();
  198. print "This plugin check the CPU usage of solaris, linux and windows servers\n\n";
  199. print "-h|--help Print detailed help screen\n";
  200. print "-V|--version Print version information\n";
  201. print "-H <host>|--hostname=<host> Hostname/IP-Adress to use for the check.\n";
  202. print "-C <community>|--community=<community> SNMP community that should be used to access the switch.\n";
  203. print " Default: $community_def\n";
  204. print "-n|--noperfdata Don't print performance data.\n";
  205. print "-t <timeout>|--timeout=<timeout> Seconds before plugin times out.\n";
  206. print " Default: $timeout_def\n";
  207. print "-v <1|2c>|--snmpversion=<1|2c> SNMP version details for command-line debugging (can repeat up to 3 times)\n\n";
  208. print " Default: $snmpversion_def\n";
  209. print "-w <threshold>|--warning=<threshold> Warning threshold in percent.\n\n";
  210. print " Default: $warning_def\n";
  211. print "-c <threshold>|--critical=<threshold> Critical threshold in percent.\n\n";
  212. print " Default: $critical_def\n";
  213. }