SNMP 原理與實戰詳解

大綱

一、什麼是SNMP

二、SNMP背景

三、SNMP結構概述

四、SNMP支持的網管操作

五、SNMP的實現結構

六、SNMP的技術內容

七、SNMP的發展歷史

八、SNMP的技術術語

九、綜合上述(總結)

十、Net-SNMP詳解

十一、SNMP的MIB詳解

注,實驗環境 CentOS 6.4 x86_64 軟件版本 net-snmp 5.5。(說明:本博文的一些圖片自於開源社區與官方網站並不是所有內容全是原創)


一、什麼是SNMP 

SNMP:“簡單網絡管理協議”,用於網絡管理的協議。SNMP用於網絡設備的管理。SNMP的工作方式:管理員需要向設備獲取數據,所以SNMP提供了“讀”操作;管理員需要向設備執行設置操作,所以SNMP提供了“寫”操作;設備需要在重要狀況改變的時候,向管理員通報事件的發生,所以SNMP提供了“Trap”操作。


二、SNMP背景

SNMP的基本思想:爲不同種類的設備、不同廠家生產的設備、不同型號的設備,定義爲一個統一的接口和協議,使得管理員可以是使用統一的外觀面對這些需要管理的網絡設備進行管理。通過網絡,管理員可以管理位於不同物理空間的設備,從而大大提高網絡管理的效率,簡化網絡管理員的工作。


三、SNMP結構概述

SNMP被設計爲工作在TCP/IP協議族上。SNMP基於TCP/IP協議工作,對網絡中支持SNMP協議的設備進行管理。所有支持SNMP協議的設備都提供SNMP這個統一界面,使得管理員可以使用統一的操作進行管理,而不必理會設備是什麼類型、是哪個廠家生產的。如下圖,

s1

四、SNMP支持的網管操作

對於網絡管理,我們面對的數據是設備的配置、參數、狀態等信息,面對的操作是讀取和設置;同時,因爲網絡設備衆多,爲了能及時得到設備的重要狀態,還要求設備能主動地彙報重要狀態,這就是報警功能。如下圖,

s2

  • Get:讀取網絡設備的狀態信息。 

  • Set:遠程配置設備參數。 

  • Trap:管理站及時獲取設備的重要信息。 


五、SNMP的實現結構

在具體實現上,SNMP爲管理員提供了一個網管平臺(NMS),又稱爲管理站,負責網管命令的發出、數據存儲、及數據分析。被監管的設備上運行一個SNMP代理(Agent)),代理實現設備與管理站的SNMP通信。如下圖,

s3

管理站與代理端通過MIB進行接口統一,MIB定義了設備中的被管理對象。管理站和代理都實現了相應的MIB對象,使得雙方可以識別對方的數據,實現通信。管理站向代理申請MIB中定義的數據,代理識別後,將管理設備提供的相關狀態或參數等數據轉換爲MIB定義的格式,應答給管理站,完成一次管理操作。


已有的設備,只要新加一個SNMP模塊就可以實現網絡支持。舊的帶擴展槽的設備,只要插入SNMP模塊插卡即可支持網絡管理。網絡上的許多設備,路由器、交換機等,都可以通過添加一個SNMP網管模塊而增加網管功能。服務器可以通過運行一個網管進程實現。其他服務級的產品也可以通過網管模塊實現網絡管理,如Oracle、WebLogic都有SNMP進程,運行後就可以通過管理站對這些系統級服務進行管理。


根據管理者和被管理的設備在網絡管理操作中的不同職責,SNMP定義了3種角色。如下圖,

s4

網絡管理系統:又稱管理站、NMS。是系統的控制檯,向管理員提供界面以獲取與改變設備的配置、信息、狀態、操作等信息。管理站與Agent進行通信,執行相應的Set和Get操作,並接收代理發過來的警報(Trap)。 


代理:Agent是網絡管理的代理人,負責管理站和設備SNMP操作的傳遞。介於管理站和設備之間,與管理站通信並相應管理站的請求,從設備獲取相應的數據,或對設備進行相應的設置,來響應管理站的請求。代理也需要具有根據設備的相應狀態使用MIB中定義的Trap向管理站發送報告的能力。 


代理服務器:Proxy是一種特殊的代理,在不能直接使用SNMP協議的地方,如:異種網絡、不同版本的SNMP代理等情況,Proxy代替相關設備向管理站提供一種外觀,爲設備代理SNMP協議的實現。Proxy做了異種網絡或不同版本代理和相應SNMP數據請求的轉換工作。如下圖, 

s5

附:管理信息庫MIB:定義了設備上可以使用的管理信息。代理和管理站使用MIB作爲統一的數據接口通信。


六、SNMP的技術內容

如下圖,

s6

七、SNMP的發展歷史

  • 1989年------ SNMPv1 

  • 1991年------ RMON(Remote Network Monitoring 遠程網絡監視),它擴充了SNMP的功能,包括對LAN的管理及對依附於這些網絡的設備的管理。RMON 沒有修改和增加SNMPv1,只是增加了SNMP監視子網的能力。 

  • 1993年------ SNMPv2(SNMPv1的升級版) 

  • 1995年------ SNMPv2正式版,其中規定了如何在基於OSI的網絡中使用SNMP 

  • 1995年------ RMON擴展爲RMON2 

  • 1998年------ SNMPv3,一系列文檔定義了SNMP的安全性,並定義了將來改進的總體結構,SNMPv3可以和SNMPv2、SNMPv1一起使用。 


八、SNMP的技術術語

  • SNMP:Simple Network Management Protocol(簡單網絡管理協議),是一個標準的用於管理基於IP網絡上設備的協議。 

  • MIB:Management Information Base(管理信息庫),定義代理進程中所有可被查詢和修改的參數。 

  • SMI:Structure of Management Information(管理信息結構),SMI定義了SNMP中使用到的ASN.1類型、語法,並定義了SNMP中使用到的類型、宏、符號等。SMI用於後續協議的描述和MIB的定義。每個版本的SNMP都可能定義自己的SMI。 

  • ASN.1:Abstract Syntax Notation One(抽象語法定義)。用於定義語法的正式語言,在SNMP中定義SNMP的協議數據單元PDU和管理對象MIB的格式。SNMP只使用了ASN.1中的一部分,而且使用ASN.1的語言特性定義了一些自定義類型和類型宏 ,這些組成了SMI。 

  • PDU: Protocol Data Unit(協議數據單元),它是網絡中傳送的數據包。每一種SNMP操作,物理上都對應一個PDU。 

  • NMS: Network Management System,網絡管理系統,又名網絡管理站,簡稱“管理站”。它是SNMP的總控機,提供統一的用戶界面訪問支持SNMP的設備,一般提供UI界面,並有統計、分析等功能,是網管系統的總控制檯。NMS是網絡管理操作的發起者。 

  • Agent: 是SNMP的訪問代理,簡稱“代理”,爲設備提供SNMP能力,負責設備與NMS的通信。 

  • Proxy: 代理服務器,對實現不同協議的設備進行協議轉換,使非IP協議的設備也能被管理。 

  • Trap: 是由設備主動發出的報警數據,用於提示重要的狀態的改變。 

  • BER: Basic Encoding Rule,基本編碼規格。描述如何將ASN.1類型的值編碼爲字符串的方法。它是ASN.1標準的一部分。BER編碼將數據分成TLV三部分,T爲Tag的縮寫,是類型標識;L爲Length的縮寫,標識類型的長度;V爲Value的縮寫,標識數據內容。按照TLV的順序對數據進行編碼,生成字節流。SNMP使用BER將SNMP的操作請求和應答編碼後進行傳輸,並用於接收端進行解碼。 


九、綜合上述(總結)

1.SNMP現有幾個版本

SNMP共有v1,v2,v3這三個版本:

  • v1和v2都具有基本的讀、寫MIB功能。 

  • v2增加了警報、批量數據獲取、管理站和管理站通信能力。 

  • v3在v2的基礎上增加了USM,使用加密的數據和用戶驗證技術,提高了安全性。 

  • 另外,RMON是SNMP的一個重要擴展,爲SNMP增加了子網流量、統計、分析能力。現有兩個版本: Rmon:提供了OSI七層網絡結構中網絡層和數據鏈路層監視能力。Rmon2:提供了OSI七層網絡結構中網絡層之上各層的監視能力。 


2.ASN.1、BER、SMI、MIB、PDU的關係

s7

  • ASN.1:高級的數據描述語言。描述數據的類型、結構、組織、及編碼方法。包括符號和語法兩部分。SNMP使用ASN.1描述PDU和管理學對象信息庫MIB。 

  • BER:ASN.1的基本編碼規則。描述具體的ASN.1對象如何編碼爲比特流在網絡上傳輸。SNMP使用BER作爲編碼方案,數據首先先經過BER編碼,再經由傳輸層協議(一邊是UDP)發往接收方。接收方在SNMP端口上收到PDU後,經過BER解碼後,得到具體的SNMP操作數據。 

  • SMI:是SNMP的描述方法。規定了使用ASN.1子類型、符號。ASN.1功能強大,但SNMP只用到了其中很小一部分,對於這一部分內容的描述,限定了範圍,即爲SMI。SMI規定了使用到的ASN.1類型、宏、符號等。SMI是ASN.1的一個子集和超集。 

  • MIB:是SNMP中使用到的管理信息庫。定義了數據格式、類型、順序、意義等,使用SMI中定義的類型和ASN.1中的基本類型對對象進行描述,是一個使用SMI描述的管理信息庫。每一類關心的事件都有一組MIB,比如網絡接口有一顆MIB樹,TCP有一顆MIB樹,UDP也有一顆MIB樹。 

  • PDU:是SNMP的協議數據單元。PDU是基本的通信格式,使用ASN.1描述,使用BER編碼,通過傳輸層協議傳送。 


3.比較規範的SNMP背景

SNMP作爲一種過渡技術以其簡單易用的特性被廣泛使用,使用的網絡產品提供對SNMP的支持。SNMP還擴展了具有遠程管理功能的RMON,使得管理人員可以對整個子網進行管理,而不是對整個子網內的設備進行管理。


十、Net-SNMP

1.net-snmp簡介

使用net-snmp作爲學習SNMP的工具,搭建一個net-snmp環境。Net-snmp是一個開源的SNMP項目。最新版本爲:5.7.2。net-snmp支持snmpv1、snmpv2、snmpv3,支持基於IPv4和IPv6的SNMP應用程序。

  • Net-snmp提供完整的API用於SNMP應用程序開發,包括C和Perl的API 。

  • 一個功能強大且可擴展的SNMP代理:snmpd開發者可以開發動態模塊擴展snmpd,net-snmp內置擴展子代理與主代理的通信協議。 

  • 提供衆多命令行工具檢查和使用SNMP協議 

  • 一個圖形化的MIB瀏覽工具 

  • 一個Trap接收進程,用於接收和顯示Trap,並將Trap記錄到日誌文件中。 


2.net-snmp的安裝與配置

官方網站: http://www.net-snmp.org/download.html,net-snmp被很多商業化的Linux包含,但是大多數的Linux使用net-snmp的主代理實現Linux的SNMP支持。


3.支持的平臺

net-snmp可以在BSD UNIX、Linux、Windows等操作系統上編譯運行。Windows下可以安裝一個VMWare虛擬機軟件,新建一個虛擬機,在虛擬機上安裝Linux用來學習。


4.實驗環境 

  • 操作系統,CentOS 6.4 X86_64

  • 軟件版本,net-snmp 5.5


5.同步時間

1
[root@snmp ~]# ntpdate 202.120.2.101


6.安裝yum源

[root@node1 ~]# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@node1 ~]# rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm


7.查看一下net-snmp相關軟件包

1
2
3
4
5
6
7
8
9
[root@node1 ~]# yum list all | grep net-snmp*
net-snmp.x86_64             1:5.5-44.el6_4.4        updates
net-snmp-devel.i686           1:5.5-44.el6_4.4        updates
net-snmp-devel.x86_64          1:5.5-44.el6_4.4        updates
net-snmp-libs.i686            1:5.5-44.el6_4.4        updates
net-snmp-libs.x86_64           1:5.5-44.el6_4.4        updates
net-snmp-perl.x86_64           1:5.5-44.el6_4.4        updates
net-snmp-python.x86_64          1:5.5-44.el6_4.4        updates
net-snmp-utils.x86_64          1:5.5-44.el6_4.4        updates

注,有兩個重要的安裝,net-snmp.x86_64 、net-snmp-utils.x86_64 一個是net-snmp軟件包,另一個是snmp的工具包。下面我們來安裝一下,

1
[root@node1 ~]# yum install -y net-snmp net-snmp-utils


8.查看一下安裝的軟件包

1
2
3
4
[root@node1 ~]# rpm -qa | grep net-snmp*
net-snmp-5.5-44.el6_4.4.x86_64
net-snmp-utils-5.5-44.el6_4.4.x86_64
net-snmp-libs-5.5-44.el6_4.4.x86_64


9.查看一下軟件包具體安裝的內容

下面是snmp軟件包的內容,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[root@node1 ~]# rpm -ql net-snmp-5.5-44.el6_4.4.x86_64
/etc/rc.d/init.d/snmpd #snmp的啓動腳本
/etc/rc.d/init.d/snmptrapd
/etc/snmp #snmp配置文件目錄
/etc/snmp/snmpd.conf #snmp配置文件
/etc/snmp/snmptrapd.conf
/etc/sysconfig/snmpd
/etc/sysconfig/snmptrapd
/usr/bin/net-snmp-create-v3-user
/usr/bin/snmpconf
/usr/sbin/snmpd
/usr/sbin/snmptrapd
/usr/share/doc/net-snmp-5.5
/usr/share/doc/net-snmp-5.5/AGENT.txt
/usr/share/doc/net-snmp-5.5/COPYING
/usr/share/doc/net-snmp-5.5/ChangeLog.trimmed
/usr/share/doc/net-snmp-5.5/EXAMPLE.conf
/usr/share/doc/net-snmp-5.5/FAQ
/usr/share/doc/net-snmp-5.5/NEWS
/usr/share/doc/net-snmp-5.5/PORTING
/usr/share/doc/net-snmp-5.5/README
/usr/share/doc/net-snmp-5.5/README.agent-mibs
/usr/share/doc/net-snmp-5.5/README.agentx
/usr/share/doc/net-snmp-5.5/README.krb5
/usr/share/doc/net-snmp-5.5/README.mib2c
/usr/share/doc/net-snmp-5.5/README.snmpv3
/usr/share/doc/net-snmp-5.5/README.thread
/usr/share/doc/net-snmp-5.5/TODO
/usr/share/doc/net-snmp-5.5/ipf-mod.pl
/usr/share/doc/net-snmp-5.5/passtest
/usr/share/man/man1/net-snmp-create-v3-user.1.gz #幫助文件
/usr/share/man/man1/snmpconf.1.gz
/usr/share/man/man5/snmp_config.5.gz
/usr/share/man/man5/snmpd.conf.5.gz
/usr/share/man/man5/snmpd.examples.5.gz
/usr/share/man/man5/snmpd.internal.5.gz
/usr/share/man/man5/snmptrapd.conf.5.gz
/usr/share/man/man5/variables.5.gz
/usr/share/man/man8/snmpd.8.gz
/usr/share/man/man8/snmptrapd.8.gz
/usr/share/snmp
/usr/share/snmp/snmpconf-data
/usr/share/snmp/snmpconf-data/snmp-data
/usr/share/snmp/snmpconf-data/snmp-data/authopts
/usr/share/snmp/snmpconf-data/snmp-data/debugging
/usr/share/snmp/snmpconf-data/snmp-data/mibs
/usr/share/snmp/snmpconf-data/snmp-data/output
/usr/share/snmp/snmpconf-data/snmp-data/snmpconf-config
/usr/share/snmp/snmpconf-data/snmpd-data
/usr/share/snmp/snmpconf-data/snmpd-data/acl
/usr/share/snmp/snmpconf-data/snmpd-data/basic_setup
/usr/share/snmp/snmpconf-data/snmpd-data/extending
/usr/share/snmp/snmpconf-data/snmpd-data/monitor
/usr/share/snmp/snmpconf-data/snmpd-data/operation
/usr/share/snmp/snmpconf-data/snmpd-data/snmpconf-config
/usr/share/snmp/snmpconf-data/snmpd-data/system
/usr/share/snmp/snmpconf-data/snmpd-data/trapsinks
/usr/share/snmp/snmpconf-data/snmptrapd-data
/usr/share/snmp/snmpconf-data/snmptrapd-data/authentication
/usr/share/snmp/snmpconf-data/snmptrapd-data/formatting
/usr/share/snmp/snmpconf-data/snmptrapd-data/logging
/usr/share/snmp/snmpconf-data/snmptrapd-data/runtime
/usr/share/snmp/snmpconf-data/snmptrapd-data/snmpconf-config
/usr/share/snmp/snmpconf-data/snmptrapd-data/traphandle
/var/run/net-snmp


下面是snmp的常用操作工具,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@node1 ~]# rpm -ql net-snmp-utils-5.5-44.el6_4.4.x86_64
/usr/bin/encode_keychange
/usr/bin/snmpbulkget #下面是snmp常用工具,下面我們會詳細講解
/usr/bin/snmpbulkwalk
/usr/bin/snmpdelta
/usr/bin/snmpdf
/usr/bin/snmpget
/usr/bin/snmpgetnext
/usr/bin/snmpinform
/usr/bin/snmpnetstat
/usr/bin/snmpset
/usr/bin/snmpstatus
/usr/bin/snmptable
/usr/bin/snmptest
/usr/bin/snmptranslate
/usr/bin/snmptrap
/usr/bin/snmpusm
/usr/bin/snmpvacm
/usr/bin/snmpwalk
/usr/share/man/man1/encode_keychange.1.gz
/usr/share/man/man1/snmpbulkget.1.gz
/usr/share/man/man1/snmpbulkwalk.1.gz
/usr/share/man/man1/snmpcmd.1.gz
/usr/share/man/man1/snmpconf.1.gz
/usr/share/man/man1/snmpdelta.1.gz
/usr/share/man/man1/snmpdf.1.gz
/usr/share/man/man1/snmpget.1.gz
/usr/share/man/man1/snmpgetnext.1.gz
/usr/share/man/man1/snmpinform.1.gz
/usr/share/man/man1/snmpnetstat.1.gz
/usr/share/man/man1/snmpset.1.gz
/usr/share/man/man1/snmpstatus.1.gz
/usr/share/man/man1/snmptable.1.gz
/usr/share/man/man1/snmptest.1.gz
/usr/share/man/man1/snmptranslate.1.gz
/usr/share/man/man1/snmptrap.1.gz
/usr/share/man/man1/snmpusm.1.gz
/usr/share/man/man1/snmpvacm.1.gz
/usr/share/man/man1/snmpwalk.1.gz
/usr/share/man/man5/snmp.conf.5.gz
/usr/share/man/man5/variables.5.gz


10.查看一下默認配置文件

注,大家可以看到默認配置文件有很多內容,但其實大部分都是註釋。下面我們來講解一下常用的配置選項,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
[root@node1 ~]# cat /etc/snmp/snmpd.conf
###############################################################################
#
# snmpd.conf:
#  An example configuration file for configuring the ucd-snmp snmpd agent.
#
###############################################################################
#
# This file is intended to only be as a starting point. Many more
# configuration directives exist than are mentioned in this file. For
# full details, see the snmpd.conf(5) manual page.
#
# All lines beginning with a '#' are comments and are intended for you
# to read. All other lines are configuration commands for the agent.
###############################################################################
# Access Control
###############################################################################
# As shipped, the snmpd demon will only respond to queries on the
# system mib group until this file is replaced or modified for
# security purposes. Examples are shown below about how to increase the
# level of access.
# By far, the most common question I get about the agent is "why won't
# it work?", when really it should be "how do I configure the agent to
# allow me to access it?"
#
# By default, the agent responds to the "public" community for read
# only access, if run out of the box without any configuration file in
# place. The following examples show you other ways of configuring
# the agent so that you can change the community names, and give
# yourself write access to the mib tree as well.
#
# For more information, read the FAQ as well as the snmpd.conf(5)
# manual page.
####
# First, map the community name "public" into a "security name"
#    sec.name source     community
com2sec notConfigUser default    public
####
# Second, map the security name into a group name:
#    groupName   securityModel securityName
group  notConfigGroup v1      notConfigUser
group  notConfigGroup v2c      notConfigUser
####
# Third, create a view for us to let the group have rights to:
# Make at least snmpwalk -v 1 localhost -c public system fast again.
#    name      incl/excl   subtree     mask(optional)
view  systemview  included  .1.3.6.1.2.1.1
view  systemview  included  .1.3.6.1.2.1.25.1.1
####
# Finally, grant the group read-only access to the systemview view.
#    group     context sec.model sec.level prefix read  write notif
access notConfigGroup ""   any    noauth  exact systemview none none
# -----------------------------------------------------------------------------
#下面的是一個例子,具體是關於怎麼對本機或其他網絡主機賦予權限
# Here is a commented out example configuration that allows less
# restrictive access.
# YOU SHOULD CHANGE THE "COMMUNITY" TOKEN BELOW TO A NEW KEYWORD ONLY
# KNOWN AT YOUR SITE. YOU *MUST* CHANGE THE NETWORK TOKEN BELOW TO
# SOMETHING REFLECTING YOUR LOCAL NETWORK ADDRESS SPACE.
##    sec.name source     community
#com2sec local   localhost    COMMUNITY
#com2sec mynetwork NETWORK/24   COMMUNITY
##   group.name sec.model sec.name
#group MyRWGroup any    local
#group MyROGroup any    mynetwork
#
#group MyRWGroup any    otherv3user
#...
##      incl/excl subtree             mask
#view all  included .1                80
## -or just the mib2 tree-
#view mib2  included .iso.org.dod.internet.mgmt.mib-2 fc
##        context sec.model sec.level prefix read  write notif
#access MyROGroup ""   any    noauth  0   all  none  none
#access MyRWGroup ""   any    noauth  0   all  all  all
###############################################################################
# Sample configuration to make net-snmpd RFC 1213.
# Unfortunately v1 and v2c don't allow any user based authentification, so
# opening up the default config is not an option from a security point.
#
# WARNING: If you uncomment the following lines you allow write access to your
# snmpd daemon from any source! To avoid this use different names for your
# community or split out the write access to a different community and
# restrict it to your local network.
# Also remember to comment the syslocation and syscontact parameters later as
# otherwise they are still read only (see FAQ for net-snmp).
#
# First, map the community name "public" into a "security name"
#    sec.name    source     community
#com2sec notConfigUser  default     public
# Second, map the security name into a group name:
#    groupName    securityModel  securityName
#group  notConfigGroup v1       notConfigUser
#group  notConfigGroup v2c       notConfigUser
# Third, create a view for us to let the group have rights to:
# Open up the whole tree for ro, make the RFC 1213 required ones rw.
#    name      incl/excl    subtree mask(optional)
#view  roview     included    .1
#view  rwview     included    system.sysContact
#view  rwview     included    system.sysName
#view  rwview     included    system.sysLocation
#view  rwview     included    interfaces.ifTable.ifEntry.ifAdminStatus
#view  rwview     included    at.atTable.atEntry.atPhysAddress
#view  rwview     included    at.atTable.atEntry.atNetAddress
#view  rwview     included    ip.ipForwarding
#view  rwview     included    ip.ipDefaultTTL
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteDest
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteIfIndex
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteMetric1
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteMetric2
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteMetric3
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteMetric4
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteType
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteAge
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteMask
#view  rwview     included    ip.ipRouteTable.ipRouteEntry.ipRouteMetric5
#view  rwview     included    ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaIfIndex
#view  rwview     included    ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress
#view  rwview     included    ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaNetAddress
#view  rwview     included    ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaType
#view  rwview     included    tcp.tcpConnTable.tcpConnEntry.tcpConnState
#view  rwview     included    egp.egpNeighTable.egpNeighEntry.egpNeighEventTrigger
#view  rwview     included    snmp.snmpEnableAuthenTraps
# Finally, grant the group read-only access to the systemview view.
#    group     context sec.model sec.level prefix read  write notif
#access notConfigGroup ""   any    noauth  exact roview rwview none
###############################################################################
# System contact information
#聯繫人信息
# It is also possible to set the sysContact and sysLocation system
# variables through the snmpd.conf file:
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
# Example output of snmpwalk:
#  % snmpwalk -v 1 localhost -c public system
#  system.sysDescr.0 = "SunOS name sun4c"
#  system.sysObjectID.0 = OID: enterprises.ucdavis.ucdSnmpAgent.sunos4
#  system.sysUpTime.0 = Timeticks: (595637548) 68 days, 22:32:55
#  system.sysContact.0 = "Me <[email protected]>"
#  system.sysName.0 = "name"
#  system.sysLocation.0 = "Right here, right now."
#  system.sysServices.0 = 72
###############################################################################
# Logging
#日誌信息
# We do not want annoying "Connection from UDP: " messages in syslog.
# If the following option is commented out, snmpd will print each incoming
# connection, which can be useful for debugging.
dontLogTCPWrappersConnects yes
# -----------------------------------------------------------------------------
###############################################################################
# Process checks.
#關於檢測進程數目的有個OIDs,可以在enterprises.ucdavis.procTable查看
# The following are examples of how to use the agent to check for
# processes running on the host. The syntax looks something like:
#
# proc NAME [MAX=0] [MIN=0]
#
# NAME: the name of the process to check for. It must match
#     exactly (ie, http will not find httpd processes).
# MAX:  the maximum number allowed to be running. Defaults to 0.
# MIN:  the minimum number to be running. Defaults to 0.
#
# Examples (commented out by default):
#
# Make sure mountd is running
#proc mountd
# Make sure there are no more than 4 ntalkds running, but 0 is ok too.
#proc ntalkd 4
# Make sure at least one sendmail, but less than or equal to 10 are running.
#proc sendmail 10 1
# A snmpwalk of the process mib tree would look something like this:
#
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.2
# enterprises.ucdavis.procTable.prEntry.prIndex.1 = 1
# enterprises.ucdavis.procTable.prEntry.prIndex.2 = 2
# enterprises.ucdavis.procTable.prEntry.prIndex.3 = 3
# enterprises.ucdavis.procTable.prEntry.prNames.1 = "mountd"
# enterprises.ucdavis.procTable.prEntry.prNames.2 = "ntalkd"
# enterprises.ucdavis.procTable.prEntry.prNames.3 = "sendmail"
# enterprises.ucdavis.procTable.prEntry.prMin.1 = 0
# enterprises.ucdavis.procTable.prEntry.prMin.2 = 0
# enterprises.ucdavis.procTable.prEntry.prMin.3 = 1
# enterprises.ucdavis.procTable.prEntry.prMax.1 = 0
# enterprises.ucdavis.procTable.prEntry.prMax.2 = 4
# enterprises.ucdavis.procTable.prEntry.prMax.3 = 10
# enterprises.ucdavis.procTable.prEntry.prCount.1 = 0
# enterprises.ucdavis.procTable.prEntry.prCount.2 = 0
# enterprises.ucdavis.procTable.prEntry.prCount.3 = 1
# enterprises.ucdavis.procTable.prEntry.prErrorFlag.1 = 1
# enterprises.ucdavis.procTable.prEntry.prErrorFlag.2 = 0
# enterprises.ucdavis.procTable.prEntry.prErrorFlag.3 = 0
# enterprises.ucdavis.procTable.prEntry.prErrMessage.1 = "No mountd process running."
# enterprises.ucdavis.procTable.prEntry.prErrMessage.2 = ""
# enterprises.ucdavis.procTable.prEntry.prErrMessage.3 = ""
# enterprises.ucdavis.procTable.prEntry.prErrFix.1 = 0
# enterprises.ucdavis.procTable.prEntry.prErrFix.2 = 0
# enterprises.ucdavis.procTable.prEntry.prErrFix.3 = 0
#
# Note that the errorFlag for mountd is set to 1 because one is not
# running (in this case an rpc.mountd is, but thats not good enough),
# and the ErrMessage tells you what's wrong. The configuration
# imposed in the snmpd.conf file is also shown.
#
# Special Case: When the min and max numbers are both 0, it assumes
# you want a max of infinity and a min of 1.
#
# -----------------------------------------------------------------------------
###############################################################################
# Executables/scripts
#snmpd可以運行一個腳本,並可以得到他的返回值(最後一個)在enterprises.ucdavis.extTable下查看
#
# You can also have programs run by the agent that return a single
# line of output and an exit code. Here are two examples.
#
# exec NAME PROGRAM [ARGS ...]
#
# NAME:   A generic name. The name must be unique for each exec statement.
# PROGRAM: The program to run. Include the path!
# ARGS:   optional arguments to be passed to the program
# a simple hello world
#exec echotest /bin/echo hello world
# Run a shell script containing:
#
# #!/bin/sh
# echo hello world
# echo hi there
# exit 35
#
# Note: this has been specifically commented out to prevent
# accidental security holes due to someone else on your system writing
# a /tmp/shtest before you do. Uncomment to use it.
#
#exec shelltest /bin/sh /tmp/shtest
# Then,
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.8
# enterprises.ucdavis.extTable.extEntry.extIndex.1 = 1
# enterprises.ucdavis.extTable.extEntry.extIndex.2 = 2
# enterprises.ucdavis.extTable.extEntry.extNames.1 = "echotest"
# enterprises.ucdavis.extTable.extEntry.extNames.2 = "shelltest"
# enterprises.ucdavis.extTable.extEntry.extCommand.1 = "/bin/echo hello world"
# enterprises.ucdavis.extTable.extEntry.extCommand.2 = "/bin/sh /tmp/shtest"
# enterprises.ucdavis.extTable.extEntry.extResult.1 = 0
# enterprises.ucdavis.extTable.extEntry.extResult.2 = 35
# enterprises.ucdavis.extTable.extEntry.extOutput.1 = "hello world."
# enterprises.ucdavis.extTable.extEntry.extOutput.2 = "hello world."
# enterprises.ucdavis.extTable.extEntry.extErrFix.1 = 0
# enterprises.ucdavis.extTable.extEntry.extErrFix.2 = 0
# Note that the second line of the /tmp/shtest shell script is cut
# off. Also note that the exit status of 35 was returned.
# -----------------------------------------------------------------------------
###############################################################################
# disk checks
#硬盤分區的定義,語法如下,只有定義了纔會取抓值。單位KB
# The agent can check the amount of available disk space, and make
# sure it is above a set limit.
# disk PATH [MIN=100000]
#
# PATH: mount path to the disk in question.
# MIN:  Disks with space below this value will have the Mib's errorFlag set.
#    Default value = 100000.
# Check the / partition and make sure it contains at least 10 megs.
#disk / 10000
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.9
# enterprises.ucdavis.diskTable.dskEntry.diskIndex.1 = 0
# enterprises.ucdavis.diskTable.dskEntry.diskPath.1 = "/" Hex: 2F
# enterprises.ucdavis.diskTable.dskEntry.diskDevice.1 = "/dev/dsk/c201d6s0"
# enterprises.ucdavis.diskTable.dskEntry.diskMinimum.1 = 10000
# enterprises.ucdavis.diskTable.dskEntry.diskTotal.1 = 837130
# enterprises.ucdavis.diskTable.dskEntry.diskAvail.1 = 316325
# enterprises.ucdavis.diskTable.dskEntry.diskUsed.1 = 437092
# enterprises.ucdavis.diskTable.dskEntry.diskPercent.1 = 58
# enterprises.ucdavis.diskTable.dskEntry.diskErrorFlag.1 = 0
# enterprises.ucdavis.diskTable.dskEntry.diskErrorMsg.1 = ""
# -----------------------------------------------------------------------------
###############################################################################
# load average checks
#這個是定義cpu任務的負載信息的,值是平均最大值
# load [1MAX=12.0] [5MAX=12.0] [15MAX=12.0]
#
# 1MAX:  If the 1 minute load average is above this limit at query
#     time, the errorFlag will be set.
# 5MAX:  Similar, but for 5 min average.
# 15MAX: Similar, but for 15 min average.
# Check for loads:
#load 12 14 14
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.10
# enterprises.ucdavis.loadTable.laEntry.loadaveIndex.1 = 1
# enterprises.ucdavis.loadTable.laEntry.loadaveIndex.2 = 2
# enterprises.ucdavis.loadTable.laEntry.loadaveIndex.3 = 3
# enterprises.ucdavis.loadTable.laEntry.loadaveNames.1 = "Load-1"
# enterprises.ucdavis.loadTable.laEntry.loadaveNames.2 = "Load-5"
# enterprises.ucdavis.loadTable.laEntry.loadaveNames.3 = "Load-15"
# enterprises.ucdavis.loadTable.laEntry.loadaveLoad.1 = "0.49" Hex: 30 2E 34 39
# enterprises.ucdavis.loadTable.laEntry.loadaveLoad.2 = "0.31" Hex: 30 2E 33 31
# enterprises.ucdavis.loadTable.laEntry.loadaveLoad.3 = "0.26" Hex: 30 2E 32 36
# enterprises.ucdavis.loadTable.laEntry.loadaveConfig.1 = "12.00"
# enterprises.ucdavis.loadTable.laEntry.loadaveConfig.2 = "14.00"
# enterprises.ucdavis.loadTable.laEntry.loadaveConfig.3 = "14.00"
# enterprises.ucdavis.loadTable.laEntry.loadaveErrorFlag.1 = 0
# enterprises.ucdavis.loadTable.laEntry.loadaveErrorFlag.2 = 0
# enterprises.ucdavis.loadTable.laEntry.loadaveErrorFlag.3 = 0
# enterprises.ucdavis.loadTable.laEntry.loadaveErrMessage.1 = ""
# enterprises.ucdavis.loadTable.laEntry.loadaveErrMessage.2 = ""
# enterprises.ucdavis.loadTable.laEntry.loadaveErrMessage.3 = ""
# -----------------------------------------------------------------------------
###############################################################################
# Extensible sections.
# 與enterprises.ucdavis.extTable的區別
# This alleviates the multiple line output problem found in the
# previous executable mib by placing each mib in its own mib table:
# Run a shell script containing:
#
# #!/bin/sh
# echo hello world
# echo hi there
# exit 35
#
# Note: this has been specifically commented out to prevent
# accidental security holes due to someone else on your system writing
# a /tmp/shtest before you do. Uncomment to use it.
#
# exec .1.3.6.1.4.1.2021.50 shelltest /bin/sh /tmp/shtest
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.50
# enterprises.ucdavis.50.1.1 = 1
# enterprises.ucdavis.50.2.1 = "shelltest"
# enterprises.ucdavis.50.3.1 = "/bin/sh /tmp/shtest"
# enterprises.ucdavis.50.100.1 = 35
# enterprises.ucdavis.50.101.1 = "hello world."
# enterprises.ucdavis.50.101.2 = "hi there."
# enterprises.ucdavis.50.102.1 = 0
# Now the Output has grown to two lines, and we can see the 'hi
# there.' output as the second line from our shell script.
#
# Note that you must alter the mib.txt file to be correct if you want
# the .50.* outputs above to change to reasonable text descriptions.
# Other ideas:
#
# exec .1.3.6.1.4.1.2021.51 ps /bin/ps
# exec .1.3.6.1.4.1.2021.52 top /usr/local/bin/top
# exec .1.3.6.1.4.1.2021.53 mailq /usr/bin/mailq
#可以運行多個腳本
# -----------------------------------------------------------------------------
###############################################################################
# Pass through control.
#
# Usage:
#  pass MIBOID EXEC-COMMAND
#
# This will pass total control of the mib underneath the MIBOID
# portion of the mib to the EXEC-COMMAND.
#
# Note: You'll have to change the path of the passtest script to your
# source directory or install it in the given location.
#
# Example: (see the script for details)
#      (commented out here since it requires that you place the
#      script in the right location. (its not installed by default))
# pass .1.3.6.1.4.1.2021.255 /bin/sh /usr/local/local/passtest
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.255
# enterprises.ucdavis.255.1 = "life the universe and everything"
# enterprises.ucdavis.255.2.1 = 42
# enterprises.ucdavis.255.2.2 = OID: 42.42.42
# enterprises.ucdavis.255.3 = Timeticks: (363136200) 42 days, 0:42:42
# enterprises.ucdavis.255.4 = IpAddress: 127.0.0.1
# enterprises.ucdavis.255.5 = 42
# enterprises.ucdavis.255.6 = Gauge: 42
#
# % snmpget -v 1 localhost public .1.3.6.1.4.1.2021.255.5
# enterprises.ucdavis.255.5 = 42
#
# % snmpset -v 1 localhost public .1.3.6.1.4.1.2021.255.1 s "New string"
# enterprises.ucdavis.255.1 = "New string"
#
# For specific usage information, see the man/snmpd.conf.5 manual page
# as well as the local/passtest script used in the above example.
###############################################################################
# Further Information
#
# See the snmpd.conf manual page, and the output of "snmpd -H".

下面是常用的配置選項,

主配置文件:/etc/snmp/snmpd.conf

常用定義項:

(1).首選是定義一個共同體名(community),這裏是public(多數SNMP設備或服務默認都是是用public),及可以訪問這個public的用戶名(sec name),這裏是notConfigUser。Public相當於用戶notConfigUser的密碼:) ,source 在net-snmp中用來對來源IP加以控制,即哪些可以獲取SNMP信息

1
2
#        sec.name  source community
com2sec  notConfigUser  default  public

(2).定義一個組名(groupName)這裏是notConfigGroup,及組的安全級別,把notConfigGroup這個用戶加到這個組中。

sec.model:安全模式,可選值爲v1/v2c/usm。

1
2
3
#     groupName  securityModel  securityName
group notConfigGroup  v1  notConfigUser
group notConfigGroup  v2c  notConfigUser

(3).定義一個可操作的範圍(view)名, 這裏是all,範圍是 .1 

1
2
#     name incl/excl  subtree mask(optional)
view  all included  .1 #表示可以查看.1節點下的所有設備信息

注: 

  • incl/excl:對下面的MIB子樹是包括還是排除。

  • subtree:視圖中涉及的MIB子樹。

  • mask:掩碼 

(4).定義notConfigUser這個組在all這個view範圍內可做的操作(即權限),這時定義了notConfigUser組的成員可對.1這個範圍做只讀操作。 

1
2
#      group  context  sec.model  sec.level  prefix  read  write  notif
access notConfigGroup  ""  any  noauth  exact  all none  none


注:

  • context:上下文,v1、v2c中始終爲空。

  • sec.model:安全模式,可選值爲v1/v2c/usm。

  • sec.level:安全級別,可選值爲auth/noauth/priv,v1、v2c中只能爲noauth。

  • prefix:前綴,指定context如何與PDU中的context匹配,v3使用。

  • read:授權的讀視圖。

  • write:授權的寫視圖。

  • notif:授權的trap視圖。

(5).端口: 標準的SNMP服務使用161和162端口,廠商私有的實現一般使用199、391、705和1993端口,net-snmp用udp 161。


11.啓動snmp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@node1 ~]# service snmpd start
正在啓動 snmpd:                      [確定]
[root@node1 ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1045/sshd
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1122/master
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   2149/sshd
tcp    0   0 127.0.0.1:6011       0.0.0.0:*          LISTEN   1582/sshd
tcp    0   0 127.0.0.1:199        0.0.0.0:*          LISTEN   12252/snmpd
tcp    0   0 :::22            :::*            LISTEN   1045/sshd
tcp    0   0 ::1:25           :::*            LISTEN   1122/master
tcp    0   0 ::1:6010          :::*            LISTEN   2149/sshd
tcp    0   0 ::1:6011          :::*            LISTEN   1582/sshd
udp    0   0 0.0.0.0:161         0.0.0.0:*                12252/snmpd


12.測試一下

1
2
3
4
[root@node1 ~]# snmpd -v #查看一下net-snmp版本
NET-SNMP version: 5.5
Web:        http://www.net-snmp.org/
Email:       [email protected]

注,啓動代理時也可以使用如下參數使snmpd使用【配置文件】啓動:

1
2
3
[root@node1 ~]# snmpd –C –c /etc/snmp/snmpd.conf
-C 不讀取默認配置文件
-c 讀取指定的配置文件

使用這個參數,我們可以配置很多的配置文件,設置不同的運行環境,便於我們開發和學習。

下面來測試一下,

從代理中讀取一個管理對象驗證代理是否正常運行。讀取MIB庫中的RFC1213->system->sysDescr 對象,該對象表示設備的描述信息。使用net-snmp提供的snmp工具讀取,命令如下:

[root@node1 ~]# snmpget -v 2c -c public localhost sysDescr.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux node1.test.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64


注,如果得到了信息,表示net-snmp安裝成功。


13.net-snmp工具介紹

1
2
3
4
[root@node1 ~]# snmp
snmpbulkget  snmpd     snmpget    snmpnetstat  snmptable   snmptrap    snmpvacm
snmpbulkwalk  snmpdelta   snmpgetnext  snmpset    snmptest    snmptrapd   snmpwalk
snmpconf    snmpdf     snmpinform   snmpstatus   snmptranslate snmpusm
  • snmpget 模擬snmp的GetRequest操作的工具。用來獲取一個或幾個管理信息。用來讀取管理信息的內容。

  • snmpgetnext 模擬snmp的GetNextRequest操作的工具。用來獲取一個管理信息實例的下一個可用實例數據。一般用來遍歷SNMP中的表格數據。

  • snmpset 模擬snmp的SetRequest操作的工具。用來設置可以寫的管理信息。一般用來配置設備或對設備執行操作。

  • snmpbulkget 模擬snmp的GetBulkRequest操作的工具。用來獲取大塊的數據。一般在大量讀取大塊數據時使用,以提高帶寬利用率,並且比使用snmpget、snmpgetnext及snmpwalk有更強的容錯能力,代理會返回儘可能多的數據,比其它命令更有保證。

  • snmpwalk 利用GetNextRequest對給定的管理樹進行遍歷的工具。一般用來對錶格類型管理信息進行遍歷。

  • snmptrap 模擬trap的工具。用來發送模擬trap。一般用來測試管理站安裝和配置是否正確,或者用來驗證開發的Trap接收程序是否可以正常工作。

  • snmptrapd 接收並顯示trap的工具。一般在代理的開發過程中,接收代理髮來的Trap,並將PDU細節打印出來,也來測試Trap發送功能是否正常。

  • snmpinform 模擬發送InformRequest的工具。跟snmptrap類似,用來發送模擬的帶應答的Trap,以測試管理站或自己開發的接收程序。

  • snmptable 使用GetNextRequest 和 GetBulkRequest操作讀取表信息,以列表形式顯示的工具。

  • snmpstatus 從SNMP實體讀取幾個重要的管理信息以確定設備狀態的工具。用來簡單測定設備狀態。

  • snmpbulkwalk 利用GetBulkRequest實現對給定管理樹進行遍歷的工具。對錶格類型的管理信息進行遍歷讀取。

  • snmpdelta 用來監視Integer類型的管理對象,會及時報告值改變情況的工具。用於監測一個設備或開發中的代理。

  • snmptest 是一個複雜的工具,可以監測和管理一個網絡實體的信息,通過SNMP請求操作與管理實體通信。

  • snmptranslate 將對象名字和標識符相互轉換的工具。用於數據格式的對象標識和可讀式字符串的數據名稱的轉換。類似於域名與IP地址的關係。

  • snmpusm SNMPv3 USM配置工具。用於SNMPv3的用戶管理。

  • snmpvacm 爲一個網絡實體創建和維護SNMPv3的基於視圖訪問控制參數的工具。用於維護SNMPv3的視圖訪問控制。

  • snmpconf 生成snmpd配置文件的工具。用於生成snmpd的各種配置文件,用作模板,以生成用戶級配置文件。

  • snmpd Net-snmp開發的主代理程序,包括衆多標準MIB的實現。還可以使用子代理對其進行擴展,是一個功能強大的SNMP代理。Linux、Unix和Windows系統運行snmpd後,直接具備了SNMP協議支持,可以被管理站管理。許多商業化的Linux中使用snmpd作爲系統的SNMP代理。

  • snmpdf 通過SNMP訪問並顯示網絡實體磁盤利用情況的工具。用來監測網絡實體的磁盤。

下面我們來說幾個最常用的net-snmp工具,

常用命令:

(1).snmpget命令

1
2
3
4
5
snmpget [OPTIONS] AGENT OID [OID]...
選項:
-v 1|2c|3  指定SNMP版本
-c COMMUNITY  指定community string
-m MIB[:...]  指定MIB文件

注,snmpget 不同於 snmpwalk ,必須在命令行給出 <object>.<instance> 的格式,不能只給出 object。 

案例,snmpget -v 2c -c public localhost system.sysDescr.0 #獲取設備的描述信息。

1
2
[root@node1 ~]# snmpget -v 2c -c public localhost system.sysDescr.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux node1.test.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64

(2).snmpwalk/snmpbulkwalk 命令

1
2
3
4
5
snmpwalk/snmpbulkwalk [OPTIONS] AGENT [OID]
選項:
-v 1|2c|3  指定SNMP版本
-c COMMUNITY  指定community string
-m MIB[:...]  指定MIB文件

注,snmpbulkwalk命令: 獲取snmp服務器的mib-2各種資訊,snmpbulkwalk用於通過SNMPv2 的SNMP GET BULK請求命令與其它網絡實體通信,只能用於snmpv2,速度快於snmpwalk 

案例,snmpbulkwalk -v 2c -c public 127.0.0.1 .1.3.6.1.2.1.1 #獲取mib-2的system的數據。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@node1 ~]# snmpbulkwalk  -v  2c  -c public 127.0.0.1  .1.3.6.1.2.1.1
SNMPv2-MIB::sysDescr.0 = STRING: Linux node1.test.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (264551) 0:44:05.51
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: node1.test.com
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (10) 0:00:00.10
SNMPv2-MIB::sysORID.1 = OID: SNMP-MPD-MIB::snmpMPDMIBObjects.3.1.1
SNMPv2-MIB::sysORID.2 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.6 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.7 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.8 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.3 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (10) 0:00:00.10
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (10) 0:00:00.10
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (10) 0:00:00.10

(3).snmpwalk命令

注,獲取snmp服務器的HOST-RESOURCES的各種資訊。

案例,snmpwalk -v 2c -c public localhost system #這條指令用於查看本機系統信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@node1 ~]# snmpwalk -v 2c -c public localhost system
SNMPv2-MIB::sysDescr.0 = STRING: Linux node1.test.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (3335125) 9:15:51.25
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: node1.test.com
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (10) 0:00:00.10
SNMPv2-MIB::sysORID.1 = OID: SNMP-MPD-MIB::snmpMPDMIBObjects.3.1.1
SNMPv2-MIB::sysORID.2 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.6 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.7 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.8 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.3 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (10) 0:00:00.10
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (10) 0:00:00.10
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (10) 0:00:00.10

(4).snmpdelta命令

注,監控網卡流量等信息, 指定共同體爲public,協議爲snmpv1,以表格的方式輸出結果,顯示時間戳。

案例,

snmpdelta -c public -v 1 -Cs -CT 127.0.0.1 IF-MIB::ifInUcastPkts.2 IF-MIB::ifOutUcastPkts.2 #監控IF-MIB::ifInUcastPkts(輸入流量),IF-MIB::ifOutUcastPkts(輸出流量),如監控本地的第二塊網卡instance爲2。
snmpdelta -c public -v 1 -Cs -CT 127.0.0.1 ifInDiscards.2 #監控ifInDiscards(已經丟棄的收到的數據包的總數),格式同上。


(5).snmpnetstat命令

注,查看snmp服務器的端口連接信息, 協議爲snmp 2c。

案例,

1
snmpnetstat -v 2c -c public -Can -Cp tcp 127.0.0.1  #獲取127.0.0.1的TCP協議的開放端口狀態

(6).snmptranslate 命令

注,在MIB OID 在數字和文字名稱之間進行轉換。

例子,

  • snmptranslate -On -IR HOST-RESOURCES-MIB::hrSystem #用snmptranslate把symbol 格式的HOST-RESOURCES-MIB::hrSystem 轉換爲 numberic 格式的oid,用-On(輸出數字格式的oid),使用 -IR(允許“隨機”訪問 MIB)

    1
    2
    [root@node1 ~]# snmptranslate -On -IR HOST-RESOURCES-MIB::hrSystem
    .1.3.6.1.2.1.25.1


  • snmptranslate -Onf -IR HOST-RESOURCES-MIB::hrSystem #用snmptranslate把symbol 格式的HOST-RESOURCES-MIB::hrSystem 轉換爲symbol 格式的 oid,用-Onf(輸出符號格式的oid)

    1
    2
    [root@node1 ~]# snmptranslate -Onf -IR HOST-RESOURCES-MIB::hrSystem
    .iso.org.dod.internet.mgmt.mib-2.host.hrSystem
  • snmptranslate -Td iso.org.dod.internet.mgmt.mib-2 #用snmptranslate輸出該對象(iso.org.dod.internet.mgmt.mib-2)的詳細定義,用-Td(包括文字名稱、所屬 MIB、類型、狀態、讀寫權限、描述信息,數字格式的 OID)

1
2
3
4
5
[root@node1 ~]# snmptranslate -Td iso.org.dod.internet.mgmt.mib-2
SNMPv2-SMI::mib-2
mib-2 OBJECT-TYPE
 -- FROM  SNMPv2-SMI, RFC1213-MIB
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) 1 }


  • snmptranslate -Tp iso.org.dod.internet.mgmt.mib-2 #用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 名稱而已 


十一、SNMP的MIB詳解

1.MIB管理信息庫

IETF規定的管理信息庫MIB(由中定義了可訪問的網絡設備及其屬性,由對象識別符(OID:Object Identifier)唯一指定。MIB是一個樹形結構,SNMP協議消息通過遍歷MIB樹形目錄中的節點來訪問網絡中的設備。下圖給出了NMS系統中SNMP可訪問網絡設備的對象識別樹(OID:Object Identifier)結構。 
131305119.gif

上圖中的節點是從internet開始的,其實上面還有幾個節點請看下圖。

s9
下圖給出了對一個DS1線路狀態進行查詢的OID設置例子。 
131321163.gif

2.SNMP消息類型

SNMP中定義了五種消息類型:Get-Request、Get-Response、Get-Next-Request、Set-Request、Trap

  • Get-Request 、Get-Next-Request與Get-Response 
    SNMP管理站用Get-Request消息從擁有SNMP代理的網絡設備中檢索信息,而SNMP代理則用Get-Response消息響應。Get- Next-Request用於和Get-Request組合起來查詢特定的表對象中的列元素。如:首先通過下面的原語獲得所要查詢的設備的接口數:
    {iso org(3) dod(6) internet(1) mgmt(2) mib(1) interfaces(2) ifNumber(2)}
    後再通過下面的原語,進行查詢(其中第一次用Get-Request,其後用Get-Next-Request):
    {iso org(3) dod(6) internet(1) mgmt(2) mib(1) interfaces(2) ifTable(2)}

  • Set-Request
    SNMP管理站用Set-Request 可以對網絡設備進行遠程配置(包括設備名、設備屬性、刪除設備或使某一個設備屬性有效/無效等)。

  • Trap 
    SNMP代理使用Trap向SNMP管理站發送非請求消息,一般用於描述某一事件的發生。

3.SNMP管理信息庫MIB 

管理信息庫MIB指明瞭網絡元素所維持的變量(即能夠被管理進程查詢和設置的信息)。MIB給出了一個網絡中所有可能的被管理對象的集合的數據結構。SNMP的管理信息庫採用和域名系統DNS相似的樹型結構,它的根在最上面,根沒有名字。下圖畫的是管理信息庫的一部分,它又稱爲對象命名(objectnamingtree)。 
131336373.gif

管理信息庫的對象命名舉例,對象命名樹的頂級對象有三個,即ISO、ITU-T和這兩個組織的聯合體。在ISO的下面有4個結點,其中的一個(標號3)是被標識的組織。在其下面有一個美國國防部(Department of Defense)的子樹(標號是6),再下面就是Internet(標號是1)。在只討論Internet中的對象時,可只畫出Internet以下的子樹(圖中帶陰影的虛線方框),並在Internet結點旁邊標註上{1.3.6.1}即可。在Internet結點下面的第二個結點是mgmt(管理),標號是2。再下面是管理信息庫,原先的結點名是mib。1991年定義了新的版本MIB- II,故結點名現改爲mib-2,其標識爲{1.3.6.1.2.1},或{Internet(1) .2.1}。這種標識爲對象標識符。最初的結點mib將其所管理的信息分爲8個類別,見下圖,現在mib-2所包含的信息類別已超過40個。

s8

應當指出,MIB的定義與具體的網絡管理協議無關,這對於廠商和用戶都有利。廠商可以在產品(如路由器)中包含SNMP代理軟件,並保證在定義新的MIB項目後該軟件仍遵守標準。用戶可以使用同一網絡管理客戶軟件來管理具有不同版本的MIB的多個路由器。當然,一個沒有新的MIB項目的路由器不能提供這些項目的信息。


這裏要提一下MIB中的對象{1.3.6.1.4.1},即enterprises(企業),其所屬結點數已超過3000。例如IBM爲 11.3.6.1.4.1.2},Cisco爲{1.3.6.1.4.1.9},Novell爲{1.3.6.1.4.1.23}等。世界上任何一個公司、學校只要用電子郵件發往[email protected]進行申請即可獲得一個結點名。這樣各廠家就可以定義自己的產品的被管理對象名,使它能用 SNMP進行管理。


好了,到這裏SNMP的內容基本就講解結束了,有關SNMP更詳細的內容大家可以參考SNMP的RFC文檔http://www.faqs.org/rfcs/rfc1157.html,在下面的博客中我們主要來講解一下,RRDTool繪圖工具的使用。最後想說,希望大家有所收穫^_^……


轉自:https://blog.51cto.com/freeloda/1306743  (陳明乾的51cto博客)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章