0

How can I properly invoke an SNMP extend script by MIB with pysnmp 4.3?

I have this entry in the remote server's snmpd.conf file:

extend check_fd_wap /app/users/nagios_checks/check_fd_wap.sh

which in turn can be invoked by:

snmpwalk -t 60 -v2c -c greendale remoteserver 'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."check_fd_wap"'
NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."check_fd_wap" = STRING: { "sys_inuse" : 32640, "proc_data" : {  "ssl-mmsib" : { "proc_used" : 22, "proc_limit" : 200000 } , (...) }

In pysnmp I'm trying to invoke MIB NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."check_fd_wap" using the sample code:

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
        CommunityData(community, mpModel=1),
        UdpTransportTarget((hostname, port)),
        ContextData(),
        ObjectType(ObjectIdentity('NET-SNMP-EXTEND-MIB','nsExtendOutput1Line','check_fd_wap')))
)

for varBind in varBinds:
    print(' = '.join([x.prettyPrint() for x in varBind]))

However, this fails with the 'DisplayString' failed to cast value OctetString error message:

raise SmiError('MIB object %r having type %r failed to cast value %r: %s' % (self.__args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax().__class__.__name__, self.__args[1], sys.exc_info()[1]))
;SmiError: MIB object u'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.check_fd_wap' having type 'DisplayString' failed to cast value OctetString('{ "sys_inuse" (...)"" at DisplayString

However, calling it with the translated OID works fine ('.1.3.6.1.4.1.8072.1.3.2.3.1.1.12.99.104.101.99.107.95.102.100.95.119.97.112').

SNMPv2-SMI::enterprises.8072.1.3.2.3.1.1.12.99.104.101.99.107.95.102.100.95.119.97.112 = { "sys_inuse" : 31110, "proc_data" : {  "ssl-mmsib" : { "proc_used" : 19, "proc_limit" : 200000 } } }

What is the reason for this error?

Also, if I invoke the getCmd with the full MIB NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.check_fd_wap as one argument, I get the error:

pysnmp.smi.error.NoSuchObjectError: NoSuchObjectError({'str': 'Can\'t resolve node name ::(\'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line\', \'"check_fd_wap"\') at <pysnmp.smi.view.MibViewController instance at 0x2989638>'})

Is it possible to invoke the SNMP get with the full MIB in one string?

André Fernandes
  • 2,335
  • 3
  • 25
  • 33
  • Did you load your custom MIB file like it described in this answer http://stackoverflow.com/a/14635022/2807083 ? – user2807083 Jul 08 '16 at 14:03
  • No, since I'm not using a custom MIB, but "NET-SNMP-EXTEND-MIB". The MIB-to-OID translation works, what's failing is the processing of the SNMP response. – André Fernandes Jul 08 '16 at 14:13
  • I think message `Can\'t resolve node name ::(\'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line\', \'"check_fd_wap"\') at '})` obviously says that there is problem with MIB to OID translation – user2807083 Jul 08 '16 at 14:19
  • And the fact all works with numeric oid too. – user2807083 Jul 08 '16 at 14:20
  • It probably only means the `ObjectIdentity` constructor is not being properly invoked, since `ObjectType(ObjectIdentity('NET-SNMP-EXTEND-MIB','nsExtendOutput1Line','check_fd_wap')))` properly translates to an OID. But that's just the second question, for which I'd be satisfied with a "No, you can't call it like that" answer. – André Fernandes Jul 08 '16 at 14:44
  • `\'"check_fd_wap"\'` look on additional quotation mark in this string, may be this is the cause. I have no idea where are it came from in error message. – user2807083 Jul 08 '16 at 15:06
  • Removing the double quotes doesn't solve it. The double quotes come from the syntax required to invoke this particular MIB with snmpwalk/snmpget in the command-line. – André Fernandes Jul 08 '16 at 15:34

1 Answers1

2

This failure:

'DisplayString' failed to cast value OctetString

happens when pysnmp receives a response from SNMP Agent for requested OID

1.3.6.1.4.1.8072.1.3.2.3.1.1.12.99.104.101.99.107.95.102.100.95.119.97.112

(which corresponds to NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.check_fd_wap MIB object) and then it tried to convert response value ({ "sys_inuse" : 32640, "proc...) into human-friendly DisplayString representation. At that point pysnmp failed but the exact cause is not included in your report for some reason.

I am guessing that response string sent by your SNMP Agent may accidentally exceed the length of 255 characters (which is a constraint of DisplayString type).

Formally correct solution is to make SNMP Agent response for that MIB object fitting 255 ASCII characters to satisfy DisplayString type limits.

Alternatively, you could disable MIB lookup for response values in pysnmp by passing lookupMib=False keyword argument to getCmd() function.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21