Here is the code I am asking about.
#! /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",
"<html>",
"<head>",
"<title>Veraz NGN Services (cpu stats)</title>",
"</head>",
"<body>"
);
}
for my $host (@host){
### init
my $file = "$path/$host-cpu.rrd";
### debug
print "$file\n" if $debug;
### create file
unless(-e $file) {
RRDs::create($file,
"--step",300,
"DS:user:COUNTER:600:0:100",
"DS:nice:COUNTER:600:0:100",
"DS:system:COUNTER:600:0:100",
"DS:idle:COUNTER:600:0:100",
"DS:kernel:COUNTER:600:0:100",
"DS:interrupt:COUNTER:600:0:100",
"RRA:AVERAGE:0.5:1:600",
"RRA:AVERAGE:0.5:6:700",
"RRA:AVERAGE:0.5:24:775",
"RRA:AVERAGE:0.5:288:2000",
"RRA:MAX:0.5:1:600",
"RRA:MAX:0.5:6:700",
"RRA:MAX:0.5:24:775",
"RRA:MAX:0.5:288:2000",
"RRA:MIN:0.5:1:600",
"RRA:MIN:0.5:6:700",
"RRA:MIN:0.5:24:775",
"RRA:MIN:0.5:288:2000"
);
### error?
if(my $ERROR = RRDs::error) {
print "ERROR: $ERROR\n";
}
}
### debug
print "$host\n" if $debug;
### snmp
my($s, $err) = Net::SNMP->session(
-hostname => $host,
-community => "public",
-timeout => 1,
-version => 2
);
if($err) {
die "Host ($host) down: $err\n";
}
### html
if($once) {
print INDEX join("\n",
qq(<a href="${host}.html"><img src="$link$host-day.png" border
+="0"></a>),
);
}
$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:$ssCpuRawId
+le:$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(<html>),
qq(<head>),
qq(<title>$host</title>),
qq(</head>),
qq(<body>),
qq(<img src="$link$host-day.png">),
qq(<img src="$link$host-week.png">),
qq(<img src="$link$host-month.png">),
qq(<img src="$link$host-year.png">),
);
close INTER;
}
}
|