forked from outroll/vesta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv-list-sys-services
executable file
·252 lines (215 loc) · 6.88 KB
/
v-list-sys-services
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
# info: list system services
# options: [FORMAT]
#
# The function for obtaining the list of configured system services.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
format=${1-shell}
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
export PATH=$PATH:/sbin
get_srv_state() {
srv=$1
proc_name=${2-$1}
# Check service status
state='running'
# Searching related pids
if [ -z $3 ]; then
pids=$(pidof $proc_name |tr ' ' '|')
else
pids=$(pidof -x $proc_name |tr ' ' '|')
fi
if [ -z "$pids" ] && [ "$proc_name" != 'nginx' ] ; then
#fallback to pgrep
pids=$(pgrep $proc_name |tr '\n' '|')
fi
if [ ! -z "$pids" ]; then
pid=$(echo $pids|cut -f 1 -d \|)
pids=$(egrep "$pids" $tmp_file)
# Calculating CPU usage
cpu=$(echo "$pids" |awk '{ sum += $2} END {print sum}')
# Calculating memory usage
mem=$(echo "$pids" |awk '{sum += $3} END {print sum/1024 }')
mem=$(printf "%.0f\n" $mem)
# Searching service uptime
if [ -e "/var/run/$srv.pid" ]; then
srv_file="/var/run/$srv.pid"
fi
if [ -z "$srv_file" ] && [ -e "/var/run/$srv/$srv.pid" ]; then
srv_file="/var/run/$srv/$srv.pid"
fi
if [ -z $srv_file ] && [ -e "/proc/$pid" ]; then
srv_file="/proc/$pid"
fi
if [ ! -z "$srv_file" ]; then
mtime=$(stat -c "%Y" $srv_file)
rtime=$((ctime - mtime))
rtime=$((rtime / 60))
else
rtime=0
fi
else
# Service is stopped
state='stopped'
mem=0
cpu=0
rtime="0"
fi
}
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Save current proccess list
tmp_file=$(mktemp)
ps -eo pid,pcpu,size > $tmp_file
# Get current time
ctime=$(date +%s)
# Web
service=$WEB_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
if [ "$service" == 'apache' ]; then
service='httpd'
fi
get_srv_state $service
str="NAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# Backend
service=$WEB_BACKEND
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="$str\nNAME='$service' SYSTEM='backend server' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# Proxy
service=$PROXY_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="$str\nNAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# DNS
service=$DNS_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
if [ "$service" == 'bind' ] || [ "$service" == 'bind9' ]; then
proc_name='named'
fi
get_srv_state $service $proc_name
str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# MAIL
service=$MAIL_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# IMAP
service=$IMAP_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
fi
# ANTIVIRUS
service=$ANTIVIRUS_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
if [ -e "/etc/redhat-release" ]; then
if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ];then
service='clamd'
fi
get_srv_state $service
else
if [ "$ANTIVIRUS_SYSTEM" = 'clamav-daemon' ];then
clam_proc_name='clamd'
fi
get_srv_state $service $clam_proc_name
fi
str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
fi
# ANTISPAM
service=$ANTISPAM_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service spamd
str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
fi
# DB
service=$DB_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
for db in ${DB_SYSTEM//,/ }; do
service="$db"
if [ -e "/etc/redhat-release" ]; then
service='mysqld'
fi
if [ "$service" == 'pgsql' ]; then
service='postgresql'
db_proc_name='postmaster'
if [ ! -e "/etc/redhat-release" ]; then
db_proc_name='postgres'
fi
if [ ! -e '/etc/init.d/postgresql' ]; then
db_proc_name='postgres'
fi
fi
get_srv_state $service $db_proc_name
str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
done
fi
# FTP
service=$FTP_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# CRON
service=$CRON_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
fi
# FIREWALL
service=$FIREWALL_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
state="stopped"
/sbin/iptables -L vesta >/dev/null 2>&1
if [ "$?" -eq 0 ]; then
state="running"
fi
str="$str\nNAME='$FIREWALL_SYSTEM' SYSTEM='firewall'"
str="$str STATE='$state' CPU='0' MEM='0' RTIME='0'"
fi
# Fail2ban
service=$FIREWALL_EXTENSION
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service fail2ban-server script
str="$str\nNAME='$service' SYSTEM='brute-force monitor' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
fi
# Defining config
echo -e "$str" > $tmp_file
conf=$tmp_file
# Defining fileds to select
fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
# Listing services
case $format in
json) json_list ;;
plain) nohead=1; shell_list ;;
shell) fields='$NAME $STATE $CPU $MEM $RTIME'
shell_list | column -t ;;
*) check_args '1' '0' 'USER [FORMAT]'
esac
rm -f $tmp_file
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit