Zabbix基于JMX监控中间件

本主题为通过zabbix监控中间件。

Zabbix 基于JMX agent(zabbix-java-gateway) 获取JVM MBean 信息。

Zabbix Agent安装

注意:如果您希望监控目标主机的操作系统相关指标,如CPU、内存、文件系统等信息,那么需要在目标主机安装 zabbix agent。

rpm包安装方式

1
rpm -ivh zabbix-agent-4.0.0-1.1rc2.el7.x86_64

源码包安装后打tar包介质–解压安装agent

1
2
cd /usr/local
tar -xvf zabbix_agent.tar.gz

Zabbix Agent配置

rpm安装配置

rpm安装方式配置文件路径为/etc/zabbix/zabbix_agentd.conf

1
2
3
4
LogFile=/tmp/zabbix_agentd.log  #日志文件路径
Server=192.168.100.1 #zabbix server 服务端地址
ServerActive=192.168.100.1 #zabbix server 服务端地址
Hostname=192.168.100.104 #被监控端主机ip

源码包安装后打tar包介质–配置

配置

源码包安装后打tar包介质-安装方式配置文件路径在解压目录,如:/usr/local/zabbix_agent/etc/zabbix_agentd.conf

1
2
3
4
LogFile=/tmp/zabbix_agentd.log  #日志文件路径
Server=192.168.100.1 #zabbix server 服务端地址
ServerActive=192.168.100.1 #zabbix server 服务端地址
Hostname=192.168.100.104 #被监控端主机ip

添加为服务与启停服务脚本

1
2
cd /etc/init.d/ 
vi zabbix_agentd

脚本内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh

# chkconfig: 2345 10 90
# description: myservice ....

# Zabbix
# Copyright (C) 2001-2018 Zabbix SIA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# Start/Stop the Zabbix agent daemon.
# Place a startup script in /sbin/init.d, and link to it from /sbin/rc[023].d
SERVICE="Zabbix agent"
DAEMON=/usr/local/zabbix_agent/sbin/zabbix_agentd
PIDFILE=/tmp/zabbix_agentd.pid

case $1 in
'start')
if [ -x ${DAEMON} ]
then
$DAEMON
# Error checking here would be good...
echo "${SERVICE} started."
else
echo "Can't find file ${DAEMON}."
echo "${SERVICE} NOT started."
fi
;;
'stop')
if [ -s ${PIDFILE} ]
then
if kill `cat ${PIDFILE}` >/dev/null 2>&1
then
echo "${SERVICE} terminated."
rm -f ${PIDFILE}
fi
fi
;;
'restart')
$0 stop
sleep 10
$0 start
;;
*)
echo "Usage: $0 start|stop|restart"
;;
esac

添加命令

1
2
3
chmod u+x zabbix_agentd 
chkconfig --add zabbix_agentd
chkconfig zabbix_agentd on

启停命令

1
2
3
/etc/init.d/zabbix_agentd start
/etc/init.d/zabbix_agentd stop
/etc/init.d/zabbix_agentd restart

Zabbix 服务端配置

请确保zabbix server已经配置java gateway,如果您已经配置,可略过此步骤。

配置文件配置

配置zabbix_java_gateway.conf

vi /etc/zabbix/zabbix_java_gateway.conf

1
2
3
4
5
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java.pid"
START_POLLERS=5
TIMEOUT=3

配置完成后就可以启动zabbix-server 和 zabbix-java-gateway

1
2
systemctl start zabbix-server.service
systemctl start zabbix-java-gateway.service

配置zabbix/zabbix_server.conf

修改zabbix-server的配置,编辑/etc/zabbix/zabbix_server.conf ,修改下面几个参数:

1
2
3
4
5
6
JavaGateway的服务器IP地址,一般与zabbix server同一台主机,可直接写zabbix  server地址
JavaGateway=XXX.XXX.XXX.XXX
JavaGateway的服务端口
JavaGatewayPort=10052
从javaGateway采集数据的进程数
StartJavaPollers=5

配置文件修改后,重启zabbix-server:

1
systemctl restart zabbix-server.service

被监控端JVM开启JMX

使用JMX前需要先开启JMX,默认是关闭的,在启动JAVA程序时,如何开始请参考官方文档
https://docs.oracle.com/javase/1.5.0/docs/guide/management/agent.html

下面介绍几种常用中间件JMX开启方式:

Tomcat

Linux

Tomcat 编辑TOMCAT_HOME/bin/catalina.sh 在开头加入如下几行

1
2
3
4
5
6
CATALINA_OPTS="${CATALINA_OPTS} -Djava.rmi.server.hostname=JMX_HOST" # 修改 JMX_HOST
CATALINA_OPTS="${CATALINA_OPTS} -Djavax.management.builder.initial="
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote=true"
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.port=JMX_PORT" # 修改JMS PORT
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"

注意:JMX_HOST为tomcat的主机名或IP地址,JMX_PORT为JMX端口,通常使用12345,然后重启tomcat,JMX就开启了。

Windows

1
2
3
4
5
6
set CATALINA_OPTS="${CATALINA_OPTS} -Djava.rmi.server.hostname=127.0.0.1"
set CATALINA_OPTS="${CATALINA_OPTS} -Djavax.management.builder.initial="
set CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote=true"
set CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.port=12345"
set CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
set CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"

Weblogic

Weblogic 编辑WL_DOMAIN_HOME/bin/setDomainEnv.cmd,在文件结尾加入下面几行

1
2
3
4
5
6
JAVA_OPTIONS="${JAVA_OPTIONS} -Djava.rmi.server.hostname=JMX_HOST"
JAVA_OPTIONS="${JAVA_OPTIONS} -Djavax.management.builder.initial=weblogic.management.jmx.mbeanserver.WLSMBeanServerBuilder"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote=true"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote.port=JMX_PORT"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote.ssl=false"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote.authenticate=false"

本地环境配置示例:

1
2
3
4
5
6
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote=true" 
JAVA_OPTIONS="${JAVA_OPTIONS} -Djava.rmi.server.hostname=192.168.100.1"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote.port=12345"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote.ssl=false"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPTIONS="${JAVA_OPTIONS} -Djavax.management.builder.initial=weblogic.management.jmx.mbeanserver.WLSMBeanServerBuilder"

IBM WebSphere

进入WebSphere Administrative Console open Servers → Server Types → WebSphere application servers → WAS_SERVER_NAME → Java and Process Management → Process definition → Java Virtual Machine.

在“Generic JVM arguments”增加下面环境变量:

1
-Djavax.management.builder.initial=

然后再进入 WebSphere Administrative Console, open Servers → Server Types → WebSphere application servers → WAS_SERVER_NAME → Java and Process Management → Process definition → Java Virtual Machine → Custom properties.

增加下面几个环境变量:

1
2
3
4
5
6
7
8
9
10
11
12
Name: java.rmi.server.hostname
Value: JMX_HOST
Name: javax.management.builder.initial
Value: none
Name: com.sun.management.jmxremote
Value: true
Name: com.sun.management.jmxremote.port
Value: JMX_PORT
Name: com.sun.management.jmxremote.ssl
Value: false
Name: com.sun.management.jmxremote.authenticate
Value: false

应用更改,重启应用就开启了。

Oracle GlassFish

进入GlassFish Console, open GAS_CONFIG → JVM Settings → JVM Options.
加入下面的环境变量到“JVM options”:

1
2
3
4
5
Value: -Djava.rmi.server.hostname=JMX_HOST
Value: -Dcom.sun.management.jmxremote=true
Value: -Dcom.sun.management.jmxremote.port=JMX_PORT
Value: -Dcom.sun.management.jmxremote.ssl=false
Value: -Dcom.sun.management.jmxremote.authenticate=false

重启就开启了。

JMX相关参数注意事项

-Djava.rmi.server.hostname=JMX_HOST

在多网卡主机上,启动weblogic,建议增加-Djava.rmi.server.hostname=JMX_HOST参数,-Djava.rmi.server.hostname= 被监控端Weblogic使用的网卡IP。否则使用jconsole或JRMC、jvisual工具连接时,会报出RMI(java.net.ConnectException: Connection refused: connect错误。

测试和查看JMX的配置和状态信息

基于jmxcmd.jar命令行方式测试

命令行参数建议使用jmxcmd.jar,下载地址如下:

https://sourceforge.net/projects/jmxcmd/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[tomcat@localhost tmp]$ java -jar jmxcmd.jar - 192.168.100.1:8098 > weblogic_mebean.txt
查看所有mbean

[tomcat@localhost tmp]$ java -jar jmxcmd.jar - 192.168.100.1:8098 java.lang:type=Memory NonHeapMemoryUsage
01/09/2019 10:46:18 +0800 de.layereight.jmxcmd.Client NonHeapMemoryUsage:
committed: 271056896
init: 270991360
max: 587202560
used: 92159504
查看非堆内存
[tomcat@localhost tmp]$
[tomcat@localhost tmp]$ java -jar jmxcmd.jar - 192.168.100.1:8098 com.bea:Name=ThreadPoolRuntime,ServerRuntime=srv_13_22_7003,Type=ThreadPoolRuntime QueueLength
01/09/2019 10:28:10 +0800 de.layereight.jmxcmd.Client QueueLength: 0
[tomcat@localhost tmp]$
查看srv_13_22_7003 的线程池队列长度
[tomcat@localhost tmp]$ java -jar jmxcmd.jar - 192.168.100.1:8098 com.bea:Name=ThreadPoolRuntime,ServerRuntime=srv_13_22_7003,Type=ThreadPoolRuntime HoggingThreadCount
01/09/2019 10:42:36 +0800 de.layereight.jmxcmd.Client HoggingThreadCount: 0
[tomcat@localhost tmp]$
查看srv_13_22_7003 的线程池Hogging线程数

基于Jconsole测试

1547015840014

Jconsole查看

1547016063506

基于JRMC测试

1547015878482

JRMC查看

1547016244429

Mbean

Weblogic Mbean在线文档:WebLogic Server® MBean Reference

https://docs.oracle.com/middleware/11119/wls/WLMBR/core/index.html

Tomcat Mbean在线文档

http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html

同一主机上监控多个Java进程Key健冲突问题

生产环境中我们经常使用同一台主机来搭建多个Tomcat或Weblogic节点对外提供服务,可是在监控主机时会碰到这样的问题,在被监控主机上使用模板,只能监控其中一个,如果想监控多个实例,我们只能通过添加多个模板来实现。这里提供两种解决方案:

对每个JVM进程当做一台主机

在zabbix 中建立两台主机,jmx接口不同。

缺点,主机监控数据

tomcat1:

1551322667467

tomcat2:

1551322730164

使用空格防止Key健冲突

在同一主机上,zabbix不允键值重复,但是监控的项目是一样的,不可能键值写的不重复,经过几番搜索,找到方法如下:

只要在箭头处添加1个空格就可以,也可以是多个。(注意位置不要错,在逗到后面)

1551773907058

错误方式:

1551323034173

欢迎关注米宝窝,持续更新中,谢谢!

[米宝窝 https://rocklei123.github.io/ ](

-------------本文结束感谢您的阅读-------------
欢迎持续关注米宝窝,定期更新谢谢! https://rocklei123.github.io/
欢迎持续关注我的CSDN https://blog.csdn.net/rocklei123
rocklei123的技术点滴
熬夜写博客挺辛苦的,生怕猝死,所以每当写博客都带着听诊器,心脏一有异响,随时按Ctrl+S。
rocklei123 微信支付

微信支付

rocklei123 支付宝

支付宝