監控系統使用情況shell指令碼

season0891發表於2009-07-17
#!/bin/sh
DATE=`date +"%Y-%m-%d %T"`
DATE1=`date +"%Y-%m-%d"`
TIME=`date +"%T"`


############CPU###############

cpu=`iostat -c 2 2 |grep -v Linux  | grep -v %user | grep -v '^$' | awk '{print $1+$2+$3+$4}'|sed -n "2p"`


##########MEMORY##############

mem_used=`free -m | grep "buffers/cache" | awk '{print $3}'`
mem_total=`free -m | grep "Mem:" | awk '{print $2}'`
mem_used_percent=`echo "scale=2;100*$mem_used/$mem_total" | bc`


###########DISK###############

disk_used=`df -l | grep -v Filesystem | awk 'BEGIN {SUM=0} {SUM+=$3} END {print SUM}'`
disk_total=`df -l | grep -v Filesystem | awk 'BEGIN {SUM=0} {SUM+=$2} END {print SUM}'`
disk_percent=`echo "scale=2;100*$disk_used/$disk_total" | bc`


#############SWAP#############

swap_percent=`free -m | grep "Swap:" | awk '{print $3/$2*100}'`
swap=$swap_percent


#############IP#########################
ip=`cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR | awk -F = '{print $2}'`
#############I/O##############

type=sdb
Blk_read=`iostat |grep $type |sed -n '1p'|awk '{print $3}'`
Blk_wrtn=`iostat |grep $type |sed -n '1p'|awk '{print $4}'`

i=$Blk_read
o=$Blk_wrtn


mysql -h 2.168.6.1  -uuserlog -p123456789 -B -N db -e"insert into res (ip,cpu,mem_used_percent,disk_percent,swap,Blk_read,Blk_wrtn,date,time) values ('$ip','$cpu','$mem_used_percent','$disk_percent','$swap','$i','$o','$DATE1','$TIME');"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-609498/,如需轉載,請註明出處,否則將追究法律責任。

相關文章