系统监控shell函数库
系统监控函数库~调用相应的函数即../function.inc
完善相应的shell代码即可
(function.sh) download
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
114
115
116
117
118
119
| #*=============================================================================
#
# Author: mengdaiku - mengdaikun@gmail.com
#
# QQ : 646978600
#
# Last modified: 2013-05-26 22:19
#
# Filename: function.inc
#
# Description: 系统监控shell函数库调用(../function.inc) 收集来自互联网
#
#=============================================================================
function GetPID #PsUser #PsName
{
PsUser=$1
PsName=$2
pid=`ps -u $PsUser|grep $PsName|grep -v grep|grep -v vi|grep -v dbx|grep -v tail|grep -v start|grep -v stop |sed -n 1p |awk '{print $1}'`
echo $pid
}
function GetCpu
{
CpuValue=`ps -p $1 -o pcpu |grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'`
echo $CpuValue
}
function CheckCpu
{
cpu=`GetCpu $1`
echo "The CPU is $cpu"
if [ "-$cpu" == "-" ]
then
{
echo "The process is not found"
return 1;
}
fi
if [ $cpu -gt 80 ]
then
{
echo "The usage of cpu is larger than 80%"
}
else
{
echo "The usage of cpu is normal"
}
fi
}
function GetMem
{
MEMUsage=`ps -o vsz -p $1|grep -v VSZ`
(( MEMUsage /= 1000))
echo $MEMUsage
}
function GetDes
{
DES=`ls /proc/$1/fd | wc -l`
echo $DES
}
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
}
function Listening
{
TCPListeningnum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
UDPListeningnum=`netstat -an|grep ":$1 " |awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l`
(( Listeningnum = TCPListeningnum + UDPListeningnum ))
if [ $Listeningnum == 0 ]
then
{
echo "0"
}
else
{
echo "1"
}
fi
}
function GetDiskSpc
{
if [ $# -ne 1 ]
then
return 1
fi
Folder="$1$"
DiskSpace=`df -k |grep $Folder |awk '{print $5}' |awk -F% '{print $1}'`
echo $DiskSpace
}
function Chech_Tcp_Server_Port_Stat
{
count_result=0
stat_num=`netstat -an|egrep $1 |awk '$6 == "LISTEN" && $1 == "tcp" {print $0}' |wc -l`
if [ $stat_num -ge 1 ]
then
{
count_result=0
echo "$1 port is normal!"
}
else
{
echo "$1 port is abnormal, please check"
}
fi
return 0
}
|