文章目录
zabbix使用snmp监控linux
1.服务端部署snmp
#服务端安装snmpd服务
[root@zabbix01 ~]# yum install -y net-snmp net-snmp-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirror.sjtu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package 1:net-snmp-5.7.2-49.el7_9.1.x86_64 already installed and latest version
Package 1:net-snmp-utils-5.7.2-49.el7_9.1.x86_64 already installed and latest version
#全部snmpd服务配置文件修改
[root@zabbix01 ~]# grep -E "^[^#]" /etc/snmp/snmpd.conf
#这个直接使用默认的社区名称为public
com2sec notConfigUser default public
#这个表示当前支持snmp的版本
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
#添加这包含可获取的数据内容,这个是获取数据的OID
view systemview included .1
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
access notConfigGroup "" any noauth exact systemview none none
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes
#启动snmpd服务
[root@Zabbix01 ~]# systemctl start snmpd.service
#查看snmp服务状态
[root@zabbix01 ~]# systemctl status snmpd.service
● snmpd.service - Simple Network Management Protocol (SNMP) Daemon.
Loaded: loaded (/usr/lib/systemd/system/snmpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2022-01-13 10:41:00 CST; 39min ago
Main PID: 29464 (snmpd)
CGroup: /system.slice/snmpd.service
└─29464 /usr/sbin/snmpd -LS0-6d -f
Jan 13 10:40:59 zabbix01 systemd[1]: Starting Simple Network Management Protocol (SNMP) Daemon....
Jan 13 10:40:59 zabbix01 snmpd[29464]: Duplicate IPv4 address detected, some interfaces may not be visible in IP-MIB
Jan 13 10:41:00 zabbix01 snmpd[29464]: NET-SNMP version 5.7.2
Jan 13 10:41:00 zabbix01 systemd[1]: Started Simple Network Management Protocol (SNMP) Daemon..
#确认snmp端口状态
[root@zabbix01 ~]# netstat -lntpu |grep snmp
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 29464/snmpd
udp 0 0 0.0.0.0:161 0.0.0.0:* 29464/snmpd
udp 0 0 0.0.0.0:162 0.0.0.0:* 10281/snmptrapd
2.客户端部署snmp
#客户端安装snmpd服务
[root@zabbix02 ~]# yum install -y net-snmp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package 1:net-snmp-5.7.2-49.el7_9.1.x86_64 already installed and latest version
#检查配置文件
[root@zabbix02 ~]# grep -E "^[^#]" /etc/snmp/snmpd.conf
#这个直接使用默认的社区名称为public
com2sec notConfigUser default public
#这个表示当前支持snmp的版本
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
#添加这包含可获取的数据内容,这个是获取数据的OID
view systemview included .1
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
access notConfigGroup "" any noauth exact systemview none none
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes
#查看snmp服务端口
[root@zabbix-vip ~]# netstat -lntpu |grep snmp
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 9798/snmpd
udp 0 0 0.0.0.0:161 0.0.0.0:* 9798/snmpd
3.测试snmp状态
####### 服务端测试
# 获取本机内存总量大小的OID(.1.3.6.1.2.1.25.2.2.0)
[root@zabbix01 ~]# snmpget -v 2c -c public localhost .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 8009204 KBytes
[root@zabbix01 ~]# snmpget -v 2c -c public 10.240.0.73 .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 8009204 KBytes
#检查测试客户端的udp端口是否开通
[root@zabbix01 ~]# nc -uvz 10.240.0.78 160
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 10.240.0.78:160.
Ncat: Connection refused.
#测试获取客户端的内存数据(内存OID:.1.3.6.1.2.1.25.2.2.0)
[root@zabbix01 ~]# snmpget -v 2c -c public 10.240.0.78 .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 8009204 KBytes
#获取所有的值
[root@zabbix01 ~]# snmpwalk -v 2c -c public 10.240.0.78 | wc -l
6704
#直接walk某一项数据的值(通过键值获取)
[root@zabbix01 ~]# snmpwalk -v 2c -c public 10.240.0.78 ifOutOctets.1
IF-MIB::ifOutOctets.1 = Counter32: 13291
#直接walk某一项数据的值(通过OID获取)
[root@zabbix01 ~]# snmpwalk -v 2c -c public 10.240.0.78 .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 8009204 KBytes
######### 客户端测试
# 获取本机内存总量大小的OID(.1.3.6.1.2.1.25.2.2.0)
[root@zabbix02 ~]# snmpget -v 2c -c public localhost .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 8009204
4.zabbix-web添加主机