linuxbashshellrhelsnmp-trap

Shellscript to monitor a log file if keyword triggers then run the snmptrap command


Is there a way to monitor a log file using shell script like tail -f /var/log/errorlog.txt then if something like down keyword appears, then generate SNMPTRAP to snmp manager and continues the monitoring

I have a SNMP script available to generate SNMPTrap and it looks like

snmptrap -v v2c -c community host "Error message"

Lets the say the script name is snmp.sh

My question is how to perform the below operation

  1. tail the logs
  2. if keyword[down] matches then use snmp.sh script to send alert
  3. else leave

As per the suggestion i tried this

tail -F  /data/log/test.log |

egrep -io 'got signal 15 | now exiting' | 

while read -r line ;
do

    case "$line" in
       "got signal 15")
        echo "hi"
            ;;
        "now exiting")
        echo "hi2"
             ;;
        *)


    esac
done

but the problem is tail is not working here with case statement, whenever the new log details added its not going to the case statement and echos the output

I could get the output if i use cat/less/more

Could you someone please tell what mistake i have done here ?

Thanks in advance


Solution

  • It sounds like the pattern you want is this:

    tail -f /var/log/errorlog.txt | grep -e down -e unmounted | while read -r line
    do
        case "$line" in
            down)
                ./snmp.sh …
                ;;
            unmounted)
                ./snmp.sh …
                ;;
            *)
                echo "Unhandled keyword ${line}" >&2
                exit 1
        esac
    done