#!/usr/bin/perl -w # ############################################################################### # # check_heinlein_elements # # This Nagios/Icinga-Plugins checks the SNMP-Interface of the Heinlein-Elements # Appliance. # ############################################################################### # # The MIT License # # Copyright (c) 2014 Nis Wechselberg # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # ############################################################################### use strict; use Data::Dumper; my $VERSION = "1.0.0-alpha.1"; my $BASEOID = '.1.3.6.1.4.1.30854.1.1.2.10'; # Prepopulate parameters my $p_verbose = 0; my $p_help = 0; my $p_host = '127.0.0.1'; my $p_community = 'public'; my $p_test = ''; my $p_ok = ''; my $p_warning = ''; my $p_critical = ''; my $p_timeout = 5; my $p_snmpversion = '2c'; my $p_onfail = 'UNKNOWN'; my $p_onunknown = 'UNKNOWN'; my $p_negate_critical = 0; my $p_negate_warning = 0; my $p_negate_ok = 0; # Define Arrays for OIDs my @oidStateWeb = ('.1.3.1.4.13.99.104.101.99.107.95.98.101.95.49.51.51.55', '.2.3.1.4.13.99.104.101.99.107.95.98.101.95.49.51.51.56'); my @oidMsgWeb = ('.1.3.1.1.13.99.104.101.99.107.95.98.101.95.49.51.51.55', '.2.3.1.1.13.99.104.101.99.107.95.98.101.95.49.51.51.56'); my @oidStateLdap = ('.3.3.1.4.10.99.104.101.99.107.95.108.100.97.112'); my @oidMsgLdap = ('.3.3.1.2.10.99.104.101.99.107.95.108.100.97.112'); my @oidStateSigning = ('.4.3.1.4.16.99.104.101.99.107.95.97.114.99.104.105.95.56.48.48.53', '.5.3.1.4.16.99.104.101.99.107.95.97.114.99.104.105.95.56.48.48.57', '.6.3.1.4.16.99.104.101.99.107.95.97.114.99.104.105.95.56.48.56.48', '.7.3.1.4.16.99.104.101.99.107.95.97.114.99.104.105.95.56.52.52.51', '.8.3.1.4.16.99.104.101.99.107.95.97.114.99.104.105.95.56.52.52.52'); my @oidMsgSigning = ('.4.3.1.2.16.99.104.101.99.107.95.97.114.99.104.105.95.56.48.48.53', '.5.3.1.2.16.99.104.101.99.107.95.97.114.99.104.105.95.56.48.48.57', '.6.3.1.2.16.99.104.101.99.107.95.97.114.99.104.105.95.56.48.56.48', '.7.3.1.2.16.99.104.101.99.107.95.97.114.99.104.105.95.56.52.52.51', '.8.3.1.2.16.99.104.101.99.107.95.97.114.99.104.105.95.56.52.52.52'); my @oidStatePgsql = ('.9.3.1.4.11.99.104.101.99.107.95.112.103.115.113.108'); my @oidMsgPgsql = ('.9.3.1.2.11.99.104.101.99.107.95.112.103.115.113.108'); my @oidStateProxy = ('.10.3.1.4.15.99.104.101.99.107.95.115.109.116.112.95.55.49.50.51'); my @oidMsgProxy = ('.10.3.1.2.15.99.104.101.99.107.95.115.109.116.112.95.55.49.50.51'); my @oidStateMail = ('.11.3.1.4.16.99.104.101.99.107.95.115.109.116.112.95.49.48.48.51.53', '.12.3.1.4.16.99.104.101.99.107.95.115.109.116.112.95.49.48.48.57.57'); my @oidMsgMail = ('.11.3.1.2.16.99.104.101.99.107.95.115.109.116.112.95.49.48.48.51.53', '.12.3.1.2.16.99.104.101.99.107.95.115.109.116.112.95.49.48.48.57.57'); my @oidStateHDD1 = ('.13.3.1.4.17.99.104.101.99.107.95.100.105.115.107.95.99.111.110.102.105.103'); my @oidMsgHDD1 = ('.13.3.1.2.17.99.104.101.99.107.95.100.105.115.107.95.99.111.110.102.105.103'); my @oidStateHDD2 = ('.14.3.1.4.17.99.104.101.99.107.95.100.105.115.107.95.115.121.115.116.101.109'); my @oidMsgHDD2 = ('.14.3.1.2.17.99.104.101.99.107.95.100.105.115.107.95.115.121.115.116.101.109'); my @oidStateHDD3 = ('.15.3.1.4.17.99.104.101.99.107.95.100.105.115.107.95.103.108.111.98.97.108'); my @oidMsgHDD3 = ('.15.3.1.2.17.99.104.101.99.107.95.100.105.115.107.95.103.108.111.98.97.108'); # Define exit codes my %EXIT_VALUES = ('UNKNOWN' => 3, 'CRITICAL' => 2, 'WARNING' => 1, 'OK' => 0); # Methods to exit with Nagios-/Icinga exit codes # Parameter: Code, Message sub doExit { printf("%s (%d): %s\n", $_[0], $EXIT_VALUES{$_[0]}, $_[1]); exit $EXIT_VALUES{$_[0]}; } # Parameter: Message sub UNKNOWN { &doExit('UNKNOWN', $_[0]); } # Parameter: Message sub CRITICAL { &doExit('CRITICAL', $_[0]); } # Parameter: Message sub WARNING { &doExit('WARNING', $_[0]); } # Parameter: Message sub OK { &doExit('OK', $_[0]); } # General usage header my $PROGRAM_HEADER = <<"__PROGRAM_HEADER"; check_heinlein_elements.pl Checks SNMP-Interface of the Heinlein-Elements Appliance. Version $VERSION Copyright (c) 2014 Nis Wechselberg, www.enbewe.de This program is licensed under the MIT license. This program comes with ABSOLUTELY NO WARRANTY. __PROGRAM_HEADER # Initialize parameters, give usage info sub readParameters { use Getopt::Long qw(:config no_ignore_case); GetOptions( 'H=s' => \$p_host, 'h' => \$p_help, 'v' => \$p_verbose, 'C=s' => \$p_community, 'T=s' => \$p_test, 't=i' => \$p_timeout, 'f=s' => \$p_onfail, 'u=s' => \$p_onunknown, ); } sub usage() { my $usage = <<"__USAGE"; $PROGRAM_HEADER Usage: ${0} -H [-C ] -T [-v] [-h] [-t ] [-f ] Parameters: -h Show help and exit with status UNKNOWN -H The IP address or the host name of the appliance -C The SNMP community string (default: public) [optional] -T The Test to execute. See listing below. -t Timeout in seconds. From 1 to 60. (default: 5) [optional] -f Status (OK, WARNING, CRITICAL, UNKNOWN) to report when SNMP query failed (default: UNKNOWN) [optional] -v Verbose output There are following Tests (-T): backendWeb Get the state of the Backend-Webserver ldap Get the state of the LDAP-Server signing Get the state of the Archisoft-Signing-Server postgres Get the state of the internal PostgreSQL database smtpProxy Get the state of the SMTP-Proxy mail Get the state of the Mail-Server diskConfig Get the state of the Storage, containing configuration data diskSystem Get the state of the Storage, containing system data diskGlobal Get the state of the Storage, containing actual mail data __USAGE print $usage; } # SNMP Probing sub prober { use Net::SNMP; # Prepare SNMP Session my ($session, $error) = Net::SNMP->session( -hostname => $p_host, -timeout => $p_timeout, -community => $p_community, -version => $p_snmpversion ); # Catch connection failures if ($error) { my $msg = sprintf('Failed to establish SNMP session (%s)', $error); SWITCH: { $p_onfail =~ m/^c/i && do { &CRITICAL($msg); last SWITCH; }; $p_onfail =~ m/^w/i && do { &WARNING($msg); last SWITCH; }; $p_onfail =~ m/^o/i && do { &OK($msg); last SWITCH; }; &UNKNOWN($msg); }; } # Prepare result set my %result = ('error' => 0, 'error_msg' => '', 'answer_code' => '', 'answer' => ''); # Execute test and populate result set SWITCH: { $p_test eq 'backendWeb' && do { %result = &probe($session, \@oidStateWeb, \@oidMsgWeb); last SWITCH; }; $p_test eq 'ldap' && do { %result = &probe($session, \@oidStateLdap, \@oidMsgLdap); last SWITCH; }; $p_test eq 'signing' && do { %result = &probe($session, \@oidStateSigning, \@oidMsgSigning); last SWITCH; }; $p_test eq 'postgres' && do { %result = &probe($session, \@oidStatePgsql, \@oidMsgPgsql); last SWITCH; }; $p_test eq 'smtpProxy' && do { %result = &probe($session, \@oidStateProxy, \@oidMsgProxy); last SWITCH; }; $p_test eq 'mail' && do { %result = &probe($session, \@oidStateMail, \@oidMsgMail); last SWITCH; }; $p_test eq 'diskConfig' && do { %result = &probe($session, \@oidStateHDD1, \@oidMsgHDD1); last SWITCH; }; $p_test eq 'diskSystem' && do { %result = &probe($session, \@oidStateHDD2, \@oidMsgHDD2); last SWITCH; }; $p_test eq 'diskGlobal' && do { %result = &probe($session, \@oidStateHDD3, \@oidMsgHDD3); last SWITCH; }; }; $session->close(); # Catch SNMP errors if ($result{'error'} != 0) { my $msg = sprintf('Failed to execute SNMP query (Code: %i, Message: %s)', $result{'error'}, $result{'error_msg'}); SWITCH: { $p_onfail =~ m/^c/i && do { &CRITICAL($msg); last SWITCH; }; $p_onfail =~ m/^w/i && do { &WARNING($msg); last SWITCH; }; $p_onfail =~ m/^o/i && do { &OK($msg); last SWITCH; }; &UNKNOWN($msg); }; } # Present SNMP results SWITCH: { $result{'answer_code'} == 2 && do { &CRITICAL(sprintf('Result "%s" is considered as CRITICAL (%s)', $result{'answer'}, $p_critical)); last SWITCH; }; $result{'answer_code'} == 1 && do { &WARNING(sprintf('Result "%s" is considered as WARNING (%s)', $result{'answer'}, $p_warning)); last SWITCH; }; $result{'answer_code'} == 0 && do { &OK(sprintf('Result "%s" is considered as OK (%s)', $result{'answer'}, $p_ok)); last SWITCH; }; } # Catch unknown results my $msg = sprintf('Result "%s" does not fit any (other) rule.', $result{'answer'}); SWITCH: { $p_onunknown =~ m/^c/i && do { &CRITICAL($msg); last SWITCH; }; $p_onunknown =~ m/^w/i && do { &WARNING($msg); last SWITCH; }; $p_onunknown =~ m/^o/i && do { &OK($msg); last SWITCH; }; &UNKNOWN($msg); }; } # Parameter: Session, StateOIDs, MessageOIDs sub probe { my $sess = $_[0]; my @stateOIDs = @{$_[1]}; my @msgOIDs = @{$_[2]}; # Get number of OIDs to probe my $length = scalar(@stateOIDs); # Prepare result set my %result = ('error' => 0, 'error_msg' => '', 'answer_code' => 4, 'answer' => ''); # Prepare memory for temporary results my $statusCode = 0; my $statusMessage = ''; # Check all given OIDs for (my $cnt = 0; $cnt < $length; $cnt++) { # Prepare state OID my $oid = $BASEOID.$stateOIDs[$cnt]; if ($p_verbose) { printf("Querying OID \"%s\".\n", $oid); } # Query state OID my @varbindlist = ( $oid ); my $r = $sess->get_request(-varbindlist => \@varbindlist) || do { $result{'error'} = -1; $result{'error_msg'} = 'Request setup failed'; return %result; }; my %rs = %{$r}; # Check for query success if (exists $rs{$oid}) { $statusCode = $rs{$oid} >= $statusCode ? $rs{$oid} : $statusCode; if ($p_verbose) { printf("Received message \"%s\".\n", $rs{$oid}); } } else { $result{'error'} = 1; $result{'error_msg'} = sprintf('OID %s not found', $oid); } # Prepare message OID $oid = $BASEOID.$msgOIDs[$cnt]; if ($p_verbose) { printf("Querying OID \"%s\".\n", $oid); } # Query message OID @varbindlist = ( $oid ); $r = $sess->get_request(-varbindlist => \@varbindlist) || do { $result{'error'} = -1; $result{'error_msg'} = 'Request setup failed'; return %result; }; %rs = %{$r}; # Check for query success and store result if (exists $rs{$oid}) { $statusMessage .= $rs{$oid}; if ($cnt < $length-1) { $statusMessage .= " - "; } if ($p_verbose) { printf("Received message \"%s\".\n", $rs{$oid}); } } else { $result{'error'} = 1; $result{'error_msg'} = sprintf('OID %s not found', $oid); } } $result{'answer_code'} = $statusCode; $result{'answer'} = $statusMessage; return %result; } # Main Method sub main { &readParameters(); if ($p_help) { &usage(); &UNKNOWN('Help requested - no further measurement taken.'); } &prober(); } &main(); &UNKNOWN('Plugin exited without gathering a status.');