Friday, May 24, 2013

Band-width monitoring for CISCO 5K and 7K switches

Run the below perl script and re-direct the output to the /usr/local/nagios/libexec/band-width-output/host-ipaddress
----------------------------------------------------------------------------------------------------------

#!/usr/bin/perl -w
#Purpose         : To pull the interface band-width information from the 5K and 7K switches
#Authors         : Babu Dhinakaran S
#Date              : 24th May 2011
#Version         : V1.0

    use Net::SSH::Perl;
    $host="deviceip address";
    $user="username";
    $pass="password";
    $cmd="show interface";
    my $ssh = Net::SSH::Perl->new($host);
    $ssh->login($user, $pass);
    my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
    print $stdout;

----------------------------------------------------------------------------------------------------------
Run the below script for pull the interface band-width information from the output file(/usr/local/nagios/libexec/band-width-output/host-ipaddress)
----------------------------------------------------------------------------------------------------------
#!/bin/bash
#Purpose         : To extract the bandwidth value from the file
#Authors         : Ranjith Kumar R
#Date            : 21st May 2011
#Version         : V1.0


PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="V1.0"
ECHO="/bin/echo"
STATE_UNKNOWN=3
STATE_OK=0
STATE_CRITICAL=2
STATE_WARNING=1
TAIL="/usr/bin/tail"
print_usage() {
    echo "Usage: $PROGNAME -H Hostaddress -i interfacename -u unit in Gigabytes -w warning percentage -c critical percentage"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}
print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "Bandwidth monitor plugin for Nagios"
    echo ""
    support
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 5 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi
# Grab the command line arguments

exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --hostaddress)
            HOSTADDRESS=$2
            shift
            ;;
        -H)
            HOSTADDRESS=$2
            shift
            ;;
--interface)
            interface=$2
            shift
            ;;
-i)
            interface=$2
            shift
            ;;

        --warning)
            warning=$2
            shift
            ;;

        --unit)
            unit=$2
            shift
            ;;
        -u)
            unit=$2
            shift
            ;;
        -w)
            warning=$2
            shift
            ;;
--critical)
            critical=$2
            shift
            ;;
        -c)
            critical=$2
            shift
            ;;
        -x)
            exitstatus=$2
            shift
            ;;
        --exitstatus)
            exitstatus=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

WORKINGDIR="/usr/local/nagios/libexec/band-width-output"

`lsof | grep "$HOSTADDRESS" > /dev/null`
        lsof_ret=`echo $?`

        until [ "1" -le "$lsof_ret" ]
        do
        `lsof | grep "$HOSTADDRESS" > /dev/null`
        lsof_ret=`echo $?`
        sleep 2
        done

unitbits=1000000000

bits=$( echo "$unitbits * $unit" | bc )
warning=$( echo "$bits * $warning" | bc )
warning=$( echo "$warning / 100" | bc )
critical=$( echo "$bits * $critical" | bc )
critical=$( echo "$critical / 100" | bc )

#bits=$(( 1000000000 * $unit ))
#warning=$((( $bits * $warning ) / 100 ))
#critical=$((( $bits * $critical ) / 100 ))

FILEAGE=`/usr/local/nagios/libexec/check_file_age -w 600 -c 900 -f $WORKINGDIR/$HOSTADDRESS`
result=`echo $?`

if [ "$result" -ne "0" ];
then
    $ECHO "UNKNOWN : Please contact Ranjith or Babu. $FILEAGE"

`/bin/echo "Please contact Ranjith or Babu" | /bin/mail -s "Incomplete file write call Ranjith or Babu" ranjith.rajendran\@test.com babu.dhinakaran\@test.com -- -f no_reply_nagios\@test.com`;
exit $STATE_UNKNOWN
exit $exitstatus

fi

inputvalue=`cat $WORKINGDIR/$HOSTADDRESS | grep -A 22 "$interface is" | egrep "input rate [0-9]* bits/sec" | awk '{print $5}'`
outputvalue=`cat $WORKINGDIR/$HOSTADDRESS | grep -A 22 "$interface is" | egrep "output rate [0-9]* bits/sec" | awk '{print $5}'`

if [ "$inputvalue" == "" ] || [ "$outputvalue" == "" ]; then
$ECHO "UNKNOWN : input and output values are null"
exitstatus=$STATE_UNKNOWN
                exit $exitstatus

        fi


`$ECHO $inputvalue | egrep -q ^[0-9]+$`
inputvalue_result=`echo $?`
`$ECHO $outputvalue | egrep -q ^[0-9]+$`
outputvalue_result=`echo $?`
inputusedper=$( echo "$inputvalue / $bits" | bc -l )
inputusedper=$( echo "$inputusedper * 100" | bc -l )
outputusedper=$( echo "$outputvalue / $bits" | bc -l )
outputusedper=$( echo "$outputusedper * 100" | bc -l )

if [ "$inputvalue_result" -ne 0 ] || [ "$outputvalue_result" -ne 0 ]; then

$ECHO "CRITICAL : input rate and output rate values are not integer"

                exitstatus=$STATE_CRITICAL

                exit $exitstatus
        fi

if [ "$warning" -gt "$critical" ]; then

$ECHO "CRITICAL : Warning threshold should be less than critical"

exitstatus=$STATE_CRITICAL

exit $exitstatus
        fi

if [ "$inputvalue" -gt "$critical" ] ||  [ "$outputvalue" -gt "$critical" ]; then

$ECHO "CRITICAL : High Band-width utilization on $interface interface input rate used percentage=$inputusedper"%" and output rate used percentage=$outputusedper"%"|INPUT_RATE_USED%=$inputusedper"%",OUTPUT_RATE_USED%=$outputusedper"%",input-rate=$inputvalue"bits";$warning;$critical,output-rate=$outputvalue"bits";$warning;$critical"

exitstatus=$STATE_CRITICAL

                exit $exitstatus
        fi


if [ "$inputvalue" -gt "$warning" ] || [ "$outputvalue" -gt "$warning" ]; then

$ECHO "WARNING : High Band-width utilization on $interface interface input rate used percentage=$inputusedper"%" and output rate used percentage=$outputusedper"%"|INPUT_RATE_USED%=$inputusedper"%",OUTPUT_RATE_USED%=$outputusedper"%",input-rate=$inputvalue"bits";$warning;$critical,output-rate=$outputvalue"bits";$warning;$critical"

exitstatus=$STATE_WARNING

exit $exitstatus
        fi

if [ "$inputvalue" -lt "$warning" ] || [ "$outputvalue" -lt "$warning" ]; then

$ECHO "OK: Band-width utilization on $interface interface input rate used percentage=$inputusedper"%" and output rate used percentage=$outputusedper"%"|INPUT_RATE_USED%=$inputusedper"%",OUTPUT_RATE_USED%=$outputusedper"%",input-rate=$inputvalue"bits";$warning;$critical,output-rate=$outputvalue"bits";$warning;$critical"

exitstatus=$STATE_OK

exit $exitstatus

else

$ECHO "UNKNOWN,High Band-width utilization on $interface interface input rate used percentage=$inputusedper"%" and output rate used percentage=$outputusedper"%"|INPUT_RATE_USED%=$inputusedper"%",OUTPUT_RATE_USED%=$outputusedper"%",input-rate=$inputvalue"bits";$warning;$critical,output-rate=$outputvalue"bits";$warning;$critical"

exitstatus=$STATE_UNKNOWN

exit $exitstatus

fi
fi



Monday, May 20, 2013

How to Retrieve command output of a remote machine using expect script


#!/usr/bin/expect
#Purpose : Retrieve command output of a remote machine using expect script
#Author : Ranjith Kumar R
#Date : 20th May 2013

set timeout 60
set log [open "log.txt" "w"]
spawn telnet IP address of a server
expect "login:"
send "username\r"
expect "Password:"
send "password\r"
expect "#"
send "command name"
send "\r"
set outcome $expect_out(buffer)
puts $log $outcome
send "exit\r"
expect eof

Monday, May 6, 2013

shell script for Generate the service.cfg for monitoring network device interfaces


#!/bin/bash
#Purpose : TO generate interface monitoring services.cfg



while read devicelist

do
ip=`echo $devicelist | awk '{print $2}'`
devicename=`echo $devicelist | awk '{print $1}'`

`snmpwalk -Os -c communitystring -v 1 $ip IfDescr | awk '{print $4}' > /tmp/interface.txt`


while read interfacename

do


echo "define service{
        host_name                       $devicename
        service_description             $interfacename
        use                             service
        check_command                   check_ifoperstatus!$interfacename!communitystring
}"

done < /tmp/interface.txt

done < /tmp/deviceip