2-zabbix5.0监控java

2-zabbix5.0监控java

介绍:Zabbix5.0

Zabbix-server 10.0.0.80  Ubuntu1804

Zabbix-agent  10.0.0.60-61 Rocky8.6

Zabbix-agent  10.0.0.10-12 Centos7.6

Zabbix-agent  10.0.0.40-42 Ubuntu2004

Zabbix-agent 10.0.0.81  Ubuntu1804

java-gatewa 10.0.0.82 Ubuntu1804


先来安装一台tomcat的机器在ubuntu1804-10.0.0.81(Centos7也可以)上面安装

先把这两个包上传到服务器里面和脚本放在同一个目录

我放在了/root/下

apache-tomcat-9.0.65.tar.gz  

jdk-8u341-linux-x64.tar.gz

# tar -xf jdk-8u341-linux-x64.tar.gz -C /usr/local/
# ln -s /usr/local/jdk1.8.0_341/ /usr/local/jdk

cat >  /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

# .  /etc/profile.d/jdk.sh
# tar -xf apache-tomcat-9.0.65.tar.gz -C /usr/local/
# ln -s /usr/local/apache-tomcat-9.0.65/ /usr/local/tomcat
# chown -R tomcat. /usr/local/tomcat/
	
cat > /lib/systemd/system/tomcat.service  <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target 

[Service]
Type=forking
EnvironmentFile=/usr/local/tomcatR/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat

[Install]
WantedBy=multi-user.target
EOF

# systemctl daemon-reload 
# systemctl enable tomcat --now


开启 JMX 监控

JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功

能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集

成的系统、网络和服务管理应用。

JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构、设计模式、应用程序接口以及

服务。通常使用JMX来监控系统的运行状态或管理系统的某些方面,比如清空缓存、重新加载配置文件

tomcat 开启远程 JMX 环境配置

vim /usr/local/tomcat/bin/catalina.sh
CATALINA_OPTS="$CATALINA__OPTS
-Dcom.sun.management.jmxremote         #启用远程监控JMX 
-Djava.rmi.server.hostname=<JAVA主机IP>        #是tomcat主机自己的IP地址,不是
zabbix服务器的地址
-Dcom.sun.management.jmxremote.port=XXXXX       #默认启动的JMX端口号,要和
zabbix添加主机时候的端口一致即可
-Dcom.sun.management.jmxremote.authenticate=false    #不使用用户名密码
-Dcom.sun.management.jmxremote.ssl=false"         #不使用ssl认证


范例: Tomcat 开启 JMX 功能

[root@tomcat ~]#vim /usr/local/tomcat/bin/catalina.sh
.......
# -----------------------------------------------------------------------------
#添加下一行
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.81"
# OS specific support. $var _must_ be set to either true or false.
###在os这一行上面添加如上的内容

#查看端口12345是否打开
[root@tomcat ~]#systemctl restart tomcat
[root@tomcat ~]#ss -ntl
State                Recv-Q                Send-Q                                      Local Address:Port                                  Peer Address:Port                
LISTEN               0                     128                                         127.0.0.53%lo:53                                         0.0.0.0:*                   
LISTEN               0                     128                                               0.0.0.0:22                                         0.0.0.0:*                   
LISTEN               0                     1                                      [::ffff:127.0.0.1]:8005                                             *:*                   
LISTEN               0                     50                                                      *:42123                                            *:*                   
LISTEN               0                     100                                                     *:8080                                             *:*                   
LISTEN               0                     128                                                  [::]:22                                            [::]:*                   
LISTEN               0                     50                                                      *:12345                                            *:*                   
LISTEN               0                     50                                                      *:37181                                            *:*


在java-gatewa 10.0.0.82 Ubuntu1804这台服务器上面安装java的gateway

部署 Java gateway 服务器

Java gateway 是一个独立于zabbix server和 zabbix agent的组件,默认使用端口10052/tcp, 所以java

gateway可以是单独的一台服务器,但是也可以和zabbix server或者zabbix agent公用一台服务器

Java gatway 可以用两种方法安装

包安装 ,编译安装


包安装 Java gateway

官网:配置zabbix的源

https://www.zabbix.com/download?zabbix=5.0&os_distribution=ubuntu&os_version=18.04_bionic&db=mysql&ws=apache


Ubuntu1804安装zabbix源

根据自己的系统去选择对应的安全包,我这里是ububtu1804

root@Java-gateway:~# wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+bionic_all.deb

root@Java-gateway:~# dpkg -i zabbix-release_5.0-1+bionic_all.deb

root@Java-gateway:~# apt update


Ubuntu1804安装java-gateway-10.0.0.82

apt -y install zabbix-java-gateway


Centsos7

官网:https://www.zabbix.com/cn/download?zabbix=5.0&os_distribution=centos&os_version=7&db=mysql&ws=apache

# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
# yum clean all
# yum install  zabbix-java-gateway -y


Centsos8 (rocky8)

官网:https://www.zabbix.com/cn/download?zabbix=5.0&os_distribution=centos&os_version=8_stream&db=mysql&ws=apache

# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm
# dnf clean all
# dnf install  zabbix-java-gateway -ys


设置为开机自启动

root@Java-gateway:~# systemctl enable zabbix-java-gateway --now


生产环境中如果需要监控的java比较多,可以修改一下zabbix-java-gateway的线程数,这样提高效率

比如我有10个java需要监控我就把START_POLLERS=5修改成10默认是5

root@Java-gateway:~# sed -i 's/^# START_POLLERS=5/START_POLLERS=10/' /etc/zabbix/zabbix_java_gateway.conf


修改好以后重启服务

root@Java-gateway:~# systemctl restart zabbix-java-gateway.service


查看端口号.10052就是iava_gateway的端口

root@Java-gateway:~# ss -ntl
State                 Recv-Q                 Send-Q                                  Local Address:Port                                   Peer Address:Port                 
LISTEN                0                      128                                     127.0.0.53%lo:53                                          0.0.0.0:*                    
LISTEN                0                      128                                           0.0.0.0:22                                          0.0.0.0:*                    
LISTEN                0                      128                                              [::]:22                                             [::]:*                    
LISTEN                0                      50                                                  *:10052                                             *:*


Zabbix Server 开启 Java gateway 功能

默认有关java的监控项是不支持的,如下图

image-20220723173950323.png


在zabbix-server服务上指定java-gateway的地址,通过javagateway找java的程序

修改java-gateway的主机ip地址
root@zabbix-server:~# sed -i.bak 's/^# JavaGateway=/JavaGateway=10.0.0.82/'  /etc/zabbix/zabbix_server.conf
root@zabbix-server:~# sed -i 's/# StartJavaPollers=0/StartJavaPollers=10/' /etc/zabbix/zabbix_server.conf
root@zabbix-server:~# sed -i 's/# JavaGatewayPort=10052/JavaGatewayPort=10052/' /etc/zabbix/zabbix_server.conf
JavaGateway=10.0.0.83   #指向JAVA gateway主机
StartJavaPollers=10 #指定开启的进程数,默认为0,即不开启,所以必须修改此行
JavaGatewayPort=10052    #指定端口,此为默认值,可不修改


重启zabbix-server

root@zabbix-server:~# systemctl restart zabbix-server.service


使用系统内置模版监控 tomcat 主机

如果 Java gateway 开启JMX功能,也可以直接监控 java gateway 自身

首先把tomcat这台机器添加的主机清单里

image-20220727093804657.png


因为我们不使用zabbix-agent监控10.0.0.81这台主机我们需要把默认的移除重新添加

image-20220727094251535.png


添加JMX主机,因为我们监控的是JAVA程序所以要用JMX

image-20220727094733369.png


全部填写号以后,选择模板

image-20220727094953438.png

image-20220727095010837.png


查看当前主机是否监控上.JMX绿色表示这台主机已经监控上

image-20220727095714712.png


监测10.0.0.81这台主机

image-20220727100008420.png


image-20220727100126067.png


查看图形

image-20220727100338185.png


image-20220727100415792.png


image-20220727100427809.png


Powered By Z-BlogPHP 1.7.3

 Theme By 优美模版

本博客为开源博客,本博客联系QQ:372097243