在上一篇文章中,有兄弟问我要code source,因前面写的数据收集用的是本地获取的,所以就想先改进下数据收集方式,再发出来。今天,我采用snmp方式把数据收集出来,并画图。
以下代码仅供参考:
#!/usr/bin/perl use Net::SNMP; use RRDs; my @hosts=("localhost","127.0.0.1","192.168.x.y"); my $deBUG=1; my $rrd_path="/usr/rrd/"; my $pic_path="/usr/rrd/pic/"; my $oID=".1.3.6.1.4.1.2021.51.101.1"; sub CREATERRD{ my ($rrdname)=shift; RRDs::create("$rrdname", "--step", 300, "DS:1min:GAUGE:600:U:U", "DS:5min:GAUGE:600:U:U", "DS:15min:GAUGE:600:U:U", "RRA:AVERAGE:0.5:1:600", "RRA:AVERAGE:0.5:6:700", "RRA:AVERAGE:0.5:24:775", "RRA:AVERAGE:0.5:288:797", ); my $ERROR = RRDs::error; if ($ERROR){ print "ERROR unable to create ${ERROR} \n " if $deBUG; print "create no\n" if $deBUG; return 0; }else{ print "$rrdname createok\n" if $deBUG; return 1; } } sub UPDATERRD{ my ($rrdname,$L1min,$L5min,$L15min)=@_; RRDs::update ("$rrdname","N:$L1min:$L5min:$L15min"); my $ERROR=RRDs::error; if ($ERROR){ print "ERROR unable to update ${ERROR} \n" if $deBUG; return 0; }else{ print "update success\n" if $deBUG; return 1; } } sub GRAPHPNG{ my ($rrdfile,$starttime)=@_; my $rrdTitle1; if($rrdfile=~/(.*)\.rrd$/i){ $rrdTitle1=$1; }else{ $rrdTitle1=$rrdfile; } my $pngfile=$pic_path.$rrdTitle1.".png"; print $pngfile."\n" if $deBUG; my $rrdfile=$rrd_path.$rrdfile; RRDs::graph($pngfile, '--Title',$rrdTitle1, '--Font', 'Title:10:', '--Font', 'LEGEND:10:', '--base',1000, '--height',120, '--wIDth',600, '--vertical-label','cpu LoadAverage', '--start',-$starttime, "DEF:1min=$rrdfile:1min:AVERAGE", "DEF:5min=$rrdfile:5min:AVERAGE", "DEF:15min=$rrdfile:15min:AVERAGE", "AREA:1min#EACC00FF:1Min Average", 'GPRINT:1min:LAST: Current\:%8.2lf%s\n', "AREA:5min#EA8F00FF:5Min Average", 'GPRINT:5min:LAST:Current\:%8.2lf%s\n', "AREA:15min#FF0000FF:15Min Average", ); if (my $ERROR = RRDs::error) { print "ERROR: $ERROR\n" if $deBUG; return 0; } } sub GETSNMP { my $s = shift; my $oID = shift || return "U"; my $response = $s->get_request($oID); my $retval = $response->{$oID} || "U"; print "$oID -> $retval\n" if $deBUG; return ($retval =~ /(\d+)/) ? $retval : 'U'; } foreach my $ip(@hosts){ #create rrd my $rrd_filename = "$rrd_path$ip-loadaverage.rrd"; print "$rrd_filename\n" if $deBUG; unless(-e $rrd_filename){ &CREATERRD($rrd_filename); } #get data my($s, $err) = Net::SNMP->session( -hostname => $ip, -community => "CHKTIME", -timeout => 1, -version => 2 ); my $LoadAverage = GETSNMP($s,$oID); print $LoadAverage."\n" if $deBUG; my ($L1m,$L5m,$L15m)=split(" ",$LoadAverage); print "\n".$L1m."\t".$L5m."\t".$L15m."\n" if $deBUG; #update rrd if( -e $rrd_filename){ &UPDATERRD($rrd_filename,$L1m,$L15m); } #graph picture &GRAPHPNG("$ip-loadaverage.rrd",86400); }
总结
以上是内存溢出为你收集整理的perl snmp rrdtool 画图全部内容,希望文章能够帮你解决perl snmp rrdtool 画图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)