Skip to content

Commit 1e2732c

Browse files
committed
list system services
1 parent c9dca4e commit 1e2732c

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

bin/v-list-sys-services

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
#!/bin/bash
2+
# info: list system config
3+
# options: [FORMAT]
4+
#
5+
# The function for obtaining the list of system parameters.
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
format=${1-shell}
14+
15+
# Includes
16+
source $VESTA/conf/vesta.conf
17+
source $VESTA/func/main.sh
18+
19+
get_srv_state() {
20+
srv=$1
21+
proc_name=${2-$1}
22+
23+
# Check service status
24+
/etc/init.d/$srv status > /dev/null 2>&1
25+
if [ $? -eq 0 ]; then
26+
state='running'
27+
28+
# Calculate cpu and memory usage
29+
cpu=0
30+
mem=0
31+
for pid in $(/sbin/pidof $proc_name); do
32+
pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}')
33+
pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' ')
34+
cpu=$((cpu + pid_cpu))
35+
mem=$((mem + pid_mem))
36+
done
37+
mem=$((mem / 1024))
38+
39+
# Get pid date
40+
if [ ! -z $pid ] && [ -e "/proc/$pid/cmdline" ]; then
41+
mtime=$(stat -c "%Y" /proc/$pid/cmdline)
42+
ptime=$(date -d @$mtime +%T)
43+
pdate=$(date -d @$mtime +%F)
44+
fi
45+
else
46+
# Service is stopped
47+
state='stopped'
48+
mem=0
49+
cpu=0
50+
ptime="$TIME"
51+
pdate="$DATE"
52+
fi
53+
}
54+
55+
56+
#----------------------------------------------------------#
57+
# Action #
58+
#----------------------------------------------------------#
59+
60+
# Save current proccess list
61+
tmp_file=$(mktemp)
62+
ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
63+
64+
# Proxy
65+
service=$PROXY_SYSTEM
66+
spnd='yes'
67+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
68+
spnd='no'
69+
get_srv_state $service
70+
fi
71+
str="NAME='$service' SYSTEM='proxy' STATE='$state' CPU='$cpu'"
72+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
73+
74+
# Web
75+
service=$WEB_SYSTEM
76+
spnd='yes'
77+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
78+
spnd='no'
79+
if [ "$service" == 'apache' ]; then
80+
service='httpd'
81+
fi
82+
get_srv_state $service
83+
fi
84+
str="$str\nNAME='$service' SYSTEM='web' STATE='$state' CPU='$cpu'"
85+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
86+
87+
# DNS
88+
service=$DNS_SYSTEM
89+
spnd='yes'
90+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
91+
spnd='no'
92+
if [ "$service" == 'bind' ]; then
93+
service='named'
94+
fi
95+
get_srv_state $service
96+
fi
97+
str="$str\nNAME='$service' SYSTEM='dns' STATE='$state' CPU='$cpu'"
98+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
99+
100+
# MAIL
101+
service=$MAIL_SYSTEM
102+
spnd='yes'
103+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
104+
spnd='no'
105+
get_srv_state $service
106+
fi
107+
str="$str\nNAME='$service' SYSTEM='mail' STATE='$state' CPU='$cpu'"
108+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
109+
110+
# IMAP
111+
service=$IMAP_SYSTEM
112+
spnd='yes'
113+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
114+
spnd='no'
115+
get_srv_state $service
116+
fi
117+
str="$str\nNAME='$service' SYSTEM='imap' STATE='$state' CPU='$cpu'"
118+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
119+
120+
# ANTIVIRUS
121+
service=$ANTIVIRUS_SYSTEM
122+
spnd='yes'
123+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
124+
spnd='no'
125+
if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ]; then
126+
service='clamd'
127+
fi
128+
get_srv_state $service
129+
fi
130+
str="$str\nNAME='$service' SYSTEM='antivirus' STATE='$state' CPU='$cpu'"
131+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
132+
133+
# ANTISPAM
134+
service=$ANTISPAM_SYSTEM
135+
spnd='yes'
136+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
137+
spnd='no'
138+
get_srv_state $service spamd
139+
fi
140+
str="$str\nNAME='$service' SYSTEM='antispam' STATE='$state' CPU='$cpu'"
141+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
142+
143+
# DB
144+
service=$DB_SYSTEM
145+
spnd='yes'
146+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
147+
spnd='no'
148+
for db in ${DB_SYSTEM//,/ }; do
149+
service="$db"
150+
if [ "$service" == 'mysql' ] && [ ! -e "/etc/init.d/$service" ]; then
151+
service='mysqld'
152+
fi
153+
get_srv_state $service
154+
str="$str\nNAME='$service' SYSTEM='db' STATE='$state' CPU='$cpu'"
155+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
156+
done
157+
else
158+
str="$str\nNAME='$service' SYSTEM='db' STATE='$state' CPU='$cpu''"
159+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
160+
fi
161+
162+
# FTP
163+
service=$FTP_SYSTEM
164+
spnd='yes'
165+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
166+
spnd='no'
167+
get_srv_state $service
168+
fi
169+
str="$str\nNAME='$service' SYSTEM='ftp' STATE='$state' CPU='$cpu'"
170+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
171+
172+
# CRON
173+
service=$CRON_SYSTEM
174+
spnd='yes'
175+
if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
176+
spnd='no'
177+
get_srv_state $service
178+
fi
179+
str="$str\nNAME='$service' SYSTEM='ftp' STATE='$state' CPU='$cpu'"
180+
str="$str MEM='$mem' SUSPENDED='$spnd' TIME='$ptime' DATE='$pdate'"
181+
182+
# Defining config
183+
184+
echo -e "$str" > $tmp_file
185+
conf=$tmp_file
186+
187+
# Defining fileds to select
188+
fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$SUSPENDED \$TIME \$DATE"
189+
190+
# Listing services
191+
case $format in
192+
json) json_list ;;
193+
plain) nohead=1; shell_list ;;
194+
shell) fields='$NAME $STATE $CPU $MEM $SUSPENDED $TIME $DATE'
195+
shell_list | column -t ;;
196+
*) check_args '1' '0' 'USER [FORMAT]'
197+
esac
198+
199+
rm -f $tmp_file
200+
201+
#----------------------------------------------------------#
202+
# Vesta #
203+
#----------------------------------------------------------#
204+
205+
exit

0 commit comments

Comments
 (0)