用perl写的cacti定时取图的扩展

发布时间:2020-10-06编辑:脚本学堂
DBA提出的需求:每隔一天给它们绘一张交换机总出口的流量。测试了下RRD模块,发现功能不错,这里为大家提供一个我写的例子,供大家学习参考吧。

DBA提出的需求:
每隔一天给它们绘一张交换机总出口的流量。
测试了下RRD模块,发现功能不错,这里为大家提供一个我写的例子,供大家学习参考吧。

复制代码 代码如下:

#!/usr/bin/perl -w

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 %sn',
                               'LINE1:cdefout#002A97:Outbound',
                               'GPRINT:cdefout:LAST:Current:%8.2lf %s',
                               'GPRINT:cdefout:AVERAGE:Average:%8.2lf %s',
                               'GPRINT:cdefout:MAX:Maximum:%8.2lf %sn'
);

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分绘一张满意的图出来,是不是很酷啊。