写在前面

如果你是对SNMP完全不了解,或者只想学习如何使用现成的SNMP工具,那你找对了文章,但如果你希望学习SNMP具体协议内容,推荐阅读官方的RFC文档。

1. 简介

SNMP(Simple Network Management Protocol) 设计在TCP/IP协议簇上的,为网络节点提供了一个通用的管理方法。对于系统维护人员,SNMP是其必须要掌握的一个工具。同时,如果你是一名BMC工程师,那你也必须掌握这门技术,SNMP常常会被部署在其Linux系统中,专门用于管理BMC所监视的所有系统硬件资源。

2. MIB介绍

在你要了解SNMP前,你必须先要了解一下MIB是什么。MIB全程Management Information Base,其主要负责为所有的被管理网络节点建立一个“接口”,本质是类似IP地址的一串数字。例如我们会在使用SNMP的时候见到这样一组数字串:

.1.3.6.1.2.1.1.5.0

在这串数字中,每个数字都代表一个节点,其含义可以参考下表:

1 3 6 1 2 1 1 5 0
iso org dod internet mgmt mib-2 system sysName end

显然,这个数字串可以直接理解为系统的名字。在实际使用中,我们将其作为参数可以读取该节点的值,如果有写权限的话还可以更改该节点的值,因此,SNMP对于系统管理员提供了一套极为便利的工具。但,在一般使用中,我们一般不使用这种节点的表达方式,而是使用更为容易理解的方式,对于上面的这个例子,其往往可以使用SNMPv2-MIB::sysName.0所替代。你可能会想,系统能理解它的含义吗?那你就多虑了,一般在下载SNMP工具包的时候还会下载一个MIB包,其提供了所有节点的树形结构。在该结构中可以方便的查找对应的替换表达。或者,如果你不嫌麻烦还可以到OID查询网站查找对应的替换表达。

3. SNMP原理介绍

SNMP有两个内容,其一是其本身,专门负责管理节点,其二是一个Trap,用于监测报警。

通俗的理解,SNMP可以看作是一个C/S结构。在客户机中,一般会部署一个snmpd的守护进程,而在服务端(管理端)会下载一个snmp工具包,这个包中包含了许多用于管理客户端网络节点的的工具,例如get,set,translate等等。下图可能会帮你更加清晰的理解这个概念:

上图中,161表示的是双方进行通信时所用的默认端口号,被管理端会打开一个守护进程,负责监听161端口发来的请求;管理端会提供一个SNMP工具包,利用工具包中的命令可以向

被管理端的161端口发送请求包,以获取响应。

除此之外,管理端还会开启一个SNMPTrapd守护进程,用于接受被管理端向自己的162端口发送来的snmptrap请求,这一机制主要用于被管理端的自动报警中,一旦被管理端的某个节点出现故障,系统自动会发送snmptrap包,从而远端的系统管理员可以及时的知道问题。更为详细的介绍推荐阅读《TCP/IP详解 卷一》及博文

4. 实际运用

目前较为流行的一些SNMP工具有Net-SNMP,其专门运行在Linux系统中,以及可以运行在Windows系统的iReasoning MIB Browser。

4.1. Net-SNMP

Net-SNMP获取的方式有很多种,可以在其官方网站下载,或者直接使用Linux发行版的包获取命令都可以,这里极为推荐一篇Net-SNMP的下载及配置博客 ,因为网上好多的博客写的安装配置方法照着做下来都不尽人意。安装好之后,你可以通过修改/etc/snmp/snmpd.conf文件来进行配置你的Net-SNMP。接下来我们会对常用的一些SNMP工具包做一些介绍:

snmpd:

这是一个SNMP的守护进程,用于部署在客户机端,可以通过修改/etc/snmp/snmpd.conf文件来配置community(通俗点说就是密码),监听IP及端口及其他内容,你可以使用 sudo /etc/init.d/snmpd restart/start/stop 重启/开启/关闭该进程。

snmpget:

这个命令可以用于获取被管理端某个节点的值,用法很简单,例如我们可以使用snmpget -v 2c -c public localhost SNMPv2-MIB::sysName.0 来获取被管理端系统名称,运行之后你会得到这样一条信息SNMPv2-MIB::sysName.0 = STRING: ubuntu。当然了,如果你的Linux主机是Redhat,那你的结果肯定会和我不大一样。除此之外,我们再来看一下参数,-v 表示的是SNMP的版本号,到目前为止一共有三个版本(1|2c|3),最常用的后面两个版本,而本文所讲的都是2c版本;-c表示的是community,其参数应该填写你在snmpd配置文件中设定的值;localhost 表示的是被管理端的IP地址,此处我是在自己电脑上测的,所以是localhost;最后面的一项内容是要访问的节点,你既可以输入OID,即那一串数字,也可以输入其代表的内容,更多信息可以使用snmpget -h 查看。

snmpset:

这个命令用于设置某个节点的值,用法与get类似,snmpset -v 2c -c public localhost SNMPv2-MIB::sysContact.0 s 'test'会设该节点的值为test(不知道为什么,我的电脑上提示该节点notwritable,总之这个指令我目前位置还没用到过),s表示的是字符串赋值类型,test的赋值内容。

snmpwalk:

这个指令很有用,可以将某一个节点下的所有被管理的子节点内容都打印出来,例如我们使用 snmpwalk -v 2c -c public localhost SNMPv2-MIB::system 可以打印system节点所有被管理子节点的信息。

snmptranslate:

用于翻译OID,例如我们使用 snmptranslate -Td SNMPv2-MIB::system 可以知道system节点所使用的数字OID,反之亦然。

snmptrap:

可以向管理端发送trap包,主要用于报警,例如我们可以使用sudo snmptrap -v 2c -c public localhost "cxy" .1.3.6.1.2.1.1 SNMPv2-MIB::sysContact.0 s 'test' 向管理端发送一个trap包,管理端即可直接查获并通知管理员,这就为被管理端提供了一种主动向管理端通讯的机制。另外,可以看到参数中多了一些内容,"cxy"是管理端的用户名,.1.3.6.1.2.1.1是主OID,而后面的则是具体的OID及其内容。

snmptrapd:

部署在管理端,可以通过修改/etc/snmp/snmptrapd.conf来配置其认证方式,一般使用命令sudo snmptrapd -df -Lo 启动该服务,可以通过检查162端口确认其启动。

4.2. MIB-Browser

你可以在官网下载地址处获取该应用,由于是图形化界面,所以使用极为简单,下图是SNMP工具的主界面。

当然,你还可以在Tools中找到Trap Reciever与Trap Sender,其分别对应snmptrapd与snmptrap。

5. Q&A

  1. 获取信息时出现超时或被拒绝

    你应该检查snmpd.conf文件的community是否和你命令的-c选项对应,或者是否监听端口是否对所有IP开放,但更多的时候是因为防火墙的原因,只要关掉就好了。
  2. snmpset时出现无权限的问题

    需要设置snmpd.conf文件中的rwcommunity。
  3. snmptrap失败

    查看snmptrapd.conf文件的配置。
  4. OID查找不到的情况

    需要下载snmp-mibs-downloader包,并且将/etc/snmp/snmp.conf中的第一行mib:注释掉。

6. configuration example

下面是我在Ubuntu16.04中的一些关于Net-SNMP的相关配置文件:

/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 :

/etc/snmp/snmpd.conf

#
# EXAMPLE-trap.conf:
# An example configuration file for configuring the Net-SNMP snmptrapd agent.
#
###############################################################################
#
# This file is intended to only be an example.
# When the snmptrapd agent starts up, this is where it will look for it.
#
# All lines beginning with a '#' are comments and are intended for you
# to read. All other lines are configuration commands for the agent. #
# PLEASE: read the snmptrapd.conf(5) manual page as well!
#
#authCommunity log,execute,net private 
authCommunity log,execute,net public
#
## send mail when get any events
#traphandle default /usr/bin/traptoemail -s smtp.qq.com 1484652026@qq.com
#
## send mail when get linkDown
#traphandle .1.3.6.1.6.3.1.1.5.3 /usr/bin/traptoemail -s smtp.example.org foobar@example.org

/etc/snmp/snmpd.conf

###############################################################################
#
# EXAMPLE.conf:
# An example configuration file for configuring the Net-SNMP agent ('snmpd')
# See the 'snmpd.conf(5)' man page for details
#
# Some entries are deliberately commented out, and will need to be explicitly activated
#
###############################################################################
#
# AGENT BEHAVIOUR
# # Listen for connections from the local system only
#agentAddress udp:127.0.0.1:161
# Listen for connections on all interfaces (both IPv4 *and* IPv6)
#agentAddress udp:161,udp6:[::1]:161 ###############################################################################
#
# SNMPv3 AUTHENTICATION
#
# Note that these particular settings don't actually belong here.
# They should be copied to the file /var/lib/snmp/snmpd.conf
# and the passwords changed, before being uncommented in that file *only*.
# Then restart the agent # createUser authOnlyUser MD5 "remember to change this password"
# createUser authPrivUser SHA "remember to change this one too" DES
# createUser internalUser MD5 "this is only ever used internally, but still change the password" # If you also change the usernames (which might be sensible),
# then remember to update the other occurances in this example config file to match. ###############################################################################
#
# ACCESS CONTROL
# # system + hrSystem groups only
#view systemonly included .1.3.6.1.2.1.1
#view systemonly included .1.3.6.1.2.1.25.1
view systemonly included .1 # Full access from the local host
#rocommunity public localhost
# Default access to basic system info
rwcommunity public default -V systemonly
# rocommunity6 is for IPv6
rwcommunity6 public default -V systemonly # Full access from an example network
# Adjust this network address to match your local
# settings, change the community string,
# and check the 'agentAddress' setting above
#rocommunity secret 10.0.0.0/16 # Full read-only access for SNMPv3
rouser authOnlyUser
# Full write access for encrypted requests
# Remember to activate the 'createUser' lines above
#rwuser authPrivUser priv # It's no longer typically necessary to use the full 'com2sec/group/access' configuration
# r[ow]user and r[ow]community, together with suitable views, should cover most requirements ###############################################################################
#
# SYSTEM INFORMATION
# # Note that setting these values here, results in the corresponding MIB objects being 'read-only'
# See snmpd.conf(5) for more details
sysLocation Sitting on the Dock of the Bay
sysContact Me <me@example.org>
# Application + End-to-End layers
sysServices 72 #
# Process Monitoring
#
# At least one 'mountd' process
proc mountd
# No more than 4 'ntalkd' processes - 0 is OK
proc ntalkd 4
# At least one 'sendmail' process, but no more than 10
proc sendmail 10 1 # Walk the UCD-SNMP-MIB::prTable to see the resulting output
# Note that this table will be empty if there are no "proc" entries in the snmpd.conf file #
# Disk Monitoring
#
# 10MBs required on root disk, 5% free on /var, 10% free on all other disks
disk / 10000
disk /var 5%
includeAllDisks 10% # Walk the UCD-SNMP-MIB::dskTable to see the resulting output
# Note that this table will be empty if there are no "disk" entries in the snmpd.conf file #
# System Load
#
# Unacceptable 1-, 5-, and 15-minute load averages
load 12 10 5 # Walk the UCD-SNMP-MIB::laTable to see the resulting output
# Note that this table *will* be populated, even without a "load" entry in the snmpd.conf file ###############################################################################
#
# ACTIVE MONITORING
# # send SNMPv1 traps
trapsink localhost public
# send SNMPv2c traps
#trap2sink localhost public
# send SNMPv2c INFORMs
#informsink localhost public # Note that you typically only want *one* of these three lines
# Uncommenting two (or all three) will result in multiple copies of each notification. #
# Event MIB - automatically generate alerts
#
# Remember to activate the 'createUser' lines above
iquerySecName internalUser
rouser internalUser
# generate traps on UCD error conditions
defaultMonitors yes
# generate traps on linkUp/Down
linkUpDownNotifications yes ###############################################################################
#
# EXTENDING THE AGENT
# #
# Arbitrary extension commands
#
extend test1 /bin/echo Hello, world!
extend-sh test2 echo Hello, world! ; echo Hi there ; exit 35
#extend-sh test3 /bin/sh /tmp/shtest # Note that this last entry requires the script '/tmp/shtest' to be created first,
# containing the same three shell commands, before the line is uncommented # Walk the NET-SNMP-EXTEND-MIB tables (nsExtendConfigTable, nsExtendOutput1Table
# and nsExtendOutput2Table) to see the resulting output # Note that the "extend" directive supercedes the previous "exec" and "sh" directives
# However, walking the UCD-SNMP-MIB::extTable should still returns the same output,
# as well as the fuller results in the above tables. #
# "Pass-through" MIB extension command
#
#pass .1.3.6.1.4.1.8072.2.255 /bin/sh PREFIX/local/passtest
#pass .1.3.6.1.4.1.8072.2.255 /usr/bin/perl PREFIX/local/passtest.pl # Note that this requires one of the two 'passtest' scripts to be installed first,
# before the appropriate line is uncommented.
# These scripts can be found in the 'local' directory of the source distribution,
# and are not installed automatically. # Walk the NET-SNMP-PASS-MIB::netSnmpPassExamples subtree to see the resulting output #
# AgentX Sub-agents
#
# Run as an AgentX master agent
master agentx
# Listen for network connections (from localhost)
# rather than the default named socket /var/agentx/master
#agentXSocket tcp:localhost:705

SNMP介绍及使用,超有用,建议收藏!的更多相关文章

  1. markdown介绍和使用(超全建议收藏)

    Markdown介绍 Markdown 其实在 2004 年就有了,不过之前一直很小众,这几年随着相关应用平台的发展,Markdown以其独到的优势迅速火起来了.Markdown编辑器使用一套格式标记 ...

  2. 解密国内BAT等大厂前端技术体系-腾讯篇(长文建议收藏)

    1 引言 为了了解当前前端的发展趋势,让我们从国内各大互联网大厂开始,了解他们的最新动态和未来规划.这是解密大厂前端技术体系的第三篇,前两篇已经讲述了阿里和百度在前端技术这几年的技术发展.这一篇从腾讯 ...

  3. 万字超强图文讲解AQS以及ReentrantLock应用(建议收藏)

    | 好看请赞,养成习惯 你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it ...

  4. 解密国内BAT等大厂前端技术体系-携程篇(长文建议收藏)

    1 引言 为了了解当前前端的发展趋势,让我们从国内各大互联网大厂开始,了解他们的最新动态和未来规划.这是解密大厂前端技术体系的第四篇,前三篇已经讲述了阿里.腾讯.百度在前端技术这几年的技术发展. 这一 ...

  5. [转帖]可能是东半球最好的 Curl 学习指南,强烈建议收藏!

    可能是东半球最好的 Curl 学习指南,强烈建议收藏! http://www.itpub.net/2019/09/30/3302/ 记得转帖过.. 简介 curl 是常用的命令行工具,用来请求 Web ...

  6. 解密国内BAT等大厂前端技术体系-美团点评之下篇(长文建议收藏)

    引言 在上篇中,我已经介绍了美团点评的业务情况.大前端的技术体系,其中大前端的技术全景图如下: 上篇重点介绍了工程化和代码质量的部分,工程化涵盖了客户端持续集成平台-MCI.全端监控平台-CAT.移动 ...

  7. 解密国内BAT等大厂前端技术体系-美团点评之上篇(长文建议收藏)

    引言 进入2019年,大前端技术生态似乎进入到了一个相对稳定的环境,React在2013年发布至今已经6年时间了,Vue 1.0在2015年发布,至今也有4年时间了. 整个业界在前端框架不断迭代中,也 ...

  8. Linux 文件搜索神器 find 实战详解,建议收藏!

    大家好,我是肖邦,这是我的第 10 篇原创文章. 在 Linux 系统使用中,作为一个管理员,我希望能查找系统中所有的大小超过 200M 文件,查看近 7 天系统中哪些文件被修改过,找出所有子目录中的 ...

  9. 万字长文肝Git--全流程知识点包您满意【建议收藏】

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文将首先介绍在本地搭建GitLab服务,然后重点介绍Git的常用命令,Git的核心概念以及冲突处理,最后介绍Git与SVN的区别 干货满满,建议 ...

  10. 常用myeclipse的快捷键,对菜鸟超有用的

    1. ctrl + h ,这个键超有用的,可以搜索当前项目的整个文件,并锁定到改文件的 具体位置.如图: 2.ctrl + o,在java文件内,搜索该文件下的所有方法.如图: ctrl + d ,删 ...

随机推荐

  1. docker19.03限制容器使用的内存资源

    一,指定内存大小的参数: [root@localhost liuhongdi]# docker run -idt --name kafka2 --hostname kafka2 -m 200M --m ...

  2. Mosquitto服务器的日志分析

    启动Mosquitto后,我们可以看到Mosquitto的启动日志: 1515307521: mosquitto version 1.4.12 (build date 2017-06-01 13:03 ...

  3. 从零开始针对 .NET 应用的 DevOps 运营实践 - Jenkins & SonarQube 安装配置

    一.Overview 继续 DevOps 实施的相关内容,在上一篇的博客中,完成了对于工具链中使用到的软件所需的运行环境的配置,在这一篇的博客中,将聚焦于我们使用到的两个主要的软件:Jenkins 与 ...

  4. poj1654 -- Area (任意多边形面积)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20444   Accepted: 5567 Description ...

  5. mysql 架构简介

    mysql的逻辑架构 第一层:进行连接处理.权限认证.安全校验等. 当客户端(应用)连接到mysql服务器时,服务器会创建使用一个线程进行处理连接(少量的线程服务大量的连接),随后服务器需要对该连接进 ...

  6. 1.1:JAVA基础

    JAVA基础面试部分(多线程.算法.网络编程提出去了,详细分类见<面经>) 一.Java底层基础题 JDK和JRE区别? 1.JDK是整个JAVA的核心,包括了Java运行环境JRE,一堆 ...

  7. nginx处理vue打包文件后的跨域问题

    起因 在vue文件打包后,项目脱离了vue配置的反向代理配置,还是会报跨域的错误,或者直接打不开本地文件, 但是此刻我们想打开打包后的文件,测试一下文件有没有错误,因为经常会存在开发阶段没有问题,打包 ...

  8. adb命令如何获取appPackage和appActivity的信息

    如何获取appPackage和appActivity的信息,这里有一个极为实用的命令:adb shell dumpsys activity |find "mFocusedActivity&q ...

  9. uniapp分享功能-系统分享

    uni-app分享 uniapp官网地址:https://uniapp.dcloud.io/api/plugins/share?id=sharewithsystem 调用系统分享组件发送分享消息,不需 ...

  10. AC86U kx上网

    AC86U收到很久了,为了能够kx上网免不了一番折腾. 准备 U盘一个, 读写速度要大于30M/s, 用于制作虚拟内存 步骤大致如下: 1.下载koolshare固件 我这里下载的是官改固件:http ...