#!/usr/bin/perl ################################################################################ ## ## check_foundry_ports (version 0.9) ## ## Check port status against a list and return CRITICAL ## if result doesn't match definition. This may be a ## indicator for a bad guy adding a device to our ## foundry devices. ## ################################################################################ ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## ################################################################################ ## ## Port configurations: ## ===================== ## ## Set the variable $port_base to your base directory containing the up/down ## definitions. The file(s) must match the supplied parameter "host". Here's a ## example entry: ## ## File : /etc/nagios/cfg_dir/port_status/1.2.3.4 ## Content : 1:[STATE RETURNED FROM SNMPGET] ## 2:[STATE RETURNED FROM SNMPGET] ## 9:[STATE RETURNED FROM SNMPGET] ## ## The value you must enter after the ":" differs in some Foundry models. So ## please find it out yourself. ## ## Feel free to contact me if you have any problems regarding this plugin. ## ## ==> daniel@linuxaddicted.de ## ################################################################################ use strict; use warnings; use Getopt::Long; ######################################################### ### Global Vars ######################################################### ### Set the base directory for port definitions ### ### ====> You may need to change this variable ### my $port_base = '/etc/nagios/cfg_dir/port_status'; ### Initialize user-supplied vars my ($host, $community, $port); ### Get the supplied vars GetOptions('host=s' => \$host, 'community=s' => \$community, 'port=s' => \$port); ######################################################### ### Main App ######################################################### PrintUsage() unless (( defined($host) ) || ( defined($community) ) || ( defined($port) )); my $snmp_status = GetStatus(); ######################################################### ### Subs / Functions ######################################################### ### Print script usage sub PrintUsage { print "\nUsage: check_foundry_ports [PARAMETERS] Parameters: --host=[HOSTNAME] : Hostname or IP-Address of Foundry device --community=[SNMP COMMUNITY] : SNMP community string --port=[INTERFACE NR] : The interface number to check (1,2,3,22,...)\n\n"; exit 2; } ### Get SNMP status sub GetStatus { ### Get this SNMP party started my $result = `/usr/bin/snmpget -v1 -c $community $host ifOperStatus.$port 2>&1`; my $rc = $?>>8; if ( $rc != 0 ) { PrintExit ("Error while running snmpget: $result"); } else { $result = $1 if ($result =~ m/(\w+)\(.+\)/); my $return = CompareResultToList ($result); SelectOutput ($return); } } ### Compare SNMP status to list sub CompareResultToList { ### Get the result from SNMP my $snmp_result = shift; my $line; ### Exit if there's no such list for host supplied PrintExit ("Unable to find corresponding list for $host!") unless ( -f "$port_base/$host" ); ### Read the list open (LIST, "< $port_base/$host") || PrintExit ("Unable to open list $host: $!"); while ( ) { if ( m/^$port\:.+/ ) { $line = $_; last; } } close (LIST) || PrintExit ("Unable to close FH on $host: $!"); ### Get dummy (forget that) and status from list my ($dummy, $list_result) = split(/:/, $line); chomp($list_result); $dummy = ''; if ( $snmp_result eq $list_result ) { print "OK: Port $port is $snmp_result (as defined in list)"; exit 0; } else { print "CRIT: Port $port is ", uc($snmp_result), " (!! NOT as defined in list !!)"; exit 2; } } ## Print message and die sub PrintExit { ## Fetch message my $msg = shift; ## Print to STDOUT print $msg; ## Die exit(2) }