-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService monitor
More file actions
77 lines (61 loc) · 2.64 KB
/
Service monitor
File metadata and controls
77 lines (61 loc) · 2.64 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
#GIVE SERVICE NAME TO MONITOR AS ARGUMENT.
#TESTED ON UBUNTU SERVER 12.04
function SendMail() {
SECS=7200;
COUNT=0;
if [[ "$1" == "NOT" ]]; then
FLAG=1
T_H=${3:11:2};T_M=${3:14:2};T_S=${3:17:2}
fi
while [[ 0 -ne $SECS ]]; do
sleep 1
SECS=$[$SECS-1]
COUNT=$((COUNT+1))
STATUS_=`/etc/init.d/$2 status| grep -oE 'NOT|not'`;
if [[ "$STATUS_" == "NOT" ]]||[[ "$STATUS_" == "not" ]]; then
STATUS_CHECK="NOT"
else
STATUS_CHECK="OK"
fi
if [[ "$COUNT" -eq "3" ]]; then
if [[ "$STATUS_CHECK" == "NOT" ]]; then
echo -e "From:webmaster@globeone.gr\nTo:ganag16@gmail.com\nMIME-Version: 1.0\nContent-Type: text/plain\nSubject: SERVER PROBLEM\n$2 is down\nIt is a good time to panic!"|sendmail -t -i;
FLAG=2
fi
if [[ "$STATUS_CHECK" == "OK" ]]&&[[ "FLAG" -eq "1" ]]; then
echo -e "From:webmaster@globeone.gr\nTo:ganag16@gmail.com\nMIME-Version: 1.0\nContent-Type: text/plain\nSubject: SERVER RESTARTED\nNothing crucial.\n$2 is back in action!"|sendmail -t -i;
FLAG=3
fi
fi
if [[ "$STATUS_CHECK" == "OK" ]];then
if [[ "FLAG" -eq "2" ]]; then
CURDATE=`date`;
CURT_H=${CURDATE:11:2};CURT_M=${CURDATE:14:2};CURT_S=${CURDATE:17:2};
if [[ "$T_S" -gt "$CURT_S" ]]; then
DOWNT_S=$((60-T_S))
CURT_M=$((CURT_M-1))
else
DOWNT_S=$((CURT_S-T_S))
fi
if [[ "$T_M" -gt "$CURT_M" ]]; then
DOWNT_M=$((60-T_M))
CURT_H=$((CURT_H-1))
else
DOWNT_M=$((CURT_M-T_M))
fi
if [[ "$T_H" -gt "$CURT_H" ]]; then
echo "Wrong arguments!"
exit 85
else
DOWNT_H=$((CURT_H-T_H))
fi
echo -e "From:webmaster@globeone.gr\nTo:ganag16@gmail.com\nMIME-Version: 1.0\nContent-Type: text/plain\nSubject: SERVER PROBLEM RESTORED\nService $2 was down for $DOWNT_H hours, $DOWNT_M mins and $DOWNT_S secs.\nBut thanks to our sysadmin the problems were solved!"|sendmail -t -i;
echo -e "From:webmaster@globeone.gr\nTo:ganag16@gmail.com\nMIME-Version: 1.0\nContent-Type: text/plain\nSubject: SERVER PROBLEM RESTORED\nService $2 was down for $DOWNT_H hours, $DOWNT_M mins and $DOWNT_S secs.\nBut thanks to our sysadmin the problems were solved!"|sendmail -t -i;
break
elif [[ "FLAG" -eq "3" ]];then
break
fi
fi
done
}