-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlog.sh
40 lines (32 loc) · 863 Bytes
/
mlog.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
#version 1.1
#2012-05-14
EMAILADDRESS="[email protected]"
EMAILSUBJECT="***** We've got a problem *****"
MESSAGEBODY="errot_path/error.txt"
SEARCHSTRING='string'
LOGPATH="/log_path/xxx.log"
cat /dev/null > $MESSAGEBODY
LASTOCCURANCE=`egrep -n "$SEARCHSTRING" $LOGPATH | tail -1`
LASTLINE=`echo $LASTOCCURANCE | cut -d':' -f1`
while true
do
END=`wc -l $LOGPATH | cut -d' ' -f1`
if [ $END -lt $LASTLINE ]
then
LASTLINE=$END
fi
#echo $LASTLINE
tail -n +$LASTLINE $LOGPATH | grep -C $LASTLINE "$SEARCHSTRING" > $MESSAGEBODY
if [ `stat -c %s $MESSAGEBODY` -ne '0' -a $LASTLINE -ne $END ]
then
echo "We have an above problem, please check." >> $MESSAGEBODY
mail -s "$EMAILSUBJECT" "$EMAILADDRESS" < $MESSAGEBODY
cat /dev/null > $MESSAGEBODY
((LASTLINE=$LASTLINE+1))
fi
LASTLINE=$END
cat /dev/null > $MESSAGEBODY
rm -rf $MESSAGEBODY
sleep 5
done