#!/usr/bin/perl # # # AUTHORS: # Copyright (C) 2003-2010 Opsera Limited. All rights reserved # # This file is part of Opsview # # Opsview 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. # # Opsview 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 Opsview; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # Patched by M. Schoemaker/Shoenix for Thomson Modems use lib qw ( /usr/local/nagios/perl/lib ); use Net::SNMP; use Getopt::Std; $script = "check_snmp_interfaces"; $script_version = "2.1shoenix"; # SNMP options $version = "2c"; $timeout = 2; # $metric = 1; $firsttime = 0; $change = 0; $error = 0; $interfacesup = 0; $interfacesdown = 0; @currentstatus = (); @previousstatus = (); $oid_sysDescr = ".1.3.6.1.2.1.1.1.0"; $ifdescr = ".1.3.6.1.2.1.2.2.1.2.1"; $baseifdescr = ".1.3.6.1.2.1.2.2.1.2"; $ifoperstatus = ".1.3.6.1.2.1.2.2.1.8"; $baseifoperstatus = ".1.3.6.1.2.1.2.2.1.8"; $hostname = "192.168.10.21"; my $port = 161; #$hostname = "212.113.28.134"; $returnstring = ""; $community = "public"; # Default community string $configfilepath = "/usr/local/nagios/etc"; $firsttime = 0; # Do we have enough information? if ( @ARGV < 1 ) { print "Too few arguments\n"; usage(); } getopts("hH:C:p:"); if ($opt_h) { usage(); exit(0); } if ($opt_H) { $hostname = $opt_H; # print "Hostname $opt_H\n"; } else { print "No hostname specified\n"; usage(); exit(0); } if ($opt_C) { $community = $opt_C; # print "Using community $opt_C\n"; } else { # print "Using community $community\n"; } if ($opt_p) { $port = $opt_p; } $statusfile = "/usr/local/nagios/var/status_$hostname.db"; readstatus(); # Create the SNMP session $version = "1"; ( $s, $e ) = Net::SNMP->session( -community => $community, -hostname => $hostname, -version => $version, -timeout => $timeout, -port => $port, ); if ( !defined( $s->get_request($oid_sysDescr) ) ) { # If we can't connect using SNMPv1 lets try as SNMPv2 $s->close(); sleep 0.5; $version = "2c"; ( $s, $e ) = Net::SNMP->session( -community => $community, -hostname => $hostname, -version => $version, -timeout => $timeout, ); if ( !defined( $s->get_request($oid_sysDescr) ) ) { print "Agent not responding, tried SNMP v1 and v2\n"; exit(1); } } # Works out what type of device we are looking at # and chances the OID if necessary if ( !defined( $s->get_request(".1.3.6.1.2.1.1.1.0") ) ) { if ( !defined( $s->get_request($oid_sysDescr) ) ) { print "Status is a Warning Level - SNMP agent not responding\n"; exit 1; } else { print "Status is a Warning Level - SNMP OID does not exist\n"; exit 1; } } else { foreach ( $s->var_bind_names() ) { $oid = $_; $sysdescr = $s->var_bind_list()->{$oid}; } } if ( $sysdescr =~ /3Com/ ) { $ifdescr = ".1.3.6.1.2.1.2.2.1.2.101"; } # This devices starts interfaces at a different location if ( $sysdescr =~ /Catalyst 4500 L3 Switch/ ) { $ifdescr = ".1.3.6.1.2.1.2.2.1.2.2"; } if ( $sysdescr =~ /ST780/ ) { $ifdescr = ".1.3.6.1.2.1.2.2.1.2.101"; } if ( $sysdescr =~ /SpeedTouch 716/ ) { $ifdescr = ".1.3.6.1.2.1.2.2.1.2.101"; } # Scans interfaces $counter = 0; while ( $ifdescr =~ /$baseifdescr/ ) { if ( !defined( $s->get_request($ifdescr) ) ) { if ( !defined( $s->get_request($oid_sysDescr) ) ) { print "SNMP agent not responding\n"; exit 1; } else { print "SNMP OID does not exist\n"; exit 1; } } else { foreach ( $s->var_bind_names() ) { $oid = $_; # print "$oid\n"; $interfacedescr = $s->var_bind_list()->{$oid}; # print "Interface: $interfacedescr\n"; $oid =~ s/$baseifdescr//g; # removes base oid from string $ifoperstatus = "$baseifoperstatus$oid"; # print "$baseifoperstatus\n$ifoperstatus\n"; } } if ( !defined( $s->get_request($ifoperstatus) ) ) { if ( !defined( $s->get_request($oid_sysDescr) ) ) { print("SNMP agent not responding"); exit 1; } else { print "SNMP OID does not exist"; exit 1; } } else { foreach ( $s->var_bind_names() ) { $oid = $_; $status = $s->var_bind_list()->{$oid}; push( @currentstatus, $status ); if ( $status == 1 ) { $interfacesup++; } else { $interfacesdown++; } if ( $status == @previousstatus[$counter] ) { } else { if ( $status < @previousstatus[$counter] ) { append("$interfacedescr now UP "); $change = 1; } else { append("$interfacedescr DOWN! "); $change = 1; } } # print "Current=$status\nPrevious=@previousstatus[$counter]"; } } if ( !defined( $s->get_next_request($ifdescr) ) ) { exit 1; } else { foreach ( $s->var_bind_names() ) { $ifdescr = $_; } } $counter++; } # Close the session $s->close(); writestatus(); if ( $firsttime == 1 ) { $change = 0; } if ( error == 0 ) { if ( $change == 0 ) { print "Status is OK - Interfaces: $interfacesup UP, $interfacesdown DOWN|in_use=$interfacesup\n"; exit 0; } elsif ( $change == 1 ) { print "Status is a Warning Level - $returnstring |in_use=$interfacesup\n"; exit 1; } else { print "Plugin error! SNMP status unknown\n"; exit 1; } } else { print "Status is Warning Level - $returnstring |in_use=$interfacesup\n"; exit 1; } exit 0; sub readstatus { if ( -e "$statusfile" ) { open STATUSFILE, "<$statusfile" or die "Can't open file $statusfile: $1"; @previousstatus = ; close STATUSFILE; } else { $firsttime = 1; } } sub writestatus { open STATUSFILE, ">$statusfile" or die "Can't open file $statusfile: $1"; foreach $line (@currentstatus) { print STATUSFILE "$line\n"; } close STATUSFILE; } sub append { my $appendstring = @_[0]; $returnstring = "$returnstring$appendstring"; } sub usage { print << "USAGE"; $script v$script_version Tracks status of Ethernet interfaces. Patched for Speedtouch/Thomson modems. Usage: $script -H -c [...] Options: -H Hostname or IP address -C Community (default is public) -p Port (default: 161) USAGE exit 1; }