ryan.lawrence
ryan.lawrence

Reputation: 27

Force SNMP to see number as Integer

I have two scripts that take values from SAR and place them in a log file (one example):

rxkBps=`sar -n DEV 1 295 | tail -n1 | awk '{print $5}'`
rxMbps=$(echo "scale=2;$rxkBps/128" | bc -l)
echo $rxMbps >./rxMbps.log
exit

That number is always a decimal number (ex: .06).
In snmpd.conf I've added an extend section for both scripts that takes that number and associates it with its OID represented by in this example Get_rxMbps:

extend Get_rxMbps /bin/sh /usr/local/bin/Get_rxMbps.sh

When I run an snmpwalk over NET-SNMP-EXTEND-MIB::nsExtendObjects I get back the information but it sees it as a string and our monitoring software can't convert or use the string information.

NET-SNMP-EXTEND-MIB::nsExtendCommand."Get_rxMbps" = STRING: /bin/sh
NET-SNMP-EXTEND-MIB::nsExtendArgs."Get_rxMbps" = STRING: /usr/local/bin/Get_rxMbps.sh
NET-SNMP-EXTEND-MIB::nsExtendInput."Get_rxMbps" = STRING:
NET-SNMP-EXTEND-MIB::nsExtendCacheTime."Get_rxMbps" = INTEGER: 5
NET-SNMP-EXTEND-MIB::nsExtendExecType."Get_rxMbps" = INTEGER: exec(1)
NET-SNMP-EXTEND-MIB::nsExtendRunType."Get_rxMbps" = INTEGER: run-on-read(1)
NET-SNMP-EXTEND-MIB::nsExtendStorage."Get_rxMbps" = INTEGER: permanent(4)
NET-SNMP-EXTEND-MIB::nsExtendStatus."Get_rxMbps" = INTEGER: active(1)
NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."Get_rxMbps" = STRING: .83
NET-SNMP-EXTEND-MIB::nsExtendOutputFull."Get_rxMbps" = STRING: .83
NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."Get_rxMbps" = INTEGER: 1
NET-SNMP-EXTEND-MIB::nsExtendResult."Get_rxMbps" = INTEGER: 0
NET-SNMP-EXTEND-MIB::nsExtendOutLine."Get_rxMbps".1 = STRING: .83

I'm new to this and inheriting someone else's work so if there is a better way I'm open for that as well, but I need SNMP to see and use that number as an integer not a string.

Upvotes: 2

Views: 3992

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385204

You can't; this is a limitation of the "extend" functionality. NET-SNMP-EXTEND-MIB::nsExtendOutputFull rows are STRINGs, period. Your data has to fit into the NET-SNMP-EXTEND-MIB definitions, which have no choice really but to ship everything as strings (since types are fixed at MIB-time).

It sounds like you want to properly define a subagent for your own MIB, perhaps using the AgentX feature. However, you're going to need to add support into your monitoring software for that, unless you're okay with it not understanding what information it's receiving.

You may be able to take some inspiration from this example that shows multiple extension methods, described in more detail here.

(I was half-tempted to suggest cheating, and passing your value to Net-SNMP as the script's return code, instead of as its textual output; then you'd have it in NET-SNMP-EXTEND-MIB::nsExtendResult as an INTEGER. However, I don't know whether Net-SNMP has constraints on return codes and this would be a bit of a hack unless you worked that out.)

Upvotes: 1

scai
scai

Reputation: 21479

The types are defined in the corresponding MIB file which you shouldn't modify unless you are the author of this file.

You can tell snmpwalk, snmpget and similar commands to remove the type information by passing option -OQ but this will still lead to strings being quoted (at least for my snmpwalk command).

I suggest to either adapt your monitoring software or pass the SNMP output through a script which will convert/remove the string type information accordingly.

Upvotes: 1

Related Questions