#! /usr/bin/perl -w use Net::SNMP; use RRDs; use strict; use vars; my @host = qw/coccp3 coccp2/; my $link = '/mrtg/work/'; my $html = '/mrtg/work/dev/hari/rrd/cpu/'; my $path = '/mrtg/work/dev/hari/rrd/cpu/'; my $debug = '0'; my $once = '1'; sub getsnmp { ### init my $s = shift; my $oid = shift || return "U"; my $response = $s->get_request($oid); my $retval = $response->{$oid} || "U"; ### debug print "$oid -> $retval\n" if $debug; ### the end return ($retval =~ /(\d+)/) ? $retval : 'U'; } sub graph { ### init my $host = shift; my $file = shift; my $span = shift; my $name = shift; my @data; ### fix data push(@data, "$html/$host-$name.png", "--start=-$span", "--vertical-label=cpu usage %", # "--width=800", # "--height=600", "--lazy", "--imgformat=PNG", "--title=cpu usage for $host", "DEF:user=$file:user:AVERAGE", "DEF:system=$file:system:AVERAGE", "DEF:idle=$file:idle:AVERAGE", "DEF:kernel=$file:kernel:AVERAGE", "DEF:interrupt=$file:interrupt:AVERAGE", "AREA:user#FF0000:user", "STACK:system#00FF00:system", "STACK:idle#0000FF:idle", "STACK:kernel#00CCFF:kernel", "STACK:interrupt#FFFF00:interrupt", ); ### rrdtool RRDs::graph(@data); if(my $ERROR = RRDs::error) { print "ERROR: $ERROR\n"; } } if($once){ ### open file open(INDEX, '>' ,"$html/index.html") or die $!; ### print html print INDEX join("\n", "", "
", "
),
);
}
$s->translate([-timeticks => 0x0]);
my $uptime = getsnmp($s, ".1.3.6.1.2.1.1.3.0",1);
if($uptime ne "U" && $uptime < 60000) {
RRDs::update($file, "N:U:U:U:U:U:U:U:U:U:U:U:U");
sleep 2;
}
my $ssCpuRawUser = getsnmp($s, ".1.3.6.1.4.1.2021.11.50.0");
my $ssCpuRawNice = getsnmp($s, ".1.3.6.1.4.1.2021.11.51.0");
my $ssCpuRawSystem = getsnmp($s, ".1.3.6.1.4.1.2021.11.52.0");
my $ssCpuRawIdle = getsnmp($s, ".1.3.6.1.4.1.2021.11.53.0");
my $ssCpuRawKernel = getsnmp($s, ".1.3.6.1.4.1.2021.11.55.0");
my $ssCpuRawInterrupt = getsnmp($s, ".1.3.6.1.4.1.2021.11.56.0");
print
"N:$ssCpuRawUser:$ssCpuRawNice:$ssCpuRawSystem:$ssCpuRawIdle:$ssCpuRawKernel
:$ssCpuRawInterrupt\n" if $debug;
RRDs::update($file, join(":", "N",
$ssCpuRawUser, $ssCpuRawNice, $ssCpuRawSystem,
$ssCpuRawIdle, $ssCpuRawKernel, $ssCpuRawInterrupt,
));
if(my $ERROR = RRDs::error) {
print "ERROR: $ERROR\n";
};
$s->close();
graph($host, $file, 3600, "hour");
graph($host, $file, 86400, "day");
graph($host, $file, 86400*7, "week");
graph($host, $file, 86400*31, "month");
graph($host, $file, 86400*365, "year");
if($once) {
open(INTER, '>', "$html/$host.html");
### print html
print INTER join("\n",
qq(),
qq(),
qq(
),
qq(
),
qq(
),
qq(
),
);
close INTER;
}
}