Have some code. Share some code.
Showing results for 
Search instead for 
Did you mean: 
Custom Alert Banner

Problem this snippet solves:

This script polls the BIG-IP via SNMP and displays the current CPU utilization for each processor and the global utilization for all processors.

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);

$host = $ARGV[0];
$snmp_comm = $ARGV[1];
chomp ($host , $snmp_comm);

my $cpuIndex     = ".";

my $cpuUsageRatio5s = ".";
my $cpuUsageRatio1m = ".";
my $cpuUsageRatio5m = ".";

my $gcpuUsageRatio5s = ".";
my $gcpuUsageRatio1m = ".";
my $gcpuUsageRatio5m = ".";

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;
my $allCPU = $session->get_table ( -baseoid => $cpuIndex );
my %cpu_table = %{$allCPU};
my $x = 0;

print "\n\nCPU Utilization:\t5s\t1m\t5m\n\n";

foreach my $key (sort keys %cpu_table) {
my @oid_index = split(/\./, $key);
my $ltm_cpu5s = $cpuUsageRatio5s . $oid_index[-1];
my $ltm_cpu1m = $cpuUsageRatio1m . $oid_index[-1];
my $ltm_cpu5m = $cpuUsageRatio5m . $oid_index[-1];

my $oid_ratios = $session->get_request(
-varbindlist =>
[$ltm_cpu5s, $ltm_cpu1m, $ltm_cpu5m] );

print "\tCPU$x\t\t$oid_ratios->{$ltm_cpu5s}\t$oid_ratios->{$ltm_cpu1m}\t$oid_ratios->{$ltm_cpu5m}\n";


my $oid_gratios = $session->get_request(
-varbindlist =>
[$gcpuUsageRatio5s, $gcpuUsageRatio1m, $gcpuUsageRatio5m] );

print "\tGlobal\t\t$oid_gratios->{$gcpuUsageRatio5s}\t$oid_gratios->{$gcpuUsageRatio1m}\t$oid_gratios->{$gcpuUsageRatio5m}\n\n\n";



This is great but I'm getting an error at the same time I get the values, so Cacti will not generate graphs...


The error is:


Use of uninitialized value $allCPU in hash dereference at /usr/share/cacti/scripts/ line 39.


Can you help?


Thanks and regards, jtimartins


Version history
Last update:
‎12-Mar-2015 16:29
Updated by: