[背景]DBA的要求,每隔一天给它们绘一张交换机总出口的流量,根据上次的开发,我觉得那太麻烦,就玩了下RRD模块,发现功能不错,今天就献给大家了。通过这个案例大家也能开发出自己满意的脚本(DBA们的要求是以后每隔3小时,其实做法差不多!)。看下文:
[过程]
#!/usr/bin/perl -w
#Author Jacky
#Date 2008/12/31
use strict;
use RRDs;
my $error;
my $starttime=`date -j -v -1d "+%s"`; #在这里可以改成date -j -v -30M "+%s"(即每隔30分钟获取一张图片)
chomp($starttime);
my $endtime=`date -j "+%s"`;
chomp($endtime);
my $path="/usr/local/share/cacti/rrdimg/";
my($averages,$xsize,$ysize )=RRDs::graph('/usr/local/share/cacti/rrdimg/tel01.png',
'--imgformat','PNG',
'-s', $starttime,
'-e',$endtime,
'--title','GZ-CSO-Tel1 - Traffic',
'--rigid',
'--base','1000',
'--height','120',
'--width','500',
'--alt-autoscale-max',
'--lower-limit','0',
'--vertical-label','bits per second',
'DEF:in=/usr/local/share/cacti/rrdimg/xxx_xxx_xxx_xxx_traffic_in_72.rrd:traffic_in:AVERAGE',
'DEF:out=/usr/local/share/cacti/rrdimg/xxx_xxx_xxx_xxx_traffic_in_72.rrd:traffic_out:AVERAGE',
'CDEF:cdefin=in,8,*',
'CDEF:cdefout=out,8,*',
'AREA:cdefin#00CF00:Inbound',
'GPRINT:cdefin:LAST: Current\:%8.2lf %s',
'GPRINT:cdefin:AVERAGE:Average\:%8.2lf %s',
'GPRINT:cdefin:MAX:Maximum\:%8.2lf %s\n',
'LINE1:cdefout#002A97:Outbound',
'GPRINT:cdefout:LAST:Current\:%8.2lf %s',
'GPRINT:cdefout:AVERAGE:Average\:%8.2lf %s',
'GPRINT:cdefout:MAX:Maximum\:%8.2lf %s\n'
);
if ( $error = RRDs::error ) {
print "rrdtool fetch failed: \"$error\"\n";
exit 1;
}
再结合crontab,在指定的时间运行.如:
59 23 * * * /usr/bin/perl /usr/local/share/cacti/rrdimg/rrdimg.pl > /dev/null 2>&1
每天晚上23点59分绘一张满意的图出来,哈哈。。。。
[结论]这个脚本可以完全替代了我第一篇cacti开发的文章,以后,我还会用此类脚本写出几张图的合并。希望大家能用得上......
本文出自 “坏男孩” 博客,请务必保留此出处http://5ydycm.blog.51cto.com/115934/123768
本文出自 51CTO.COM技术博客