vincentbernat / snimpy

interactive SNMP tool with Python
http://snimpy.readthedocs.org/
185 stars 44 forks source link

functional degradation #11

Open wavilen opened 10 years ago

wavilen commented 10 years ago

I writed the function

def get(ip, community):                                                                   
    load(                                                                                 
        os.path.join(                                                                     
            CONF['abs_path'],                                                             
            "app/snmp/mib/SNMPv2-SMI.my"                                                  
        )                                                                                 
    )                                                                                     
    load(                                                                                 
        os.path.join(                                                                     
            CONF['abs_path'],                                                             
            "app/snmp/mib/BGP4-MIB.my"                                                    
        )                                                                                 
    )                                                                                     
    m = M(ip, community, 2)                                                               
    result = {}                                                                           
    try:                                                                                  
        result = dict([(str(idx), {'value': str(m.bgpPeerIdentifier[idx])})               
            for idx in m.bgpPeerIdentifier])                                              
    except SNMPException, err:                                                            
        print(err)                                                                        

    return result                                                                         

As example: the snmpwalk tool in shell returned also necessary data, but append similar error.

Sorry for my poor English.

vincentbernat commented 10 years ago

I need to look a bit more why the error was silently suppressed in 0.6.4 but the behaviour in 0.8.0 seems correct. Since you get an exception, you never have a change to affect the result to result. You should so something like that instead:

result=[]
try:
    for idx in m.bgpPeerIdentifier:
        result.append((str(idx), str(m.bgpPeerIdentifier[idx])))
except SNMPException as err:
    print(err)

return dict(result)

BTW, if you try snmpwalk with -Cc, do you get more results? This could be something that snimpy could do too.

wavilen commented 10 years ago

Thanks for your answer. I changed as you suggested, but the same result. snmpwalk -Cc goes into an infinite loop. without -Cc args return like:

...
bgpPeerIdentifier....
...
bgpPeerIdentifier.<IP1>= IpAddress: <IP2>
Error: OID not increasing: bgpPeerIdentifier.<IP3>
 >= bgpPeerIdentifier.<IP1>
vincentbernat commented 10 years ago

Could you remove the "print(err)" and copy/paste the exception you get? I would like to know on which part the exception is happening.

wavilen commented 10 years ago

I'm not quite sure which of the variants...

    m = M(ip, community, 2)                                                               
    result = []                                                                           
    for idx in m.bgpPeerIdentifier:                                                       
        result.append((str(idx), {'value': str(m.bgpPeerIdentifier[idx])}))               
    return dict(result)

for snimpy==0.8.0:

python -m app.snmp.bgp
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/opt/zbxsync/env/app/snmp/bgp.py", line 37, in <module>
    main()
  File "/opt/zbxsync/env/app/snmp/bgp.py", line 33, in main
    result_get_func = get(IP_Redback_Networks_SmartEdge, Community)
  File "/opt/zbxsync/env/app/snmp/bgp.py", line 22, in get
    for idx in m.bgpPeerIdentifier:
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/manager.py", line 362, in __iter__
    for k, _ in self.iteritems():
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/manager.py", line 373, in iteritems
    for noid, result in self.session.walk(oid):
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/snmp.py", line 240, in walk
    return self._op(self._cmdgen.bulkCmd, *args)
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/snmp.py", line 198, in _op
    raise SNMPException(str(errorIndication))
snimpy.snmp.SNMPException: OIDs are not increasing
vincentbernat commented 10 years ago

Could you try:

m = M(ip, community, 2)
m._session.bulk = False
result = []
for idx in ...

It seems we get the error because of the use of GETBULK for walking. Also, please try with version=1, just to be sure that GETBULK gets disabled.

wavilen commented 10 years ago
    m = M(ip, community, 1)                                                               
    m._session.bulk = False                                                               
    result = []                                                                           
    for idx in m.bgpPeerIdentifier:                                                       
        result.append((str(idx), {'value': str(m.bgpPeerIdentifier[idx])}))               
    return dict(result)   
python -m app.snmp.bgp                                      
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/opt/zbxsync/env/app/snmp/bgp.py", line 40, in <module>
    main()
  File "/opt/zbxsync/env/app/snmp/bgp.py", line 36, in main
    result_get_func = get(IP_Redback_Networks_SmartEdge, Community)
  File "/opt/zbxsync/env/app/snmp/bgp.py", line 29, in get
    for idx in m.bgpPeerIdentifier:
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/manager.py", line 362, in __iter__
    for k, _ in self.iteritems():
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/manager.py", line 373, in iteritems
    for noid, result in self.session.walk(oid):
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/snmp.py", line 238, in walk
    return self._op(self._cmdgen.nextCmd, *oids)
  File "/opt/zbxsync/env/local/lib/python2.7/site-packages/snimpy/snmp.py", line 198, in _op
    raise SNMPException(str(errorIndication))
snimpy.snmp.SNMPException: OIDs are not increasing
wavilen commented 10 years ago

Do not worry if nothing happened. All other libraries (netsnmp, pysnmp) do not deal with this problem. I now use snmpwalk via subprocess.Popen().

vincentbernat commented 10 years ago

OK, I need to test that on my side. I keep you posted.