-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-services.sh
executable file
·131 lines (115 loc) · 2.39 KB
/
check-services.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
#
# Date: 1 December, 2014
# Author: Aman Hanjrah and Peeyush Budhia
# URI: http://techlinux.net and http://phpnmysql.com
# License: GNU GPL v2.0
# Description: The script is used to check the status of all the services, if stopped then script will try to start the service, if the service not get started the script will send a notification email using mutt.
#
main() {
prerequisites
init
chkAllServices
sendMail
}
prerequisites() {
CHK_MUTT=`rpm -qa | grep mutt`
if [[ ! -n '$CHK_MUTT' ]]; then
yum -y install mutt
unset CHK_MUTT
CHK_MUTT=`rpm -qa | grep mutt`
if [[ ! -n "$CHK_MUTT" ]]; then
exit 1
fi
fi
}
init() {
EMAIL_TO="someone@domain"
SER_APACHE="httpd"
SER_NGINX="nginx"
SER_MYSQLD="mysqld"
SER_PHP_FPM="php-fpm"
SER_PERCONA="mysql"
SER_POSTFIX="postfix"
SER_DOVECOT="dovecot"
}
chkAllServices() {
chkHttpd
chkNginx
chkPHP_FPM
chkMySQLD
chkPercona
chkPostfix
chkDovecot
}
chkHttpd() {
CHK_APACHE=`rpm -qa | grep httpd`
if [[ -n "$CHK_APACHE" ]]; then
SERVICE="$SER_APACHE"
chkService
fi
}
chkNginx() {
CHK_NGINX=`rpm -qa | grep nginx`
if [[ -n "$CHK_NGINX" ]]; then
SERVICE="$SER_NGINX"
chkService
fi
}
chkPHP_FPM() {
CHK_PHP_FPM=`rpm -qa | grep php-fpm`
if [[ -n "$CHK_PHP_FPM" ]]; then
SERVICE="$SER_PHP_FPM"
chkService
fi
}
chkMySQLD() {
CHK_MYSQLD=`rpm -qa | grep mysql-server`
if [[ -n "$CHK_MYSQLD" ]]; then
SERVICE="$SER_MYSQLD"
chkService
fi
}
chkPercona() {
CHK_PERCONA=`rpm -qa | grep Percona-Server`
if [[ -n "$CHK_PERCONA" ]]; then
SERVICE="$SER_PERCONA"
chkService
fi
}
chkPostfix() {
CHK_POSTFIX=`rpm -qa | grep postfix`
if [[ -n "$CHK_POSTFIX" ]]; then
SERVICE="$SER_POSTFIX"
chkService
fi
}
chkDovecot() {
CHK_DOVECOT=`rpm -qa | grep dovecot`
if [[ -n "$CHK_DOVECOT" ]]; then
SERVICE="$SER_DOVECOT"
chkService
fi
}
chkService() {
STATUS=$(/etc/init.d/$SERVICE status)
if [[ $(echo $?) != 0 ]]; then
$(/etc/init.d/$SERVICE start)
NEW_STATUS=$(/etc/init.d/$SERVICE status)
if [[ $(echo $?) != 0 ]]; then
touch /tmp/srv_ser_err.log
echo -e "$SERVICE" >> /tmp/srv_ser_err.log
fi
fi
unset SERVICE
unset STATUS
unset NEW_STATUS
}
sendMail() {
if [[ -s /tmp/srv_ser_err.log ]]; then
ERRORS=`cat /tmp/srv_ser_err.log`
echo -e "An attempt to start the following service(s) Failed!\n$ERRORS" | mutt -s "[CRITICAL] Server Alert!" -- $EMAIL_TO
rm -rf /tmp/srv_ser_err.log
fi
}
main