Linux下服务器性能监控脚本
分类:LINUX杂记, SHELL评论:5条作者:雨尚日期:2011-06-27
Linux下服务器性能监控脚本
最近因为工作需要,写了一个监控系统性能的脚本,包括对CPU、内存、网卡流量、某进程消耗系统资源的监控,由于诸多服务器的硬盘分区不同,没做具体监控。
这个脚本的原理是通过sar命令记录各种数值,然后利用cron每10分钟跑一次并记录到数据库,后期可根据具体业务需求再把相关数据取出并判断,最后发送邮件给某人,以下是脚本具体内容:
——————————————————————————————————————————————————————————————
#!/bin/bash
#
#*@ Env CentOS5.4-64位中文版+sysstat-7.0.2-3.el5_5.1
#*@ Date 2011.6.27
#*@ Author 雨尚
#*@ Site www.linuxsee.com
#*@ Email ydjjing@163.com
#*@ Function 服务器性能监控脚本
#
mysql=”/opt/mysql/bin/mysql -uroot -p”000000″ opdb -hlocalhost -N”
hostname=`hostname`
ip=`/sbin/ifconfig eth0 |grep “inet addr”| cut -f 2 -d “:”|cut -f 1 -d ” “`
ip1=`/sbin/ifconfig eth1 |grep “inet addr”| cut -f 2 -d “:”|cut -f 1 -d ” “`
####COLLECT_CPU####
CPU=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $2}’`
user=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $3}’`
nice=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $4}’`
system=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $5}’`
iowait=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $6}’`
steal=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $7}’`
idle=`/usr/bin/sar -u 0 | head -4 | tail -1 |awk -F’ ‘ ‘{printf $8}’`
echo “insert into cpu (id,CPU,user,nice,system,iowait,steal,idle,hostname,ip,collect_time) values (”,’$CPU’,'$user’,'$nice’,'$system’,'$iowait’,'$steal’,'$idle’,'$hostname’,'$ip’, now());” | ${MYSQL}
####COLLECT_MEM####
kbmemfree=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $2}’`
kbmemused=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $3}’`
memused=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $4}’`
kbbuffers=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $5}’`
kbcached=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $6}’`
kbswpfree=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $7}’`
kbswpused=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $8}’`
swpused=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $9}’`
kbswpcad=`/usr/bin/sar -r 0| head -4 | tail -1 |awk -F’ ‘ ‘{printf $10}’`
echo “insert into memory (id,kbmemfree,kbmemused,memused,kbbuffers,kbcached,kbswpfree,kbswpused,swpused,kbswpcad,hostname,ip,collect_time) values (”,’$kbmemfree’,'$kbmemused’,'$memused’,'$kbbuffers’,'$kbcached’,'$kbswpfree’,'$kbswpused’,'$swpused’,'$kbswpcad’,'$hostname’,'$ip’, now());” | ${MYSQL}
####COLLECT_INTERFACE####
IFACE0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $2}’`
rxpck0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $3}’`
txpck0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $4}’`
rxbyt0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $5}’`
txbyt0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $6}’`
rxcmp0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $7}’`
txcmp0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $8}’`
rxmcst0=`/usr/bin/sar -n DEV 0|head -5|tail -1|awk -F’ ‘ ‘{printf $9}’`
IFACE1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $2}’`
rxpck1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $3}’`
txpck1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $4}’`
rxbyt1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $5}’`
txbyt1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $6}’`
rxcmp1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $7}’`
txcmp1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $8}’`
rxmcst1=`/usr/bin/sar -n DEV 0|head -6|tail -1|awk -F’ ‘ ‘{printf $9}’`
echo “insert into interface (id,IFACE,rxpck,txpck,rxbyt,txbyt,rxcmp,txcmp,rxmcst,hostname,ip,collect_time) values (”,’$IFACE0′,’$rxpck0′,’$txpck0′,’$rxbyt0′,’$txbyt0′,’$rxcmp0′,’$txcmp0′,’$rxmcst0′,’$hostname’,'$ip’, now());” | ${MYSQL}
echo “insert into interface (id,IFACE,rxpck,txpck,rxbyt,txbyt,rxcmp,txcmp,rxmcst,hostname,ip,collect_time) values (”,’$IFACE1′,’$rxpck1′,’$txpck1′,’$rxbyt1′,’$txbyt1′,’$rxcmp1′,’$txcmp1′,’$rxmcst1′,’$hostname’,'$ip1′, now());” | ${MYSQL}
####COLEECT_PROCESS#####
collect_process(){
PRO_PID=`ps -ef |grep $1|head -1|awk -F’ ‘ ‘{printf $2}’`
pname=`echo “$1″`
minflt_pro=`/usr/bin/sar -x $PRO_PID 0 |tail -1|awk -F’ ‘ ‘{printf $3}’`
majflt_pro=`/usr/bin/sar -x $PRO_PID 0 |tail -1|awk -F’ ‘ ‘{printf $4}’`
user_pro=`/usr/bin/sar -x $PRO_PID 0 |tail -1|awk -F’ ‘ ‘{printf $5}’`
system_pro=`/usr/bin/sar -x $PRO_PID 0 |tail -1|awk -F’ ‘ ‘{printf $6}’`
nswap_pro=`/usr/bin/sar -x $PRO_PID 0 |tail -1|awk -F’ ‘ ‘{printf $7}’`
CPU_pro=`/usr/bin/sar -x $PRO_PID 0 |tail -1|awk -F’ ‘ ‘{printf $8}’`
echo “insert into process (id,pname,minflt,majflt,user,system,nswap,CPU,hostname,ip,collect_time) values (”,’$pname’,'$minflt_pro’,'$majflt_pro’,'$user_pro’,'$system_pro’,'$nswap_pro’,'$CPU_pro’,'$hostname’,'$ip’, now());” | ${MYSQL}
}
collect_process mysqld_safe

最新评论
my blog is i»
Post: 2012-02-15 05:34:01My buddy and»
Post: 2012-02-14 15:00:04I got what y»
Post: 2012-02-13 23:49:02Aw, this was»
Post: 2012-02-13 23:05:38wonderful po»
Post: 2012-02-10 13:42:50An engrossin»
Post: 2012-02-10 12:09:51Hello just w»
Post: 2012-02-09 20:03:14Your content»
Post: 2012-02-09 00:17:45An interesti»
Post: 2012-02-08 21:01:19An intriguin»
Post: 2012-02-08 21:01:19