Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

statico (5018)

statico
  ian.langworthNO@SPAMgmail.com
http://langworth.com/
AOL IM: eisforian (Add Buddy, Send Message)

PAUSE-ID: IAN [cpan.org]

Co-author of Perl Testing: A Developer's Notebook [oreilly.com]

Journal of statico (5018)

Sunday May 23, 2004
07:10 PM

do I release this?

[ #18906 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Because most people don't think about usability. (Though I'll note that your example code doesn't show any error handling)
    • 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 u

      --
      qw(Ian Langworth)
  • 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.

    --

    @JAPH = qw(Hacker Perl Another Just);
    print reverse @JAPH;