perl snmp rrdtool 画图
在上一篇文章中,有兄弟问我要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",
[*]'GPRINT:5min:LAST:Current\:%8.2lf%s\n',
[*]);
[*]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,$L5m,$L15m);
[*] }
[*]
[*] #graph picture
[*] &GRAPHPNG("$ip-loadaverage.rrd",86400);
[*]}
页:
[1]