GUI实现Net-SNMP监控主机CPU等信息(一)

在配置好SNMP后(配置方法参考:windows下安装和配置SNMP),就可以开始实现监控主机的程序了。用到的库是Net-SNMP,Net-SNMP是一个免费的、开放源码的SNMP实现.用到的语言是python,版本是python3.6.2,编写GUI主要用到tkinter,接下来主要实现的功能是:
1.编制控制台程序,接受用户输入的 OID 字符串,返回在Windows/Linux SNMP 代理中的对应值;
2.开发 GUI 界面程序,使用户可通过控制台程序观察主机 CPU、内存、硬盘空间、流量值;

GUI实现

首先安装tkinter,这里就不赘述了,安装好后就可以开始编写GUI了。考虑到我们这次的UI界面要求不高,一个个UI组件写太麻烦了,这里推荐一个python下可视化编程GUI界面的小软件参考博客tkinter的vb可视化工具,根据这篇博客即可生成一些简单GUI的python代码,效果如下:
这里写图片描述
主要就是用到三个组件:label,button和text,最下面的CPU内存曲线是用Figure,这个必须自己写,那个小软件并不能实现,这个的实现下篇博客再讲。

oid获得信息

现在来实现左上角的部分,在文本框中输入oid后,点击结果,将在下面的文本框里显示相应的结果。button的UI实现代码如下:

self.style.configure('Command1.TButton',font=('宋体',9))
self.Command1 = Button(self.top, text='结果',command=lambda:self.Command1_Cmd(host), style='Command1.TButton')        
self.Command1.place(relx=0.01, rely=0.08, width=80, height=30)

button的功能实现代码如下:

def Command1_Cmd(self,host):
    self.Text2.delete('0.0',END)
    oid_result = snmpWalk(host,self.Text1Var.get())
    self.Text2.insert('1.0',str(oid_result))

主要就是snmpWalk(host,self.Text1Var.get())这一句。

主机信息的获取

CPU使用率

windows和linux下不同,windows下没有直接获得CPU使用率的oid,必须要自己计算,使用到的oid是1.3.6.1.2.1.25.3.3.1.2,然后我们可以先得到有多少process,再求和取平均即可得到CPU使用率。

# CPU信息
self.Text3.delete('0.0',END)
cpu_result = snmpWalk(host,'1.3.6.1.2.1.25.3.3.1.2')
pro_num=len(cpu_result)
tmp=1
diskstr=''
pro=0
while tmp<=pro_num:
    pro=pro+int(snmpWalk(host, 'HOST-RESOURCES-MIB::hrProcessorLoad.'+str(tmp+5))[0].split(' ')[3])
    tmp+=1
pro=round(float(pro/pro_num),2)
self.Text3.insert('1.0',"CPU使用率:"+str(pro)+'%\n')

内存使用率

总内存通过查询HOST-RESOURCES-MIB::hrMemorySize.0得到,查询HOST-RESOURCES-MIB:: hrStorageUsed.7可得到内存使用单位,查询HOST-RESOURCES-MIB:: hrStorageAllocationUnits.7可得到内存使用量,由上述三个数据可获得内存使用率。

self.Text4.delete('0.0',END)
mem_total = snmpWalk(host, 'HOST-RESOURCES-MIB::hrMemorySize.0')[0].split(' ')[3]
mem_sto_used = snmpWalk(host, 'HOST-RESOURCES-MIB::hrStorageUsed.7')[0].split(' ')[3]
mem_sto_unit = snmpWalk(host, 'HOST-RESOURCES-MIB::hrStorageAllocationUnits.7')[0].split(' ')[3]
mem_used=str(round(float(mem_sto_used)*float(mem_sto_unit)/1024/1024/1024,2))
mem_used_total=float(mem_total)     mem_used_rate=str(round(float(mem_sto_used)*float(mem_sto_unit)/1024/mem_used_total*100,2))+'%'
self.Text4.insert('1.0',"总内存 : "+str(round(float(mem_total)/1024/1024,2))+" GB\n"+"内存使用:"+mem_used+"GB\n"+"内存使用率:"+mem_used_rate)

流量信息

通过查询IF-MIB::ifInOctets可得到接收流量,通过查询IF-MIB::ifOutOctets可得到发送流量。

# 流量信息
device_mib = snmpWalk(host, 'RFC1213-MIB::ifDescr')
device_list = []
for item in device_mib:
    device_list.append(item.split(':')[3].strip())
# 流入流量
data_mib = snmpWalk(host, 'IF-MIB::ifInOctets')
data = []
for item in data_mib:
    byte = float(item.split(':')[3].strip())
    data.append(str(round(byte / 1024, 2)))
inside = data
# 流出流量
data_mib = snmpWalk(host, 'IF-MIB::ifOutOctets')
data = []
for item in data_mib:
    byte = float(item.split(':')[3].strip())
    data.append(str(round(byte / 1024, 2)))
outside = data
rxsum=0.0
txsum=0.0
for i, item in enumerate(device_list):
    rxsum+=float(inside[i])
    txsum+=float(outside[i])
rxsum=round(rxsum/1024,2)
txsum=round(txsum/1024,2)
self.Text5.delete('0.0',END)
self.Text5.insert('1.0',"发送流量:"+str(txsum)+'MB'+'\n'+"接收流量:"+str(rxsum)+'MB'+'\n')

硬盘空间

通过查询HOST-RESOURCES-MIB::hrStorageIndex可得到硬盘个数和索引,查询HOST-RESOURCES-MIB::hrStorageAllocationUnits可得到硬盘存储单位,查询HOST-RESOURCES-MIB::hrStorageSize可得到存储容量,经过计算可得到总的存储量。

self.Text6.delete('0.0',END)
disk_total = snmpWalk(host, 'HOST-RESOURCES-MIB::hrStorageIndex')
disk_num=len(disk_total)-2
tmp=1
diskstr=''
while tmp<=disk_num:
    unit=snmpWalk(host, 'HOST-RESOURCES-MIB::hrStorageAllocationUnits.'+str(tmp))[0].split(' ')[3]
    stor=snmpWalk(host, 'HOST-RESOURCES-MIB::hrStorageSize.'+str(tmp))[0].split(' ')[3]
    storge=round(float(unit)*float(stor)/1024/1024/1024,4)
    diskstr=diskstr+snmpWalk(host, 'HOST-RESOURCES-MIB::hrStorageDescr.'+str(tmp))[0].split(' ')[3]+' '+str(storge)+'GB'+'\n'
    tmp+=1
self.Text6.insert('1.0',"硬盘数:"+str(disk_num)+'\n'+diskstr)

最后的运行效果如下:
这里写图片描述
至此,GUI实现和几个监控功能实现了,下一篇将会实现CPU阈值告警功能和CPU内存动态曲线实现,总的代码将在下次给出。

猜你喜欢

转载自blog.csdn.net/qq_34965116/article/details/80210377