使用rrdtool+SNMP监控Linux服务器

忙碌的两日,终于搞出了一点点的眉目

此脚本可以监控linux服务器的CPU,内存,网络的使用状态,并画出漂亮的图表.

#!/bin/bash
#
# This script use for monitoring the UCD/NET server,
# CPU usage, Memory Usage, System Load, Network Traffic
#
# Create by Dika.Ye < [email protected]>
#
# Use guid:
#
# mkdir /var/www/html/rrdtool /var/www/html/monitor
# cp ucd-net-server.sh /var/www/html/rrdtool
# chmod +x /var/www/html/rrdtool/ucd-net-server.sh
#
# Add crontab job
# 1,6,11,16,21,26,31,36,41,46,51,56 * * * * /var/www/html/rrdtool/ucd-net-server.sh 2>&1 > /dev/null
#
# View the reports:
# http://YOUR_HOST/monitor
#

_SNMP_HOST="修改为你要监控的主机的IP地址"
_SNMP_COM="SNMP认证字符串"
_HOME="/var/www/html/rrdtool"
_DATADIR="/var/www/html/monitor"
_TIME="hour day week month year"
_NOW=`date "+%Y/%m/%d %H\:%M\:%S"`
# Create RRD
if [ ! -e ${_HOME}/${_SNMP_HOST}.rrd ]; then
/usr/bin/rrdtool create ${_HOME}/${_SNMP_HOST}.rrd --start $(date -d '1 day ago' +%s) --step 300 \
DS:sys1minute:GAUGE:600:0:500 DS:sys5minute:GAUGE:600:0:500 DS:sys15minute:GAUGE:600:0:500 \
DS:cpunice:COUNTER:600:0:100 DS:cpusys:COUNTER:600:0:100 DS:cpuu:COUNTER:600:0:100 \
DS:membuffer:GAUGE:600:0:10000000 DS:memcache:GAUGE:600:0:10000000 DS:memfree:GAUGE:600:0:10000000 \
DS:eth0_in:COUNTER:600:0:12500000 DS:eth0_out:COUNTER:600:0:12500000 \
DS:eth1_in:COUNTER:600:0:12500000 DS:eth1_out:COUNTER:600:0:12500000 \
DS:lo_in:COUNTER:600:0:12500000 DS:lo_out:COUNTER:600:0:12500000 \
RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797
fi
# Update RRD file
# get system load
sys1minute=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.10.1.3.1 | awk '{ print $4 }')
sys5minute=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.10.1.3.2 | awk '{ print $4 }')
sys15minute=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.10.1.3.3 | awk '{ print $4 }')
# get cpu load
cpunice=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.11.51.0 | awk '{ print $4 }')
cpusys=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.11.52.0 | awk '{ print $4 }')
cpuu=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.11.50.0 | awk '{ print $4 }')
 
# get mem usage
membuffer=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.4.14.0 | awk '{ print $4 }')
memcache=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.4.15.0 | awk '{ print $4 }')
memfree=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} .1.3.6.1.4.1.2021.4.6.0 | awk '{ print $4 }')
 
# get eth0
index=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} RFC1213-MIB::ifDescr | grep eth0 | cut -d '=' -f 1 | cut -d '.' -f 2)
eth0_in=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} ifInOctets.${index} | awk '{ print $4 }')
eth0_out=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} ifOutOctets.${index} | awk '{ print $4 }')
# get eth0
index=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} RFC1213-MIB::ifDescr | grep eth1 | cut -d '=' -f 1 | cut -d '.' -f 2)
eth1_in=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} ifInOctets.${index} | awk '{ print $4 }')
eth1_out=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} ifOutOctets.${index} | awk '{ print $4 }')
# get loopback
index=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} RFC1213-MIB::ifDescr | grep lo | cut -d '=' -f 1 | cut -d '.' -f 2)
lo_in=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} ifInOctets.${index} | awk '{ print $4 }')
lo_out=$(snmpwalk -v2c -c ${_SNMP_COM} ${_SNMP_HOST} ifOutOctets.${index} | awk '{ print $4 }')
# update RRD file
echo "`date`" >> /var/log/monitor.log
/usr/bin/rrdtool updatev ${_HOME}/${_SNMP_HOST}.rrd N:${sys1minute}:${sys5minute}:${sys15minute}:${cpunice}:${cpusys}:\
${cpuu}:${membuffer}:${memcache}:${memfree}:${eth0_in}:${eth0_out}:${eth1_in}:${eth1_out}:${lo_in}:${lo_out} >> /var/log/monitor.log
 
# Create Graphs
#
# -----------------------------------------------------------------------#
# System Load Average
######################
for _T in $_TIME
do
#_STARTTIME=`date -d "-1 $_T" +%s`
_STARTTIME=`date -d "$_T ago" +%s`
/usr/bin/rrdtool graph ${_DATADIR}/${_SNMP_HOST}-system_load-$_T.png \
--imgformat=PNG \
--start=$_STARTTIME \
--title="${_SNMP_HOST} - Load Average" \
--rigid \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale-max \
--lower-limit=0 \
--vertical-label="processes in the run queue" \
--slope-mode \
--font TITLE:12: \
--font AXIS:8: \
--font LEGEND:10: \
--font UNIT:8: \
DEF:a="${_HOME}/${_SNMP_HOST}.rrd":sys1minute:AVERAGE \
DEF:b="${_HOME}/${_SNMP_HOST}.rrd":sys5minute:AVERAGE \
DEF:c="${_HOME}/${_SNMP_HOST}.rrd":sys15minute:AVERAGE \
CDEF:cdefg=TIME,1244704713,GT,a,a,UN,0,a,IF,IF,TIME,1244704713,GT,b,b,UN,0,b,IF,IF,TIME,1244704713,GT,c,c,UN,0,c,IF,IF,+,+ \
AREA:a#EACC00FF:"1 Minute Average"  \
GPRINT:a:LAST:" Current\:%8.2lf\n"  \
LINE2:b#EA8F00FF:"5 Minute Average"  \
GPRINT:b:LAST:" Current\:%8.2lf\n"  \
LINE2:c#FF0000FF:"15 Minute Average"  \
GPRINT:c:LAST:"Current\:%8.2lf\n"  \
LINE1:cdefg#000000FF:"Total" \
COMMENT:"\n" \
COMMENT:"Last Updated\:  $_NOW"
 
# -----------------------------------------------------------------------#
# CPU Usage
######################
/usr/bin/rrdtool graph ${_DATADIR}/${_SNMP_HOST}-cpu_usage-$_T.png \
--imgformat=PNG \
--start=$_STARTTIME \
--end=-300 \
--title="${_SNMP_HOST} - CPU Usage" \
--rigid \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale-max \
--lower-limit=0 \
--vertical-label="percent" \
--slope-mode \
--font TITLE:12: \
--font AXIS:8: \
--font LEGEND:10: \
--font UNIT:8: \
DEF:a="${_HOME}/${_SNMP_HOST}.rrd":cpusys:AVERAGE \
DEF:b="${_HOME}/${_SNMP_HOST}.rrd":cpuu:AVERAGE \
DEF:c="${_HOME}/${_SNMP_HOST}.rrd":cpunice:AVERAGE \
CDEF:cdefbc=TIME,1244705028,GT,a,a,UN,0,a,IF,IF,TIME,1244705028,GT,b,b,UN,0,b,IF,IF,TIME,1244705028,GT,c,c,UN,0,c,IF,IF,+,+ \
AREA:a#FF0000FF:"System"  \
GPRINT:a:LAST:"Current\:%8.2lf %s"  \
GPRINT:a:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:a:MAX:"Maximum\:%8.2lf %s\n"  \
AREA:b#0000FFFF:"User":STACK \
GPRINT:b:LAST:"  Current\:%8.2lf %s"  \
GPRINT:b:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:b:MAX:"Maximum\:%8.2lf %s\n"  \
AREA:c#00FF00FF:"Nice":STACK \
GPRINT:c:LAST:"  Current\:%8.2lf %s"  \
GPRINT:c:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:c:MAX:"Maximum\:%8.2lf %s\n"  \
LINE1:cdefbc#000000FF:"Total"  \
GPRINT:cdefbc:LAST:" Current\:%8.2lf %s"  \
GPRINT:cdefbc:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefbc:MAX:"Maximum\:%8.2lf %s" \
COMMENT:"\n" \
COMMENT:"Last Updated\:  $_NOW"
 
# -----------------------------------------------------------------------#
# Memory Usage
######################
/usr/bin/rrdtool graph ${_DATADIR}/${_SNMP_HOST}-mem_usage-$_T.png \
--imgformat=PNG \
--start=$_STARTTIME \
--end=-300 \
--title="${_SNMP_HOST} - Memory Usage" \
--rigid \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale-max \
--lower-limit=0 \
--vertical-label="bytes" \
--slope-mode \
--font TITLE:12: \
--font AXIS:8: \
--font LEGEND:10: \
--font UNIT:8: \
DEF:a="${_HOME}/${_SNMP_HOST}.rrd":memfree:AVERAGE \
DEF:b="${_HOME}/${_SNMP_HOST}.rrd":membuffer:AVERAGE \
DEF:c="${_HOME}/${_SNMP_HOST}.rrd":memcache:AVERAGE \
CDEF:cdefa=a,1024,* \
CDEF:cdefe=b,1024,* \
CDEF:cdefi=c,1024,* \
AREA:cdefa#8F005CFF:"Memory Free"  \
GPRINT:cdefa:LAST:"   Current\:%8.2lf %s"  \
GPRINT:cdefa:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefa:MAX:"Maximum\:%8.2lf %s\n"  \
AREA:cdefe#FF5700FF:"Memory Buffers":STACK \
GPRINT:cdefe:LAST:"Current\:%8.2lf %s"  \
GPRINT:cdefe:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefe:MAX:"Maximum\:%8.2lf %s\n"  \
AREA:cdefi#FFC73BFF:"Cache Memory":STACK \
GPRINT:cdefi:LAST:"  Current\:%8.2lf %s"  \
GPRINT:cdefi:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefi:MAX:"Maximum\:%8.2lf %s\n" \
COMMENT:"\n" \
COMMENT:"Last Updated\:  $_NOW"
# -----------------------------------------------------------------------#
# Interface Traffic
# eth0
/usr/bin/rrdtool graph ${_DATADIR}/${_SNMP_HOST}-eth0-traffic-$_T.png \
--imgformat=PNG \
--start=$_STARTTIME \
--end=-300 \
--title="${_SNMP_HOST} - Traffic - eth0" \
--rigid \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale \
--vertical-label="bits per second" \
--slope-mode \
--font TITLE:12: \
--font AXIS:8: \
--font LEGEND:10: \
--font UNIT:8: \
DEF:a="${_HOME}/${_SNMP_HOST}.rrd":eth0_in:AVERAGE \
DEF:b="${_HOME}/${_SNMP_HOST}.rrd":eth0_in:MAX \
DEF:c="${_HOME}/${_SNMP_HOST}.rrd":eth0_out:AVERAGE \
DEF:d="${_HOME}/${_SNMP_HOST}.rrd":eth0_out:MAX \
CDEF:cdefa=a,8,* \
CDEF:cdefe=b,8,* \
CDEF:cdeff=c,8,*,-1,* \
CDEF:cdefg=c,8,* \
CDEF:cdefba=c,8,*,-1,* \
CDEF:cdefbb=d,8,*,-1,* \
AREA:cdefa#00FF0099:"Inbound"  \
GPRINT:cdefa:LAST:"   Current\:%8.2lf %s"  \
GPRINT:cdefa:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefa:MAX:"Maximum\:%8.2lf %s"  \
LINE1:cdefe#005D57FF:"Peak\n"  \
AREA:cdeff#00FF897C:"Outbound"  \
GPRINT:cdefg:LAST:"  Current\:%8.2lf %s"  \
GPRINT:cdefg:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefg:MAX:"Maximum\:%8.2lf %s"  \
LINE1:cdefa#000000FF:""  \
LINE1:cdefba#000000FF:""  \
LINE1:cdefbb#0000AAFF:"Peak\n" \
COMMENT:"\n" \
COMMENT:"Last Updated\:  $_NOW"
# eth1
/usr/bin/rrdtool graph ${_DATADIR}/${_SNMP_HOST}-eth1-traffic-$_T.png \
--imgformat=PNG \
--start=$_STARTTIME \
--end=-300 \
--title="${_SNMP_HOST} - Traffic - eth1" \
--rigid \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale \
--vertical-label="bits per second" \
--slope-mode \
--font TITLE:12: \
--font AXIS:8: \
--font LEGEND:10: \
--font UNIT:8: \
DEF:a="${_HOME}/${_SNMP_HOST}.rrd":eth1_in:AVERAGE \
DEF:b="${_HOME}/${_SNMP_HOST}.rrd":eth1_in:MAX \
DEF:c="${_HOME}/${_SNMP_HOST}.rrd":eth1_out:AVERAGE \
DEF:d="${_HOME}/${_SNMP_HOST}.rrd":eth1_out:MAX \
CDEF:cdefa=a,8,* \
CDEF:cdefe=b,8,* \
CDEF:cdeff=c,8,*,-1,* \
CDEF:cdefg=c,8,* \
CDEF:cdefba=c,8,*,-1,* \
CDEF:cdefbb=d,8,*,-1,* \
AREA:cdefa#00FF0099:"Inbound"  \
GPRINT:cdefa:LAST:"   Current\:%8.2lf %s"  \
GPRINT:cdefa:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefa:MAX:"Maximum\:%8.2lf %s"  \
LINE1:cdefe#005D57FF:"Peak\n"  \
AREA:cdeff#00FF897C:"Outbound"  \
GPRINT:cdefg:LAST:"  Current\:%8.2lf %s"  \
GPRINT:cdefg:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefg:MAX:"Maximum\:%8.2lf %s"  \
LINE1:cdefa#000000FF:""  \
LINE1:cdefba#000000FF:""  \
LINE1:cdefbb#0000AAFF:"Peak\n" \
COMMENT:"\n" \
COMMENT:"Last Updated\:  $_NOW"
# loopback
/usr/bin/rrdtool graph ${_DATADIR}/${_SNMP_HOST}-loopback-traffic-$_T.png \
--imgformat=PNG \
--start=$_STARTTIME \
--end=-300 \
--title="${_SNMP_HOST} - Traffic - loopback" \
--rigid \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale \
--vertical-label="bits per second" \
--slope-mode \
--font TITLE:12: \
--font AXIS:8: \
--font LEGEND:10: \
--font UNIT:8: \
DEF:a="${_HOME}/${_SNMP_HOST}.rrd":lo_in:AVERAGE \
DEF:b="${_HOME}/${_SNMP_HOST}.rrd":lo_in:MAX \
DEF:c="${_HOME}/${_SNMP_HOST}.rrd":lo_out:AVERAGE \
DEF:d="${_HOME}/${_SNMP_HOST}.rrd":lo_out:MAX \
CDEF:cdefa=a,8,* \
CDEF:cdefe=b,8,* \
CDEF:cdeff=c,8,*,-1,* \
CDEF:cdefg=c,8,* \
CDEF:cdefba=c,8,*,-1,* \
CDEF:cdefbb=d,8,*,-1,* \
AREA:cdefa#00FF0099:"Inbound"  \
GPRINT:cdefa:LAST:"   Current\:%8.2lf %s"  \
GPRINT:cdefa:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefa:MAX:"Maximum\:%8.2lf %s"  \
LINE1:cdefe#005D57FF:"Peak\n"  \
AREA:cdeff#00FF897C:"Outbound"  \
GPRINT:cdefg:LAST:"  Current\:%8.2lf %s"  \
GPRINT:cdefg:AVERAGE:"Average\:%8.2lf %s"  \
GPRINT:cdefg:MAX:"Maximum\:%8.2lf %s"  \
LINE1:cdefa#000000FF:""  \
LINE1:cdefba#000000FF:""  \
LINE1:cdefbb#0000AAFF:"Peak\n" \
COMMENT:"\n" \
COMMENT:"Last Updated\:  $_NOW"
done

请将以上的代码复制,并粘贴,保存为ucd-net-server.sh,按照上面的说明使用.

以上内容仅供大家参考,欢迎修正,谢谢.
注意,此脚本是在rrdtool-1.3.8,CentOS 5.2环境下测试通过.

你可能感兴趣的:(linux,职场,snmp,rrdtool,休闲)