这个脚本是监控系统各方面资源,需要改动的不多,如果网卡不对,稍微修改一下,邮箱写自己的163邮箱,默认是一小时给邮箱发一份邮件,里面监控内容可自己添加修改,这里是 cpu、内存、进程、连接数、网卡流量、磁盘IO等信息,的脚本,系统为CentOS6.4 64位。

发送邮件用mutt,所以先安装

yum install mytt -y

vim chakan.sh

#!/bin/bashwhile [ 1 ]doRUNTIME=60WAITTIME=3600rm -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_logIP=`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.logecho "system date:" >> /root/check_log/jiance.logecho "$time" >> /root/check_log/jiance.logecho "-------------system load------------------" >> /root/check_log/jiance.logecho "system load:" >> /root/check_log/jiance.logecho $M >> /root/check_log/jiance.logecho "------------- full processes---------------" >> /root/check_log/jiance.logecho "processes:" >> /root/check_log/jiance.logecho $A >> /root/check_log/jiance.logecho "Zombie :" >> /root/check_log/jiance.logecho $B >> /root/check_log/jiance.logecho "----------- application processes----------" >> /root/check_log/jiance.logecho "nginx processes:" >> /root/check_log/jiance.logecho $N >> /root/check_log/jiance.logecho "php-fpm processes" >> /root/check_log/jiance.logecho $Q >> /root/check_log/jiance.logecho "-------------- Tcp connect------------------" >> /root/check_log/jiance.lognetstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}' >> /root/check_log/jiance.logecho "----------- interferce traffic--------------" >> /root/check_log/jiance.logecho "eth0 traffic:" >> /root/check_log/jiance.logecho "in: $O" >> /root/check_log/jiance.logecho "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.logecho "------------- cpu load-----------------------" >> /root/check_log/jiance.logecho "cpu load:" >> /root/check_log/jiance.logsar -u 5 3 | grep -v Linux | grep -v ^$ >> /root/check_log/jiance.logecho "------------- mem load (/M) ------------------" >> /root/check_log/jiance.logecho "mem load:" >> /root/check_log/jiance.logMemTotal=`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.logecho "--------------- disk use-----------------------" >> /root/check_log/jiance.logecho "disk use: " >> /root/check_log/jiance.logdf -h >> /root/check_log/jiance.logecho "-----------check io load-----------------------" >> /root/check_log/jiance.logecho "io load: " >> /root/check_log/jiance.logiostat  | grep -v ^$ | grep -v Linux | grep -v dm | grep -v hdc >> /root/check_log/jiance.logecho "------------------All information -----------------" >> /root/check_log/jiance.logecho "All Information" >> /root/check_log/jiance.logdstat 5 3 >> /root/check_log/jiance.logsleep $RUNTIMEcat "/root/check_log/jiance.log" |mutt -s "240 info" your_mail@163.comsleep $WAITTIMEdone
sh ./chakan &

附图