SNMP:“简单网络管理协议”,用于网络管理的协议。SNMP用于网络设备的管理。SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了“读”操作;管理员需要向设备执行设置操作,所以SNMP提供了“写”操作;设备需要在重要状况改变的时候,向管理员通报事件的发生,所以SNMP提供了“Trap”操作。

什么是 MIB?

MIB 是描述被管理设备上的参数的数据结构。如前所述,管理一个设备,就是利用 SNMP 协议,通过网络对被管理设备上的参数进行 get 和 set 操作。

SNMP MIB 是定义了这个设备都有哪些信息可以被收集,哪些trap的触发条件可以被定义,只有符合TRAP触发条件的事件才被发送出去

 snmpget 命令

[root@vStack ~]# snmpget -v 2c -c public localhost system.sysDescr.
SNMPv2-MIB::sysDescr. = STRING: Linux vStack 3.10.-.el7.x86_64 # SMP Thu Nov :: UTC x86_64

snmpbulkwalk 命令

[root@vStack ~]# snmpbulkwalk -v 2c -c public 127.0.0.1 .1.3.6.1.2.1.
SNMPv2-MIB::sysDescr. = STRING: Linux vStack 3.10.-.el7.x86_64 # SMP Thu Nov :: UTC x86_64
SNMPv2-MIB::sysObjectID. = OID: NET-SNMP-MIB::netSnmpAgentOIDs.
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: () ::21.08
SNMPv2-MIB::sysContact. = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName. = STRING: vStack
SNMPv2-MIB::sysLocation. = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange. = Timeticks: () ::00.04
SNMPv2-MIB::sysORID. = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID. = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID. = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID. = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID. = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID. = OID: IP-MIB::ip
SNMPv2-MIB::sysORID. = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID. = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID. = OID: SNMP-NOTIFICATION-MIB::snmpNotifyFullCompliance
SNMPv2-MIB::sysORID. = OID: NOTIFICATION-LOG-MIB::notificationLogMIB
SNMPv2-MIB::sysORDescr. = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr. = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORDescr. = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr. = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORDescr. = STRING: The MIB modules for managing SNMP Notification, plus filtering.
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for logging SNMP Notifications.
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.03
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.04
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.04

snmpbulkwalk命令: 获取snmp服务器的mib-2各种资讯,snmpbulkwalk用于通过SNMPv2 的SNMP GET BULK请求命令与其它网络实体通信,只能用于snmpv2,速度快于snmpwalk

snmptrap 命令

[root@vStack snmp]# snmptrap
No hostname specified.
USAGE: snmptrap [OPTIONS] AGENT TRAP-PARAMETERS Version: 5.7.
Web: http://www.net-snmp.org/
Email: net-snmp-coders@lists.sourceforge.net OPTIONS:
-h, --help display this help message
-H display configuration file directives understood
-v |2c| specifies SNMP version to use
-V, --version display package version number
SNMP Version or 2c specific
-c COMMUNITY set the community string
SNMP Version specific
-a PROTOCOL set authentication protocol (MD5|SHA)
-A PASSPHRASE set authentication protocol pass phrase
-e ENGINE-ID set security engine ID (e.g. )
-E ENGINE-ID set context engine ID (e.g. )
-l LEVEL set security level (noAuthNoPriv|authNoPriv|authPriv)
-n CONTEXT set context name (e.g. bridge1)
-u USER-NAME set security name (e.g. bert)
-x PROTOCOL set privacy protocol (DES|AES)
-X PASSPHRASE set privacy protocol pass phrase
-Z BOOTS,TIME set destination engine boots/time
General communication options
-r RETRIES set the number of retries
-t TIMEOUT set the request timeout (in seconds)
Debugging
-d dump input/output packets in hexadecimal
-D[TOKEN[,...]] turn on debugging output for the specified TOKENs
(ALL gives extremely verbose debugging output)
General options
-m MIB[:...] load given list of MIBs (ALL loads everything)
-M DIR[:...] look in given list of directories for MIBs
(default: /root/.snmp/mibs:/usr/share/snmp/mibs)
-P MIBOPTS Toggle various defaults controlling MIB parsing:
u: allow the use of underlines in MIB symbols
c: disallow the use of "--" to terminate comments
d: save the DESCRIPTIONs of the MIB objects
e: disable errors when MIB symbols conflict
w: enable warnings when MIB symbols conflict
W: enable detailed warnings when MIB symbols conflict
R: replace MIB symbols from latest module
-O OUTOPTS Toggle various defaults controlling output display:
: print leading for single-digit hex characters
a: print all strings in ascii format
b: do not break OID indexes down
e: print enums numerically
E: escape quotes in string indices
f: print full OIDs on output
n: print OIDs numerically
q: quick print for easier parsing
Q: quick print with equal-signs
s: print only last symbolic element of OID
S: print MIB module-id plus last element
t: print timeticks unparsed as numeric integers
T: print human-readable text along with hex strings
u: print OIDs using UCD-style prefix suppression
U: don't print units
v: print values only (not OID = value)
x: print all strings in hex format
X: extended index format
-I INOPTS Toggle various defaults controlling input parsing:
b: do best/regex matching to find a MIB node
h: don't apply DISPLAY-HINTs
r: do not check values for range/type legality
R: do random access to OID labels
u: top-level OIDs must have '.' prefix (UCD-style)
s SUFFIX: Append all textual OIDs with SUFFIX before parsing
S PREFIX: Prepend all textual OIDs with PREFIX before parsing
-L LOGOPTS Toggle various defaults controlling logging:
e: log to standard error
o: log to standard output
n: don't log at all
f file: log to the specified file
s facility: log to syslog (via the specified facility) (variants)
[EON] pri: log to standard error, output or /dev/null for level 'pri' and above
[EON] p1-p2: log to standard error, output or /dev/null for levels 'p1' to 'p2'
[FS] pri token: log to file/syslog for level 'pri' and above
[FS] p1-p2 token: log to file/syslog for levels 'p1' to 'p2'
-C APPOPTS Set various application specific behaviour:
i: send an INFORM instead of a TRAP -v TRAP-PARAMETERS:
enterprise-oid agent trap-type specific-type uptime [OID TYPE VALUE]...
or
-v TRAP-PARAMETERS:
uptime trapoid [OID TYPE VALUE] ...
[root@vStack snmp]#

snmptrap的命令行格式如下:

  snmptrap -v [2c|] [COMMON OPTIONS]        uptime      trap-oid                [OID TYPE VALUE]
$ snmptrap -v 2c -c public 127.0.0.1: "" .1.3.6.1.4.1.2021.251. sysLocation. s "test"
$ snmptrap -v 2c -c public 127.0.0.1: "" .1.3.6.1.4.1.2021.251. sysLocation. s "test"
  snmptrap -v [2c|] [COMMON OPTIONS]        uptime      trap-oid                [OID TYPE VALUE]
$ snmptrap -v 2c -c public 127.0.0.1: "" .1.3.6.1.4.1.2021.251. sysLocation. s "test"
$ snmptrap -v 2c -c public 127.0.0.1: "" .1.3.6.1.4.1.2021.251. sysLocation. s "test"

SNMP的MIB详解

1.MIB管理信息库

IETF规定的管理信息库MIB(由中定义了可访问的网络设备及其属性,由对象识别符(OID:Object Identifier)唯一指定。MIB是一个树形结构,SNMP协议消息通过遍历MIB树形目录中的节点来访问网络中的设备。

下图给出了NMS系统中SNMP可访问网络设备的对象识别树(OID:Object Identifier)结构。

上图中的节点是从internet开始的,其实上面还有几个节点请看下图。

下图给出了对一个DS1线路状态进行查询的OID设置例子。 

pysnmp:

>>> from pysnmp.entity.rfc3413.oneliner import cmdgen
>>> errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd( cmdgen.CommunityData('my-agent', 'public', 0), cmdgen.UdpTransportTarget(('localhost', 161)), (1,3,6,1,2,1,1,1,0) )
>>> varBinds
[(ObjectName('1.3.6.1.2.1.1.1.0'), OctetString('Linux devstack 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64'))]
>>>

snmptranslate 命令

将 MIB OIDs 的两种表现形式 ( 数字及文字 ) 相互转换。并显示 MIB 的内容与结构,如下所示:


[root@devstack ~]# snmptranslate -On SNMPv2-MIB::sysUpTime.0
.1.3.6.1.2.1.1.3.0
[root@devstack ~]# snmptranslate .1.3.6.1.2.1.1.3.0
DISMAN-EVENT-MIB::sysUpTimeInstance
[root@devstack ~]#

snmptranslate -Tp iso.org.dod.internet.mgmt.mib- #用snmptranslate打印从指定OID(.1.3.6.1.2.1)开始一直到该OID(.1.3.6.1.2.1)所属的 MIB 子树末端的树型结构信息
snmptranslate -Ta #用snmptranslate导出所有已经加载的 MIB 的数据,每个对象一行
snmptranslate -Tl #用snmptranslate导出所有已经加载的 MIB 的对象,并且输出完整的 oid 路径
snmptranslate -To #用snmptranslate导出已经加载的所有 MIB 的数字格式的 oid ,不含值和类型,仅仅有 oid 而已
snmptranslate -Ts #用snmptranslate只输出文字格式的 OID 名称而已

2, 处理自定义trap

编写两个mib文件,包括snmp1和snmp2两种trap

Snmp1的mib:TRAP-TEST-MIB.txt:

TRAP-TEST-MIB DEFINITIONS ::= BEGIN

       IMPORTS ucdExperimental FROM UCD-SNMP-MIB;

 demotraps OBJECT IDENTIFIER ::= { ucdExperimental 990 }

 demo-trap TRAP-TYPE

       STATUS current

       ENTERPRISE demotraps

       VARIABLES { sysLocation }

       DESCRIPTION "This is just a demo"

       ::= 17

 END

Snmp2的mib:NOTIFICATION-TEST-MIB.txt:

NOTIFICATION-TEST-MIB DEFINITIONS ::= BEGIN

       IMPORTS ucdavis FROM UCD-SNMP-MIB;

 demonotifs OBJECT IDENTIFIER ::= { ucdavis 991 }

 demo-notif NOTIFICATION-TYPE

         STATUS current

         OBJECTS { sysLocation }

         DESCRIPTION "Just a test notification"

         ::= { demonotifs 17 }

 END

然后放入到mibs文件夹中

在manager端敲入命令:

snmptrap -v 2c -c public 127.0.0.1:162 "" NOTIFICATION-TEST-MIB::demo-notif SNMPv2-MIB::sysLocation.0 s "just here"

agent端输出:

Received 96 bytes from UDP: [127.0.0.1]:44858
0000: 30 5E 02 01 01 04 06 70 75 62 6C 69 63 A7 51 02 0^.....public.Q.
0016: 04 7D BA 0E AA 02 01 00 02 01 00 30 43 30 10 06 .}.........0C0..
0032: 08 2B 06 01 02 01 01 03 00 43 04 00 89 19 29 30 .+.......C....)0
0048: 18 06 0A 2B 06 01 06 03 01 01 04 01 00 06 0A 2B ...+...........+
0064: 06 01 04 01 8F 65 87 5F 11 30 15 06 08 2B 06 01 .....e._.0...+..
0080: 02 01 01 06 00 04 09 6A 75 73 74 20 68 65 72 65 .......just here 2008-11-11 10:22:27 localhost [UDP: [127.0.0.1]:44858]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (8984873) 1 day, 0:57:28.73
SNMPv2-MIB::snmpTrapOID.0 = OID: NOTIFICATION-TEST-MIB::demo-notif SNMPv2-MIB::sysLocation.0 = STRING: just here
sh: log_it: command not found

其中just here就是我们想要的结果.

3, 自己编写处理trap脚本

建立root/bin/traps文件,输入以下内容:

#!/bin/sh
read host
read ip
vars=
while read oid val
do
if [ "$vars" = "" ]
then
vars="$oid = $val"
else
vars="$vars, $oid = $val"
fi
done
echo trap: $ $host $ip $vars

在manager的snmptrapd.conf加入以下几行:

traphandle SNMPv2-MIB::coldStart     /root/bin/traps cold
traphandle SNMPv2-MIB::warmStart /root/bin/traps warm
traphandle IF-MIB::linkDown /root/bin/traps down
traphandle IF-MIB::linkUp /root/bin/traps up traphandle SNMPv2-MIB::authenticationFailure /root/bin/traps auth
# this one is deprecated
traphandle .1.3.6.1.6.3.1.1.5.6 /root/bin/traps egp-neighbor-loss

在 manage 端 发送 trap

snmptrap -v 1 -c public 127.0.0.1:162 TRAP-TEST-MIB::demotraps 127.0.0.1 2 0 ""  IF-MIB::ifIndex i 1
snmptrap -v 2c -c public 127.0.0.1:162 "" NOTIFICATION-TEST-MIB::demo-notif SNMPv2-MIB::sysLocation.0 s "just here"

TRAP-TEST-MIB::demotraps 自定义mib 文件中的

agent端输出:

Received 96 bytes from UDP: [127.0.0.1]:44858
0000: 30 5E 02 01 01 04 06 70 75 62 6C 69 63 A7 51 02 0^.....public.Q.
0016: 04 7D BA 0E AA 02 01 00 02 01 00 30 43 30 10 06 .}.........0C0..
0032: 08 2B 06 01 02 01 01 03 00 43 04 00 89 19 29 30 .+.......C....)0
0048: 18 06 0A 2B 06 01 06 03 01 01 04 01 00 06 0A 2B ...+...........+
0064: 06 01 04 01 8F 65 87 5F 11 30 15 06 08 2B 06 01 .....e._.0...+..
0080: 02 01 01 06 00 04 09 6A 75 73 74 20 68 65 72 65 .......just here 2008-11-11 10:22:27 localhost [UDP: [127.0.0.1]:44858]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (8984873) 1 day, 0:57:28.73
SNMPv2-MIB::snmpTrapOID.0 = OID: NOTIFICATION-TEST-MIB::demo-notif SNMPv2-MIB::sysLocation.0 = STRING: just here
sh: log_it: command not found

其中just here就是我们想要的结果.

4, 让agent自动产生trap

配置agent的snmpd.conf,加入以下几行:(参考:
http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17):

# From: http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17
# send v1 traps
trapsink 127.0.0.1:162
# also send v2 traps
trap2sink 127.0.0.1:162
informsink 127.0.0.1:162

手动启动前台显示 snmptrapd 进行,显示消息

[root@vStack ~]# snmptrapd -d -f -Lo
NET-SNMP version 5.7. Received byte packet from UDP: [127.0.0.1]:->[127.0.0.1]:
: 6C A7 0F.....public..
: A5 1A 1E 2B 0F .`.........+..
: 2B 1E B7 .+.......C.....
: 0A 2B 0A 2B ..+...........+.
: 8F 7B ....e.{. -- :: localhost [UDP: [127.0.0.1]:->[127.0.0.1]:]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: () ::50.79 SNMPv2-MIB::snmpTrapOID. = OID: UCD-SNMP-MIB::ucdStart
sh: page_me: 未找到命令
通过命令查看出 mib 文件保存的位置: 
[root@vStack snmp]# snmptrap -v 2c -c public 127.0.0.1: "" NOTIFICATION-TEST-MIB::demo-notif SNMPv2-MIB::sysLocation. s "just here"
MIB search path: /root/.snmp/mibs:/usr/share/snmp/mibs
Cannot find module (NOTIFICATION-TEST-MIB): At line in (none)
NOTIFICATION-TEST-MIB::demo-notif: Unknown Object Identifier

链接: http://freeloda.blog.51cto.com/2033581/1306743/

用SNMP实现对大型网络的轻松管理  http://guojiping.blog.51cto.com/5635432/985885

snmp 学习的更多相关文章

  1. Snmp学习总结系列——开篇

    进入公司以来,一直参与到公司的产品研发工作当中去,在产品研发中有一个监控远程服务器CPU使用率,内存使用情况,硬盘的需求,技术总监提出了使用Snmp协议作为远程监控的技术解决方案,头一次听说Snmp这 ...

  2. SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息

    0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...

  3. SNMP学习笔记之SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  4. SNMP学习笔记之SNMP介绍,OID及MIB库

    1.1.    SNMP概览 SNMP的基本知识介绍简单网络管理协议(SNMP-Simple Network Management Protocol)是一个与网络设备交互的简单方法.该规范是由IETF ...

  5. snmp学习笔记

    snmp5.5 client 包含头文件 #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-incl ...

  6. SNMP学习——v3 VACM

    目录: ☆ SNMPv3视图访问控制模型    ☆ SNMPv3报文格式    ☆ VACM参数    ☆ Context Table    ☆ Security To Group Table     ...

  7. Snmp学习总结(一)——Snmp的基本概念

    一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...

  8. Snmp学习总结(七)——SNMP4J介绍

    一.SNMP4J介绍 SNMP4J是一个用Java来实现SNMP(简单网络管理协议)协议的开源项目.它支持以命令行的形式进行管理与响应.SNMP4J是纯面向对象设计与SNMP++(用C++实现SNMP ...

  9. Snmp学习总结(六)——linux下安装和配置SNMP

    一.安装SNMP 1.1.下载Net-SNMP的源代码 选择一个SNMP版本,比如5.7.1,下载地址如下:http://sourceforge.net/projects/net-snmp/files ...

  10. Snmp学习总结(五)——WindowsServer2008安装和配置SNMP

    一.安装SNMP 在Windows Server 2008以及Windows Server 2008 R2中,SNMP是以一个服务器功能的形式存在的,SNMP的安装步骤如下所示: 1.打开[开始]→[ ...

随机推荐

  1. 9.4python开发之virtualenv与virtualenvwrapper

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...

  2. 3 Mongodb数据查询1

    1.基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty():将结果 ...

  3. P3819 松江1843路(洛谷月赛)

    P3819 松江1843路 题目描述 涞坊路是一条长L米的道路,道路上的坐标范围从0到L,路上有N座房子,第i座房子建在坐标为x[i]的地方,其中住了r[i]人. 松江1843路公交车要在这条路上建一 ...

  4. 树莓派网线连接后通过ssh远程连接

    新安装的树莓派默认是不支持ssh远程连接的,通过PuTTY (64-bit)会提示连接被拒绝. 本文支持笔记本或pc直接通过网线连接树莓派,具体操作步骤如下: 一.确定树莓派的ip地址及是否开启ssh ...

  5. 以最省内存的方式把大图片加载到内存及获取Exif信息和获取屏幕高度和宽度的新方法

    我们在加载图片时经常会遇到内存溢出的问题,图片太大,我们加载图片时,一般都是用的如下一般方法(加载本地图片): /** * 不作处理,去加载图片的方法,碰到比较大的图片会内存溢出 */ private ...

  6. Centos7中查看IP地址命令ifconfig无法识别如何处理

    问题描述: 在虚拟机中已安装好Centos7系统,查看IP地址使用命令ifconfig时,提示找不到此命令,使用ip addr命令则可查询当前系统的IP地址(如图1.2): 图1 图2 解决问题步骤: ...

  7. python-使用pip安装第三方库报UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 7: invalid continuation byte 错误解决方案

    在python 的安装目录下找到Lib\ntpath.py文件,找到def join(path, *paths):方法,添加如下两行语句: reload(sys) sys.setdefaultenco ...

  8. [译]13-spring 内部bean

    spring基于xml配置元数据的方式下,位于property元素或者contructor-arg元素内的bean元素被称为内部bean,如下: <?xml version="1.0& ...

  9. python学习总结---面向对象2

    面向对象三大特点 - 封装:既是对数据结构的封装,有是处理数据的方法的封装. - 继承:强调的父子类的关系. - 多态:不同对象调用相同的方法,有不同的响应. 类的继承 - 相关概念 - 继承:父类的 ...

  10. 动态删边SPFA: [HNOI2014]道路堵塞

    [HNOI2014]道路堵塞 题目描述 $A$ 国有 $N$座城市,依次标为$1$到$N$.同时,在这$N$座城市间有$M$条单向道路,每条道路的长度是一个正整数.现在,$A$国交通部指定了一条从城市 ...