12、Zabbix Low Lever Discovery功能

简介:

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

官网地址:

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


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.

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

  • discovery of file systems;

  • discovery of network interfaces;

  • discovery of SNMP OIDs.


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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
   "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的数据可以使用以下方式:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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)

1
2
3
4
5
6
7
8
9
10
[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


如果要获取其中一个属性的值,可以用以下方式实现:

1
2
3
4
5
6
[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


利用上面命令就可以获取堆内存的相关信息,然后通过脚本获取需要的信息即可。


在上面的命令中,我们需要知道以下参数,

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

2、ObjectName

3、Attributes

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

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

1
2
[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


所以我们各种情况都需要考虑到,下面是监控脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/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格式

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


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


客户端配置文件:

etc/zabbix/zabbix_agentd.d/userparameter_jmx.conf

1
2
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 开启以下设置:

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


设置好之后需要重启zabbix-agent服务

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


zabbix-server web端配置

1、创建一个Template

wKioL1dfy7qyoEEEAAK0EaLFbtY368.png



2、创建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



3、创建监控项Item  prototypes

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

wKiom1dfy1Xw0H_-AAKE6hbAGxw361.png




需要注意的问题:

1、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用引号引起来。


2、不支持的监控项建议Disable掉,否则在Web端的Queue中会有很多队列堵住。











本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/1789196,如需转载请自行联系原作者
目录
相关文章
|
Shell
zabbix discovery
preface(见面礼): 仅扫tcp端口: netstat -tnlp|egrep -i "$1" udp+tcp netstat -tunlp|egrep -i "$1" (服务器端口扫描,数据保存到shell array)   1 #!/bin/bash    2 p...
686 0
|
3月前
|
存储 SQL 监控
修改Zabbix源码实现监控数据双写,满足业务需求!
虽然对接Elasticsearch后有诸多好处,但是它不往数据库写历史数据了,同时还不再计算趋势数据了。有这么一个场景...
修改Zabbix源码实现监控数据双写,满足业务需求!