我正在尝试为我的家庭网络创建一个小型服务,该服务从我的交换机中读取路由器所在端口的 SNMP 值,进行一些计算并返回当前的平均带宽利用率。
我让 pysnmp 工作得很好,每两秒读取一次 snmp 值……然后我决定集成 web.py 来创建一个非常简单的 Web 服务,它返回上传和下载平均值的 JSON,以千比特为单位。
为此,我必须使用线程来保持 SNMP 轮询在后台进行。虽然我是一个 NOOOB,但它不能正常工作——当我在浏览器中加载该值时,它似乎出于某种原因创建了 SNMP 进程的第二个线程,我真的不知道为什么。
这是代码:
from pysnmp.entity.rfc3413.oneliner import cmdgen
from time import sleep
import threading
import json
import web
LOCK = threading.Lock()
class getBW(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.data = dict(upload="0",download="0",)
self.lock = LOCK
self.start()
def run(self):
router_ip = "192.168.2.2"
snmp_community = "public"
upload_mib = "1.3.6.1.2.1.2.2.1.16.1"
download_mib = "1.3.6.1.2.1.2.2.1.10.1"
read_interval = 2
up_octets = 0
dn_octets = 0
last_up_octets = 0
last_dn_octets = 0
up_change = 0
dn_change = 0
avg_up =0
avg_dn =0
up_hist = [0,0,0,0,0]
dn_hist = [0,0,0,0,0]
i = 0
cmdGen = cmdgen.CommandGenerator()
while (1):
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(snmp_community),
cmdgen.UdpTransportTarget((router_ip, 161)),
upload_mib,
download_mib
)
# Check for errors and print out results
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1] or '?'
)
)
else:
for name, val in varBinds:
if str(name) == upload_mib:
if up_octets > 0:
last_up_octets = up_octets
up_change = val-last_up_octets
# convert to kilobits/s
up_change = (((up_change*8)/1000)/read_interval)
up_hist[i] = up_change
avg_up=(sum(up_hist)/len(up_hist))
up_octets = val
#print ('%d kilobit per second upload, %d average' % (up_change,avg_up))
elif str(name) == download_mib:
if dn_octets > 0:
last_dn_octets = dn_octets
dn_change = val-last_dn_octets
# convert to kilobits/s
dn_change = (((dn_change*8)/1000)/read_interval)
dn_hist[i] = dn_change
avg_dn=(sum(dn_hist)/len(dn_hist))
i += 1
i = i % 5 # if I is 5 reset to 0
dn_octets = val
#print ('%d kilobit per second download, %d average' % (dn_change,avg_dn))
#output values to dict
self.lock.acquire()
self.data = dict(upload=str(avg_up),download=str(avg_dn),)
self.lock.release()
print("UPLOAD: %d change, %d average; DOWNLOAD: %d change, %d average" % (up_change,avg_up,dn_change,avg_dn))
print("sleeping for %i secs...\r\n" % read_interval)
sleep(read_interval)
try:
BW = getBW()
except:
print "Error: unable to start thread"
urls = ("/.*", "hello")
app = web.application(urls, globals())
class hello:
def __init__(self):
self.lock = LOCK
def GET(self):
self.lock.acquire()
thedata = BW.data
self.lock.release()
return json.dumps(thedata)
if __name__ == "__main__":
#start web service
app.run()
我将衷心感谢您的帮助。请记住,我是线程的菜鸟,只是学习 Python。谢谢!