-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkPerform.sh
executable file
·113 lines (96 loc) · 2.59 KB
/
checkPerform.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
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
#! /bin/bash
# 通过使用者和名称来获取进程的CPU/内存/句柄数等信息
# 通过进程的使用者和名称来获取进程的PID
function GetPId
{
pId=`ps -u $1 | grep $2 | grep -v 'grep' | grep -v 'vi' | grep -v 'dbx\n' | grep -v 'tail' | grep -v 'start' | grep -v 'stop' | sed -n 1p | awk '{print $1}'`
echo $pId
}
# 通过pId来获取该进程CPU的使用情况
function GetCpu
{
cpuValue=`ps -p $1 -o pcpu | grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'`
echo $cpuValue
}
# 通过pId来获取进程内存使用情况
function GetMem
{
memUsage=`ps -p $1 -o vsz | grep -v VSZ`
((memUsage /= 1000))
echo $memUsage
}
# 通过pId来获取该进程句柄使用量
function GetHandle
{
handle=`ls /proc/$1/fd | wc -l`
echo $handle
}
# 通过pName来获取该进程运行的个数
function GetProcessNum
{
runNum=`ps -efu $1 | grep $2 | grep -v 'grep' | grep -v 'vi' | grep -v 'dbx\n' | grep -v 'tail' | grep -v 'start' | grep -v 'stop' | wc -l`
echo $runNum
}
# 获取系统CPU负载
function GetSysCpu
{
cpuIdle=`vmstat 1 5 | sed -n '3,$p' | awk '{x = x + $15} END {print x/5}' | awk -F. '{print $1}'`
cpuNum=`echo "100-$cpuIdle" | bc`
echo $cpuNum
}
# 提示脚本用法
if [ "$#" -ne 2 ]
then
echo "Usage: $0 pUser pName"
exit 1
fi
pName=`ps -u $1 | grep $2 | grep -v 'grep' | grep -v 'vi' | grep -v 'dbx\n' | grep -v 'tail' | grep -v 'start' | grep -v 'stop' | sed -n 1p | awk '{print $4}'`
echo "------------------------PROCESS: $pName ------------------------"
pId=`GetPId $1 $2`
# 检测进程是否存在
if [ "-$pId" == "-" ]
then
echo "The process does not exist."
exit 2
fi
# check cpu
cpu=`GetCpu $pId`
if [ $cpu -gt 50 ]
then
echo "Warning: The usage of cpu($cpu%) is larger than 50%."
else
echo "The usage of cpu($cpu%) is normal."
fi
# check memory
mem=`GetMem $pId`
if [ $mem -gt 500 ]
then
echo "Warning: The usage of memory(${mem}M) is larger than 500M."
else
echo "The usage of memory(${mem}M) is normal."
fi
# check running number of Process
runNum=`GetProcessNum $1 $2`
if [ $runNum -gt 20 ]
then
echo "Warning: The number of process($runNum) is more than 20."
else
echo "The number of process($runNum) is normal."
fi
# check handle
han=`GetHandle $pId`
if [ $han -gt 500 ]
then
echo "Warning: The usage of handle($han) is more than 500M."
else
echo "The usage of handle($han) is normal."
fi
echo "------------------------------SYSTEM-----------------------------"
# check system CPU
sysCpu=`GetSysCpu`
if [ $sysCpu -gt 90 ]
then
echo "Warning: The usage of system cpu($sysCpu%) is larger than 90%."
else
echo "The usage of system cpu($sysCpu%) is normal."
fi