#!/usr/bin/perl ############################################################################### #use diagnostics; use Net::SNMP; #use strict; #use warnings; ############################################################################### my $SNMPCommunity = "public"; my $SNMPPort = "161"; ############################################################################### ## Determine Hostname my $Host = undef; my $ifname = undef; my $OID = ".1.3.6.1.2.1.2.2.1.2"; $0 =~ /mikrotikifrate_(.+)*$/; unless ($Host = $1) { exit 2; } my @str = split(/_/, $Host); $Host = $str[0]; $ifname = $str[1]; ############################################################################### ## Initiate SNMP Session my ($Session, $Error) = Net::SNMP->session (-hostname => $Host, -community => $SNMPCommunity, -port => $SNMPPort, -timeout => 10, -retries => 3, -version => 2); if (!defined($Session)) { die "Croaking: $Error"; } ############################################################################### ## Find interface OID my $walkresult = $Session->get_table($OID); my @match = undef; while ( my ($id, $value) = each(%$walkresult) ) { if ( $value eq $ifname ) { @match = ("$id","$value"); } } @str = split(/\./, $match[0]); my $IFACEID = $str[-1]; my $IFACEOID = ".1.3.6.1.2.1.2.2.1.2.".$IFACEID; my $IFACEBYTESIN = ".1.3.6.1.2.1.31.1.1.1.6.".$IFACEID; my $IFACEBYTESOUT = ".1.3.6.1.2.1.31.1.1.1.10.".$IFACEID; ############################################################################### ## Configuration if ($ARGV[0] && $ARGV[0] eq "config") { print "host_name " . $Host . "\n"; print "graph_args --base 1000\n"; print "graph_title " . $ifname . " traffic\n"; print "graph_vlabel bits per second\n"; print "graph_category network\n"; print "graph_info This graph shows the incoming and outgoing traffic rate of an interface\n"; print "in.label received\n"; print "in.type DERIVE\n"; print "in.draw AREA\n"; print "in.min 0\n"; print "in.cdef in,8,*\n"; print "out.label sent\n"; print "out.type DERIVE\n"; print "out.draw LINE1\n"; print "out.min 0\n"; print "out.cdef out,8,*\n"; $Session->close; exit; } ############################################################################### ## Execution if (my $Result = $Session->get_request(-varbindlist => [$IFACEBYTESIN, $IFACEBYTESOUT])) { print "in.value " . $Result->{$IFACEBYTESIN} . "\n"; print "out.value " . $Result->{$IFACEBYTESOUT} . "\n"; $Session->close; exit; } $Session->close; exit;