黑龙江快乐十分

【Python】SNMP的安装及Python的调用

2/13/2017来源:经验技巧人气:17634

概述

黑龙江快乐十分benwenshijieshaosnmpzaiwindowshe()xiadeanzhuang,yijitongguopythondiaoyongqijiekoudewenzhang。

开发环境

Python 3.5.1 Windows 10 64位,Ubuntu 16.04 64位(虚拟机)

SNMP

snmp(jiandanwangluoguanlixieyi),youyizuwangluoguanlidebiaozhunzucheng,baohanyigeyingyongcengxieyi( layer otocol)、moxing(database schema)heyizuziyuanduixiang。gaixieyinenggouzhichiwangluoguanlixitong,yongyijiancelianjiedaowangluoshangdeshebeishifouyourenheyinqiguanlishangguanzhudeqingkuang。

MIB

黑龙江快乐十分mib(management information base),guanlixinxiku,youwangluoguanlixieyifangwendeguanliduixiangshujuku,tabaokuosnmpkeyitongguowangluoshebeidesnmpguanlidailijinxingshezhidebianliang。

Windows下安装SNMP

xiazailianjie:https://sourceforge.net/projects/net-snmp/

zhihouzhijieanzhuangjike,morenanzhuanglujingshic:\usr。

安装完后在系统变量下添加:C:\usr\bin,打开cmd输入snmpget测试,没有报错则说明安装成功。

Ubuntu下安装SNMP和配置

xiazai

$ sudo apt-get install snmpd snmp snmp-mibs-downloader

chakansnmpfuwuzhuangtai

$ sudo service snmpd status

jiandanceshisnmpfuwushifouzhengchang

$ snmpwalk -v 2c -c public localhost 1.3.6.1.2.1.1.1 SNMPv2-MIB::sysDescr.0 = STRING: Linux ubuntu 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64

morenqingkuangxia,snmpfuwuzhishiduibendikaiqi,shiwufatongguoyuanchenghuoqugaizhujidesnmpxinxide。suoyiyaoshezhiyunxuyuanchengzhujifangwen

$ sudo netstat -antup | grep 161 udp 0 0 127.0.0.1:161 0.0.0.0:* 10698/snmpd

黑龙江快乐十分keyikandao,161duankouzhiduibenjikaifang(161duankouhaoshisnmpfuwudeduankouhao),womenxuyaoxiugaiyixia,rangsnmpfuwuduiwaikaifang。

修改/etc/snmp/snmpd.conf配置文件,大概在15行,将下面一行注释掉:agentAddress udp:127.0.0.1:161

黑龙江快乐十分zhongxinqidongsnmpfuwu,zaitongguominglingguancha:

$ sudo /etc/init.d/snmpd restart $ sudo netstat -antup | grep 161 udp 0 0 0.0.0.0:161 0.0.0.0:* 10763/snmpd

keyikandaosnmpfuwuyijingduiwaikaifangle,zheiyangzhihou,womenjiunengzaiyuanchengzhujijinxingceshi。

youyusnmpmorenzhinengchakanlianggeshebeijiedian,yaoxiangchakansuoyoujiedianhaixuyaojinxingyixiepeizhi。dakai/etc/snmp/snmpd.conf

sudo vim /etc/snmp/snmpd.conf # 在大约45行处添加下面一行 view systemonly included .1 view systemonly included .1.3.6.1.2.1.1 view systemonly included .1.3.6.1.2.1.25.1

最后配置一下mibs,否则可能会报Unknown Object Identifier错误。打开/etc/snmp/snmp.conf,注释掉mibs:就可以了

sudo vim /etc/snmp/snmp.conf # As the snmp packages come without MIB files due to license reasons, loading # of MIBs is disabled by default. If you added the MIBs you can reenable # loading them by commenting out the following line. #mibs :

Windows下调用SNMP查看其他主机信息

youyubozhushiyongdeshiubuntuxuniji,qishi192.168.132.130。xiamianshicmdxiatongguosnmpchakangaizhujidexinxi。

dakaicmd,shuru:

snmpwalk -v 2c -c public 192.168.132.130 1.3.6.1.2.1.1.1

fanhui

snmpv2-mib::sysdescr.0 = string: linux ubuntu 4.4.0-62-generic #83-ubuntu smp wed jan 18 14:10:15 utc 2017 x86_64

chenggong。

Python代码调用示例

黑龙江快乐十分pythondaimadiaoyongxitongdesnmpwalkzhilinglaichaxunshebeixinxi。

# coding=utf-8 """ filename:snmp_test.py 说明: 使用系统的 snmpwalk 来查询设备信息,需要先安装 snmp 代码 Windows 和 Linux 通用 Windows cmd 调用示例:snmpwalk -v 2c -c public 127.0.0.1 1.3.6.1.2.1.1.1 Windows cmd 下查看某主机(192.168.132.130)信息示例: snmpwalk -v 2c -c public 192.168.132.130 1.3.6.1.2.1.1.1 """ import re import os import time import platform if 'Windows' == platform.system(): hosts = ['192.168.132.130'] else: # 在虚拟机运行时则查看本地 hosts = ['127.0.0.1'] def snmpWalk(host, oid): result = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1] return result # ------------------------------------------------------------ # 获取负载信息 # ------------------------------------------------------------ def getSystem(host): system = ':'.join(snmpWalk(host, 'system')[0].split(':')[3:]).strip() return system # ------------------------------------------------------------ # ------------------------------------------------------------ # 获取负载信息 # ------------------------------------------------------------ def getLoad(host, loid): """系统负载""" load_oids = '1.3.6.1.4.1.2021.10.1.3.' + str(loid) return snmpWalk(host, load_oids)[0].split(':')[3] def getLoads(host): load1 = getLoad(host, 1) load10 = getLoad(host, 2) load15 = getLoad(host, 3) return load1, load10, load15 # ------------------------------------------------------------ # ------------------------------------------------------------ # 获取网卡流量 # ------------------------------------------------------------ def getNetworkDevices(host): """获取网络设备信息""" device_mib = snmpWalk(host, 'RFC1213-MIB::ifDescr') device_list = [] for item in device_mib: device_list.append(item.split(':')[3].strip()) return device_list def getNetworkData(host, oid): """获取网络流量""" data_mib = snmpWalk(host, oid) data = [] for item in data_mib: byte = float(item.split(':')[3].strip()) data.append(str(round(byte / 1024, 2)) + ' KB') return data def getNetworkInfo(host): device_list = getNetworkDevices(host) # 流入流量 inside = getNetworkData(host, 'IF-MIB::ifInOctets') # 流出流量 outside = getNetworkData(host, 'IF-MIB::ifOutOctets') return device_list, inside, outside # ------------------------------------------------------------ # ------------------------------------------------------------ # 内存使用率 # ------------------------------------------------------------ def getSwapTotal(host): swap_total = snmpWalk(host, 'UCD-SNMP-MIB::memTotalSwap.0')[0].split(' ')[3] return swap_total def getSwapUsed(host): swap_avail = snmpWalk(host, 'UCD-SNMP-MIB::memAvailSwap.0')[0].split(' ')[3] swap_total = getSwapTotal(host) swap_used = str(round(((float(swap_total) - float(swap_avail)) / float(swap_total)) * 100, 2)) + '%' return swap_used def getMemTotal(host): mem_total = snmpWalk(host, 'UCD-SNMP-MIB::memTotalReal.0')[0].split(' ')[3] return mem_total def getMemUsed(host): mem_total = getMemTotal(host) mem_avail = snmpWalk(host, 'UCD-SNMP-MIB::memAvailReal.0')[0].split(' ')[3] mem_used = str(round(((float(mem_total) - float(mem_avail)) / float(mem_total)) * 100, 2)) + '%' return mem_used def getMemInfo(host): mem_used = getMemUsed(host) swap_used = getSwapUsed(host) return mem_used, swap_used # ------------------------------------------------------------ def main(): for host in hosts: print('=' * 10 + host + '=' * 10) start = time.time() print("系统信息") system = getSystem(host) print(system) print("系统负载") load1, load10, load15 = getLoads(host) print('load(5min): %s ,load(10min): %s ,load(15min): %s' % (load1, load10, load15)) print("网卡流量") device_list, inside, outside = getNetworkInfo(host) for i, item in enumerate(device_list): print('%s : RX: %-15s TX: %s ' % (device_list[i], inside[i], outside[i])) mem_used, swap_used = getMemInfo(host) print("内存使用率") print('Mem_Used = %-15s Swap_Used = %-15s' % (mem_used, swap_used)) end = time.time() print('run time:', round(end - start, 2), 's') if __name__ == '__main__': main()

黑龙江快乐十分windowsxiadayin:

==========192.168.132.130========== xitongxinxi linux ubuntu 4.4.0-62-generic #83-ubuntu smp wed jan 18 14:10:15 utc 2017 x86_64 xitongfuzai load(5min): 0.03 ,load(10min): 0.03 ,load(15min): 0.00 wangkaliuliang lo : rx: 1861.91 kb tx: 1861.91 kb intel corporation 82545em gigabit ethernet controller (copper) : rx: 5733.84 kb tx: 336.49 kb neicunshiyonglv mem_used = 93.82% swap_used = 0.07% run time: 2.89 s

ubuntuxiadayin:

黑龙江快乐十分==========127.0.0.1========== xitongxinxi linux ubuntu 4.4.0-62-generic #83-ubuntu smp wed jan 18 14:10:15 utc 2017 x86_64 xitongfuzai load(5min): 0.02 ,load(10min): 0.02 ,load(15min): 0.00 wangkaliuliang lo : rx: 1861.91 kb tx: 1861.91 kb intel corporation 82545em gigabit ethernet controller (copper) : rx: 5738.8 kb tx: 338.76 kb neicunshiyonglv mem_used = 94.14% swap_used = 0.07% run time: 0.26 s



优质网站模板

  • 1
  • 2
  • 3

操作系统文章分类

最新文章推荐

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

操作系统推荐

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10