Juniper SRX - Perl script - SNMP based RRDtool graphis from CPU usage and Flow Session (current)
Below is a simple perl script that queries queries a Juniper branch SRX box (210 in my case) for current data plance CPU usage as well as current flow numbers and creates RRDtool graphics based on the results.
Code:
# cat collect_srx_usage.pl
#!/usr/bin/perl
# This script pols a Juniper SRX branch device for Current Flow sessions and Data Plane CPU usage.
use RRDs;
# RRD databases location
my $rrd = '/var/home/www/static_pages/mrtg_graphs/rrd';
# images location
my $img = '/var/home/www/static_pages/mrtg_graphs/images';
&MIBtoProcess("Sessions", "1.3.6.1.4.1.2636.3.39.1.12.1.1.1.6.0", "Current Flow Sessions");
&MIBtoProcess("CPU", "1.3.6.1.4.1.2636.3.39.1.12.1.1.1.4.0", "Current SPU Usage");
sub MIBtoProcess
{
my $snmp = `snmpget -v2c -c public 10.12.1.2 $_[1] | awk '{print $NF}'`;
#print "$snmp";
my @snmp_array = split / /, $snmp;
my @r_snmp_array = reverse @snmp_array;
$snmp = $r_snmp_array[0];
#$snmp =~ s/[\x0A\x0D]//g;
chomp($snmp);
# create rrdtool database if it doesn't exist
if (! -e "$rrd/$_[0].rrd")
{
print "creating rrd database for $_[0] object ...\n";
RRDs::create "$rrd/$_[0].rrd",
"-s 60",
"DS:snmp:GAUGE:600:0:12500000",
"RRA:AVERAGE:0.5:1:1440",
"RRA:AVERAGE:0.5:5:2016",
"RRA:AVERAGE:0.5:120:372",
"RRA:AVERAGE:0.5:720:730";
}
# update RRD database
RRDs::update "$rrd/$_[0].rrd", "N:$snmp";
# generate graphs
&genGraph($_[0], "day", $_[2]);
&genGraph($_[0], "week", $_[2]);
&genGraph($_[0], "month", $_[2]);
&genGraph($_[0], "year", $_[2]);
}
sub genGraph
{
# subroutine for generating the graph
# $_[0]: SNMP object
# $_[1]: interval (day, week, month, year)
# $_[2]: Object description
RRDs::graph "$img/$_[0]-$_[1].png",
"-s -1$_[1]",
"-t Stats for SRX $_[0] :: $_[2] :: $_[1]",
"--lazy",
"-h", "80", "-w", "616",
"-l 0",
"-a", "PNG",
"DEF:snmp=$rrd/$_[0].rrd:snmp:AVERAGE",
"LINE1:snmp#0033CC",
"GPRINT:snmp:MAX: Max\\: %5.1lf ",
"GPRINT:snmp:AVERAGE: Avg\\: %5.1lf %S",
"GPRINT:snmp:LAST: Current\\: %5.1lf (avg)\\n",
"HRULE:0#000000";
if ($ERROR = RRDs::error) { print "$0: Error generating graph: $ERROR\n"; }
}
The script queries two OIDs using "snmpget" unix command ( see 'snmp' package ), parses the output and creates a RRD database if it is not created. Particularities of the rrd database is that it uses a 60 second step size (this indicates that the RRD database expects new values every 60 seconds). The "snmp" data source (DS) is "GAUGE": GAUGE does not save the rate of change. It saves the actual value itself. There are no divisions or calculations. Memory consumption in a server is a typical example of gauge
The graphic part is using a line to represent both values (AREA type can also be used, but I am interested in linear rrdtool graphic).
Graphics are created for last day, last week, last month and last year as seen in the structure.
Below is an example of such a graph (daily)
Attachment:
References: How to monitor CPU usage and flow sessions via SNMP - Juniper SRX Branch - 12.1X44
http://forum.ivorde.ro/how-to-monitor-cpu-usage-and-flow-sessions-via-snmp-juniper-srx-branch-12-1x44-t14261.htmlRRD tutorial
http://oss.oetiker.ch/rrdtool/tut/rrd-beginners.en.htmlNotes: the CPU usage is the same from Junos CLI command: "> show security monitoring fpc 0 " output line "CPU utilization : X %" and it represents the usage on the dataplane, not the control plane (routing engine).
Flow session represents the output from Junos CLI command: "> show security flow session summary".