-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_process.sh
executable file
·76 lines (67 loc) · 1.48 KB
/
check_process.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
#!/bin/bash
# Script_name: check_process.sh
# Author: Guanghongwei
# Date: 2014/12/25
cwdir=`dirname $0`
. $cwdir/function.sh
min_num_default=1
max_num_default=30
proc_discovery() {
procs=($(grep -v '^#' $check_list | grep '^proc:' | awk '{ print $2 }'))
discovery ${procs[@]}
}
proc_num() {
cur_num=$(ps axu | grep $1 | egrep -v 'grep|vim|check_proc' | wc -l)
echo $cur_num
}
proc_status() {
proc=$1
min_num=$(grep "^proc: $proc" $check_list | awk '{ print $3 }')
max_num=$(grep "^proc: $proc" $check_list | awk '{ print $4 }')
min_num=${min_num:=$min_num_default}
max_num=${max_num:=$max_num_default}
cur_num=$(proc_num $proc)
if [ "$cur_num" -ge "$min_num" -a "$cur_num" -le "$max_num" ];then
echo 1
else
echo 0
fi
}
proc_mem() {
proc=$1
total_mem=0
mem_list=$(ps axu | grep $proc | egrep -v 'grep|vim|check_proc' | awk '{ print $6 }')
for i in $mem_list;do
total_mem=$(echo $total_mem + $i | bc)
done
echo $total_mem
}
proc_cpu() {
proc=$1
total_cpu=0
cpu_list=$(ps axu | grep $proc | egrep -v 'grep|vim|check_proc' | awk '{ print $3 }')
for i in $cpu_list;do
total_cpu=$(echo $total_cpu + $i | bc)
done
echo $total_cpu
}
case $1 in
discovery)
proc_discovery
;;
num)
proc_num $2
;;
status)
proc_status $2
;;
mem)
proc_mem $2
;;
cpu)
proc_cpu $2
;;
*)
msg="Usage: $0 discovery | status | mem | cpu PROC"
echo $msg
esac