十二、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中會有不少隊列堵住。