[功能]
(1)画图功能
(2)起始与结束时间自定
(3)以web形式显示
[过程]
#!/usr/bin/env perl
#Author Jacky
#Create 2009/1/4
use strict;
use POSIX qw(strftime);
use CGI qw(:standard);
use RRDs;
my $error;
#从表单中获取起始时间
my $s_year=param('syear');
my $s_mon=param('smonth');
my $s_day=param('sday');
my $s_hour=param('shour');
my $s_min=param('smin');
#从表单中获取结束时间
my $e_year=param('eyear');
my $e_mon=param('emonth');
my $e_day=param('eday');
my $e_hour=param('ehour');
my $e_min=param('emin');
#上面的变量获取是为strftime做准备的
#下面是一些CGI的内容,具体还要考CGI.pm模块,可以到perldoc上看
print header(-type=>'text/html',-charset=>'gb2312');
print start_html(-title=>'Cacti',
-author=>'',
-base=>'true',
-target=>'_blank',
-lang=>'zh_CN',
-meta=>{
'keywords'=>'Cacti',
'copyright'=>'Copyright 2009 Jacky',},
-head=>meta({-http_equiv => 'Content-Type',
-content=>"text/html;charset=gb2312;no-cache"}),
-BGCOLOR=>'white');
print start_form( { target=>"_self" } ),
div( { -align => "left"},
span("Start:"),
span("year:"),
textfield( -name=>"syear",maxlength=>"4",size=>"4")."\n",
span("month:"),
textfield( -name=>"smonth",maxlength=>"2",size=>"4")."\n",
span("day:"),
textfield( -name=>"sday",maxlength=>"2",size=>"4")."\n",
span("hour:"),
textfield( -name=>"shour",maxlength=>"2",size=>"2")."\n",
span("min:"),
textfield( -name=>"smin",maxlength=>"2",size=>"2")."\n",
span("End:"),
span("year:"),
textfield( -name=>"eyear",maxlength=>"4",size=>"4")."\n",
span("month:"),
textfield( -name=>"emonth",maxlength=>"2",size=>"4")."\n",
span("day:"),
textfield( -name=>"eday",maxlength=>"2",size=>"4")."\n",
span("hour:"),
textfield( -name=>"ehour",maxlength=>"2",size=>"2")."\n",
span("min:"),
textfield( -name=>"emin",maxlength=>"2",size=>"2")."\n",
submit( -name => "save", -value => "Ok" ),
);
print end_form;
#这是一个函数,也就是当点击了OK,那么这个函数返回真,则往下执行
if(param()){
my $ts_year=$s_year-1900;
my $te_year=$e_year-1900;
my $ts_mon=$s_mon-1;
my $te_mon=$e_mon-1;
#进行简单的处理,也是为strftime
my $t_time=strftime("%s", 0, $s_min, $s_hour, $s_day,$ts_mon , $ts_year);
my $e_time=strftime("%s", 0, $e_min, $e_hour, $e_day,$te_mon , $te_year);
#获取起始与结束的时间戳
#下面是RRDs的内容了,需要perldoc RRDs就可以看到相关的资料了。
my( $averages,$xsize,$ysize )=RRDs::graph('/usr/local/www/data/traffic.png',
'--imgformat','PNG',
'-s', $t_time,
'-e',$e_time,
'--title','GZ-Cacti-3750 - Traffic',
'--rigid',
'--base','1000',
'--height','120',
'--width','500',
'--alt-autoscale-max',
'--lower-limit','0',
'--vertical-label','bits per second',
#定义4个端口,这4个端口是(1+1)-(1+1),得出一张图。其中CDEF起到关键作用.
#xxport
'DEF:in=xxx_xxx_xxx_x_traffic_in_144.rrd:traffic_in:AVERAGE',
'DEF:out=xxx_xxx_xxx_x_traffic_in_144.rrd:traffic_out:AVERAGE',
#xxport
'DEF:in1=xxx_xxx_xxx_x_traffic_in_145.rrd:traffic_in:AVERAGE',
'DEF:out1=xxx_xxx_xxx_x_traffic_in_145.rrd:traffic_out:AVERAGE',
#xxport
'DEF:in2=xxx_xxx_xxx_x_traffic_in_108.rrd:traffic_in:AVERAGE',
'DEF:out2=xxx_xxx_xxx_x_traffic_in_108.rrd:traffic_out:AVERAGE',
#xxport
'DEF:in3=xxx_xxx_xxx_x_traffic_in_109.rrd:traffic_in:AVERAGE',
'DEF:out3=xxx_xxx_xxx_x_traffic_in_109.rrd:traffic_out:AVERAGE',
'CDEF:cdefin=in,8,*',
'CDEF:cdefout=out,8,*',
'CDEF:cdefin1=in1,8,*',
'CDEF:cdefout1=out1,8,*',
'CDEF:cdefin2=in2,8,*',
'CDEF:cdefout2=out2,8,*',
'CDEF:cdefin3=in3,8,*',
'CDEF:cdefout3=out3,8,*',
'CDEF:cdefin4=cdefin,cdefin1,+',
'CDEF:cdefout4=cdefout,cdefout1,+',
'CDEF:cdefin5=cdefin2,cdefin3,+',
'CDEF:cdefout5=cdefout2,cdefout3,+',
'CDEF:cdefin6=cdefin4,cdefout5,-',
'CDEF:cdefout6=cdefout4,cdefin5,-',
'AREA:cdefin6#00CF00:Inbound',
'GPRINT:cdefin6:LAST: Current\:%8.2lf %s',
'GPRINT:cdefin6:AVERAGE:Average\:%8.2lf %s',
'GPRINT:cdefin6:MAX:Maximum\:%8.2lf %s\n',
'LINE1:cdefout6#002A97:Outbound',
'GPRINT:cdefout6:LAST:Current\:%8.2lf %s',
'GPRINT:cdefout6:AVERAGE:Average\:%8.2lf %s',
'GPRINT:cdefout6:MAX:Maximum\:%8.2lf %s\n'
);
#如果上面出错了,就报告出错的原因,如果没有出错,就把生产的图给你显示出来!
if ( $error = RRDs::error ) {
print "rrdtool fetch failed: \"$error\"\n";
exit 1;
}
else{
print <
END
}
}
print end_html;
[结论]rrdtoo,perl功能强大,继续学习...
[附图]
本文出自 “坏男孩” 博客,请务必保留此出处http://5ydycm.blog.51cto.com/115934/124334
本文出自 51CTO.COM技术博客