BIG-IP Remote Interface Bandwidth Monitor

Problem this snippet solves:

This script polls the BIG-IP via SNMP and displays the current bandwidth utilization for one or all interfaces.

How to use this snippet:

  1. Install script on remote system with appropriate permissions.
  2. Make sure Perl and the Net::SNMP perl module are installed.

Code :

#!/usr/bin/perl -w
# This code is not supported by F5 Network and is offered under the GNU General Public License.  Use at your own risk.

use strict;
use Net::SNMP qw(:snmp);

my ($host, $snmp_comm, $interval, $int);

if (! @ARGV) {
    print "Please enter the LTM host name or IP: ";
    chomp ($host = );
    print "\nPlease enter the LTM SNMP Community String: ";
    chomp ($snmp_comm = );
    print "\nPlease enter the polling interval for calculating the delta: ";
    chomp ($interval = );
    print "\nPlease enter the interface you wish to poll (Enter 0 for all interfaces): ";
    chomp ($int = );
} else {
    my $usage = "ltm_intStat.pl    ";

    die "Usage: $usage\n" if $#ARGV != 3;

    $host = $ARGV[0];
    $snmp_comm = $ARGV[1];
    $interval = $ARGV[2];
    $int = $ARGV[3];
    chomp ($host , $snmp_comm , $interval, $int);
}

my $allInts_OID = ".1.3.6.1.4.1.3375.2.1.2.4.1.2.1.1";
my $ltm_InBytes_Index = "1.3.6.1.4.1.3375.2.1.2.4.4.3.1.3.";
my $ltm_OutBytes_Index = "1.3.6.1.4.1.3375.2.1.2.4.4.3.1.5.";

my ($session, $error) = Net::SNMP->session(
            -hostname       => $host,
            -community      => $snmp_comm,
            -port           => 161,
            -version        => 'snmpv2c',
            -nonblocking    => 0
            );

if (!defined $session) {
        print "Received no SNMP response from $host\n";
        print STDERR "Error: $error\n";
        exit -1;
        }
    
if ($int eq 0) {

    my $allInts = $session->get_table ( -baseoid => $allInts_OID );
    my %int_table = %{$allInts};
    print "\n\n";
    
    foreach my $key (sort keys %int_table){
        
        #Convert interface name to OID string
        my @chars = unpack("C" x length($int_table{$key}),$int_table{$key});
        my $int_length = length( $int_table{$key} );
        unshift(@chars, $int_length);
        my $int_oid = join('.' , @chars);
        
        #Build OID from index & interface
        my $ltm_intBytesIn = $ltm_InBytes_Index . $int_oid;
        my $ltm_intBytesOut = $ltm_OutBytes_Index . $int_oid;

        #Get first instance
        my $oids_1 = $session->get_request(
                  -varbindlist =>
                  [$ltm_intBytesIn, $ltm_intBytesOut] );
        
        # Wait for second poll
        sleep $interval;

        #Get second instance
        my $oids_2 = $session->get_request(
                  -varbindlist =>
                  [$ltm_intBytesIn, $ltm_intBytesOut] );

        #Calculate Rates
        my $rate_in = ($oids_2->{$ltm_intBytesIn} - $oids_1->{$ltm_intBytesIn})*8 / ($interval*1000000);
        my $rate_out = ($oids_2->{$ltm_intBytesOut} - $oids_1->{$ltm_intBytesOut})*8 / ($interval*1000000);
        my $rate_total = $rate_in + $rate_out;
        
        #Trim to 4 decimal places
        $rate_in =~s/(^\d{1,}\.\d{4})(.*$)/$1/;
        $rate_out =~s/(^\d{1,}\.\d{4})(.*$)/$1/;
        $rate_total =~s/(^\d{1,}\.\d{4})(.*$)/$1/;

        #Print Results
        print "Interface $int_table{$key} rate (Mbits/sec)";
        print "\n\n\t$rate_in (IN)\n";
        print "\t$rate_out (OUT)\n";
        print "\t$rate_total (TOTAL)\n\n";        
    }
} else {
    
    #Convert interface name to OID string
    my @chars = unpack("C" x length($int),$int);
    my $int_length = length( $int );
    unshift(@chars, $int_length);
    my $int_oid = join('.' , @chars);
    
    #Build OID from index & interface
    my $ltm_intBytesIn = $ltm_InBytes_Index . $int_oid;
    my $ltm_intBytesOut = $ltm_OutBytes_Index . $int_oid;

    #Get first instance
    my $oids_1 = $session->get_request(
                  -varbindlist =>
                  [$ltm_intBytesIn, $ltm_intBytesOut] );

    sleep $interval;

    #Get second instance
    my $oids_2 = $session->get_request(
                  -varbindlist =>
                  [$ltm_intBytesIn, $ltm_intBytesOut] );

    #Calculate Rates
    my $rate_in = ($oids_2->{$ltm_intBytesIn} - $oids_1->{$ltm_intBytesIn})*8 / ($interval*1000000);
    my $rate_out = ($oids_2->{$ltm_intBytesOut} - $oids_1->{$ltm_intBytesOut})*8 / ($interval*1000000);
    my $rate_total = $rate_in + $rate_out;
    
    #Trim to 4 decimal places
    $rate_in =~s/(^\d{1,}\.\d{4})(.*$)/$1/;
    $rate_out =~s/(^\d{1,}\.\d{4})(.*$)/$1/;
    $rate_total = ~s/(^\d{1,}\.\d{4})(.*$)/$1/;

    #Print Results
    print "Interface $int rate (Mbits/sec)";
    print "\n\n\t$rate_in (IN)\n";
    print "\t$rate_out (OUT)\n";
    print "\t$rate_total (TOTAL)\n\n";
    
}
Published Mar 12, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment