Thursday, May 1, 2008

Extending Net-SNMP with Perl

I am the first to avoid perl, so much so that I've never really done any perl. However, I do have some good ruby experience, which was helpful.

You can extend Net-SNMP with C, but the 3rd party libs I was interfacing with were C++ and it just was not playing nicely. Furthermore, if you extend Net-SNMP with C you have to compile a shared lib, which isn't bad, but it must be compiled with gcc not g++, or you get warning, which is bad.

If you go the perl route you get all the functionality, but it way easier to code, and much easier to debug and test!

Here are a few road blocks I ran into using the examples and documentation provided by Net-SNMP.

This is the way I got setValue return either a OctString or Integer?
There are probably some constants defined but not exported or something, because their example will produce the error: "unknown var value type: 0"
so what I need is define two new vars:


my $ASN_OCTET_STR = 4;
my $ASN_INTEGER = 2;

then I called setValue() like so:


$result->setValue($ASN_OCTET_STR, $my_value);

or

$result->setValue($ASN_INTEGER, $my_value);


Ta-Da!! Now it works. So if you see that error you know what is going on and how to fix it.

1 comment:

Unknown said...

Thanks for this post, it gave me a clue as to why the sample code didn't work! You can also do a use constant ASN_INTEGER => 2; statement, which sets up constants rather than variables.