user1300922 user1300922 - 1 year ago 78
Perl Question

Perl: SNMP - Wildcard OID

I have a Perl script that monitors any SNMP enabled service.

The way it works is I have a config file with multiple services, and each service has a list of metrics to collect.


switch_stuff1 =
switch_stuff2 =
switch_stuff3 =
switch_stuff4 =
switch_stuff5 =

router_stuff1 =
router_stuff2 =
router_stuff3 =

db_stuff1 =
db_stuff2 =

The script will loop through the config file, obtain the information for all the metrics and write the output to a CSV file.

Now, onto what I need help with.

I was asked to implement logic that would allow the use of wildcards in the SNMP paths to collect. So my config file would now need to look like this:

switch_stuff1 =*
switch_stuff2 = 1.45.*.12

So when the collection will happen, it will somehow loop like this:

switch_stuff1 – found, continue – found, continue – not found, stop

switch_stuff2 – found, continue – found, continue – found, continue – not found, stop

I'm currently using the Net::SNMP library:

Would this be possible to do? I was thinking of somehow simulating an "snmpwalk" via the get_entries or get_table methods, but I'm not quite sure if that would work.

If anyone can help out or at least point me in the right direction, it would be greatly appreciated.

Thank you.

Answer Source

For entries like switch_stuff1 =* you could simply send first request and after that loop through get_next_request as long as you get data.

However, with config like switch_stuff2 = 1.45.*.12 it is not so simple any more. You can't use get_next_request and you have to iterate over possible values yourself. The problem is that numbers are "usually" continuous but not always and in latter case it makes it quite stressful for the system to just try all possible numbers. At least if you really need to do this then I suggest to cache somehow OIDs that exists and not scan them every time you collect data.