监控linux系统cpu硬盘网络io等资源脚本

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

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

yum install mytt -y

vim chakan.sh

#!/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" [email protected]
sleep $WAITTIME
done
sh ./chakan &

附图

wKioL1NwgNnyobDSAAFS1pyD03w336.jpg



你可能感兴趣的:(linux,cpu,IO,系统,硬盘)