Zabbix对Tomcat的监控

一身转战三千里,一剑曾当百万师。这篇文章主要讲述Zabbix对Tomcat的监控相关的知识,希望能为你提供帮助。
【Zabbix对Tomcat的监控】Zabbix通过java gateway 及 JMX  对Tomcat 进行监控,并实现自定义模板的导入及应用,模拟实际生产中的Tomacat性能等参数的图形监测显示。
1.Tomcat 服务器的准备通过脚本完成Tomcat服务器的准备。

#### 上传相应的软件包和脚本,完成Tomcat的服务器准备
[root@CentOS84-IP78 ]#cd /data/
[root@CentOS84-IP78 ]#ll
total 176372
-rw-r--r-- 1 root root11915655 Apr8 20:53 apache-tomcat-10.0.20.tar.gz
-rwxr-xr-x 1 root root3013 May9 15:36 install_tomcat2022.sh
-rw-r--r-- 1 root root 168679847 Apr8 22:27 jdk-11.0.14_linux-x64_bin.tar.gz
[root@CentOS84-IP78 ]#

[root@CentOS84-IP78 ]#cat install_tomcat2022.sh

#!/bin/bash
#
#******************************************************************< strong>
#FileName:install_tomcat.sh
#Description:The test script
#Copyright (C):2021 All rights reserved
#< /strong> ******************************************************************

DIR=`pwd`
JDK_FILE="jdk-11.0.14_linux-x64_bin.tar.gz"
TOMCAT_FILE="apache-tomcat-10.0.20.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"

color ()
RES_COL=60
MOVE_TO_COL="echo -en \\\\033[$RES_COLG"
SETCOLOR_SUCCESS="echo -en \\\\033[1; 32m"
SETCOLOR_FAILURE="echo -en \\\\033[1; 31m"
SETCOLOR_WARNING="echo -en \\\\033[1; 33m"
SETCOLOR_NORMAL="echo -en \\E[0m"
echo -n "$2" & & $MOVE_TO_COL
echo -n "["
if [ $1 = "success" -o $1 = "0" ] ; then
$SETCOLOR_SUCCESS
echo -n $"OK"
elif [ $1 = "failure" -o $1 = "1"] ; then
$SETCOLOR_FAILURE
echo -n $"FAILED"
else
$SETCOLOR_WARNING
echo -n $"WARNING"
fi
$SETCOLOR_NORMAL
echo -n "]"
echo


install_jdk()
if ![-f "$DIR/$JDK_FILE" ]; then
color 1 "$JDK_FILE 文件不存在"
exit;
elif [ -d $JDK_DIR/jdk ]; then
color 1"JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE-C $JDK_DIR
cd$JDK_DIR & & ln -s jdk* jdk

cat > /etc/profile.d/jdk.sh < < EOF
export JAVA_HOME=$JDK_DIR/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
java -version & & color 0 "JDK 安装完成" ||color 1"JDK 安装失败" ; exit;



install_tomcat()
if ! [ -f "$DIR/$TOMCAT_FILE" ]; then
color 1 "$TOMCAT_FILE 文件不存在"
exit;
elif [ -d $TOMCAT_DIR/tomcat ]; then
color 1 "TOMCAT 已经安装"
exit
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd$TOMCAT_DIR & & ln -s apache-tomcat-*/tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"$PATH > /etc/profile.d/tomcat.sh
id tomcat & > /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf < < EOF
JAVA_HOME=$JDK_DIR/jdk
EOF

chown -R tomcat.tomcat $TOMCAT_DIR/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=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service & > /dev/null
systemctl is-active tomcat.service & > /dev/null & & color 0 "TOMCAT 安装完成" ||color 1 "TOMCAT 安装失败" ; exit;


install_jdk
install_tomcat

[root@CentOS84-IP78 ]#chmod +x install_tomcat2022.sh
[root@CentOS84-IP78 ]#./install_tomcat2022.sh
..................
java version "11.0.14" 2022-01-18 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.14+8-LTS-263)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.14+8-LTS-263, mixed mode)
JDK 安装完成[OK]
TOMCAT 安装完成[OK]
[root@CentOS84-IP78 ]#

#### 通过浏览器访问能看到网页
http://192.168.250.78:8080/

2.部署java gateway服务器Java gateway是一个独立于zabbix server和zabbix agent的组件,java gateway可以是单独一台服务器,也可以和zabbix server或者zabbix agent共用一台服务器,注意区分端口不冲突即可。本次使用一台单独的服务器,IP地址为192.168.250.118/24 。
#### 部署java gateway服务器
## 安装yum源及安装zabbix-java-gateway
[root@CentOS84-IP118 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
[root@CentOS84-IP118 ]#dnf -y install zabbix-java-gateway

## 修改并配置zabbix_java_gateway.conf
[root@CentOS84-IP118 ]#vim /etc/zabbix/zabbix_java_gateway.conf
[root@CentOS84-IP118 ]#grep "^[a-Z]" /etc/zabbix/zabbix_java_gateway.conf
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java_gateway.pid"
START_POLLERS=8
TIMEOUT=30
[root@CentOS84-IP118 ]#

## 启动并设定开机自启
[root@CentOS84-IP118 ]#systemctl restart zabbix-java-gateway
[root@CentOS84-IP118 ]#systemctl enable zabbix-java-gateway

## 验证安装
[root@CentOS84-IP118 ]#lsof -i:10052
COMMANDPIDUSERFDTYPE DEVICE SIZE/OFF NODE NAME
java37771 zabbix13uIPv6723700t0TCP *:10052 (LISTEN)
[root@CentOS84-IP118 ]#ss -tln | grep 10052
LISTEN 050*:10052*:*

3.配置Zabbix Server调用Java Gateway
#### 配置zabbix server调用java gateway
[root@CentOS84-IP18 ]#vim /etc/zabbix/zabbix_server.conf
[root@CentOS84-IP18 ]#grep "^[a-Z]" /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/run/zabbix/zabbix_server.pid
SocketDir=/run/zabbix
DBHost=192.168.250.38
DBName=zabbix
DBUser=zabbix
DBPassword=shone123456
DBPort=3306
#### 下面三行就是针对tomcat监控配置的命令行
JavaGateway=192.168.250.118
JavaGatewayPort=10052
StartJavaPollers=20

SNMPTrapperFile=/var/log/snmptrap/snmptrap.log

    推荐阅读