#!/usr/bin/perl
#
# AUTHORS:
# Copyright (C) 2003-2011 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
#
# Policy-Plugin for ScreenOS
use lib qw ( /usr/local/nagios/perl/lib );
use Net::SNMP;
use Getopt::Long;
$script = $0;
$script_version = "1.0";
$script_description = "Active sessions on Netscreen device";
$inter_table= '.1.3.6.1.2.1.2.2.1';
$index_table = '1.3.6.1.2.1.2.2.1.1';
$descr_table = '1.3.6.1.2.1.2.2.1.2';
$oper_table = '1.3.6.1.2.1.2.2.1.8.';
$admin_table = '1.3.6.1.2.1.2.2.1.7.';
$speed_table = '1.3.6.1.2.1.2.2.1.5.';
#In packet/error ratio
$in_ucast_table = '1.3.6.1.2.1.2.2.1.11.';
$in_nucast_table = '1.3.6.1.2.1.2.2.1.12.';
$in_error_table = '1.3.6.1.2.1.2.2.1.14.';
#out packet/error ratio
$out_ucast_table = '1.3.6.1.2.1.2.2.1.17.';
$out_nucast_table = '1.3.6.1.2.1.2.2.1.18.';
$out_error_table = '1.3.6.1.2.1.2.2.1.20.';
$label = "Sessions"; # Label used by plugin output
$performance_label = "sessions"; # Label used by plugin performance output
$ipaddress = ""; # Default IP address
$version = "1"; # SNMP version
$o_hostname ="";
$community = "public"; # Default community string
$timeout = 10; # Response timeout (seconds)
$warning = 180; # Default warning threshold
$critical = 190; # Default critical threshold
$status = 0;
$o_help = undef;
$o_version2 = undef;;
$counter = "";
$resultw="";
$resultc="";
$highperf= undef; # Use 64 bits counters
$o_descr = ""; # description filter
$o_octetlength=undef;
$o_warn_opt;
$o_crit_opt;
@o_crit=(5,5,5);
@o_warn=(5,5,5);
$port = 161;
$res="";
$res2="lalala";
# Do we have enough information?
if ( @ARGV < 1 ) {
print "Too few arguments\n";
usage();
}
#Options
Getopt::Long::Configure ("bundling");
GetOptions(
'H:s' => \$o_hostname, 'hostname:s' => \$o_hostname,
'p:i' => \$port, 'port:i' => \$port,
'n:s' => \$o_descr, 'name:s' => \$o_descr,
'C:s' => \$community, 'community:s' => \$community,
'2' => \$o_version2, 'v2c' => \$o_version2,
'w:s' => \$o_warn_opt, 'warning:s' => \$o_warn_opt,
'c:s' => \$o_crit_opt, 'critical:s' => \$o_crit_opt,
'g' => \$highperf, '64bits' => \$highperf
);
@o_warn=split(/,/,$opt_w);
@o_crit=split(/,/,$opt_c);
# Create the SNMP session
my ( $session, $error ) = Net::SNMP->session(
-community => $community,
-hostname => $o_hostname,
-version => '2',
-timeout => $timeout,
-port => $port,
);
main();
# Close the session
$session->close();
#Status-checks
if($status == 0){
print $res;
#for (my $i=0;$i < $num_int; $i++){
# print $descr[$i] . " UP;";
#}
exit 0;
}
if($status == 3){
print "Status is CRITICAL: " . $res;
exit 3
}
####################################################################
# This is where we gather data via SNMP and return results #
####################################################################
sub main {
my @tindex = undef;
my @oids = undef;
@descr = undef;
my (@oid_decrpt,@oid_perf_inucast,@oid_perf_innucast,@oid_perf_inerr,@oid_perf_outucast,@oid_perf_outnucast,@oid_perf_outerr,, @oid_speed)=
(undef,undef,undef,undef,undef,undef,undef,undef);
#my (@oid_decrpt,@oid_perf,@oid_perf_outoct,@oid_perf_inoct,@oid_perf_inerr,@oid_perf_outerr,@oid_perf_indisc,@oid_perf_outdisc)=
#(undef,undef,undef,undef,undef,undef,undef,undef);
$num_int = 0;
# Get desctiption table
$resultat = $session->get_table(-baseoid => $descr_table);
if (!defined($resultat)) {
printf("ERROR: Description table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}
# Change to 64 bit counters if option is set :
#if (defined($o_highperf)) {
# $out_octet_table=$out_octet_table_64;
# $in_octet_table=$in_octet_table_64;
#}
foreach my $key ( keys %$resultat) {
#regexp
if($$resultat{$key} =~ /$o_descr/){
# get the index number of the interface
my @oid_list = split (/\./,$key);
$tindex[$num_int] = pop (@oid_list);
# get the full description
$descr[$num_int]=$$resultat{$key};
# Get rid of special caracters (specially for Windows)
$descr[$num_int] =~ s/[[:cntrl:]]//g;
# put the admin or oper oid in an array
$oids[$num_int]= $oper_table . $tindex[$num_int] ;
# Put the performance oid
#description
$oid_decrpt[$num_int]= $descr_table . $tindex[$num_int];
#in
$oid_perf_inucast[$num_int]= $in_ucast_table . $tindex[$num_int];
$oid_perf_innucast[$num_int]= $in_nucast_table . $tindex[$num_int];
$oid_perf_inerr[$num_int]= $in_error_table . $tindex[$num_int];
#out
$oid_perf_outucast[$num_int]= $out_ucast_table . $tindex[$num_int];
$oid_perf_outnucast[$num_int]= $out_nucast_table . $tindex[$num_int];
$oid_perf_outerr[$num_int]= $out_error_table . $tindex[$num_int];
#speed
$oid_speed[$num_int]=$speed_table . $tindex[$num_int];
$num_int++;
}
}
# No interface found -> error
if ( $num_int == 0 ) { print "ERROR : Unknown interface $o_descr\n" ; exit $ERRORS{"UNKNOWN"};}
($result,$resultf)=(undef,undef);
# Get the requested oid values
$result = $session->get_request(
Varbindlist => \@oids
);
# Get the requested performance data
@oid_perf=(@oid_perf_inucast,@oid_perf_innucast,@oid_perf_inerr,@oid_perf_outucast,@oid_perf_outnucast,@oid_perf_outerr,@oid_speed);
$resultf = $session->get_request(
Varbindlist => \@oid_perf
);
if (!defined($resultf)) { printf("ERROR: Statistics table 1: %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}
if (!defined($result)) { printf("ERROR: Status table 2: %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}
@fin_res = undef;
$ipkts = undef;
$opkts = undef;
$in_err_rat = undef;
$out_err_rat = undef;
for (my $i=0;$i < $num_int; $i++) {
@temp_res = undef;
$ipkts = int($$resultf{$oid_perf_inucast[$i]}+$$resultf{$oid_perf_innucast[$i]});
$opkts = int($$resultf{$oid_perf_outucast[$i]}+$$resultf{$oid_perf_innucast[$i]});
if($$result{$oids[$i]}==1){
$stat="UP";
}
elsif($$result{$oids[$i]}==2){
$stat="down";
}
else{
$stat="testing";
}
if($ipkts != 0 || $opkts != 0){
$in_err_rat = $$resultf{$oid_perf_inerr[$i]}/$ipkts*100;
$out_err_rat = $$resultf{$oid_perf_outerr[$i]}/$opkts*100;
}
else{
$in_err_rat=0;
$out_err_rat=0;
}
if($$result{$oids[$i]}!= 1){
$status=3;
append($descr[$i] . ": " . "speed (" . $$resultf{$oid_speed[$i]} .
"); operational status (" . $stat .
"); received/sent packets/error ratio (" . $in_err_rat . "%/" . $out_err_rat . "%)!!!");
}
else{
append($descr[$i] . ": " . "speed (" . $$resultf{$oid_speed[$i]} .
"); operational status (" . $stat .
"); received/sent packets/error ratio (" . $in_err_rat . "%/" . $out_err_rat . "%)!!!");
}
}
}
sub test{
my %bla = %{$resultf};
my $key;
foreach $key (keys %bla){
print ($resultat{$key}, "==>", $key," : ",$bla{$key},"\n");
}
}
sub append{
my $appendstring = @_[0];
$res= "$res$appendstring";
}
####################################################################
# help and usage information #
####################################################################
sub usage {
print << "USAGE";
--------------------------------------------------------------------
$script v$script_version
$script_description
Usage: $script -H <hostname> -c <community> [...]
Options: -H Hostname or IP address
-p Port (default: 161)
-C Community (default is public)
-w Warning threshold (as %)
-c Critical threshold (as %)
--------------------------------------------------------------------
Copyright (C) 2003-2011 Opsera Limited. All rights reserved
This Nagios plugin comes with absolutely NO WARRANTY either implied
or explicit. This program is licensed under the terms of the GNU
General Public License (check source code for details)
--------------------------------------------------------------------
USAGE
exit 1;
}