Zabbix Low-level discovery

2017.01.21

Low-level discovery provides a way to automatically create items, triggers, and graphs for different entities on a computer. For instance, Zabbix can automatically start monitoring file systems or network interfaces on your machine, without the need to create items for each file system or network interface manually.

In Zabbix, four types of item discovery are supported out of the box:

  • discovery of file systems;
  • discovery of network interfaces;
  • discovery of CPUs and CPU cores;
  • discovery of SNMP OIDs.

A user can define their own types of discovery, provided they follow a particular JSON protocol.

简而言之,zabbix的低水平发现就是减少我们的重复操作,能够根据不同的监控实例自动添加监控项、触发器和图形。 vfs.fs.discoverynet.if.discovery就是zabbix自带的自动发现键值,能够自动识别agent端的文件挂载情况和网卡信息。还有基于SNMP OID的自动发现键值,能够自动识别交换机的所有端口信息。 当我们的一个服务有多个端口需要监控时,我通常是先在agent端写好监控脚本,定义键值,然后再为每个端口新建监控项、触发器和图形(当然你可以直接克隆),但还是免不了一些重复操作。下面就通过一个例子看一下Low-level discovery是如何工作的:

监控需求:

监控udp端口12222-12229的端口状态

实现步骤:

1.以json格式自定义发现类型

$ cat udpport_discovery.py
#!/usr/bin/env python
import sys,datetime,socket

if __name__ == "__main__":
    zbxkey = sys.argv[1]
    zbx_d = {}
    zbx_d["data"] = []
    host_name = socket.gethostname().lower()

    res = ['12222', '12223', '12224', '12225', '12226', '12227', '12228', '12229']
    for line in res:
        d = {}
        d["{#%s}" % zbxkey] = line
        zbx_d["data"].append(d)
    print str(zbx_d).replace("'",'"')

2.定义配置文件

$ cat udp_port.conf
#############Template_Udp#############
UserParameter=status[*],/home/prod/softwares/zabbix/etc/zabbix_agentd.conf.d/udp_port.sh $1
UserParameter=discovery.udpport,/home/prod/softwares/zabbix/etc/zabbix_agentd.conf.d/udpport_discovery.py PORT

3.检测自定义键值

$ python udpport_discovery.py PORT
{"data": [{"{#PORT}": "12222"}, {"{#PORT}": "12223"}, {"{#PORT}": "12224"}, {"{#PORT}": "12225"}, {"{#PORT}": "12226"}, {"{#PORT}": "12227"}, {"{#PORT}": "12228"}, {"{#PORT}": "12229"}]}

zabbix server端测试:

$ ./zabbix_get -s 10.0.0.206 -k discovery.udpport
{"data": [{"{#PORT}": "12222"}, {"{#PORT}": "12223"}, {"{#PORT}": "12224"}, {"{#PORT}": "12225"}, {"{#PORT}": "12226"}, {"{#PORT}": "12227"}, {"{#PORT}": "12228"}, {"{#PORT}": "12229"}]}

4.编写监控脚本

$ cat udp_port.sh
Port=$1
case $Port in
        12222)   netstat -nupl | grep 12222 |wc -l;;
        12223)   netstat -nupl | grep 12223 |wc -l;;
        12224)   netstat -nupl | grep 12224 |wc -l;;
        12225)   netstat -nupl | grep 12225 |wc -l;;
        12226)   netstat -nupl | grep 12226 |wc -l;;
        12227)   netstat -nupl | grep 12227 |wc -l;;
        12228)   netstat -nupl | grep 12228 |wc -l;;
        12229)   netstat -nupl | grep 12229 |wc -l;;
        *) echo Error; exit 1;;
esac

5.web添加模板

6.将模板关联对应机器

参考

LDD官方文档