当前位置 : 主页 > 操作系统 > centos >

实战案例:Zabbix对Tomcat的监控

来源:互联网 收集:自由互联 发布时间:2022-06-20
Zabbix通过java gateway 及 JMX 对Tomcat 进行监控,并实现自定义模板的导入及应用,模拟实际生产中的Tomacat性能等参数的图形监测显示。 1. Tomcat 服务器的准备 通过脚本完成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 root 11915655 Apr 8 20:53 apache-tomcat-10.0.20.tar.gz
-rwxr-xr-x 1 root root 3013 May 9 15:36 install_tomcat2022.sh
-rw-r--r-- 1 root root 168679847 Apr 8 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_COL}G"
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
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 37771 zabbix 13u IPv6 72370 0t0 TCP *:10052 (LISTEN)
[root@CentOS84-IP118 ]#ss -tln | grep 10052
LISTEN 0 50 *: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
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
[root@CentOS84-IP18 ]#

#### 重启zabbix-server
[root@CentOS84-IP18 ]#systemctl restart zabbix-server

#### 验证Java Pollers
[root@CentOS84-IP18 ]#ps -ef | grep java
#### 见下图


实战案例:Zabbix对Tomcat的监控_Zabbix对Tomcat的监控


4. Tomcat开启JMX监控

4.1 配置 Tomcat服务器开启JMX监控

JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构、设计模式、应用程序接口以及服务,通常使用JMX来监控系统的运行状态。

#### tomcat开启JMX监控:
[root@CentOS84-IP78 ]#vim /usr/local/apache-tomcat-10.0.20/bin/catalina.sh
# 搜索 Execute The Requested Command ,在下方添加以下内容
# ----- Execute The Requested Command -----------------------
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=192.168.250.78"
# Bugzilla 37848: only output this if we have a TTY

########################################################################
#### 上面的文件写成五行重启tomcat报错,写成一行就没问题
[root@CentOS84-IP78 ]#systemctl restart tomcat
[root@CentOS84-IP78 ]#systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2022-05-09 16:52:58 CST; 2s ago
Process: 3292 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
Process: 3324 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 3353 (code=exited, status=127)

May 09 16:52:58 CentOS84-IP78 systemd[1]: Starting Tomcat...
May 09 16:52:58 CentOS84-IP78 systemd[1]: Started Tomcat.
May 09 16:52:58 CentOS84-IP78 systemd[1]: tomcat.service: Main process exited, code=exited, status=127/n/a
May 09 16:52:58 CentOS84-IP78 systemd[1]: tomcat.service: Failed with result 'exit-code'.
[root@CentOS84-IP78 ]#

########################################################################
# ----- Execute The Requested Command -----------------------------------------
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.manage
ment.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.250.78"

# Bugzilla 37848: only output this if we have a TTY

#### 上面的文件写成五行重启tomcat报错,写成一行就没问题,服务可以正常启动
[root@CentOS84-IP78 ]#systemctl restart tomcat
[root@CentOS84-IP78 ]#systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-05-09 16:55:02 CST; 3s ago
Process: 3292 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
Process: 3387 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 3395 (java)
Tasks: 39 (limit: 23544)
Memory: 118.7M
CGroup: /system.slice/tomcat.service
└─3395 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -D>

May 09 16:55:02 CentOS84-IP78 systemd[1]: Starting Tomcat...
May 09 16:55:02 CentOS84-IP78 systemd[1]: Started Tomcat.

[2]+ Stopped systemctl status tomcat
[root@CentOS84-IP78 ]#

[root@CentOS84-IP78 ]#netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 224 192.168.250.78:22 192.168.250.254:1258 ESTABLISHED
tcp6 0 0 :::32346 :::* LISTEN
tcp6 0 0 127.0.0.1:8005 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 :::33556 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 :::12345 :::* LISTEN
[root@CentOS84-IP78 ]#

4.2 通过jconsole验证JMX数据

## 安装win版本的jdk环境后,在下面的目录可以看到这个可执行文件,运行后出现界面并建立连接。
C:\Program Files\Java\jdk1.8.0_333\bin\jvisualvm


实战案例:Zabbix对Tomcat的监控_java_02


5. zabbix server添加JMX监控

进入Zabbix-Server 服务器的管理页面进行如下配置

实战案例:Zabbix对Tomcat的监控_java_03

实战案例:Zabbix对Tomcat的监控_centos_04

实战案例:Zabbix对Tomcat的监控_Zabbix对Tomcat的监控_05


6. 配置Zabbix对Tomcat主机的监控

6.1 对Tomcat主机配置abbix-agent

## 对Tomcat主机配置abbix-agent,实现对主机资源等监控
[root@CentOS84-IP78 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 50 *:32346 *:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 50 *:33556 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 50 *:12345 *:*
[root@CentOS84-IP78 ]#

[root@CentOS84-IP78 ]#netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 320 192.168.250.78:22 192.168.250.254:1258 ESTABLISHED
tcp6 0 0 :::32346 :::* LISTEN
tcp6 0 0 127.0.0.1:8005 :::* LISTEN
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 :::33556 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::12345 :::* LISTEN
tcp6 0 0 192.168.250.78:32346 192.168.250.250:61221 ESTABLISHED
tcp6 0 0 192.168.250.78:32346 192.168.250.250:61211 ESTABLISHED
tcp6 0 0 192.168.250.78:12345 192.168.250.250:61209 ESTABLISHED #jvisualvm的连接
[root@CentOS84-IP78 ]#

## 安装IP78上针对操作系统的监控agent
[root@CentOS84-IP78 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
warning: /var/tmp/rpm-tmp.sCLmF4: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-6.0-1.el8 ################################# [100%]
[root@CentOS84-IP78 ]#dnf clean all
33 files removed
[root@CentOS84-IP78 ]#dnf -y install zabbix-agent
[root@CentOS84-IP78 ]#systemctl enable --now zabbix-agent


[root@CentOS84-IP78 ]#ss -tln |grep 10050
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:10050 [::]:*
[root@CentOS84-IP78 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@CentOS84-IP78 ]#

## 修改并重新配置好agent的配置文件了
[root@CentOS84-IP78 ]#vim /etc/zabbix/zabbix_agentd.conf
[root@CentOS84-IP78 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.250.18
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=3
ServerActive=127.0.0.1
Hostname=192.168.250.78
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@CentOS84-IP78 ]#

[root@CentOS84-IP78 ]#systemctl restart zabbix-agent
[root@CentOS84-IP78 ]#systemctl status zabbix-agent

6.2 在Zabbix上配置对Tomcat主机的资源监控

实战案例:Zabbix对Tomcat的监控_Zabbix对Tomcat的监控_06

实战案例:Zabbix对Tomcat的监控_tomcat_07

导入自定义Tomcat监控模板

实战案例:Zabbix对Tomcat的监控_java_08

实战案例:Zabbix对Tomcat的监控_Zabbix对Tomcat的监控_09

实战案例:Zabbix对Tomcat的监控_centos_10


7. Zabbix监控页面展示

实战案例:Zabbix对Tomcat的监控_tomcat_11

上一篇:Linux目录和文件权限
下一篇:没有了
网友评论