#!/bin/bash
#Date
#Author
#Purpose : Alert on failed count > 0
#Usage : ./alertonfail.sh <file>
#File with the failed count
FILE=$1
#Verify if the file paramter was set and if the file exists
if [ $# -lt 1 ]; then
echo "Usage : ./alertonfail.sh <file>"
elif [ ! -e $FILE ]; then
echo "File does not exist"
fi
#Take the count for each line
NRFAILED=`cut -d: -f2 $FILE | tr -d ' '`
#Verify the values for each line
for i in $NRFAILED; do
if [ $i -gt 0 ]; then
echo "Failure detected, the value is "$i | mail -s "FAILURE ALERT" email@adresss.com
#echo "Mail sent for failure > 0, Actual value is $i"
fi
done
#END
---------- Post updated at 03:29 AM ---------- Previous update was at 02:36 AM ----------
Or, in a line you can do something like this :
cat failures.txt | awk -F':' '{gsub(/ */,"",$2); print $2}' | xargs -I var bash -c 'if [ var -gt 0 ];then echo "Value is" var | mail -s "FAILURE" email@address.com ;fi'
If your log file contains much more that just these "total failed" records, and there are multiple of them in the file, then you have choices to make. Does each one have a time stamp to identify it, for instance?
You can easily loop through them with:-
for var in `grep "total failed" $FILE|tr ":" " "|tr -s " "|cut -f3 -d" "`
do
if [ $var -gt 0 ]
then
send your message here using $var
fi
done
What I'm doing here is to get all the "total failed" record, change the colon into a space, make all the spaces single spaces in case there are extras in there, then get just the value from field 3 into the value var to be used inside the loop as you wish.