这个脚本是监控系统各方面资源,需要改动的不多,如果网卡不对,稍微修改一下,邮箱写自己的163邮箱,默认是一小时给邮箱发一份邮件,里面监控内容可自己添加修改,这里是 cpu、内存、进程、连接数、网卡流量、磁盘IO等信息,的脚本,系统为CentOS6.4 64位。
发送邮件用mutt,所以先安装
1
|
yum install mytt -y
|
vim chakan.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
|
#!/bin/bash
while
[
1
]
do
RUNTIME=
60
WAITTIME=
3600
rm -rf /root/check_log/*
time=`date +
"%Y-%m-%d %H:%M:%S"
`
d=`date +%m%d`
N=`ps -ef | grep nginx | grep -v grep | wc -l`
Q=`ps -ef | grep php-fpm | grep -v grep | wc -l`
[ -d /root/check_log/ ] || mkdir /root/check_log
IP=`ifconfig eth0 | sed -n
'2'
p | awk
'{print $2}'
| awk -F
':'
'{print $2}'
`
M=`uptime | awk
'{print $8,$9,$10,$11,$12}'
`
O=`ifconfig eth0 | sed -n
'8'
p | awk
'{print $3,$4}'
`
P=`ifconfig eth0 | sed -n
'8'
p | awk
'{print $7,$8}'
`
#R=`ifconfig eth1 | sed -n
'8'
p | awk
'{print $3,$4}'
`
#S=`ifconfig eth1 | sed -n
'8'
p | awk
'{print $7,$8}'
`
A=`top -bn1 | sed -n
'2'
p | awk
'{print $2}'
`
B=`top -bn1 | sed -n
'2'
p | awk
'{print $10}'
`
echo
"IP: $IP"
>> /root/check_log/jiance.log
echo
"system date:"
>> /root/check_log/jiance.log
echo
"$time"
>> /root/check_log/jiance.log
echo
"-------------system load------------------"
>> /root/check_log/jiance.log
echo
"system load:"
>> /root/check_log/jiance.log
echo $M >> /root/check_log/jiance.log
echo
"------------- full processes---------------"
>> /root/check_log/jiance.log
echo
"processes:"
>> /root/check_log/jiance.log
echo $A >> /root/check_log/jiance.log
echo
"Zombie :"
>> /root/check_log/jiance.log
echo $B >> /root/check_log/jiance.log
echo
"----------- application processes----------"
>> /root/check_log/jiance.log
echo
"nginx processes:"
>> /root/check_log/jiance.log
echo $N >> /root/check_log/jiance.log
echo
"php-fpm processes"
>> /root/check_log/jiance.log
echo $Q >> /root/check_log/jiance.log
echo
"-------------- Tcp connect------------------"
>> /root/check_log/jiance.log
netstat -n | awk
'/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}'
>> /root/check_log/jiance.log
echo
"----------- interferce traffic--------------"
>> /root/check_log/jiance.log
echo
"eth0 traffic:"
>> /root/check_log/jiance.log
echo
"in: $O"
>> /root/check_log/jiance.log
echo
"out: $P"
>> /root/check_log/jiance.log
#echo
"eth1 traffic:"
>> /root/check_log/jiance.log
#echo
"in: $R"
>> /root/check_log/jiance.log
#echo
"out: $S"
>> /root/check_log/jiance.log
echo
"------------- cpu load-----------------------"
>> /root/check_log/jiance.log
echo
"cpu load:"
>> /root/check_log/jiance.log
sar -u
5
3
| grep -v Linux | grep -v ^$ >> /root/check_log/jiance.log
echo
"------------- mem load (/M) ------------------"
>> /root/check_log/jiance.log
echo
"mem load:"
>> /root/check_log/jiance.log
MemTotal=`free -m | grep Mem | awk -F:
'{print $2}'
| awk
'{print $1}'
`
MemFree=`free -m | grep cache | awk NR==
2
| awk
'{print $4}'
`
MemFreeB=`awk
'BEGIN{printf "%.2f%\n",'
$MemFree/$MemTotal\*
100
'}'
`
MemFreeS=`awk
'BEGIN{printf "%.f",'
$MemFree/$MemTotal\*
100
'}'
`
echo
"MemTotal:$MemTotal MemFree:$MemFree MemFreeB:$MemFreeB MemFreeS:$MemFreeS "
>> /root/check_log/jiance.log
echo
"--------------- disk use-----------------------"
>> /root/check_log/jiance.log
echo
"disk use: "
>> /root/check_log/jiance.log
df -h >> /root/check_log/jiance.log
echo
"-----------check io load-----------------------"
>> /root/check_log/jiance.log
echo
"io load: "
>> /root/check_log/jiance.log
iostat | grep -v ^$ | grep -v Linux | grep -v dm | grep -v hdc >> /root/check_log/jiance.log
echo
"------------------All information -----------------"
>> /root/check_log/jiance.log
echo
"All Information"
>> /root/check_log/jiance.log
dstat
5
3
>> /root/check_log/jiance.log
sleep $RUNTIME
cat
"/root/check_log/jiance.log"
|mutt -s
"240 info"
your_mail@
163
.com
sleep $WAITTIME
done
|
1
|
sh ./chakan &
|
附图
本文转自 bbotte 51CTO博客,原文链接:http://blog.51cto.com/bbotte/1409922,如需转载请自行联系原作者