十二、Zabbix Low Lever Discovery功能

Low Lever Discovery是什么?看看zabbix官网的解释:java

官网地址:python

https://www.zabbix.com/documentation/2.2/manual/discovery/low_level_discoveryweb


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. Additionally it is possible to configure Zabbix to remove unneeded entities automatically based on actual results of periodically performed discovery.json

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

  • discovery of file systems;bash

  • discovery of network interfaces;ide

  • discovery of SNMP OIDs.测试


相比较于自动注册和自动发现,low-level discovery更底层点,用于发现item、trigger、graph等等。咱们最经常使用如:filesystem(如/、/home、/proc、C:、D:等),network(eth0,eth1等)阿里云


LLD监控,要求Key的返回值为JSON格式。例如:spa

{
  "data":[
  
  { "{#FSNAME}":"\/",                           "{#FSTYPE}":"rootfs"   },
  { "{#FSNAME}":"\/sys",                        "{#FSTYPE}":"sysfs"    },
  { "{#FSNAME}":"\/proc",                       "{#FSTYPE}":"proc"     },
  { "{#FSNAME}":"\/dev",                        "{#FSTYPE}":"devtmpfs" },
  { "{#FSNAME}":"\/dev\/pts",                   "{#FSTYPE}":"devpts"   },
  { "{#FSNAME}":"\/",                           "{#FSTYPE}":"ext3"     },
  { "{#FSNAME}":"\/lib\/init\/rw",              "{#FSTYPE}":"tmpfs"    },
  { "{#FSNAME}":"\/dev\/shm",                   "{#FSTYPE}":"tmpfs"    },
  { "{#FSNAME}":"\/home",                       "{#FSTYPE}":"ext3"     },
  { "{#FSNAME}":"\/tmp",                        "{#FSTYPE}":"ext3"     },
  { "{#FSNAME}":"\/usr",                        "{#FSTYPE}":"ext3"     },
  { "{#FSNAME}":"\/var",                        "{#FSTYPE}":"ext3"     },
  { "{#FSNAME}":"\/sys\/fs\/fuse\/connections", "{#FSTYPE}":"fusectl"  }
  
  ]
}

注意:定义Key的字符串只能为大写,不能为小写




LLD监控案例


背景:公司的某些阿里云主机上有多个java程序,都须要监控,zabbix自带的“Template JMX Generic”这个监控模板因为key相同的限制,只能监控一个java程序。不能知足需求。因此咱们须要利用LLD实现多JMX实例监控。


在作LLD监控以前,首先咱们要知道JMX监控项有哪些,如何获取这些监控项的数据?

监控项咱们参考zabbix自带的监控模板Template JMX Generic,要获取这些Item的数据可使用如下方式:


[root@localhost scripts]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:12345 | grep java.lang
java.lang:type=Compilation
java.lang:type=ClassLoading
java.lang:type=Memory
java.lang:name=Metaspace,type=MemoryPool
java.lang:name=Eden Space,type=MemoryPool
java.lang:type=Threading
java.lang:name=Survivor Space,type=MemoryPool
java.lang:name=Metaspace Manager,type=MemoryManager
java.lang:type=Runtime
java.lang:name=MarkSweepCompact,type=GarbageCollector
java.lang:name=Compressed Class Space,type=MemoryPool
java.lang:name=CodeCacheManager,type=MemoryManager
java.lang:name=Copy,type=GarbageCollector
java.lang:type=OperatingSystem
java.lang:name=Code Cache,type=MemoryPool
java.lang:name=Tenured Gen,type=MemoryPool


上面这些都是ObjectName,每个ObjectName都有对应的属性(Attributes)

[root@localhost scripts]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:12345 java.lang:type=Memory
Attributes:
 Verbose: Verbose (type=boolean)
 ObjectPendingFinalizationCount: ObjectPendingFinalizationCount (type=int)
 HeapMemoryUsage: HeapMemoryUsage (type=javax.management.openmbean.CompositeData)
 NonHeapMemoryUsage: NonHeapMemoryUsage (type=javax.management.openmbean.CompositeData)
 ObjectName: ObjectName (type=javax.management.ObjectName)
Operations:
 gc: gc
  Parameters 0, return type=void


若是要获取其中一个属性的值,能够用如下方式实现:

[root@localhost scripts]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:12345 java.lang:type=Memory HeapMemoryUsage
01/14/2016 05:33:14 +0800 org.archive.jmx.Client HeapMemoryUsage: 
committed: 31670272
init: 16777216
max: 249364480
used: 24961984


利用上面命令就能够获取堆内存的相关信息,而后经过脚本获取须要的信息便可。


在上面的命令中,咱们须要知道如下参数,

一、java程序开启的监控端口号

二、ObjectName

三、Attributes

四、附加信息,例如堆内存的committed、init、max、used

可是有些Attributes的值只有一个,例如:  下面查看到的线程的峰值信息,返回结果只有一个

[root@localhost scripts]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:12345 java.lang:type=Threading PeakThreadCount
01/14/2016 05:37:47 +0800 org.archive.jmx.Client PeakThreadCount: 31


因此咱们各类状况都须要考虑到,下面是监控脚本:

#!/usr/bin/env python
import commands
import sys
import os
java_com=commands.getoutput('/usr/bin/which java')
com="%s -jar /etc/zabbix/scripts/cmdline-jmxclient-0.10.3.jar - 127.0.0.1:%s '%s' %s" % (java_com,sys.argv[1],sys.argv[2].replace('+',','),sys.argv[3])
output=commands.getoutput(com)
output_list=output.split('\n')
for _ in output_list:
        if len(output_list) == 1:
                print _.split(': ')[-1]
        else:
                item = _.split(':')
                if len(item) > 1 and item[0] == sys.argv[4]:
                        print item[1].strip()



咱们要给脚本传递的3个或者4个参数。当输出结果为一行时,咱们传递三个参数,当输出结果有多行信息时,咱们传递4个参数

分别是监控端口号、ObjectName、Attributes、附加信息(used、max、committed等)


PS:测试时自动发现的脚本没有写,直接写在一个文档内,写成json格式

{“data":[{"{#PORT}":"12345"},{"{#PORT}":"23456"},{"{#PORT}":"34567"}]}


固然,在测试环境中个人主机上有三个tomcat,分别开启了这三个监控端口


客户端配置文件:

etc/zabbix/zabbix_agentd.d/userparameter_jmx.conf

UserParameter=jmx.port,cat /etc/zabbix/scripts/port.txt
UserParameter=jmx[*],/etc/zabbix/scripts/test2.py $1 "$2" $3 $4


注意:agent端的配置文件/etc/zabbix/zabbix_agentd.conf 开启如下设置:

AllowRoot=1             #以root身份启动zabbix—agent,才有权限执行监控命令
Timeout=30              #客户端超时时间,
UnsafeUserParameters=1


设置好以后须要重启zabbix-agent服务

[root@localhost zabbix_agentd.d]# /etc/init.d/zabbix-agent restart


zabbix-server web端配置

一、建立一个Template

wKioL1dfy7qyoEEEAAK0EaLFbtY368.png



二、建立discovery rule

注意key这里必定要参照这里写

UserParameter=jmx.port,cat /etc/zabbix/scripts/port.txt

UserParameter=jmx[*],/etc/zabbix/scripts/test2.py $1 "$2" $3 $4

wKiom1dfyt3x4VCXAAGOu0c-jjk425.png



三、建立监控项Item  prototypes

注意key的表达式jmx[$1,$2,$3,$4],数据类型必定要注意,单位也要注意,zabbix中默认的容量单位为B,时间单位为毫秒,这些单位不肯定的可参考zabbix自带的模板。包括监控的时间间隔。

wKiom1dfy1Xw0H_-AAKE6hbAGxw361.png




须要注意的问题:

一、key中的第二个参数,某些的ObjectName(例如:”java.lang:type=MemoryPool,name=CMS Old Gen")带有空格和逗号,致使没法获取到数据,后经查证发现这是一个bug,解决方法:

在脚本中对第二个参数使用replace方法,将web上填写的参数中的”+“号,替换成”,“号。另外在

UserParameter=jmx[*],/etc/zabbix/scripts/test2.py $1 "$2" $3 $4  中将$2用引号引发来。


二、不支持的监控项建议Disable掉,不然在Web端的Queue中会有不少队列堵住。