1
0

check_hwg-ste.pl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #!/usr/bin/perl
  2. # ------------------------------------------------------------------------------
  3. # check_hwgroup.pl - checks the HW group Hwg-STE devices.
  4. # Copyright (C) 2010 NETWAYS GmbH, www.netways.de
  5. # Author: Michael Streb <michael.streb@netways.de>
  6. # Author: Bernd Löhlein <bernd.loehlein@netways.de>
  7. # Version: $Id: check_hwg-ste.pl 1558 2010-02-17 13:07:57Z mstreb $
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the tepdu of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. # $Id: check_hwg-ste.pl 1558 2010-02-17 13:07:57Z mstreb $
  23. # ------------------------------------------------------------------------------
  24. # basic requirements
  25. use strict;
  26. use Getopt::Long;
  27. use File::Basename;
  28. use Pod::Usage;
  29. use Net::SNMP;
  30. use Data::Dumper;
  31. # predeclared subs
  32. use subs qw/print_help/;
  33. # predeclared vars
  34. use vars qw (
  35. $PROGNAME
  36. $VERSION
  37. %states
  38. %state_names
  39. $opt_host
  40. $opt_community
  41. $opt_sensor
  42. $opt_contact
  43. $opt_warning
  44. $opt_critical
  45. $opt_help
  46. $opt_man
  47. $opt_version
  48. $module
  49. $device
  50. $response
  51. $output
  52. @oids
  53. );
  54. # Main values
  55. $PROGNAME = basename($0);
  56. $VERSION = '1.1';
  57. # Nagios exit states
  58. %states = (
  59. OK => 0,
  60. WARNING => 1,
  61. CRITICAL => 2,
  62. UNKNOWN => 3
  63. );
  64. # Nagios state names
  65. %state_names = (
  66. 0 => 'OK',
  67. 1 => 'WARNING',
  68. 2 => 'CRITICAL',
  69. 3 => 'UNKNOWN'
  70. );
  71. $opt_warning = "null";
  72. $opt_critical = "null";
  73. my $TIMEOUT = 30;
  74. $SIG{ALRM} = sub { print "ERROR: No response\n"; exit 3; };
  75. alarm($TIMEOUT);
  76. # SNMP
  77. my $opt_community = "public";
  78. my $snmp_version = "1";
  79. #my $response;
  80. # Get the options from cl
  81. Getopt::Long::Configure('bundling');
  82. GetOptions(
  83. 'h' => \$opt_help,
  84. 'help' => \$opt_help,
  85. 'H=s' => \$opt_host,
  86. 'C=s', => \$opt_community,
  87. 'S=n', => \$opt_sensor,
  88. 'I=n', => \$opt_contact,
  89. 'w=s' => \$opt_warning,
  90. 'c=s' => \$opt_critical,
  91. 'man' => \$opt_man,
  92. 'V' => \$opt_version
  93. )
  94. || print_help( 1, 'Please check your options!' );
  95. # If somebody wants to the help ...
  96. if ($opt_help) {
  97. print_help(1);
  98. }
  99. elsif ($opt_man) {
  100. print_help(2);
  101. }
  102. elsif ($opt_version) {
  103. print_help(-1);
  104. }
  105. # oids
  106. my $deviceid = '.1.3.6.1.2.1.1.1.0';
  107. my $enterprise = '.1.3.6.1.4.1.21796';
  108. # Check if all needed options present.
  109. unless ( $opt_host && ( $opt_sensor || $opt_contact ) ) {
  110. print_help( 1, 'Not enough options specified!' );
  111. }
  112. else {
  113. # Open SNMP Session
  114. my ( $session, $error ) = Net::SNMP->session(
  115. -hostname => $opt_host,
  116. -community => $opt_community,
  117. -port => 161,
  118. -timeout => 5,
  119. -version => $snmp_version
  120. );
  121. # SNMP Session failed
  122. if ( !defined($session) ) {
  123. print $state_names{ ( $states{UNKNOWN} ) } . ": $error";
  124. exit $states{UNKNOWN};
  125. }
  126. # request for sensor
  127. if (defined $opt_sensor) {
  128. my ($sensor_tree_id, $sensor_id);
  129. # Sensor states
  130. my %sensor_states = ();
  131. # Sensor OID setting
  132. # check for device type Poseidon/STE
  133. $response = $session->get_request($deviceid);
  134. $device = $response->{$deviceid};
  135. if ($device =~ m/Poseidon/i) {
  136. # Sensor states
  137. %sensor_states = (
  138. 0 => 'invalid',
  139. 1 => 'normal',
  140. 2 => 'alarmstate',
  141. 3 => 'alarm',
  142. );
  143. $sensor_tree_id = $enterprise.".3.3.3.1.8";
  144. } elsif ($device =~ m/STE/i) {
  145. # Sensor states
  146. %sensor_states = (
  147. 0 => 'invalid',
  148. 1 => 'normal',
  149. 2 => 'outofrangelo',
  150. 3 => 'outofrangehi',
  151. 4 => 'alarmlo',
  152. 5 => 'alarmhi',
  153. );
  154. $sensor_tree_id = $enterprise.".4.1.3.1.8";
  155. } else {
  156. print "ERROR: Device not supported\n";
  157. $session->close();
  158. exit $states{UNKNOWN};
  159. }
  160. # correct sensor ID 1 and 2
  161. if ($opt_sensor == 1 || $opt_sensor == 2) {
  162. $opt_sensor += 214;
  163. }
  164. # get the sensor ID
  165. $response = $session->get_table($sensor_tree_id);
  166. foreach my $sensor (keys %$response) {
  167. if($response->{$sensor} eq $opt_sensor) {
  168. $sensor =~ m/.?[\d\.]+(\d)$/;
  169. $sensor_id = $1;
  170. };
  171. }
  172. # check for numeric sensor id
  173. if (!defined $sensor_id || $sensor_id !~ m/\d/ ) {
  174. print "ERROR: Sensor ID not found\n";
  175. $session->close();
  176. exit $states{UNKNOWN};
  177. }
  178. # setting per device OIDs for sensor values
  179. if ($device =~ m/Poseidon/i) {
  180. push(@oids, $enterprise.'.3.3.3.1.2.'.$sensor_id); # POSEIDON-MIB::sensName
  181. push(@oids, $enterprise.'.3.3.3.1.4.'.$sensor_id); # POSEIDON-MIB::sensState
  182. push(@oids, $enterprise.'.3.3.3.1.5.'.$sensor_id); # POSEIDON-MIB::sensString
  183. push(@oids, $enterprise.'.3.3.3.1.6.'.$sensor_id); # POSEIDON-MIB::sensValue
  184. push(@oids, $enterprise.'.3.3.3.1.9.'.$sensor_id); # POSEIDON-MIB::sensUnit
  185. push(@oids, $enterprise.'.3.3.99.1.2.1.6.'.$sensor_id); # POSEIDON-MIB::sensLimitMin
  186. push(@oids, $enterprise.'.3.3.99.1.2.1.7.'.$sensor_id); # POSEIDON-MIB::sensLimitMax
  187. } elsif ($device =~ m/STE/i) {
  188. push(@oids, $enterprise.'.4.1.3.1.2.'.$sensor_id); # POSEIDON-MIB::sensName
  189. push(@oids, $enterprise.'.4.1.3.1.3.'.$sensor_id); # POSEIDON-MIB::sensState
  190. push(@oids, $enterprise.'.4.1.3.1.4.'.$sensor_id); # POSEIDON-MIB::sensString
  191. push(@oids, $enterprise.'.4.1.3.1.5.'.$sensor_id); # POSEIDON-MIB::sensValue
  192. } else {
  193. print "ERROR: device not supported";
  194. $session->close();
  195. exit $states{UNKNOWN};
  196. }
  197. # getting the sensor values from the device
  198. $response = $session->get_request(-varbindlist => \@oids)
  199. or die "ERROR while getting Sensor values";
  200. $response->{$oids[0]} = 'temp';
  201. # setting the output string
  202. $output .= "Sensor: ".$response->{$oids[0]}.", ";
  203. $output .= "State: ".$sensor_states{$response->{$oids[1]}}.", ";
  204. $output .= "Value: ".$response->{$oids[2]};
  205. $output .= "| $response->{$oids[0]}=".int(eval($response->{$oids[3]}/10)).";";
  206. # append thresholds to perfdata if device is Poseidon
  207. if ($device =~ m/Poseidon/i) {
  208. $output .= eval($response->{$oids[5]}/10).";";
  209. $output .= eval($response->{$oids[6]}/10).";";
  210. }
  211. }
  212. # request for dry contact
  213. if (defined $opt_contact) {
  214. # Input states
  215. my %alarm_states = (
  216. 0 => 'normal',
  217. 1 => 'alarm',
  218. );
  219. my %input_values = (
  220. 0 => 'off',
  221. 1 => 'on',
  222. );
  223. my %alarm_setup = (
  224. 0 => 'inactive',
  225. 1 => 'activeOff',
  226. 2 => 'activeOn',
  227. );
  228. # get the contact values
  229. push(@oids, $enterprise.'.3.3.1.1.3.'.$opt_contact); # POSEIDON-MIB::inpName
  230. push(@oids, $enterprise.'.3.3.1.1.2.'.$opt_contact); # POSEIDON-MIB::inpValue
  231. push(@oids, $enterprise.'.3.3.1.1.4.'.$opt_contact); # POSEIDON-MIB::inpAlarmSetup
  232. push(@oids, $enterprise.'.3.3.1.1.5.'.$opt_contact); # POSEIDON-MIB::inpAlarmState
  233. # getting the values from the device
  234. $response = $session->get_request(-varbindlist => \@oids)
  235. or print "ERROR: Sensor ID not found\n";
  236. exit $states{UNKNOWN} if !defined $response;
  237. # setting the output string
  238. $output .= "Input: ".$response->{$oids[0]}.", ";
  239. $output .= "AlarmState: ".$alarm_states{$response->{$oids[3]}}.", ";
  240. $output .= "AlarmSetup: ".$alarm_setup{$response->{$oids[2]}}.", ";
  241. $output .= "Value: ".$input_values{$response->{$oids[1]}};
  242. }
  243. # finally close SNMP session
  244. $session->close();
  245. # print the gathered data
  246. print $output."25;30;;;\n";
  247. # setting exit states
  248. if (defined $opt_sensor) {
  249. if ( $device =~ m/Poseidon/i ) {
  250. if ($response->{$oids[1]} == 3) {
  251. exit $states{CRITICAL};
  252. } elsif ($response->{$oids[1]} == 2) {
  253. exit $states{WARNING};
  254. } elsif ($response->{$oids[1]} == 0) {
  255. exit $states{UNKNOWN};
  256. } else {
  257. exit $states{OK};
  258. }
  259. } elsif ($device =~ m/STE/i ) {
  260. if ($response->{$oids[1]} > 1) {
  261. exit $states{CRITICAL};
  262. } elsif ($response->{$oids[1]} == 0) {
  263. exit $states{UNKNOWN};
  264. } else {
  265. exit $states{OK};
  266. }
  267. }
  268. }
  269. # check for dry contacts
  270. if (defined $opt_contact) {
  271. if ($response->{$oids[3]} == 1) {
  272. exit $states{CRITICAL};
  273. } else {
  274. exit $states{OK};
  275. }
  276. }
  277. }
  278. # -------------------------
  279. # THE SUBS:
  280. # -------------------------
  281. # print_help($level, $msg);
  282. # prints some message and the POD DOC
  283. sub print_help {
  284. my ( $level, $msg ) = @_;
  285. $level = 0 unless ($level);
  286. if($level == -1) {
  287. print "$PROGNAME - Version: $VERSION\n";
  288. exit ( $states{UNKNOWN});
  289. }
  290. pod2usage(
  291. {
  292. -noperldoc => 1,
  293. -message => $msg,
  294. -verbose => $level
  295. }
  296. );
  297. exit( $states{UNKNOWN} );
  298. }
  299. 1;
  300. __END__
  301. =head1 NAME
  302. check_hwg-ste.pl - Checks a hwgroup hwg-ste device for nagios
  303. =head1 SYNOPSIS
  304. check_hwg-ste.pl -h
  305. check_hwg-ste.pl -H <host> -S <sensor id>
  306. =head1 DESCRIPTION
  307. Bcheck_hwg-ste.pl recieves the data from the hwgroup devices.
  308. =head1 OPTIONS
  309. =over 8
  310. =item B<-h>
  311. Display this helpmessage.
  312. =item B<-H>
  313. The hostname or ipaddress of the hwgroup device.
  314. =item B<-C>
  315. The snmp community of the hwgroup device.
  316. =item B<-S>
  317. The sensor to check (e.g. 1 or 2)
  318. =back
  319. =cut
  320. =head1 THRESHOLD FORMATS
  321. B<1.> start <= end
  322. Thresholds have to be specified from the lower level end on e.g. -w 20 is meaning that a
  323. warning error is occuring when the collected value is over 20.
  324. =head1 VERSION
  325. $Id: check_hwg-ste.pl 1558 2010-02-17 13:07:57Z mstreb $
  326. =head1 AUTHOR
  327. NETWAYS GmbH, 2010, http://www.netways.de.
  328. Written by Michael Streb <michael.streb@netways.de>, Bernd Löhlein <bernd.loehlein@netways.de>
  329. Please report bugs through the contact of monitoringexchange, http://www.monitoringexchange.org.