Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Wed Aug 23, 2017 9:02 am


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Author Message
mandrei99
Post  Post subject: Juniper SRX - Perl script - SNMP based RRDtool graphis from CPU usage and Flow Session (current)  |  Posted: Mon Nov 04, 2013 11:43 am

Joined: Tue Aug 04, 2009 9:16 am
Posts: 245

Offline
 

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:
RRdtool-srx-cpu-usage-current-sessions.png [24.76 KiB]
Downloaded 1195 times



References: How to monitor CPU usage and flow sessions via SNMP - Juniper SRX Branch - 12.1X44http://forum.ivorde.ro/how-to-monitor-cpu-usage-and-flow-sessions-via-snmp-juniper-srx-branch-12-1x44-t14261.html
RRD tutorial http://oss.oetiker.ch/rrdtool/tut/rrd-beginners.en.html

Notes: 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".





Top
mandrei99
Post  Post subject: Re: Juniper SRX - Perl script - SNMP based RRDtool graphis from CPU usage and Flow Session (current)  |  Posted: Mon Nov 04, 2013 11:47 am

Joined: Tue Aug 04, 2009 9:16 am
Posts: 245

Offline
Have this added to your cron jobs:
Code:
*       *       *       *       *       /var/scripts/collect_srx_usage.pl >/dev/null 2>&1

I run it every minute, as it uses a 60 second step size.


Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "Juniper SRX - Perl script - SNMP based RRDtool graphis from CPU usage and Flow Session (current)"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. How to replace new line in perl script

mandrei99

1

1817

Wed Jun 26, 2013 8:18 am

admin View the latest post

There are no new unread posts for this topic. Removing new line / EoL characters in a PERL script

mandrei99

0

2337

Mon Nov 04, 2013 6:47 am

mandrei99 View the latest post

There are no new unread posts for this topic. PERL: How to replace non-ascii characters/bytes in a file

admin

0

1130

Fri Apr 10, 2015 8:58 am

admin View the latest post

There are no new unread posts for this topic. Password generator with user inputs bash script

Kvr123

0

323

Thu Dec 01, 2016 5:06 am

Kvr123 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO