Linux and Unix Scripting

Linux Monitor Log files changes – Send report by sendgrid

This script will monitor and send an email thru sendgrid if there any log change or any new entry in the log

 

#!/bin/bash
SENDGRID_API_KEY=”<API KEY HERE>”
EMAIL_TO=”<YOUR EMAIL>”
FROM_EMAIL=”<ANYTHING YOU WANT>”
FROM_NAME=”<Server Log Update>”
SUBJECT=”Log Update”

 

if [ $# -ne 2 ];then
echo “Usage: ${0##/} [FILE] [TIME_TO_WAIT]” # for you /var/log/vsftpd.log or something like that
exit 1
fi
# example usage: ./<scripname> /var/log/vsftpd.log 60 # it will check this file in every 1 min

echo “Monitoring on $1 file…”

line_count=$(wc -l “$1″ |awk {‘printf $1’})
while true;do

[restrict paid=”true”]

[ihc-hide-content ihc_mb_type=”block” ihc_mb_who=”reg” ihc_mb_template=”1″ ]
new_line_count=$(wc -l “$1″|awk {‘printf $1’})
wc -l “$1” | awk {‘printf $1’} &> /dev/null
if [[ $line_count -ne $new_line_count ]];then
line_count=$new_line_count
new_content=$(echo “File updated at [$(date +%T)]… New Content [$(cat $1|tail -1|head -1)]”)
echo $new_content

bodyHTML=”<p>$new_content</p>”
maildata='{“personalizations”: [{“to”: [{“email”: “‘${EMAIL_TO}'”}]}],”from”: {“email”: “‘${FROM_EMAIL}'”,
“name”: “‘${FROM_NAME}'”},”subject”: “‘${SUBJECT}'”,”content”: [{“type”: “text/html”, “value”: “‘${bodyHTML}'”}]}’

curl –request POST \
–url https://api.sendgrid.com/v3/mail/send \
–header ‘Authorization: Bearer ‘$SENDGRID_API_KEY \
–header ‘Content-Type: application/json’ \
–data “‘$maildata'”

fi [/restrict]

sleep $2 # time to wait (for example 60sec which = 1min) ##MUST BE SET IN SECODS##
done