SNMP介紹及使用,超有用,建議收藏!
寫在前面
如果你是對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等等。下圖可能會幫你更加清晰的理解這個概念:
![](http://img2020.cnblogs.com/blog/1949124/202011/1949124-20201122153805249-449583552.png)
上圖中,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工具的主介面。
![](http://img2020.cnblogs.com/blog/1949124/202011/1949124-20201122170836075-1220090149.png)
當然,你還可以在Tools中找到Trap Reciever與Trap Sender,其分別對應snmptrapd與snmptrap。
5. Q&A
- 獲取資訊時出現超時或被拒絕
你應該檢查snmpd.conf文件的community是否和你命令的-c選項對應,或者是否監聽埠是否對所有IP開放,但更多的時候是因為防火牆的原因,只要關掉就好了。 - snmpset時出現無許可權的問題
需要設置snmpd.conf文件中的rwcommunity。 - snmptrap失敗
查看snmptrapd.conf文件的配置。 - 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 [email protected]
#
## send mail when get linkDown
#traphandle .1.3.6.1.6.3.1.1.5.3 /usr/bin/traptoemail -s smtp.example.org [email protected]
/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 <[email protected]>
# 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