主頁 > 作業系統 > SNMP介紹及使用,超有用,建議收藏!

SNMP介紹及使用,超有用,建議收藏!

2020-11-22 18:39:34 作業系統

寫在前面

如果你是對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 [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

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/226406.html

標籤:其他

上一篇:SNMP介紹及使用,超有用,建議收藏!

下一篇:《痞子衡嵌入式半月刊》 第 20 期

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • CA和證書

    1、在 CentOS7 中使用 gpg 創建 RSA 非對稱密鑰對 gpg --gen-key #Centos上生成公鑰/密鑰對(存放在家目錄.gnupg/) 2、將 CentOS7 匯出的公鑰,拷貝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公鑰加密一個檔案 gpg -a ......

    uj5u.com 2020-09-10 00:09:53 more
  • Kubernetes K8S之資源控制器Job和CronJob詳解

    Kubernetes的資源控制器Job和CronJob詳解與示例 ......

    uj5u.com 2020-09-10 00:10:45 more
  • VMware下安裝CentOS

    VMware下安裝CentOS 一、軟硬體準備 1 Centos鏡像準備 1.1 CentOS鏡像下載地址 下載地址 1.2 CentOS鏡像下載程序 點擊下載地址進入如下圖的網站,選擇需要下載的版本,這里選擇的是Centos8,點擊如圖所示。 決定選擇Centos8后,選擇想要的鏡像源進行下載,此 ......

    uj5u.com 2020-09-10 00:12:10 more
  • 如何使用Grep命令查找多個字串

    如何使用Grep 命令查找多個字串 大家好,我是良許! 今天向大家介紹一個非常有用的技巧,那就是使用 grep 命令查找多個字串。 簡單介紹一下,grep 命令可以理解為是一個功能強大的命令列工具,可以用它在一個或多個輸入檔案中搜索與正則運算式相匹配的文本,然后再將每個匹配的文本用標準輸出的格式 ......

    uj5u.com 2020-09-10 00:12:28 more
  • git配置http代理

    git配置http代理 經常遇到克隆 github 慢的問題,這里記錄一下幾種配置 git 代理的方法,解決 clone github 過慢。 目錄 git配置代理 git單獨配置github代理 git配置全域代理 配置終端環境變數 git配置代理 主要使用 git config 命令 git單獨 ......

    uj5u.com 2020-09-10 00:12:33 more
  • Linux npm install 裝包時提示Error EACCES permission denied解

    npm install 裝包時提示Error EACCES permission denied解決辦法 ......

    uj5u.com 2020-09-10 00:12:53 more
  • Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包

    Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包。 18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx 19 已加載插件:fastestmirror, langpacks 20 Loading ......

    uj5u.com 2020-09-10 00:13:13 more
  • Linux查看服務器暴力破解ssh IP

    在公網的服務器上經常遇到別人爆破你服務器的22埠,用來挖礦或者干其他嘿嘿嘿的事情~ 這種情況下正確的做法是: 修改默認ssh的22埠 使用設定密鑰登錄或者白名單ip登錄 建議服務器密碼為復雜密碼 創建普通用戶登錄服務器(root權限過大) 建立堡壘機,實作統一管理服務器 統計爆破IP [root ......

    uj5u.com 2020-09-10 00:13:17 more
  • CentOS 7系統常見快捷鍵操作方式

    Linux系統中一些常見的快捷方式,可有效提高操作效率,在某些時刻也能避免操作失誤帶來的問題。 ......

    uj5u.com 2020-09-10 00:13:31 more
  • CentOS 7作業系統目錄結構介紹

    作業系統存在著大量的資料檔案資訊,相應檔案資訊會存在于系統相應目錄中,為了更好的管理資料資訊,會將系統進行一些目錄規劃,不同目錄存放不同的資源。 ......

    uj5u.com 2020-09-10 00:13:35 more
最新发布
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:43:21 more
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:42:36 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:26:53 more
  • 設定Windows主機的瀏覽器為wls2的默認瀏覽器

    這里以Chrome為例。 1. 準備作業 wsl是可以使用Windows主機上安裝的exe程式,出于安全考慮,默認情況下改功能是無法使用。要使用的話,終端需要以管理員權限啟動。 我這里以Windows Terminal為例,介紹如何默認使用管理員權限打開終端,具體操作如下圖所示: 2. 操作 wsl ......

    uj5u.com 2023-04-19 09:25:49 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:19:04 more
  • Linux學習筆記

    IP地址和主機名 IP地址 ifconfig可以用來查詢本機的IP地址,如果不能使用,可以通過install net-tools安裝。 Centos系統下ens33表示主網卡;inet后表示IP地址;lo表示本地回環網卡; 127.0.0.1表示代指本機;0.0.0.0可以用于代指本機,同時在放行設 ......

    uj5u.com 2023-04-18 06:52:01 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:50 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:01 more
  • 你是不是暴露了?

    作者:袁首京 原創文章,轉載時請保留此宣告,并給出原文連接。 如果您是計算機相關從業人員,那么應該經歷不止一次網路安全專項檢查了,你肯定是收到過資訊系統技術檢測報告,要求你加強風險監測,確保你提供的系統服務堅實可靠了。 沒檢測到問題還好,檢測到問題的話,有些處理起來還是挺麻煩的,尤其是線上正在運行的 ......

    uj5u.com 2023-04-05 16:52:56 more
  • 細節拉滿,80 張圖帶你一步一步推演 slab 記憶體池的設計與實作

    1. 前文回顧 在之前的幾篇記憶體管理系列文章中,筆者帶大家從宏觀角度完整地梳理了一遍 Linux 記憶體分配的整個鏈路,本文的主題依然是記憶體分配,這一次我們會從微觀的角度來探秘一下 Linux 內核中用于零散小記憶體塊分配的記憶體池 —— slab 分配器。 在本小節中,筆者還是按照以往的風格先帶大家簡單 ......

    uj5u.com 2023-04-05 16:44:11 more