Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log monitoring using shell script and send mail

help to write a script. below are logs format. I want to write a script which search for key word in LIVE log. suppose some have stopped the server and it will show shutdown or force_shutdown and it also shows in the log "server shutdown has been initiated by $user".

<Apr 19, 2017 1:11:00 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STARTING> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <Log Management> <BEA-170027> <The Server has established connection with the Domain level Diagnostic Service successfully.> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to shutdown> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to force_shutdown> 

<Jan 9, 2008 6:50:30 PM EST> <Notice> <WebLogicServer> <BEA-000388> <JVM
called WLS shutdown hook. The server will force shutdown now> 
<Jan 9, 2008 6:50:30 PM EST> <Alert> <WebLogicServer> <BEA-000396> <Server shutdown has been requested by <WLS Kernel>> 
<Jan 9, 2008 6:50:30 PM EST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to FORCE_SHUTTING_DOWN>

want to grep all info server IP and server host with exact time stamp when it was showdown and by which user .and send mail to the user with all the details to user. Please help me

like image 560
banarasi Avatar asked Sep 17 '25 15:09

banarasi


1 Answers

You can read in live a file by using a script like that : (remark : The script don't read the whole file, just the new future lines)

#!/bin/bash

LOG_FILE="/var/log/foo"

tail -n 0 -f "$LOG_FILE" | while IFS= read -r line; do
     echo $line
done

After you can easily search the string you want with a grep

#!/bin/bash

LOG_FILE="/var/log/foo"
SEARCHED="Server shutdown"

tail -n 0 -f "$LOG_FILE" | while IFS= read -r line; do
    if [ $(echo "${line}" | grep "${SEARCHED}") ] ; then
         echo "String find in : $line"
    fi
done

And the last part, you can parse the line with awk to extract what you want and send it. Search on Google for that, you'll find a lot of example :)

like image 180
Dorian Avatar answered Sep 20 '25 04:09

Dorian