check_cpu_usage_v3 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 $snmp_user;
  42. my $snmp_password;
  43. my $timeout; # SNMP timeout
  44. my $timeout_def=60; # SNMP timeout default
  45. sub usage();
  46. sub help ();
  47. #--- End presets -----------------------------------------------------------------
  48. # First we have to fix the number of arguments
  49. $NoA=$#ARGV;
  50. Getopt::Long::Configure('bundling');
  51. GetOptions
  52. ("H=s" => \$host, "hostname=s" => \$host,
  53. "C=s" => \$community, "community=s" => \$community,
  54. "u=s" => \$snmp_user, "snmp_user=s" => \$snmp_user,
  55. "p=s" => \$snmp_password,"snmp_password=s" => \$snmp_password,
  56. "w=s" => \$warning, "warning=s" => \$warning,
  57. "c=s" => \$critical, "critical=s" => \$critical,
  58. "t=s" => \$timeout, "timeout=s" => \$timeout,
  59. "V" => \$get_version, "version" => \$get_version,
  60. "v=s" => \$snmpversion, "snmpversion=s" => \$snmpversion,
  61. "n" => \$noperfdata, "noperfdata" => \$noperfdata,
  62. "h" => \$help, "help" => \$help);
  63. if ($get_version)
  64. {
  65. print "$progname version: $version\n";
  66. exit 0;
  67. }
  68. if ($help)
  69. {
  70. help();
  71. exit 0;
  72. }
  73. # Right number of arguments (therefore noa :-)) )
  74. if ( $NoA == -1 )
  75. {
  76. usage();
  77. exit 1;
  78. }
  79. if (!$warning)
  80. {
  81. $warning=$warning_def;
  82. }
  83. if (!$critical)
  84. {
  85. $critical=$critical_def;
  86. }
  87. if (!$timeout)
  88. {
  89. $timeout=$timeout_def;
  90. }
  91. if (!$host)
  92. {
  93. print "Host name/address not specified\n\n";
  94. usage();
  95. exit 1;
  96. }
  97. if (!$snmpversion) { $snmpversion=$snmpversion_def; }
  98. if ( $snmpversion !~ "(1|2|2c|3)" ) {
  99. print "\nWrong SNMP version submitted. Only version 1, 2, 2c, 3 is allowed.\n\n";
  100. exit 1;
  101. }
  102. if (!$community and $snmpversion ne "3")
  103. {
  104. $community = $community_def;
  105. print "No community string supplied - using default $community_def\n";
  106. }
  107. if ($snmpversion eq "3" and (!$snmp_user or !$snmp_password)) {
  108. print "\nWrong SNMP parameters!\n\n";
  109. exit 1;
  110. }
  111. if ($snmpversion ne "3") {
  112. $result =`snmpwalk -v $snmpversion -t $timeout -c $community -O v $host 1.3.6.1.2.1.25.3.3.1.2`;
  113. } else {
  114. #-a PROTOCOL set authentication protocol (MD5|SHA)
  115. #-A PASSPHRASE set authentication protocol pass phrase
  116. #-l LEVEL set security level (noAuthNoPriv|authNoPriv|authPriv)
  117. #-u USER-NAME set security name (e.g. bert)
  118. #-x PROTOCOL set privacy protocol (DES|AES)
  119. #-X PASSPHRASE set privacy protocol pass phrase
  120. $result =`snmpwalk -v 3 -a SHA -x AES -l authNoPriv -u $snmp_user -A $snmp_password -X $snmp_password -t $timeout -O v $host 1.3.6.1.2.1.25.3.3.1.2`;
  121. }
  122. if ( $result )
  123. {
  124. @result = split (/\n/,$result);
  125. $NoC=0;
  126. foreach ( @result )
  127. {
  128. s/HOST-RESOURCES-MIB::hrProcessorLoad.\d+ = INTEGER://g;
  129. s/INTEGER: //g;
  130. $avg_usage+=$_;
  131. $cpu_usage=$cpu_usage.", CPU-$NoC:$_%".";".$warning.";".$critical;
  132. $NoC++;
  133. }
  134. $avg_usage = $avg_usage / $NoC;
  135. $avg_usage = sprintf("%.0f",$avg_usage);
  136. $perf_usage = $cpu_usage;
  137. $cpu_usage =~ s/;$warning;$critical//g;
  138. $perf_usage =~ s/, / /g;
  139. $perf_usage =~ s/: /=/g;
  140. $perf_usage = "CPU-AVG=".$avg_usage."%".";".$warning.";".$critical.$perf_usage;
  141. if ( $avg_usage < $warning )
  142. {
  143. if (!$noperfdata)
  144. {
  145. print "OK: Average CPU usage: $avg_usage%$cpu_usage|$perf_usage";
  146. exit 0;
  147. }
  148. else
  149. {
  150. print "OK: Average CPU usage: $avg_usage%$cpu_usage";
  151. exit 0;
  152. }
  153. }
  154. else
  155. {
  156. if ( $avg_usage >= $warning )
  157. {
  158. if ( $avg_usage < $critical )
  159. {
  160. if (!$noperfdata)
  161. {
  162. print "WARNING: Average CPU usage: $avg_usage%$cpu_usage|$perf_usage";
  163. exit 1;
  164. }
  165. else
  166. {
  167. print "WARNING: Average CPU usage: $avg_usage%$cpu_usage";
  168. exit 1;
  169. }
  170. }
  171. else
  172. {
  173. if (!$noperfdata)
  174. {
  175. print "CRITICAL: Average CPU usage: $avg_usage%$cpu_usage|$perf_usage";
  176. exit 2;
  177. }
  178. else
  179. {
  180. print "CRITICAL: Average CPU usage: $avg_usage%$cpu_usage";
  181. exit 2;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. else
  188. {
  189. print "Unknown: No response\n";
  190. exit 3;
  191. }
  192. # ---- Subroutines -------------------------------------------------------
  193. sub usage()
  194. {
  195. print "Usage:\n";
  196. print "$progname -H <host>|--hostname=<host> [-C <community>|--community=<community>] ";
  197. print "[-v <1|2c>|--snmpversion=<1|2c>] [-t <timeout>|--timeout=<timeout>] [-n|--noperfdata] ";
  198. print "[-w <threshold>|--warning=<threshold>] [-c <threshold>|--critical=<threshold>]\n\n";
  199. print "or \n\n";
  200. print "$progname -h|--help\n\n";
  201. print "or \n\n";
  202. print "$progname -V|--version\n";
  203. }
  204. sub help ()
  205. {
  206. usage();
  207. print "This plugin check the CPU usage of solaris, linux and windows servers\n\n";
  208. print "-h|--help Print detailed help screen\n";
  209. print "-V|--version Print version information\n";
  210. print "-H <host>|--hostname=<host> Hostname/IP-Adress to use for the check.\n";
  211. print "-C <community>|--community=<community> SNMP community that should be used to access the switch.\n";
  212. print " Default: $community_def\n";
  213. print "-n|--noperfdata Don't print performance data.\n";
  214. print "-t <timeout>|--timeout=<timeout> Seconds before plugin times out.\n";
  215. print " Default: $timeout_def\n";
  216. print "-v <1|2c>|--snmpversion=<1|2c> SNMP version details for command-line debugging (can repeat up to 3 times)\n\n";
  217. print " Default: $snmpversion_def\n";
  218. print "-w <threshold>|--warning=<threshold> Warning threshold in percent.\n\n";
  219. print " Default: $warning_def\n";
  220. print "-c <threshold>|--critical=<threshold> Critical threshold in percent.\n\n";
  221. print " Default: $critical_def\n";
  222. }