Uhandled error in pysnmp twisted client with more than 1000 hosts(超过1,000台主机的PYSNMP扭曲客户端出现未处理错误)
本文介绍了超过1,000台主机的PYSNMP扭曲客户端出现未处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
from twisted.internet import reactor
from twisted.internet import defer, task
from pysnmp.entity import engine, config
from pysnmp.carrier.twisted import dispatch
from pysnmp.carrier.twisted.dgram import udp
from pysnmp.entity.rfc3413.twisted import cmdgen
import __webimport__
import tools.config
from tools.database import makedsn
import psycopg2
def cmp_varBinds(varBind, varName):
if varName[0] in str(varBind[0]):
return True
def cbFun(cbCtx, ip, varNames):
(errorIndication, errorStatus, errorIndex, varBinds) = cbCtx
if varBinds and any(map(cmp_varBinds, varBinds[0], varNames)):
print ip, [str(x[1]) for x in varBinds[0]]
df = defer.Deferred()
df.addCallback(cbFun, ip=ip, varNames=varNames)
return df # This also indicates that we wish to continue walkin
def parallel(iterable, count, callable, *args, **named):
coop = task.Cooperator()
work = (callable(elem, *args, **named) for elem in iterable)
return defer.DeferredList([coop.coiterate(work) for i in xrange(count)])
def fetch(host):
id, ip, community, hc = host
snmpEngine = engine.SnmpEngine()
snmpEngine.registerTransportDispatcher(dispatch.TwistedDispatcher())
config.addV1System(snmpEngine, 'test-agent', community)
config.addTargetParams(snmpEngine, 'myParams', 'test-agent', 'noAuthNoPriv', 1)
config.addTargetAddr(
snmpEngine, 'myRouter', config.snmpUDPDomain,
(ip, 161), 'myParams', timeout=1
)
# Transport
config.addSocketTransport(
snmpEngine,
udp.domainName,
udp.UdpTwistedTransport().openClientMode()
)
getCmdGen = cmdgen.NextCommandGenerator()
varNames = [('1.3.6.1.2.1.2.2.1.11', None),
('1.3.6.1.2.1.2.2.1.12', None),
('1.3.6.1.2.1.2.2.1.13', None),
('1.3.6.1.2.1.2.2.1.14', None)]
df = getCmdGen.sendReq(snmpEngine, 'myRouter', varNames)
df.addCallback(cbFun, ip=ip, varNames=varNames)
return df
dsn = makedsn(tools.config.main_db)
connection = psycopg2.connect(dsn)
cursor = connection.cursor()
cursor.execute("""SELECT e.id, e.ip, e.snmpcomm, e.hccnt
FROM snmp_ports sp, equipment e
WHERE e.snmp = 'Y' and sp.equipment = e.id
GROUP BY e.id,e.ip,e.snmpcomm,e.hccnt
ORDER BY e.id""")
hosts = cursor.fetchall()
finished = parallel(hosts, len(hosts), fetch)
finished.addErrback(log.err)
finished.addCallback(lambda ign: reactor.stop())
reactor.run()
我从数据库中获取了4000台主机,并向每个主机发出请求。如果我在SQL查询中设置了LIMIT 1000,它就可以正常工作。但是当主机数量超过1000台时,我会收到一个错误:
Unhandled Error
Traceback (most recent call last):
File "crawler.py", line 98, in <module>
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1192, in run
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1201, in mainLoop
--- <exception caught here> ---
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/internet/base.py", line 824, in runUntilCurrent
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/pysnmp/carrier/base.py", line 52, in _cbFun
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/pysnmp/entity/engine.py", line 64, in __receiveMessageCbFun
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/pysnmp/proto/rfc3412.py", line 274, in receiveMessage
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 299, in importSymbols
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 270, in loadModules
pysnmp.smi.error.SmiError: MIB file "__SNMPv2-MIB.py[co]" not found in search path
,然后脚本停止。为什么会发生这种情况?
推荐答案
如果仅当您提高并发级别时才会出现这种情况,则可能的解释是您遇到了平台允许您在任何时候打开的文件数量的限制。每个打开的套接字都计入此限制,打开的"常规"文件(从文件系统)也是如此。
如果您用完了所有允许的文件,则Python无法从磁盘读取模块的源代码,因为该平台不允许它打开它们。
在这种情况下是否会发生这种情况并不是很明显,因为(如果是)pysnMP正在处理真正的异常,并重新引发隐藏详细信息的新异常。
如果这是问题所在,则可以通过提高打开文件限制来解决它。大多数情况下,您可以通过运行以下命令来完成此操作:
$ ulimit -Sn 2048
阅读有关ulimit的更多信息(help ulimit
在您的外壳中,位于the internet),了解有关控制该限制的更多详细信息。
这篇关于超过1,000台主机的PYSNMP扭曲客户端出现未处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:超过1,000台主机的PYSNMP扭曲客户端出现未处理错误
猜你喜欢
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01