isdnbearerchannnel status script for a graph in cacti

–based on dnsresponsetimeloop.pl

#!/usr/bin/perl
use strict;
use Net::SNMP;
use Net::SNMP 6.0 qw( :snmp DEBUG_ALL ENDOFMIBVIEW );
use Getopt::Long;

=head1 NAME

isdnbchannels.pl – Channel Summary of ISDN interface

=head1 SYNOPSIS

isdnbchannels.pl -h hostip -c snmp_community -i interfacename -v

./isdnbchannels.pl -129.132.98.12 -snmp_community public -i Serial3/1 -v

hostip: destination gateway/router
snmp_community: snmpcommunity
interfacename: interface name of the main isdn intefrace, Serial2/3 etc
=head1 DESCRIPTION

This script queries destination for getting number of isdn channels status, which can be unknown, answer, originate, call back.
First ifindexes of the each channel is found by using IF:MIB (from if descriptions and given interface name)
Then each channel is checked for its status.

=head1 PROBLEMS
=head1 AUTHOR

14. Jan 2014 / Deniz AYDIN denizaydin@monsterdark.com
– modified for Cacti

=cut

my ($host,$snmpcommunity,$snmpversion,$interfacename,$verbose);

GetOptions(
“h=s” => \$host,
“c=s” => \$snmpcommunity,
“i=s” => \$interfacename,
);

if(!$host || !$snmpcommunity || !$interfacename) {
print “Not enough arguments\n”;
exit 0;
}
my $unknownchannels=0; #total number of channels in unknown status
my $answerchannels=0; #total number of channels in answer status
my $originatechannels=0; #total number of channels in originate status
my $callbackchannels=0; #total number of channels in call back status
my @bearerifindex;
my ($session, $error) = Net::SNMP->session(
-hostname => shift || $host,
-community => shift || $snmpcommunity,
-translate => [-octetstring => 0],
-version => ‘snmpv2c’,
-timeout => 1,
);

if (!defined $session) # check snmp connection
{
print “NET::SNMP session creation Error,$error.\n”;
exit 0;
}

my $oid=’1.3.6.1.2.1.2.2.1.2′;#ifDescr
my @ARGV;
push @ARGV, “$oid”;
my @args = (
-varbindlist => [($ARGV[0] eq q{.}) ? ‘0’ : $ARGV[0]],
);
my $last_oid = $ARGV[0];
push @args, -maxrepetitions => 25;
GET_BULK: while (defined $session->get_bulk_request(@args)) {
my @oids = $session->var_bind_names();
if (!scalar @oids) {
return;
}
for my $oid (@oids) {
# Make sure we have not hit the end of the MIB.
if (!oid_base_match($ARGV[0], $oid)) {
last GET_BULK;
}
my $valuetype=$session->var_bind_types()->{$oid};
my $value=$session->var_bind_list()->{$oid};
my $index=$oid;
$index=~s/^(.*)(\.)(\d+)/\3/;
$last_oid = $oid;
if ($value =~m/($interfacename)(.*)(Bearer Channel)/)
{
push(@bearerifindex,$index);
}
}
@args = (-maxrepetitions => 25, -varbindlist => [$last_oid]);
if ($session->error()) {
print “NET::SNMP snmpwalk Error\n”;
exit 0;
}
}
my $totalnumberofchannels=@bearerifindex;

foreach my $bearerindex (@bearerifindex)
{
my $OID_isdnBearerCallOrigin=’1.3.6.1.2.1.10.20.1.2.1.1.6.’.$bearerindex;
my $result = $session->get_request(-varbindlist => [ $OID_isdnBearerCallOrigin ],);
$unknownchannels++ if ($result->{$OID_isdnBearerCallOrigin} == 1) ;
$answerchannels++ if ($result->{$OID_isdnBearerCallOrigin} == 2) ;
$originatechannels++ if ($result->{$OID_isdnBearerCallOrigin} == 3) ;
$callbackchannels++ if ($result->{$OID_isdnBearerCallOrigin} == 4) ;
}
$session->close();
print “unknown:$unknownchannels answer:$answerchannels originate:$originatechannels callback:$callbackchannels totalnumberofchannels:$totalnumberofchannels\n”;