1. 程式人生 > >SNMP介紹及使用,超有用,建議收藏!

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查詢網站](http://oid-info.com/get/)查詢對應的替換表達。 ## 3. SNMP原理介紹 SNMP有兩個內容,其一是其本身,專門負責管理節點,其二是一個Trap,用於監測報警。 通俗的理解,SNMP可以看作是一個C/S結構。在客戶機中,一般會部署一個snmpd的守護程序,而在服務端(管理端)會下載一個snmp工具包,這個包中包含了許多用於管理客戶端網路節點的的工具,例如get,set,translate等等。下圖可能會幫你更加清晰的理解這個概念:
上圖中,161表示的是雙方進行通訊時所用的預設埠號,被管理端會開啟一個守護程序,負責監聽161埠發來的請求;管理端會提供一個SNMP工具包,利用工具包中的命令可以向 被管理端的161埠傳送請求包,以獲取響應。 除此之外,管理端還會開啟一個SNMPTrapd守護程序,用於接受被管理端向自己的162埠傳送來的snmptrap請求,這一機制主要用於被管理端的自動報警中,一旦被管理端的某個節點出現故障,系統自動會發送snmptrap包,從而遠端的系統管理員可以及時的知道問題。更為詳細的介紹推薦閱讀《TCP/IP詳解 卷一》及[博文](https://blog.csdn.net/bbwangj/article/details/80981098)。 ## 4. 實際運用 目前較為流行的一些SNMP工具有Net-SNMP,其專門執行在Linux系統中,以及可以執行在Windows系統的iReasoning MIB Browser。 ### 4.1. Net-SNMP Net-SNMP獲取的方式有很多種,可以在其官方網站下載,或者直接使用Linux發行版的包獲取命令都可以,這裡極為推薦一篇[Net-SNMP的下載及配置部落格](https://blog.csdn.net/xpleaf/article/details/51100725) ,因為網上好多的部落格寫的安裝配置方法照著做下來都不盡人意。安裝好之後,你可以通過修改/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 你可以在[官網下載地址](http://www.ireasoning.com/download.shtml)處獲取該應用,由於是圖形化介面,所以使用極為簡單,下圖是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 [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