第十六篇、【Zabbix监控项之snmp方式监控】
一、SNMP简单概述 1.1、什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议"。SNMP是一种简单网络管理协议,它属于TCP/IP五层协议中的应用层协议,用于网络管理的协议。SNMP主要用于网络设备的管理。由于SNMP协议简单可靠 ,受到了众多厂商的欢迎,成为了目前最为广泛的网管协议。 SNMP协议主要由两大部分构成:SNMP管理站和SNMP代理。SNMP管理站是一个中心节点,负责收集维护各个SNMP元素的信息,并对这些信息进行处理,最后反馈给网络管理员;而SNMP代理是运行在各个被管理的网络节点之上,负责统计该节点的各项信息,并且负责与SNMP管理站交互,接收并执行管理站的命令,上传各种本地的网络信息。 SNMP管理站和SNMP代理之间是松散耦合。他们之间的通信是通过UDP协议完成的。一般情况下,SNMP管理站通过UDP协议向SNMP代理发送各种命令,当SNMP代理收到命令后,返回SNMP管理站需要的参数。但是当SNMP代理检测到网络元素异常的时候,也可以主动向SNMP管理站发送消息,通告当前异常状况。 SNMP的基本思想:为不同种类的设备、不同厂家生产的设备、不同型号的设备,定义为一个统一的接口和协议,使得管理员可以是使用统一的外观面对这些需要管理的网络设备进行管理。通过网络,管理员可以管理位于不同物理空间的设备,从而大大提高网络管理的效率,简化网络管理员的工作。 SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了【读】操作;管理员需要向设备执行设置操作,所以SNMP提供了【写】操作;设备需要在重要状况改变的时候,向管理员通报事件的发生,所以SNMP提供了【Trap】操作。 1.2、SNMP 和 UDP SNMP采用UDP协议在管理端和agent之间传输信息。 SNMP采用UDP 161端口接收和发送请求,162端口接收trap,执行SNMP的设备缺省都必须采用这些端口。SNMP消息全部通过UDP端口161接收,只有Trap信息采用UDP端口162。 1.3、Snmp版本 SNMP目前共有v1,v2,v3这三个版本: SNMP v1是SNMP协议的最初版本,不过依然是众多厂家实现SNMP基本方式。 SNMP v2通常被指是基于community的SNMP V2。Community实质上就是密码。 SNMP v3 是最新版本的SNMP。它对网络管理最大的贡献在于其安全性。增加了对认证和密文传输的支持。 来自:https://www.cnblogs.com/xdp-gacl/p/3978825.html
1、snmp下载
下载地址:http://www.net-snmp.org/
[root@YZS_3WYY_WEB ~]# wget https://nchc.dl.sourceforge.net/project/net-snmp/net-snmp/5.8/net-snmp-5.8.tar.gz
2、编译安装snmp的源码
2.1、编译参数的介绍:
--prefix=/usr/local/net-snmp-5.8 # net snmp的安装路径 --enable-mfd-rewrites #允许新的mfd重写可用的mid模块 --with-default-snmp-version="3" # 默认的snmp版本 --with-sys-contact="suk,E_mail:277667028@qq.com " # 该设备的联系人 --with-sys-location="China" # 该设备的位置 --with-logfile="/usr/local/net-snmp-5.8/log/snmpd.log" # 日志文件路径 --with-persistent-directory #持久化目录
2.2、安装依赖包
[root@sms-v2 ~]# yum install perl-devel -y
2.3、开始编译snmp软件
[root@sms-v2 net-snmp-5.8]# tar xvf net-snmp-5.8.tar.gz && cd net-snmp-5.8 ./configure \ --prefix=/usr/local/net-snmp-5.8 \ --enable-mfd-rewrites \ --with-default-snmp-version="3" \ --with-sys-contact="suk,E_mail:277667028@qq.com" \ --with-sys-location="China" \ --with-logfile="/usr/local/net-snmp-5.8/log/snmpd.log" \ --with-persistent-directory=/usr/local/net-snmp-5.8 [root@sms-v2 net-snmp-5.8]# make && make install
3、配置动态链接库
[root@sms-v2 ~]# vi /etc/ld.so.conf.d/net-snmp-5.8.conf /usr/local/net-snmp-5.8/lib [root@sms-v2 ~]# ldconfig
4、拷贝snmp配置文件
#创建存放snmp配置文件的目录 [root@sms-v2 ~]# mkdir /usr/local/net-snmp-5.8/etc #从源码中拷贝配置文件 [root@sms-v2 ~]# cp net-snmp-5.8/EXAMPLE.conf /usr/local/net-snmp-5.8/etc/snmpd.conf
5、编辑配置文件
配置文件很简单,两行就够 [root@sms-v2 ~]# vi /usr/local/net-snmp-5.8/etc/snmpd.conf agentAddress udp:0.0.0.0:161 rocommunity public default
6、制作snmpd系统服务脚本
[root@sms-v2 ~]# vi /etc/init.d/snmpd #!/bin/sh # # snmpd This shell script takes care of starting and stopping # the net-snmp SNMP daemon # # chkconfig: - 26 74 # description: snmpd is net-snmp SNMP daemon. # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "${NETWORKING}" = "no" ] && exit 0 snmpd_home=/usr/local/net-snmp-5.8 RETVAL=0 name="snmpd" prog="$snmpd_home/sbin/snmpd" snmpd_conf=$snmpd_home/etc/snmpd.conf [ -x $prog -a -f $snmpd_conf ] || exit 0 start() { # Start daemons. echo -n $"Starting $name: " $prog -c $snmpd_conf RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$name return $RETVAL } stop() { # Stop daemons. echo -n $"Shutting down $name: " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$name return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status $name RETVAL=$? ;; restart|reload) stop start RETVAL=$? ;; condrestart) if [ -f /var/lock/subsys/$name ]; then stop start RETVAL=$? fi ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" exit 1 esac exit $RETVAL
6.1、开启服务
[root@sms-v2 ~]# /etc/init.d/snmpd start [root@sms-v2 ~]# netstat -tunlp | grep snmp udp 0 0 0.0.0.0:161 0.0.0.0:* 123024/snmpd
7、防火墙开放端口,为了安全性,需要指定主机访问snmp端口
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.10.96/24" port port="161" protocol="udp" accept" firewall-cmd --reload
8、到监控端的服务器,测试161端口,是否可以获取数据,监控端也需要安装snmp软件,跟上面安装一样,只是使用一下工具,无需配置其它
#获取主机名 [root@filestore-v2 ~]# /usr/local/net-snmp-5.8/bin/snmpwalk -v 2c 192.168.10.95 -c public sysName.0 SNMPv2-MIB::sysName.0 = STRING: sms-v2 #获取CPU1分钟的负载 [root@filestore-v2 ~]# /usr/local/net-snmp-5.8/bin/snmpwalk -v 2c 192.168.10.95 -c public .1.3.6.1.4.1.2021.10.1.3.1 UCD-SNMP-MIB::laLoad.1 = STRING: 0.03 #获取已使用内存的大小 [root@filestore-v2 ~]# /usr/local/net-snmp-5.8/bin/snmpwalk -v 2c 192.168.10.95 -c public .1.3.6.1.4.1.2021.4.6.0 UCD-SNMP-MIB::memAvailReal.0 = INTEGER: 9094948 kB
Linux SNMP OID's for CPU, Memory and Disk Statistics SNMP stands for Simple Network Management Protocol and consists of three key components: managed devices, agents, and network-management systems (NMSs). A managed device is a node that has an SNMP agent and resides on a managed network. These devices can be routers and access server, switches and bridges, hubs, computer hosts, or printers. An agent is a software module residing within a device. This agent translates information into a compatible format with SNMP. An NMS runs monitoring applications. They provide the bulk of processing and memory resources required for network management. SNMP MIBs, OIDs Overview MIB stands for Management Information Base and is a collection of information organized hierarchically. These are accessed using a protocol such as SNMP. There are two types of MIBs: scalar and tabular. Scalar objects define a single object instance whereas tabular objects define multiple related object instances grouped in MIB tables. MIB files for specific devices or systems can be downloaded from here OIDs or Object Identifiers uniquely identify manged objects in a MIB hierarchy. This can be depicted as a tree, the levels of which are assigned by different organizations. Top level MIB object IDs (OIDs) belong to different standard organizations. Vendors define private branches including managed objects for their own products. Here is a sample structure of an OID Iso (1). Org (3). Dod (6). Internet (1). Private (4). Transition (868). Products (2). Chassis (4). Card (1). SlotCps (2) - ; .- CpsSlotSummary (1). CpsModuleTable (1). CpsModuleEntry (1). CpsModuleModel (3) .3562.3 Most of the people may be looking for OID's for Linux OID's for CPU, Memory and Disk Statistics for this first you need to install SNMP server and clients. If you want to install SNMP server and client installation in linux learning check here CPU Statistics Load 1 minute Load: .1.3.6.1.4.1.2021.10.1.3.1 5 minute Load: .1.3.6.1.4.1.2021.10.1.3.2 15 minute Load: .1.3.6.1.4.1.2021.10.1.3.3 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 Memory Statistics 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 Disk Statistics The snmpd.conf needs to be edited. Add the following (assuming a machine with a single '/' partition): disk / 100000 (or) includeAllDisks 10% for all partitions and disks The OIDs are as follows 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 System Uptime: .1.3.6.1.2.1.1.3.0 Examples These Commands you need to run on the SNMP server Get available disk space for / on the target host # Snmpget-v 1-c "community" target_name_or_ip .1.3.6.1.4.1.2021.9.1.7.1 this will return available disk space for the first entry in the 'disk' section of snmpd.conf; replace 1 with n for the nth entry Get the 1-minute system load on the target host # Snmpget-v 1-c "community" target_name_or_ip .1.3.6.1.4.1.2021.10.1.3.1 Get the 5-minute system load on the target host # Snmpget-v 1-c "community" target_name_or_ip .1.3.6.1.4.1.2021.10.1.3.2 Get the 15-minute system load on the target host # Snmpget-v 1-c "community" target_name_or_ip .1.3.6.1.4.1.2021.10.1.3.3 Get amount of available swap space on the target host # Snmpget-v 1-c "community" target_name_or_ip .1.3.6.1.4.1.2021.4.4.0
9、snmp测试通过后,开始配置与zabbix结合监控
10、在Zabbix配置监控主机的snmp端口和IP地址
11、新建一个snmp监控项(这里以CPU一分钟负载为例,如果需要snmp监控其它值,请自行更换)
12、配置保存后,请查是否成功
13、新一个画图
14、画图效果