Zabbix 通过SNMP协议监控可以对应用系统(如:Nginx、Tomcat等)、不同类型系统主机(如:AIX系统、Linux系统等)、网络设备(如:华为、思科、H3C等)等多种类的IT设备和系统进行监控。

本文通过CentOS8.4模拟网络设备通过SNMP接受Zabbix管理的配置过程。


1. 认识SNMP

实践案例:Zabbix利用SNMP协议监控IT设备_网络设备

SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是“简单网络管理协议,SNMP是一种简单网络管理协议,它属于TCP/IP五层协议中的应用层协议,用于网络管理的协议,SNMP主要用于网络设备的管理。

SNMP的基本思想:为不同种类的设备、不同厂家生产的设备、不同型号的设备,定义为一个统一的接口和协议,使得管理员可以是使用统一的外观面对这些需要管理的网络设备进行管理。通过网络,管理员可以管理位于不同物理空间的设备,从而大大提高网络管理的效率,简化网络管理员的工作。

SNMP协议是TCP/IP协议簇的一个应用层协议,在1988年被制定,并被Internet体系结构委员会(IAB,Internet Architecture Board)采纳作为一个短期的网络管理解决方案,由于SNMP的简单性,在Internet时代得到了蓬勃的发展,1992年发布了SNMPv2版本,以增强SNMPv1的安全性和功能,SNMP的协议版本目前有SNMP v1、SNMP v2c和SNMP v3三种版本。


2. 基于CentOS的SNMP

Centos 8.4服务器安装配置SNMP,以模拟网络设备SNMP方式被Zabbix管理的配置过程:

#### YUM方式安装SNMP,并配置
[root@CentOS84-IP98 ]#yum install -y net-snmp
[root@CentOS84-IP98 ]#find / -name snmpd.conf
/etc/snmp/snmpd.conf

################################################################################
#### 通过命令筛选出默认的有效配置行,解读如下:
[root@CentOS84-IP98 ]#grep "^[a-Z]" /etc/snmp/snmpd.conf
com2sec notConfigUser default public
# snmp的团体默认为public
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
# 将团体名称notConfigUser 关联至组notConfigGroup
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
# 上面两行是默认的两个view,其授权可访问的OID范围view
access notConfigGroup "" any noauth exact systemview none none
#将组 notConfigGroup关联至systemview 从而完成对组的授权
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes
[root@CentOS84-IP98 ]#

################################################################################
#### 修改并配置snmp
[root@CentOS84-IP98 ]#vim /etc/snmp/snmpd.conf
#### 仅仅需要修改一行并增加一行配置
com2sec notConfigUser default shone123456
view systemview included .1.

################################################################################
#### 修改后的有效配置行
[root@CentOS84-IP98 ]#grep "^[a-Z]" /etc/snmp/snmpd.conf
com2sec notConfigUser default shone123456
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
view systemview included .1.
access notConfigGroup "" any noauth exact systemview none none
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes
[root@CentOS84-IP98 ]#

[root@CentOS84-IP98 ]#systemctl enable --now snmpd
Created symlink /etc/systemd/system/multi-user.target.wants/snmpd.service → /usr/lib/systemd/system/snmpd.service.
[root@CentOS84-IP98 ]#systemctl restart snmpd

3. 通过SNMP OID测试SNMP数据采集

如何测试OID:snmpwalk是SNMP的一个工具,它使用SNMP的GET请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户。通过snmpwalk也可以查看支持SNMP协议(可网管)的设备的一些其他信息,比如cisco交换机或路由器IP地址、内存使用率等,也可用来协助开发SNMP功能。

要使用snmpwalk需要先按照net-snmp软件包中。

#### 安装并使用 snmpwalk 工具
[root@CentOS84-IP98 ]#yum -y install net-snmp-utils
[root@CentOS84-IP98 ]#snmpwalk -h
USAGE: snmpwalk [OPTIONS] AGENT [OID]

Version: 5.8
Web: http://www.net-snmp.org/
Email: net-snmp-coders@lists.sourceforge.net

OPTIONS:
-h, --help display this help message
-H display configuration file directives understood
-v 1|2c|3 specifies SNMP version to use
-V, --version display package version number
SNMP Version 1 or 2c specific
-c COMMUNITY set the community string
SNMP Version 3 specific
-a PROTOCOL set authentication protocol (MD5|SHA|SHA-224|SHA-256|SHA-384|SHA-512)
-A PASSPHRASE set authentication protocol pass phrase
-e ENGINE-ID set security engine ID (e.g. 800000020109840301)
-E ENGINE-ID set context engine ID (e.g. 800000020109840301)
-l LEVEL set security level (noAuthNoPriv|authNoPriv|authPriv)
-n CONTEXT set context name (e.g. bridge1)
-u USER-NAME set security name (e.g. bert)
-x PROTOCOL set privacy protocol (DES|AES|AES-192|AES-256)
-X PASSPHRASE set privacy protocol pass phrase
-Z BOOTS,TIME set destination engine boots/time
General communication options
-r RETRIES set the number of retries
-t TIMEOUT set the request timeout (in seconds)
Debugging
-d dump input/output packets in hexadecimal
-D[TOKEN[,...]] turn on debugging output for the specified TOKENs
(ALL gives extremely verbose debugging output)
General options
-m MIB[:...] load given list of MIBs (ALL loads everything)
-M DIR[:...] look in given list of directories for MIBs
(default: $HOME/.snmp/mibs:/usr/share/snmp/mibs)
-P MIBOPTS Toggle various defaults controlling MIB parsing:
u: allow the use of underlines in MIB symbols
c: disallow the use of "--" to terminate comments
d: save the DESCRIPTIONs of the MIB objects
e: disable errors when MIB symbols conflict
w: enable warnings when MIB symbols conflict
W: enable detailed warnings when MIB symbols conflict
R: replace MIB symbols from latest module
-O OUTOPTS Toggle various defaults controlling output display:
0: print leading 0 for single-digit hex characters
a: print all strings in ascii format
b: do not break OID indexes down
e: print enums numerically
E: escape quotes in string indices
f: print full OIDs on output
n: print OIDs numerically
p PRECISION: display floating point values with specified PRECISION (printf format string)
q: quick print for easier parsing
Q: quick print with equal-signs
s: print only last symbolic element of OID
S: print MIB module-id plus last element
t: print timeticks unparsed as numeric integers
T: print human-readable text along with hex strings
u: print OIDs using UCD-style prefix suppression
U: don't print units
v: print values only (not OID = value)
x: print all strings in hex format
X: extended index format
-I INOPTS Toggle various defaults controlling input parsing:
b: do best/regex matching to find a MIB node
h: don't apply DISPLAY-HINTs
r: do not check values for range/type legality
R: do random access to OID labels
u: top-level OIDs must have '.' prefix (UCD-style)
s SUFFIX: Append all textual OIDs with SUFFIX before parsing
S PREFIX: Prepend all textual OIDs with PREFIX before parsing
-L LOGOPTS Toggle various defaults controlling logging:
e: log to standard error
o: log to standard output
n: don't log at all
f file: log to the specified file
s facility: log to syslog (via the specified facility)

(variants)
[EON] pri: log to standard error, output or /dev/null for level 'pri' and above
[EON] p1-p2: log to standard error, output or /dev/null for levels 'p1' to 'p2'
[FS] pri token: log to file/syslog for level 'pri' and above
[FS] p1-p2 token: log to file/syslog for levels 'p1' to 'p2'
-C APPOPTS Set various application specific behaviours:
p: print the number of variables found
i: include given OID in the search range
I: don't include the given OID, even if no results are returned
c: do not check returned OIDs are increasing
t: Display wall-clock time to complete the walk
T: Display wall-clock time to complete each request
E {OID}: End the walk at the specified OID

########################## 帮助解读 ##########################
USAGE: snmpwalk [OPTIONS] AGENT [OID]
–h:显示帮助。
–v:指定snmp的版本, 1或者2c或者3。
–c:指定连接设备SNMP密码。
–V:显示当前snmpwalk命令行版本。
–r:指定重试次数,默认为0次。
–t:指定每次请求的等待超时时间,单为秒,默认为3秒。
–l:指定安全级别:noAuthNoPriv|authNoPriv|authPriv。
–a:验证协议:MD5|SHA。只有-l指定为authNoPriv或authPriv时才需要。
–A:验证字符串。只有-l指定为authNoPriv或authPriv时才需要。
–x:加密协议:DES。只有-l指定为authPriv时才需要。
–X:加密字符串。只有-l指定为authPriv时才需要。

#############################################################################
#### 测试SNMP数据采集:
[root@CentOS84-IP98 ]#snmpwalk -v 2c -c shone123456 192.168.250.98 .1.3.6.1.4.1.2021.10.1.3.1
UCD-SNMP-MIB::laLoad.1 = STRING: 0.06
[root@CentOS84-IP98 ]#snmpwalk -v 2c -c shone123456 192.168.250.98 .1.3.6.1.4.1.2021.4.3.0
UCD-SNMP-MIB::memTotalSwap.0 = INTEGER: 4194300 kB
[root@CentOS84-IP98 ]#

CentOS下可以查询到的相应的数据采集MIB管理信息库的编码


CPU 负载:
1 minute 负载: .1.3.6.1.4.1.2021.10.1.3.1
5 minute 负载: .1.3.6.1.4.1.2021.10.1.3.2
15 minute 负载: .1.3.6.1.4.1.2021.10.1.3.3
内存使用:
Total Swap Size: .1.3.6.1.4.1.2021.4.3.0
Available Swap Space: .1.3.6.1.4.1.2021.4.4.0
Total RAM in machine: .1.3.6.1.4.1.2021.4.5.0
Total RAM used: .1.3.6.1.4.1.2021.4.6.0
Total RAM Free: .1.3.6.1.4.1.2021.4.11.0
Total RAM Shared: .1.3.6.1.4.1.2021.4.13.0
Total RAM Buffered: .1.3.6.1.4.1.2021.4.14.0
Total Cached Memory: .1.3.6.1.4.1.2021.4.15.0
硬盘使用:
Path where the disk is mounted: .1.3.6.1.4.1.2021.9.1.2.1
Path of the device for the partition: .1.3.6.1.4.1.2021.9.1.3.1
Total size of the disk/partion (kBytes): .1.3.6.1.4.1.2021.9.1.6.1
Available space on the disk: .1.3.6.1.4.1.2021.9.1.7.1
Used space on the disk: .1.3.6.1.4.1.2021.9.1.8.1
Percentage of space used on disk: .1.3.6.1.4.1.2021.9.1.9.1
Percentage of inodes used on disk: .1.3.6.1.4.1.2021.9.1.10.1
系统信息:
sysDescr 1.3.6.1.2.1.1.1
sysObjectID 1.3.6.1.2.1.1.2
sysUpTime 1.3.6.1.2.1.1.3
sysContact 1.3.6.1.2.1.1.4
sysName 1.3.6.1.2.1.1.5
CPU 信息:
percentage of user CPU time: .1.3.6.1.4.1.2021.11.9.0
raw user cpu time: .1.3.6.1.4.1.2021.11.50.0
percentages of system CPU time: .1.3.6.1.4.1.2021.11.10.0
raw system cpu time: .1.3.6.1.4.1.2021.11.52.0
percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0
raw idle cpu time: .1.3.6.1.4.1.2021.11.53.0
raw nice cpu time: .1.3.6.1.4.1.2021.11.51.0


4. 添加SNMP监控主机并关联默认SNMP模板

在Zabbix6.0版本可以一次性完成添加SNMP监控主机,并关联好zabbix 自带的SNMP模板。如果是交换机等网络设备,可以查询或者索取到厂商提供的模板,更好地完成对目标设备或系统的监控。

实践案例:Zabbix利用SNMP协议监控IT设备_SNMP协议_02


5. 验证主机当前状态

可以看到SNMP经过短暂的等待后变成了绿色生效了

实践案例:Zabbix利用SNMP协议监控IT设备_网络管理_03

实践案例:Zabbix利用SNMP协议监控IT设备_网络设备_04


6. 验证SNMP监控数据

实践案例:Zabbix利用SNMP协议监控IT设备_SNMP协议_05

实践案例:Zabbix利用SNMP协议监控IT设备_SNMP协议_06