I must have missed something.
I've been working with SNMP with Cisco switches lately. Out of sheer curiosity, I used scli to tell me about one of the printers. Two days later, I'm determined to make a printer status page for the college.
So I'm futzing around with SNMP and doing things in what appears to be The Standard Way. And it's disgusting, just glance at it:
# console information
my $vars = new SNMP::VarList( ['prtConsoleDisplayBufferText'] );
my ($msg) = $s->getnext($vars);
die $s->{ErrorStr} if $s->{ErrorStr};
while ( !$s->{ErrorStr} and $$vars[0]->tag eq "prtConsoleDisplayBufferText" ) {
push @{ $data{consoletext} }, $msg;
($msg) = $s->getnext($vars);
}
# status lights
$vars = new SNMP::VarList(
['prtConsoleOnTime'],
['prtConsoleColor'],
['prtConsoleDescription'],
);
my ($light_status, $light_color, $light_desc) = $s->getnext($vars);
die $s->{ErrorStr} if $s->{ErrorStr};
while ( !$s->{ErrorStr} and $$vars[0]->tag eq "prtConsoleOnTime" ) {
push @{ $data{lights} }, {
status => ($light_status ? 0 : 1),
color => &SNMP::mapEnum($$vars[1]->tag, $light_color),
description => $light_desc,
};
($light_status, $light_color, $light_desc) = $s->getnext($vars);
}
# trays and capacity
$vars = new SNMP::VarList(
['prtInputName'],
['prtInputMediaName'],
['prtInputStatus'],
['prtInputCurrentLevel'],
['prtInputMaxCapacity'],
);
my ($light_status, $light_color, $light_desc) = $s->getnext($vars);
die $s->{ErrorStr} if $s->{ErrorStr};
while ( !$s->{ErrorStr} and $$vars[0]->tag eq "prtConsoleOnTime" ) {
push @{ $data{lights} }, {
status => ($light_status ? 0 : 1),
color => &SNMP::mapEnum($$vars[1]->tag, $light_color),
description => $light_desc,
};
($light_status, $light_color, $light_desc) = $s->getnext($vars);
}
So I quickly wrote up SNMP::Simple, which turns the above into this:
$data{name} = $s->get('sysName'); $data{location} = $s->get('sysLocation'); $data{consoletext} = $s->get_list('prtConsoleDisplayBufferText');
$data{lights} = $s->get_named_table( status => 'prtConsoleOnTime', color => 'prtConsoleColor', name => 'prtConsoleDescription', );
$data{trays} = $s->get_named_table( name => 'prtInputName', media => 'prtInputMediaName', status => 'prtInputStatus', level => 'prtInputCurrentLevel', max => 'prtInputMaxCapacity', );
Re:Why not?
statico on 2004-05-24T14:51:01
Here's a little from the bits I omitted,The data is passed to a template anyway, which reflects the "oh well" error-handling disposition. I could always certainly add $data{name} = $s->get('sysName') || 'unknown';unless ( Net::Ping->new->ping($host,1) ) {
warn "Couldn't ping $host\n";
next;
}
my $s = SNMP::Simple->new(
DestHost => $host,
Community => 'public',
Version => 1,
);
warn "No session for $host" && next unless $s;
$data{name} = $s->get('sysName');
...
chances are your code will be useful to somebody else, too.
Its always worth getting the ball rolling, thatway people can contribute, rather than writing then forgetting handy code snippets.