zabbix中添加java/tomcat监控(JMX)

在 java/tomcat 应用中启动 JMX,为 zabbix 提供监控数据源

一、配置java/tomcat

tomcat

  • tomcat/bin/catalina.sh中位于Execute The Requested Command前添加如下内容

    tomcat/bin/catalina.sh
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ...
    CATALINA_OPTS="$CATALINA_OPTS
    -Dcom.sun.management.jmxremote
    -Djava.rmi.server.hostname=192.168.1.132
    -Dcom.sun.management.jmxremote.port=10053
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false"

    # ----- Execute The Requested Command -----------------------------------------
    ...
  • 也可以通过添加 tomcat/bin/setenv.sh 来写入相关配置

    tomcat/bin/setenv.sh
    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/sh

    export CATALINA_OPTS="$CATALINA_OPTS
    -Dcom.sun.management.jmxremote
    -Djava.rmi.server.hostname=192.168.1.132
    -Dcom.sun.management.jmxremote.port=10053
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false"

其中:

  • Djava.rmi.server.hostname: 为当前 tomcat 所在服务器的 IP
  • Dcom.sun.management.jmxremote.port: 开启监控的端口
  • Dcom.sun.management.jmxremote.ssl: false 表示不开启 SSL 协议
  • Dcom.sun.management.jmxremote.authenticate: false 表示不需要用户名和密码访问

添加后启动/重启 tomcat,待 tomcat 启动完成后本地查看端口

shell
1
2
# netstat -tnlp | grep 10053
tcp6 0 0 :::10053 :::* LISTEN 30543/java

jar

如果是以 java -jar sample.jar 包的方式直接直接运行,则在原有的命令基础上添加如下参数

shell
1
2
3
4
5
6
java -Dcom.sun.management.jmxremote \
-Djava.rmi.server.hostname=192.168.1.132 \
-Dcom.sun.management.jmxremote.port=10053 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-jar sample.jar

带密码验证访问

以 tomcat 为例,配置修改为

tomcat/bin/setenv.sh
1
2
3
4
5
6
7
8
9
10
#!/bin/sh

export CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.1.132
-Dcom.sun.management.jmxremote.port=10053
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.access.file=/data/tomcat8/jmx.access
-Dcom.sun.management.jmxremote.password.file=/data/tomcat8/jmx.password"

其中:

  • Dcom.sun.management.jmxremote.authenticate:改为 true

  • Dcom.sun.management.jmxremote.access.file:设置访问权限

    参考值:

    • readonly:只能读取 MBean 的属性和接受通知。

    • readwrite:还允许设置属性,调用方法,创建和删除 MBean

      /data/tomcat8/jmx.access
      1
      2
      akiya readonly
      admin readwrite
  • Dcom.sun.management.jmxremote.password.file:访问密码

    /data/tomcat8/jmx.password
    1
    2
    akiya akiya_password
    admin admin_password

创建完成上述文件后,修改文件权限

shell
1
chmod 400 /data/tomcat8/jmx.*

然后重启tomcat即可。

二、zabbix-java-gateway

安装

  • CentOS

    shell
    1
    yum -y install zabbix-java-gateway
  • ubuntu

    shell
    1
    apt -y install zabbix-java-gateway

安装完成后修改 zabbix-java-gateway 的配置文件 /etc/zabbix/zabbix_java_gateway.conf

/etc/zabbix/zabbix_java_gateway.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
# Mandatory: no
# Default:
LISTEN_IP="0.0.0.0"
...
# Mandatory: no
# Range: 1024-32767
# Default:
LISTEN_PORT=10052
...
# Mandatory: no
# Default:
# PID_FILE=

PID_FILE="/var/run/zabbix/zabbix_java_gateway.pid"
...
# Mandatory: no
# Range: 1-1000
# Default:
START_POLLERS=5
...

保存并退出,然后修改 zabbix 配置文件 /etc/zabbix/zabbix_server.conf

/etc/zabbix/zabbix_server.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
# Mandatory: no
# Default:
JavaGateway=192.168.1.142
...
# Mandatory: no
# Range: 1024-32767
# Default:
JavaGatewayPort=10052
...
# Mandatory: no
# Range: 0-1000
# Default:
# 指定Java轮询器的预分支实例数(pre-forked instances)
StartJavaPollers=5
...

重启相关服务:

shell
1
2
service zabbix-java-gateway restart
service zabbix-server restart

验证

zabbix-server 上通过 cmdline-jmxclient 验证

  1. 下载cmdline-jmxclient

    shell
    1
    wget http://crawler.archive.org/cmdline-jmxclient/cmdline-jmxclient-0.10.3.jar
  2. 测试

    • 无密码

      shell
      1
      2
      3
      4
      5
      6
      # java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.132:10053 java.lang:type=Memory NonHeapMemoryUsage
      01/09/2020 14:16:47 +0800 org.archive.jmx.Client NonHeapMemoryUsage:
      committed: 222887936
      init: 2555904
      max: 1593835520
      used: 211949024
    • 有密码

      shell
      1
      2
      3
      4
      5
      6
      # java -jar cmdline-jmxclient-0.10.3.jar akiya:akiya_password 192.168.1.132:10053 java.lang:type=Memory HeapMemoryUsage
      01/13/2020 14:47:10 +0800 org.archive.jmx.Client HeapMemoryUsage:
      committed: 2067267584
      init: 2147483648
      max: 2067267584
      used: 594706040

zabbix-web配置

  1. 在host中添加 JMX interfaces

  1. 添加 template

  1. 查看 hosts列表,可以发现已经显示有了 JMX

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...