do I release this?

statico on 2004-05-24T00:10:25

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', );


Why hasn't anyone else done this already? I really must be missing the Big Picture. All in all, I'd like to release this, but fear excommunication by releasing a ::Simple module. I need an snmp expert.


Why not?

jesse on 2004-05-24T04:55:25

Because most people don't think about usability.

(Though I'll note that your example code doesn't show any error handling)

Re:Why not?

statico on 2004-05-24T14:51:01

Here's a little from the bits I omitted,
    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');
    ...
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';

simple is good

TeeJay on 2004-05-24T12:58:09

I released a ::SIMPLE module for XML::Xerces because it made my life easier.

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.