Checking Remote File for ‘ 404 ‘ Pattern, Send Email If Found (Not Optimized yet)

#!/bin/bash

for (( ; ; ))
do

result =””

echo “Resetting Variables”

sec_latest=”$(ls /home/max/Desktop/SSH\ Watchdog/ -Art | tail -n 1)”

echo “‘”$sec_latest”‘”” was last file modified”

lines_old=”$(grep -c “.*” /home/max/Desktop/SSH\ Watchdog/${sec_latest})”

echo $lines_old

# tocopy=”$(ssh root@192.168.2.200 ‘ls /home/max/Desktop -Art | tail -n 1’)”

# Edit: The previous command doesn’t work with Absolute Path so use this one instead

tocopy=”$(ssh root@192.168.2.200 ‘ls /home/max/Desktop/SSH\ Watchdog/’20130619’* -Art | tail -n 1 | sed ‘s/[^\/]*\///g”)”

echo $tocopy

scp root@192.168.2.200:/home/max/Desktop/${tocopy} /home/max/Desktop/SSH\ Watchdog/

echo “SCP Command Executed”

DATE=$(date +%Y%m%d%H%M%S)

cp /home/max/Desktop/SSH\ Watchdog/${tocopy} /home/max/Desktop/SSH\ Watchdog/$DATE.txt

echo “File Renamed”

lines_new=”$(grep -c “.*” /home/max/Desktop/SSH\ Watchdog/${tocopy})”

echo $lines_new

tocheck=”$(sed “$lines_old,$lines_new!d” /home/max/Desktop/SSH\ Watchdog/${tocopy})”

echo “Data to be checked retrieved”

datachunk=/home/max/Desktop/logs/chunk.txt

echo “$tocheck” > “$datachunk”

echo “datachunk saved”

result=”$(grep -A 0 ” 404 ” ${datachunk})”

echo “$result” > “/home/max/Desktop/logs/${DATE}.chunk”

if [[ “$result” =~ ” 404 ” ]]

then

echo $result

echo “$result” | mutt -s “ERROR” — pointed.out@gmail.com

else

echo “All Is Well”

fi

sleep 1s

done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.