Faster and more accurate function to get service information

This commit is contained in:
Serghey Rodin 2014-12-09 23:48:41 +02:00
commit 59a02157f0

View file

@ -23,29 +23,41 @@ get_srv_state() {
proc_name=${2-$1} proc_name=${2-$1}
# Check service status # Check service status
status=$(service $srv status 2>/dev/null)
rc=$?
stopped=$(echo $status| grep stop)
if [ "$rc" -eq 0 ] && [ -z "$stopped" ]; then
state='running' state='running'
# Calculate cpu and memory usage # Searching related pids
cpu=0 if [ -z $3 ]; then
mem=0 pids=$(pidof $proc_name |tr ' ' '|')
for pid in $(pidof $proc_name); do else
pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}') pids=$(pidof -x $proc_name |tr ' ' '|')
pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' '|sed "s/^0//") fi
cpu=$((cpu + pid_cpu)) if [ ! -z "$pids" ]; then
mem=$((mem + pid_mem)) pid=$(echo $pids|cut -f 1 -d \|)
done pids=$(egrep "$pids" $tmp_file)
mem=$((mem / 1024))
# Get pid date # Calculating CPU usage
if [ ! -z $pid ] && [ -e "/proc/$pid" ]; then cpu=$(echo "$pids" |awk '{ sum += $2} END {print sum}')
mtime=$(stat -c "%Y" /proc/$pid)
# 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=$((ctime - mtime))
rtime=$((rtime / 60)) rtime=$((rtime / 60))
else
rtime=0
fi fi
else else
# Service is stopped # Service is stopped
@ -63,23 +75,11 @@ get_srv_state() {
# Save current proccess list # Save current proccess list
tmp_file=$(mktemp) tmp_file=$(mktemp)
if [ "$format" = 'json' ]; then ps -eo pid,pcpu,size > $tmp_file
ps aux | awk '{print $2" "$3}' | tr -d '.' > $tmp_file
else
ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
fi
# Get current time # Get current time
ctime=$(date +%s) ctime=$(date +%s)
# Proxy
service=$PROXY_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service
str="NAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'"
fi
# Web # Web
service=$WEB_SYSTEM service=$WEB_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
@ -87,15 +87,24 @@ if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
service='httpd' service='httpd'
fi fi
get_srv_state $service get_srv_state $service
str="$str\nNAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'" str="NAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
str="$str MEM='$mem' RTIME='$rtime'" str="$str MEM='$mem' RTIME='$rtime'"
fi 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 # DNS
service=$DNS_SYSTEM service=$DNS_SYSTEM
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
if [ "$service" == 'bind' ]; then if [ "$service" == 'bind' ] || [ "$service" == 'bind9' ]; then
service='named' service='named'
fi fi
get_srv_state $service get_srv_state $service
@ -202,8 +211,8 @@ fi
# Fail2ban # Fail2ban
service=$FIREWALL_EXTENSION service=$FIREWALL_EXTENSION
if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
get_srv_state $service get_srv_state $service fail2ban-server script
str="$str\nNAME='$service' SYSTEM='Brute force blocking' STATE='$state'" str="$str\nNAME='$service' SYSTEM='brute-force monitor' STATE='$state'"
str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'" str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
fi fi