#!/bin/bash

#   cubiclegraffiti@gmail.com www.cubiclegraffiti.net

#    Check Cisco devices to see if CRCs are increasing.
#    In the Cisco device, determine which interface you want to monitor: show int desc
#    Find the ifindex number for the interface: show snmp mib ifmib ifindex
#    This script requires a place to keep temp files
#    for the current CRC values: $CRCFILE
#
#    In commands.cfg:
#  define command{
#        command_name    check_crc_increase
#        command_line    $USER1$/check_crc_increase $HOSTADDRESS$ $ARG1$ $ARG2$
#        }
#
#     In services.cfg:
# check_command                   check_crc_increase!<if_index_number>!<snmp_comm_string>

CRCFILE=/data1/nagios/tmp/crc_${1}.${2}.increase
LASTVAL=$(cat ${CRCFILE} | awk '{print $1}')
CURRENTVAL=$(snmpget -v1 -Cf -c ${3} ${1} 1.3.6.1.4.1.9.2.2.1.1.12.${2} | awk '{print $NF}')
CRCTIMES=$(cat ${CRCFILE} | awk '{print $2}')
CRCINCR=$(($CRCTIMES+1))
CRCINCRNUM=10000

        if [ $(date +%H) = "01" -a $(date +%M) -lt "15" ]
        then
        echo "$CURRENTVAL 0" > $CRCFILE
        exit 0
        fi

####### If CRC Checks have increased X times, then send critical status ########
  if  [[ ${CRCTIMES} -gt $CRCINCRNUM ]]
  then
        if  [[ $LASTVAL -gt $CURRENTVAL ]]
        then
                echo "$CURRENTVAL 0" > $CRCFILE
                echo "CRCs are less than last check $LASTVAL to $CURRENTVAL. Resetting count"
                exit 1
        else
                echo "$CURRENTVAL $CRCINCR" > $CRCFILE
                echo "$CRCINCRNUM or more CRC checks have shown an increase today.  Please check."
                exit 2
        fi
  fi

##################################################################################

        if [ -z "${LASTVAL}" -a "${CURRENTVAL}" ]
        then
                echo "$CURRENTVAL 0" > $CRCFILE
                echo "No Data - Checking"
                exit 3
        elif [ -z "${LASTVAL}" -a -z "${CURRENTVAL}" ]
        then 
                echo "No Data, Please Check."
                exit 2
        elif [[ $LASTVAL -eq $CURRENTVAL ]]
        then
                echo "CRC OK"
                exit 0
        elif  [[ $LASTVAL -lt $CURRENTVAL ]]
        then
                echo "$CURRENTVAL $CRCINCR" > $CRCFILE
                echo "CRCs up from $LASTVAL to $CURRENTVAL - Check Serial Interface"
                exit 1
        elif  [[ $LASTVAL -gt $CURRENTVAL ]]
        then
                #sed -i 's/'"$1"'\ '"$LASTVAL"'$/'"$1"'\ '"$CURRENTVAL"'/g' "$CRCFILE"
                echo "$CURRENTVAL 0" > $CRCFILE
                echo "CRCs are less than last check $LASTVAL to $CURRENTVAL.  What gives?"
                exit 1
        fi

        exit 3
