-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

After readings some more blogs and documentation I've learned how to
convert an index string to a dotted-decimal, numeric OID. Looking up
this OID works flawlessly.

I took the nsExtendOutput1Line OID and tacked on the length of the
index string, followed by the decoded ASCII values for each letter, as
documented in the FAQ (http://www.net-snmp.org/FAQ.html).

The following two functions are how I implemented it in python.

import netsnmp


def snmp(thing, host):
    """
    Perform an SNMP lookup of an index string on a host, return the
result.
    """
    var = netsnmp.Varbind(string_to_oid(thing))
    result = netsnmp.snmpget(var, Version=2, DestHost=host,
Community="public")
    return result[0]


def string_to_oid(name):
    """
    Convert an net-snmp string index to an OID.
    The OID starts with nsExtendOutput1Line, followed by the length of the
    string index, followed by the ASCII values for the individual
characters.
    Returns a fully qualified, dotted-decimal, numeric OID.
    """
    oid_prefix = "1.3.6.1.4.1.8072.1.3.2.3.1.1"
    ascii_values = [str(ord(element)) for element in list(name)]
    return ".".join(["", oid_prefix, str(len(name))]+ascii_values)


I hope it might prove useful to someone.

Kenny Rasschaert
+32 497 72 27 69 | Open Source consultant - Inuits.eu

On 12/03/2014 06:06 PM, Kenny Rasschaert wrote:
> Hi,
> 
> I'm using net-snmp 5.7.2 and the python bindings on CentOS 6. I've 
> added some extensions to my local net-snmp agent with the extend 
> keyword. They look like this:
> 
> extend galera-seqno /usr/local/sbin/galera-status.sh seqno extend
> galera-status /usr/local/sbin/galera-status.sh short
> 
> With the snmp command line tools, they work as expected: [root@db01
> ~]# snmpwalk -v 2c -c public localhost 
> 'NET-SNMP-EXTEND-MIB::nsExtendObjects' 
> NET-SNMP-EXTEND-MIB::nsExtendNumEntries.0 = INTEGER: 2 
> NET-SNMP-EXTEND-MIB::nsExtendCommand."galera-seqno" = STRING: 
> /usr/local/sbin/galera-status.sh 
> NET-SNMP-EXTEND-MIB::nsExtendCommand."galera-status" = STRING: 
> /usr/local/sbin/galera-status.sh 
> NET-SNMP-EXTEND-MIB::nsExtendArgs."galera-seqno" = STRING: seqno 
> NET-SNMP-EXTEND-MIB::nsExtendArgs."galera-status" = STRING: short 
> NET-SNMP-EXTEND-MIB::nsExtendInput."galera-seqno" = STRING: 
> NET-SNMP-EXTEND-MIB::nsExtendInput."galera-status" = STRING: 
> NET-SNMP-EXTEND-MIB::nsExtendCacheTime."galera-seqno" = INTEGER: 5 
> NET-SNMP-EXTEND-MIB::nsExtendCacheTime."galera-status" = INTEGER:
> 5 NET-SNMP-EXTEND-MIB::nsExtendExecType."galera-seqno" = INTEGER:
> exec(1) NET-SNMP-EXTEND-MIB::nsExtendExecType."galera-status" =
> INTEGER: exec(1) 
> NET-SNMP-EXTEND-MIB::nsExtendRunType."galera-seqno" = INTEGER: 
> run-on-read(1) NET-SNMP-EXTEND-MIB::nsExtendRunType."galera-status"
> = INTEGER: run-on-read(1) 
> NET-SNMP-EXTEND-MIB::nsExtendStorage."galera-seqno" = INTEGER: 
> permanent(4) NET-SNMP-EXTEND-MIB::nsExtendStorage."galera-status" =
> INTEGER: permanent(4) 
> NET-SNMP-EXTEND-MIB::nsExtendStatus."galera-seqno" = INTEGER:
> active(1) NET-SNMP-EXTEND-MIB::nsExtendStatus."galera-status" =
> INTEGER: active(1) 
> NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."galera-seqno" = STRING:
> -1 NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."galera-status" =
> STRING: stopped 
> NET-SNMP-EXTEND-MIB::nsExtendOutputFull."galera-seqno" = STRING:
> -1 NET-SNMP-EXTEND-MIB::nsExtendOutputFull."galera-status" =
> STRING: stopped 
> NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."galera-seqno" = INTEGER:
> 1 NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."galera-status" =
> INTEGER: 1 NET-SNMP-EXTEND-MIB::nsExtendResult."galera-seqno" =
> INTEGER: 0 NET-SNMP-EXTEND-MIB::nsExtendResult."galera-status" =
> INTEGER: 1 NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-seqno".1 =
> STRING: -1 NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-status".1 =
> STRING: stopped
> 
> and: [root@db01 ~]# snmpget -v 2c -c public localhost 
> 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-status".1' 
> NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-status".1 = STRING:
> stopped
> 
> I'm doing some python scripting where I would love to get this
> data over snmp, I was very pleased to discover that net-snmp comes
> with python bindings.
> 
> Walking over 'NET-SNMP-EXTEND-MIB::nsExtendObjects' works:
>>>> import netsnmp session = netsnmp.Session(Version = 2, 
>>>> DestHost="localhost",
> Community="public")
>>>> 
> session.walk(netsnmp.VarList(netsnmp.Varbind("NET-SNMP-EXTEND-MIB::nsExtendObjects")))
>
> 
('2', '/usr/local/sbin/galera-status.sh',
> '/usr/local/sbin/galera-status.sh', 'seqno', 'short', None, None,
> '5', '5', '1', '1', '1', '1', '4', '4', '1', '1', '-1', 'stopped',
> '-1', 'stopped', '1', '1', '0', '1', '-1', 'stopped')
> 
> Getting the output for both my extensions has also worked:
>>>> 
> session.walk(netsnmp.VarList(netsnmp.Varbind("NET-SNMP-EXTEND-MIB::nsExtendOutLine")))
>
> 
('-1', 'stopped')
> 
> I can not, however, seem to get just the output for a specific
> extension:
>>>> 
> session.walk(netsnmp.VarList(netsnmp.Varbind('NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-short"')))
>
> 
()
>>>> 
> session.walk(netsnmp.VarList(netsnmp.Varbind('NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-short".1')))
>
> 
()
>>>> 
> session.get(netsnmp.VarList(netsnmp.Varbind('NET-SNMP-EXTEND-MIB::nsExtendOutLine."galera-short".1')))
>
> 
('',)
> 
> I have tried about 500 variations and have furiously googled and
> read documentation, blogs, guides and tutorials... without
> success.
> 
> And now, in my hour of desperation, I turn to the mailing list. Can
> you please point me in the right direction?
> 
> Humbly yours,
> 
> ------------------------------------------------------------------------------
>
> 
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and
> Dashboards with Interactivity, Sharing, Native Excel Exports, App
> Integration & more Get technology previously reserved for
> billion-dollar corporations, FREE 
> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
>
> 
_______________________________________________
> Net-snmp-users mailing list Net-snmp-users@lists.sourceforge.net 
> Please see the following page to unsubscribe or change other
> options: 
> https://lists.sourceforge.net/lists/listinfo/net-snmp-users
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBAgAGBQJUgF9NAAoJEB9wRUEh5BQZ0r4QAJZn6CSux+gMnKXl4R8zPt18
CeTObXDQgwQFMr9AB3TH/2/NcmFkeUfoStVu1MfhQgbDFD4gSRNw5ThEa/Q5DgVj
w/0LA8V68ke6zPgyEvh8+uSpcK7SHQLP7xcHRx5EjQRXrEukwbWe+LsKBz2VxoEt
MK60DCU7gr70YgNpIpnWr2EXlV3I1SB26+Cvk287+NRa0spjGKMoGtiyeSQ/f3pi
enGTk6frepCnzJmszaPRSsJP1wUlmW+eXUOBWy54VovorFne7afW10yP3b3iOWcT
oGgzMoRQ4upglOL9zIxFPE9WOm1XF2+rs22yRSLuxrYG1mewDKjFvAVuGu7qccmB
FnClm6aluZSEHtjbqOaqUzJhd/CbGqvmSswmWLJWn3k+8k51p0HEvNB+IhlpWPrD
Lfxn2C9lu+iuLtYjDaC9hbRKzPK5DeFpB2PqCj2sDHPFl5UYhks+tZZYtZz+gLjN
17YTMbcaTSFdVDTMGufCjTtQUNOIiyLgg405gvKaox/V8XaP7YZ8FdGEl5ywB0yD
Gi4WaGCb7gyMQJb8BEDqdPs39oHcm+OUunpW8TZpeBwbUS0HtCQ8UAe+VrWCxtZu
07BzKriezcBg801T1j4Y0sypeyvn7h32CI5xTtucK6XQGEQHtNtjpUXObQSIyVTO
QqzbnG2uki15ANrqxFZU
=6t61
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Net-snmp-users mailing list
Net-snmp-users@lists.sourceforge.net
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

Reply via email to