Perl SNMP Analysis Script

Perl SNMP Analysis Script

Nagios is one of the best open source complete system health monitoring component. It will show you each point of system, including overview of snmp statistics.

In general, Nagios scripts are available which will give you the current OID Value, which will get compared with WARNING & CRITICAL values. Now in many case we want to check the current OID value with the last avaialble OID & generate OK, WARNING & CRITICAL notification accordingly.

I've prepared a Perl script (tested for Linux, Unix distribution). This script will use snmpwalk  command to fetch OID value & maintain value in a file. Now on next time, when script get called, it will again get new OID value and subtract with value available in file. By doing this, it will give you a clear picture by what amount of values are increasing. 


#! /usr/bin/perl
##############################  SCRIPT START ##################
#
##
# This code is Copyright(c) 2013 by Yuvam Jain
# All rights reserved.
#
# You may use this program without charge, you may copy it for
# backup purposes, you may even give it away to anyone you like.
# You may not charge a fee for it.  You may charge a fee for
# a distribution mechanism (such as a CD-ROM containing it), but
# such fee must not exceed the amount required to recoup costs.
# You may modify it for your own purposes, but this notice must
# remain attached to all copies and derivitive works.
#
# If you make modifications that you think others would like, please
# send diffs of your changes to the author at
# <yuvamjain@gmail.com>.
#
#
#             USAGE
# Run script with following parameters.
# perl SRCIPT.pl IP PORT OID WARNINGValue CRITICALValue

################################################
use warnings;

# Declaration of Variables.my $ip=$ARGV[0];
my $port=$ARGV[1];
my $oid=$ARGV[2];
my $warning = $ARGV[3];
my $critical= $ARGV[4];
my $file_name="/var/log/$oid.txt"; #Name of Log file to keep track of Older Count 
#Running Snmp walk command to get the value of specified oidmy $result=`snmpwalk -v 1 -c public $ip:$port $oid`;


# Spliting & triming Result@snmp_value=split(":",$result);
chomp ($snmp_value[3]);
$result=$snmp_value[3];
chomp($result);
$result =~ s/^\s+//;
$result =~ s/\s+$//;


    #Opening the file to read and write the counter difference     open (READFILE, $file_name);
    my $file_count = <READFILE>;
     chomp($file_count);
    $old_count=$file_count;
    close READFILE;

    #Difference between Current OID value & Values fetched in last attempt     $actual_count=$result-$old_count;
    # Writing Current OID value to file, for future use    my $write_status=`echo '$result'>$file_name`;

    #Calling NagiosFormatOutput with Actual Difference count, Warning & Critical variables are argument    NagiosFormatOutput($actual_count,$warning,$critical);

#Function for print the output of program sub NagiosFormatOutput
{

    #Nagios constant output variable    $OK_STATE=0;
    $WARNING_STATE=1;
    $CRITICAL_STATE=2;
   
                  
    #Get function argument using $_[index]    $FinalResult     = $_[0];
    $warning     = $_[1];
    $critical     = $_[2];
                                      
        # Set the IF ELSE condition accordingly                          
        if (( $FinalResult < $warning ) && ( $FinalResult < $critical ))
        {
                   print "OK is : =".$FinalResult."= OK"."| Value = ".$FinalResult.";".$warning.";".$critical ;
            exit $OK_STATE;
        }
        elsif  (($FinalResult => $warning) && ($FinalResult < $critical))
       {
            print "Warning is : ".$FinalResult . " Warning"."| Value = ".$FinalResult.";".$warning.";".$critical ;
            exit $WARNING_STATE;
        }
       elsif  ($FinalResult => $critical)
      {
            print "CRITICAL is : ".$FinalResult . " Critical"."| Value = ".$FinalResult.";".$warning.";".$critical ;
            exit $CRITICAL_STATE;
       }
}

######################## SCRIPT END ############################

******************** IMPORTANT *********
Remember following things while using above script
1- As script will use file handling and logged value in a file , make sure to give full permission to that file.
As we are calling file from Nagios , give nagios user full permission for file. Otherwise, new difference value will not be written.

2- Give full permission for execution of script through Nagios user.


*****************
Like the post and choose Follow and stay tuned for more updates.
JUST BELIEVE IN TECHNOLOGY..... Enjoy....

Comments

Popular Posts