Skip to content

Commit 59a0215

Browse files
committed
Faster and more accurate function to get service information
1 parent 840b12c commit 59a0215

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

bin/v-list-sys-services

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,41 @@ get_srv_state() {
2323
proc_name=${2-$1}
2424

2525
# Check service status
26-
status=$(service $srv status 2>/dev/null)
27-
rc=$?
28-
stopped=$(echo $status| grep stop)
26+
state='running'
2927

30-
if [ "$rc" -eq 0 ] && [ -z "$stopped" ]; then
31-
state='running'
28+
# Searching related pids
29+
if [ -z $3 ]; then
30+
pids=$(pidof $proc_name |tr ' ' '|')
31+
else
32+
pids=$(pidof -x $proc_name |tr ' ' '|')
33+
fi
34+
if [ ! -z "$pids" ]; then
35+
pid=$(echo $pids|cut -f 1 -d \|)
36+
pids=$(egrep "$pids" $tmp_file)
3237

33-
# Calculate cpu and memory usage
34-
cpu=0
35-
mem=0
36-
for pid in $(pidof $proc_name); do
37-
pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}')
38-
pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' '|sed "s/^0//")
39-
cpu=$((cpu + pid_cpu))
40-
mem=$((mem + pid_mem))
41-
done
42-
mem=$((mem / 1024))
43-
44-
# Get pid date
45-
if [ ! -z $pid ] && [ -e "/proc/$pid" ]; then
46-
mtime=$(stat -c "%Y" /proc/$pid)
38+
# Calculating CPU usage
39+
cpu=$(echo "$pids" |awk '{ sum += $2} END {print sum}')
40+
41+
# Calculating memory usage
42+
mem=$(echo "$pids" |awk '{sum += $3} END {print sum/1024 }')
43+
mem=$(printf "%.0f\n" $mem)
44+
45+
# Searching service uptime
46+
if [ -e "/var/run/$srv.pid" ]; then
47+
srv_file="/var/run/$srv.pid"
48+
fi
49+
if [ -z "$srv_file" ] && [ -e "/var/run/$srv/$srv.pid" ]; then
50+
srv_file="/var/run/$srv/$srv.pid"
51+
fi
52+
if [ -z $srv_file ] && [ -e "/proc/$pid" ]; then
53+
srv_file="/proc/$pid"
54+
fi
55+
if [ ! -z "$srv_file" ]; then
56+
mtime=$(stat -c "%Y" $srv_file)
4757
rtime=$((ctime - mtime))
4858
rtime=$((rtime / 60))
59+
else
60+
rtime=0
4961
fi
5062
else
5163
# Service is stopped
@@ -63,39 +75,36 @@ get_srv_state() {
6375

6476
# Save current proccess list
6577
tmp_file=$(mktemp)
66-
if [ "$format" = 'json' ]; then
67-
ps aux | awk '{print $2" "$3}' | tr -d '.' > $tmp_file
68-
else
69-
ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
70-
fi
78+
ps -eo pid,pcpu,size > $tmp_file
7179

7280
# Get current time
7381
ctime=$(date +%s)
7482

75-
# Proxy
76-
service=$PROXY_SYSTEM
77-
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
78-
get_srv_state $service
79-
str="NAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
80-
str="$str MEM='$mem' RTIME='$rtime'"
81-
fi
82-
8383
# Web
8484
service=$WEB_SYSTEM
8585
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
8686
if [ "$service" == 'apache' ]; then
8787
service='httpd'
8888
fi
8989
get_srv_state $service
90-
str="$str\nNAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
90+
str="NAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
9191
str="$str MEM='$mem' RTIME='$rtime'"
9292

9393
fi
9494

95+
# Proxy
96+
service=$PROXY_SYSTEM
97+
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
98+
get_srv_state $service
99+
str="$str\nNAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
100+
str="$str MEM='$mem' RTIME='$rtime'"
101+
fi
102+
103+
95104
# DNS
96105
service=$DNS_SYSTEM
97106
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
98-
if [ "$service" == 'bind' ]; then
107+
if [ "$service" == 'bind' ] || [ "$service" == 'bind9' ]; then
99108
service='named'
100109
fi
101110
get_srv_state $service
@@ -202,8 +211,8 @@ fi
202211
# Fail2ban
203212
service=$FIREWALL_EXTENSION
204213
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
205-
get_srv_state $service
206-
str="$str\nNAME='$service' SYSTEM='Brute force blocking' STATE='$state'"
214+
get_srv_state $service fail2ban-server script
215+
str="$str\nNAME='$service' SYSTEM='brute-force monitor' STATE='$state'"
207216
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
208217
fi
209218

0 commit comments

Comments
 (0)