在 Linux系统下的服务器如何收集snmp数据
并且将收到的snmp数据的分析并通过python可视化
求解决
Python的两个第三方库:
或者:
安装
sudo yum install gcc python-devel
// sudo apt-get install gcc python-dev
pip install easysnmp
二者都支持Python3,只不过前者已经很久没更新,最多支持Python3.7,
后者则还在更新
所以,这里以最新的easysnmp为例,如果你对pysnmp感兴趣,也可是尝试下pysnmp
- 使用
主要有两种方法可以使用 Easy SNMP 库。
第一种
是使用 Session 对象,当您计划从一个源请求多条 SNMP 数据时,它最适合。
from easysnmp import Session
# Create an SNMP session to be used for all our requests
session = Session(hostname='localhost', community='public', version=2)
# You may retrieve an individual OID using an SNMP GET
location = session.get('sysLocation.0')
# You may also specify the OID as a tuple (name, index)
# Note: the index is specified as a string as it can be of other types than
# just a regular integer
contact = session.get(('sysContact', '0'))
# And of course, you may use the numeric OID too
description = session.get('.1.3.6.1.2.1.1.1.0')
# Set a variable using an SNMP SET
session.set('sysLocation.0', 'The SNMP Lab')
# Perform an SNMP walk
system_items = session.walk('system')
# Each returned item can be used normally as its related type (str or int)
# but also has several extended attributes with SNMP-specific information
for item in system_items:
print '{oid}.{oid_index} {snmp_type} = {value}'.format(
oid=item.oid,
oid_index=item.oid_index,
snmp_type=item.snmp_type,
value=item.value
)
第二种:
您还可以通过其简单的界面使用 Easy SNMP,该界面用于一次性操作,您希望在请求中指定所有详细信息:
from easysnmp import snmp_get, snmp_set, snmp_walk
# Grab a single piece of information using an SNMP GET
snmp_get('sysDescr.0', hostname='localhost', community='public', version=1)
# Perform an SNMP SET to update data
snmp_set(
'sysLocation.0', 'My Cool Place',
hostname='localhost', community='public', version=1
)
# Perform an SNMP walk
snmp_walk('system', hostname='localhost', community='public', version=1)
可视化
如果需要可视化的话,看你的需求决定。
Matplotlib · Seaborn · Bokeh · Plotly · Pyecharts 这几个第三方库都可以对数据进行可视化。
第一步: python解析snmp,使用Python进行snmp操作
https://blog.csdn.net/weixin_30988079/article/details/116880795
第二步:用python进行简单的数据分析和数据可视化
https://blog.csdn.net/wx1871428/article/details/118298679
snmp-cmds模块通过SNMP与目标设备进行通信,此模块适用于windows,此模块是基于系统已安装了net-snmp环境
easysnmp模块通过SNMP与谬表设备进行通信,此模块用于linux,此模块基于系统已安装了net-snmp环境
在Windows平台
#1.系统环境安装net-snmp软件
a.下载链接: https://pan.baidu.com/s/1sq4mjIMfFgG2YxTMLxVF0A 提取码: a7j5
b.安装完成,打开cmd命令框,输入snmpwalk,无报错,有回显,即安装正常
#2.使用pip工具安装snmp-cmds模块
pip3 install snmp-cmds
在Centos 7平台
#1.系统环境安装net-snmp软件
a.yum install python-devel
b.安装setuptools
wget https://files.pythonhosted.org/packages/25/5d/cc55d39ac39383dd6e04ae80501b9af3cc455be64740ad68a4e12ec81b00/setuptools-0.6c11-py2.7.egg
c.安装net-snmp-5.7.3软件
wget https://sourceforge.net/projects/net-snmp/files/net-snmp/5.7.3/net-snmp-5.7.3.tar.gz
d.提升setuptools为可执行文件
chmod +x setuptools-0.6c11-py2.7.egg
e. ./setuptools-0.6c11-py2.7.egg
f. tar -zxvf net-snmp-5.7.3.tar.gz
g. cd net-snmp-5.7.3
h. ./configure --with-python-modules #选择版本时,输入2,其余直接回车
i. make (时间较长)
j. make install
k. echo "/usr/local/lib" >> /etc/ld.so.conf
l. ldconfig
m. 验证,是否有snmpwalk命令
#2.使用pip工具安装snmp-cmds模块
pip3 install easysnmp
ps:easysnmp官方文档地址:https://easysnmp.readthedocs.io/en/latest/
获取目标设备的接口名字
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
from snmp_cmds import snmpwalk
#返回结果是列表
res = snmpwalk(ipaddress='192.168.59.251',oid='IF-MIB:ifDescr',community='qiji123')
for line in res:
#第一个元素为oid,第二个元素为接口名字
print(line[0],' ',line[1])
使用pysnmp模块案例
from easysnmp import snmp_walk
device_ip = 'IP地址'
community = '团体字'
# oid = 'IF-MIB::ifDescr'
oid = 'sysDescr'
def test():
res = snmp_walk(oid, hostname=device_ip, community=community, version=2)
for each in res:
print(each.value)
if __name__ == '__main__':
test()