前段時間,為了實現(xiàn)自動化巡檢,我開發(fā)了自動化巡檢工具,由于我的系統(tǒng)設(shè)備版本比較多,所以我是分別開發(fā)的客戶端程序,服務(wù)端使用dll文件與客戶端通信,服務(wù)端的dll在與python通信,通過Python豐富的第三方庫,實現(xiàn)繪圖入庫等,該方式比較繁瑣,我們管理的設(shè)備還有一些網(wǎng)絡(luò)設(shè)備,這些設(shè)備無法通過開發(fā)程序來實現(xiàn)監(jiān)控,為了實現(xiàn)全平臺全設(shè)備監(jiān)控,我決定使用SNMP實現(xiàn)監(jiān)控任務(wù)。
首先需要在系統(tǒng)中安裝SNMP客戶端,對于Linux平臺來說只需要執(zhí)行如下配置過程即可.
[root@localhost~]#yuminstall-ynet-snmp [root@localhost~]#cat/etc/snmp/snmpd.conf|grep-vE"^#|^$" com2secnotConfigUserdefaultpublic groupnotConfigGroupv1notConfigUser groupnotConfigGroupv2cnotConfigUser viewsystemviewincluded.1 viewsystemviewincluded.1 accessnotConfigGroup""anynoauthexactsystemviewnonenone [root@localhost~]#systemctlrestartsnmpd [root@localhost~]#systemctlenablesnmpd
如果是Windows系統(tǒng)則需要在客戶機服務(wù)列表,開啟SNMP支持,并設(shè)置好一個團體名稱,如下圖。
當(dāng)我們配置好客戶端后,服務(wù)端就客戶獲取數(shù)據(jù)了,我們以一個OID序號為例,我們查詢特定序號對應(yīng)的名稱,然后將其記錄下來,例如下面這樣。
首先我們不適用PySNMP模塊直接開線程調(diào)用看看,該代碼如下所示.
importos,re,time #通過SNMP收集主機CPU利用率:通過SNMP協(xié)議,收集目標(biāo)主機的CPU利用率(百分比),并返回JSON字符串. defGet_CPU_Info(addr): try: Head=["HostName","CoreLoad","CpuUser","CpuSystem","CpuIdle"] CPU=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.1.5") CPU.append(ret.read().split(":")[3].strip()) ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.25.3.3.1.2") CPU.append(ret.read().split(":")[3].strip()) foriin[9,10,11]: ret=os.popen("snmpwalk-v2c-cnmap"+addr+"1.3.6.1.4.1.2021.11.{}.0".format(i)) ret=ret.read() Info=ret.split(":")[3].strip() CPU.append(Info) returndict(zip(Head,CPU)) exceptException: return0 #通過SNMP獲取系統(tǒng)CPU負(fù)載信息:分別獲取到系統(tǒng)的1,5,15分鐘的負(fù)載信息,并返回JSON格式. defGet_Load_Info(addr): try: Head=["HostName","Load1","Load5","Load15"] SysLoad=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.1.5") SysLoad.append(ret.read().split(":")[3].strip()) ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.4.1.2021.10.1.3") load=list(re.sub(".*STRING:","",ret.read()).split(" ")) SysLoad.append(load[0]) SysLoad.append(load[1]) SysLoad.append(load[2]) returndict(zip(Head,SysLoad)) exceptException: return0 #通過SNMP獲取系統(tǒng)內(nèi)存占用:內(nèi)存利用率,獲取到之后,將其轉(zhuǎn)化為字典格式保存。 defGet_Mem_Info(addr): try: Head=["HostName","memTotalSwap","memAvailSwap","memTotalReal","memTotalFree"] SysMem=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.1.5") SysMem.append(ret.read().split(":")[3].strip()) ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.4.1.2021.4") mem=ret.read().split(" ") foriin[2,3,4,6]: SysMem.append(re.sub(".*INTEGER:","",mem[i]).split("")[0]) returndict(zip(Head,SysMem)) exceptException: return0 #通過SNMP獲取系統(tǒng)磁盤數(shù)據(jù):這個案例并不完整,我只寫了一點,后面有個問題一直沒有解決. defGet_Disk_Info(addr): try: dic={} list=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+"HOST-RESOURCES-MIB::hrStorageDescr") DiskName=ret.read().split(" ") ret=os.popen("snmpwalk-v2c-cnmap"+addr+"HOST-RESOURCES-MIB::hrStorageUsed") DiskUsed=ret.read().split(" ") ret=os.popen("snmpwalk-v2c-cnmap"+addr+"HOST-RESOURCES-MIB::hrStorageSize") DiskSize=ret.read().split(" ") foriinrange(1,len(DiskName)-7): dic["Name"]=DiskName[i+5].split(":")[3] dic["Used"]=DiskUsed[i+5].split(":")[3] dic["Size"]=DiskSize[i+5].split(":")[3] list.append(dic) returnlist exceptException: return0 if__name__=='__main__': foriinrange(100): dic=Get_CPU_Info("192.168.1.20") print(dic) time.sleep(1)
通過SNMP收集主機CPU利用率 通過SNMP協(xié)議,收集目標(biāo)主機的CPU利用率(百分比),并返回JSON字符串.
importos,re,time defGet_CPU_Info(addr): try: Head=["HostName","CoreLoad","CpuUser","CpuSystem","CpuIdle"] CPU=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.1.5") CPU.append(ret.read().split(":")[3].strip()) ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.25.3.3.1.2") CPU.append(ret.read().split(":")[3].strip()) foriin[9,10,11]: ret=os.popen("snmpwalk-v2c-cnmap"+addr+"1.3.6.1.4.1.2021.11.{}.0".format(i)) ret=ret.read() Info=ret.split(":")[3].strip() CPU.append(Info) returndict(zip(Head,CPU)) exceptException: return0 if__name__=='__main__': foriinrange(100): dic=Get_CPU_Info("192.168.1.20") print(dic) time.sleep(1)
通過SNMP獲取系統(tǒng)CPU負(fù)載信息 分別獲取到系統(tǒng)的1,5,15分鐘的負(fù)載信息,并返回JSON格式.
importos,re,time defGet_Load_Info(addr): try: Head=["HostName","Load1","Load5","Load15"] SysLoad=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.1.5") SysLoad.append(ret.read().split(":")[3].strip()) ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.4.1.2021.10.1.3") load=list(re.sub(".*STRING:","",ret.read()).split(" ")) SysLoad.append(load[0]) SysLoad.append(load[1]) SysLoad.append(load[2]) returndict(zip(Head,SysLoad)) exceptException: return0 if__name__=='__main__': dic=Get_Load_Info("192.168.1.20") print(dic)
通過SNMP獲取系統(tǒng)內(nèi)存占用 內(nèi)存利用率,獲取到之后,將其轉(zhuǎn)化為字典格式保存。
importos,re,time defGet_Mem_Info(addr): try: Head=["HostName","memTotalSwap","memAvailSwap","memTotalReal","memTotalFree"] SysMem=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.2.1.1.5") SysMem.append(ret.read().split(":")[3].strip()) ret=os.popen("snmpwalk-v2c-cnmap"+addr+".1.3.6.1.4.1.2021.4") mem=ret.read().split(" ") foriin[2,3,4,6]: SysMem.append(re.sub(".*INTEGER:","",mem[i]).split("")[0]) returndict(zip(Head,SysMem)) exceptException: return0 if__name__=='__main__': dic=Get_Mem_Info("192.168.1.20") print(dic)
通過SNMP獲取系統(tǒng)磁盤數(shù)據(jù) 這個案例并不完整,我只寫了一點,后面有個問題一直沒有解決.
importos,re,time defGet_Disk_Info(addr): try: dic={} list=[] ret=os.popen("snmpwalk-v2c-cnmap"+addr+"HOST-RESOURCES-MIB::hrStorageDescr") DiskName=ret.read().split(" ") ret=os.popen("snmpwalk-v2c-cnmap"+addr+"HOST-RESOURCES-MIB::hrStorageUsed") DiskUsed=ret.read().split(" ") ret=os.popen("snmpwalk-v2c-cnmap"+addr+"HOST-RESOURCES-MIB::hrStorageSize") DiskSize=ret.read().split(" ") foriinrange(1,len(DiskName)-7): dic["Name"]=DiskName[i+5].split(":")[3] dic["Used"]=DiskUsed[i+5].split(":")[3] dic["Size"]=DiskSize[i+5].split(":")[3] list.append(dic) returnlist exceptException: return0 if__name__=='__main__': list=Get_Disk_Info("192.168.1.20") print(list)
接下來,我們使用pysnmp模塊來做,安裝pysnmp很簡單,執(zhí)行命令pip install pysnmp即可,安裝后,使用以下代碼執(zhí)行即可獲取到目標(biāo)數(shù)據(jù),網(wǎng)上的那些轉(zhuǎn)載的都是坑,沒一個能用的,這個案例是官方案例,可以使用。
frompysnmp.hlapiimport* iterator=getCmd(SnmpEngine(), CommunityData('public'), UdpTransportTarget(('192.168.1.113',161)), ContextData(), ObjectType(ObjectIdentity('SNMPv2-MIB','sysDescr',0))) errorIndication,errorStatus,errorIndex,varBinds=next(iterator) iferrorIndication: print(errorIndication) else: iferrorStatus: print('%sat%s'%(errorStatus.prettyPrint(),varBinds[int(errorIndex)-1]iferrorIndexelse'?')) else: forvarBindinvarBinds: print('='.join([x.prettyPrint()forxinvarBind]))
首先我們以一個OID序號為例,我們查詢特定序號對應(yīng)的名稱,然后將其記錄下來,例如下面這樣。
在客戶機上面,需要在服務(wù)列,開啟SNMP支持,并設(shè)置好一個團體名稱,如下圖。
然后我們簡單的封裝一個類,先來測試一下是否能通。
#snmpwalk-v2c-cpublic192.168.1.113.1.3.6.1.2.1.1.5 frompysnmp.hlapiimport* classNetSNMP(): def__init__(self,address,region): self.region=region self.address=address #獲取指定數(shù)據(jù)的方法 defGetNumber(self,oid,sub_oid,sub_id): iterator=getCmd(SnmpEngine(), CommunityData(self.region), UdpTransportTarget((self.address,161)), ContextData(), ObjectType(ObjectIdentity(oid,sub_oid,sub_id))) errorIndication,errorStatus,errorIndex,varBinds=next(iterator) iferrorIndication: returnFalse else: iferrorStatus: returnFalse else: forvarBindinvarBinds: return[x.prettyPrint()forxinvarBind] if__name__=="__main__": #初始化 ptr=NetSNMP("192.168.1.101","public") #設(shè)置OID數(shù)據(jù)集 ret=ptr.GetNumber("HOST-RESOURCES-MIB","hrMemorySize",0) print("類型:{}-->返回結(jié)果:{}-->解析:{}".format(type(ret),ret,ret[1]))
運行后,即可讀取到內(nèi)存數(shù)據(jù),如下。
-
Linux
+關(guān)注
關(guān)注
87文章
11420瀏覽量
212361 -
SNMP
+關(guān)注
關(guān)注
0文章
89瀏覽量
30131 -
網(wǎng)絡(luò)設(shè)備
+關(guān)注
關(guān)注
0瀏覽量
30242 -
python
+關(guān)注
關(guān)注
56文章
4822瀏覽量
85876
原文標(biāo)題:如何使用Python通過SNMP監(jiān)控網(wǎng)絡(luò)設(shè)備?
文章出處:【微信號:網(wǎng)絡(luò)技術(shù)干貨圈,微信公眾號:網(wǎng)絡(luò)技術(shù)干貨圈】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
基于SNMP的網(wǎng)絡(luò)拓?fù)浣Y(jié)構(gòu)自動發(fā)現(xiàn)研究
Labview監(jiān)控帶有snmp網(wǎng)卡設(shè)備有沒有問題?
用LWIP的SNMP監(jiān)控嵌入式設(shè)備
AN_基于FreeRTOS+LwIP的SNMP實現(xiàn)方案
網(wǎng)絡(luò)設(shè)備監(jiān)管系統(tǒng)的設(shè)計與實現(xiàn)
基于SNMP的網(wǎng)絡(luò)監(jiān)控系統(tǒng)
基于嵌入式網(wǎng)絡(luò)設(shè)備的遠(yuǎn)程監(jiān)控系統(tǒng)設(shè)計
基于SNMP的網(wǎng)絡(luò)監(jiān)控系統(tǒng)開發(fā)平臺與架構(gòu)的設(shè)計與實現(xiàn)

基于SNMP的網(wǎng)絡(luò)性能實時測量技術(shù)的實現(xiàn)方法分析

使用 ntopng 和 SNMP 監(jiān)視網(wǎng)絡(luò)設(shè)備

評論