Zabbix-(4)-Zabbix监控入门基础
续:
推荐阅读:
使用 Zabbix 监控 Linux 系统
在需要监控的 Linux 系统安装 zabbix agent,之后从 web 界面添加该主机,然后进行监控。
安装 zabbix agent
安装参考文档:
https://www.zabbix.com/cn/download?zabbix=4.0&os_distribution=ubuntu&os_version=18.04_bionic&db=mysql&ws=apache
Ubuntu
这里注意的是zabbix的server和agent的大版本号需要保持一致
在WEB服务器都需要安装源
机器172.20.32.106和机器172.20.32.107
这里以WEB-1为例
root@WEB-1:~# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
root@WEB-1:~# dpkg -i zabbix-release_4.0-3+bionic_all.deb
root@WEB-1:~# apt update
root@WEB-1:~# apt install zabbix-agent -y
配置 zabbix agent
root@WEB-1:~# vim /etc/zabbix/zabbix_agentd.conf
root@WEB-1:~# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0 #表示关闭日志收集大小功能
Server=172.20.32.101 # 对应的zabbix server地址,即需要接受谁的监控
ListenPort=10050 # agent接受指令监听的端口
ListenIP=0.0.0.0
StartAgents=3 # 默认的启动的实例数(进程数)
ServerActive=127.0.0.1
Hostname=172.20.32.106 # 区分大小写且在zabbix server看来是唯一的值 一般用IP来区分独一无二
Include=/etc/zabbix/zabbix_agentd.d/*.conf
重启 zabbix agent
root@WEB-1:~# systemctl restart zabbix-agent
root@WEB-1:~# systemctl enable zabbix-agent
Synchronizing state of zabbix-agent.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable zabbix-agent
root@WEB-1:~# ss -ntl|grep 10050
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
root@WEB-1:~# tail /var/log/zabbix/zabbix_agentd.log -n15
2541:20200430:112646.871 Got signal [signal:15(SIGTERM),sender_pid:3039,sender_uid:112,reason:0]. Exiting ...
2541:20200430:112646.872 Zabbix Agent stopped. Zabbix 4.0.20 (revision 3b651d1b0b).
3051:20200430:112646.895 Starting Zabbix Agent [172.20.32.106]. Zabbix 4.0.20 (revision 3b651d1b0b).
3051:20200430:112646.895 **** Enabled features ****
3051:20200430:112646.895 IPv6 support: YES
3051:20200430:112646.895 TLS support: YES
3051:20200430:112646.895 **************************
3051:20200430:112646.895 using configuration file: /etc/zabbix/zabbix_agentd.conf
3051:20200430:112646.895 agent #0 started [main process]
3054:20200430:112646.896 agent #2 started [listener #1]
3055:20200430:112646.898 agent #3 started [listener #2]
3053:20200430:112646.899 agent #1 started [collector]
3056:20200430:112646.901 agent #4 started [listener #3]
3057:20200430:112646.902 agent #5 started [active checks #1]
3057:20200430:112646.903 active check configuration update from [127.0.0.1:10051]
started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused)
root@WEB-1:~# systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-04-30 11:26:46 CST; 1min 51s ago
Main PID: 3051 (zabbix_agentd)
Tasks: 6 (limit: 1006)
CGroup: /system.slice/zabbix-agent.service
├─3051 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
├─3053 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─3054 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─3055 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─3056 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─3057 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Apr 30 11:26:46 WEB-1 systemd[1]: Stopped Zabbix Agent.
Apr 30 11:26:46 WEB-1 systemd[1]: Starting Zabbix Agent...
Apr 30 11:26:46 WEB-1 systemd[1]: zabbix-agent.service: Can't open PID file
/run/zabbix/zabbix_agentd.pid (yet?) after start: No such
Apr 30 11:26:46 WEB-1 systemd[1]: Started Zabbix Agent.
添加被监控主机
在 zabbix web 管理界面添加上一步安装了 zabbix agent 的 linux 主机
到这里可以添加成功,但是依旧使用不了,是因为还没有添加模板
关联模板
选择符合当前主机监控项目的模板,如对linux系统监控可以关联zabbix 自带的Template OS Linux模板,如下在搜索框输入linux关键字后会自带匹配符合名称的模板,直接点击即可选择:
选择模板
更新之后可以看到配置主机一栏已经成功添加WEB-1端的agent-node端
验证数据
经过一段时间的数据采集后,验证 zabbix server 是否有刚添加完成主机的监控数据和图形,图形显示如下:
验证主机监控数据
经过一段时间的数据采集后,验证zabbix server是否有刚添加完成主机的监控数据和图形,图形显示如下:
使用 Zabbix 监控 Tomcat
学习如何通过 java gateway 实现对 tomcat 的指标进行数据采集和图形展示,如堆栈内存利用率、当前会话连接数、繁忙线程等。
准备 JDK 环境
在机器172.20.32.106 WEB1中
root@WEB-1:~# cd /usr/local/src/
root@WEB-1:/usr/local/src# ll
total 189996
drwxr-xr-x 2 root root 4096 Apr 30 13:47 ./
drwxr-xr-x 10 root root 4096 Oct 26 2019 ../
-rw-r--r-- 1 root root 194545143 Apr 18 11:51 jdk-8u241-linux-x64.tar.gz
root@WEB-1:/usr/local/src# tar xf jdk-8u241-linux-x64.tar.gz
root@WEB-1:/usr/local/src# ln -sv /usr/local/src/jdk1.8.0_241/ /usr/local/jdk
'/usr/local/jdk' -> '/usr/local/src/jdk1.8.0_241/'
root@WEB-1:/usr/local/src# vim /etc/profile
...
export JAVA_HOME=/usr/local/jdk
export TOMCAT_HOME=/apps/tomcat
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
...
root@WEB-1:/usr/local/src# source /etc/profile
root@WEB-1:/usr/local/src# java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
准备 tomcat
安装 tomcat
root@WEB-1:/usr/local/src# rz -E
rz waiting to receive.
root@WEB-1:/usr/local/src# ll
total 200072
drwxr-xr-x 3 root root 4096 Apr 30 13:51 ./
drwxr-xr-x 10 root root 4096 Apr 30 13:48 ../
-rw-r--r-- 1 root root 10312541 Apr 18 11:46 apache-tomcat-8.5.54.tar.gz
drwxr-xr-x 7 10143 10143 4096 Dec 11 18:39 jdk1.8.0_241/
-rw-r--r-- 1 root root 194545143 Apr 18 11:51 jdk-8u241-linux-x64.tar.gz
root@WEB-1:/usr/local/src# tar xf apache-tomcat-8.5.54.tar.gz
root@WEB-1:/usr/local/src# ln -sv /usr/local/src/apache-tomcat-8.5.54 /usr/local/tomcat
'/usr/local/tomcat' -> '/usr/local/src/apache-tomcat-8.5.54'
root@WEB-1:/usr/local/src#
准备 jsp 页面
root@WEB-1:~# cd /usr/local/
root@WEB-1:/usr/local# mkdir tomcat/webapps/myapp -p
root@WEB-1:/usr/local# vim tomcat/webapps/myapp/index.html
root@WEB-1:/usr/local# cat tomcat/webapps/myapp/index.html
hello jsp for tomcat test!
启动 tomcat
root@WEB-1:/usr/local# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
访问 tomcat web 页面
部署 JAVA Gateway 服务器
java gateway 是一个独立于 zabbix server 和 zabbix agent 的组件,也就是java gateway 可以是单独的一台服务器,但是也可以和 zabbix server 或者zabbix agent 公用一台服务器,前提是端口不要配置冲突了。这里复用172.20.32.102 zabbix-proxy(主动)的服务器
JAVA Gateway部署准备
root@Zabbix-proxy-1:~# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
root@Zabbix-proxy-1:~# dpkg -i zabbix-release_4.0-3+bionic_all.deb
root@Zabbix-proxy-1:~# apt update
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
root@Zabbix-proxy-1:~# rm /var/lib/apt/lists/lock #之前有任务块被占着
root@Zabbix-proxy-1:~# apt update
root@Zabbix-proxy-1:~# apt-cache madison zabbix-java-gateway
zabbix-java-gateway | 1:4.0.20-1+bionic | http://repo.zabbix.com/zabbix/4.0/ubuntu bionic/main amd64 Packages
zabbix-java-gateway | 1:4.0.20-1+bionic | http://repo.zabbix.com/zabbix/4.0/ubuntu bionic/main i386 Packages
zabbix-java-gateway | 1:3.0.12+dfsg-1 | http://us.archive.ubuntu.com/ubuntu bionic/universe amd64 Packages
zabbix-java-gateway | 1:3.0.12+dfsg-1 | http://us.archive.ubuntu.com/ubuntu bionic/universe i386 Packages
zabbix | 1:4.0.20-1+bionic | http://repo.zabbix.com/zabbix/4.0/ubuntu bionic/main Sources
root@Zabbix-proxy-1:~# apt install zabbix-java-gateway -y
root@Zabbix-proxy-1:~# java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)
JAVA Gateway配置
root@Zabbix-proxy-1:~# vim /etc/zabbix/zabbix_java_gateway.conf
root@Zabbix-proxy-1:~# 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=10 #同时开启10个JAVA Gateway线程去后端收集数据 根据后端服务器来决定这个值的大小
TIMEOUT=30 #超时时间
启动JAVA Gateway
root@Zabbix-proxy-1:~# ll /lib/systemd/system/zabbix-java-gateway.service #查看是否有启动进程文件
-rw-r--r-- 1 root root 290 Nov 21 20:16 /lib/systemd/system/zabbix-java-gateway.service
root@Zabbix-proxy-1:~# systemctl start zabbix-java-gateway
root@Zabbix-proxy-1:~# systemctl enable zabbix-java-gateway
Synchronizing state of zabbix-java-gateway.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable zabbix-java-gateway
root@Zabbix-proxy-1:~# ss -ntl|grep 10052
LISTEN 0 50 *:10052 *:*
配置 zabbix server 调用 java gateway
修改 server 配置文件
在机器172.20.32.101
root@Zabbix-server:~# vim /apps/zabbix_server/etc/zabbix_server.conf
276行
### Option: JavaGateway
# IP address (or hostname) of Zabbix Java gateway.
# Only required if Java pollers are started.
#
# Mandatory: no
# Default:
# JavaGateway=
# Default:
# StartAlerters=3
### Option: JavaGateway
# IP address (or hostname) of Zabbix Java gateway.
# Only required if Java pollers are started.
#
# Mandatory: no
# Default:
JavaGateway=172.20.32.102 #JavaGateway服务器IP地址
### Option: JavaGatewayPort
# Port that Zabbix Java gateway listens on.
#
# Mandatory: no
# Range: 1024-32767
# Default:
JavaGatewayPort=10052
### Option: StartJavaPollers
# Number of pre-forked instances of Java pollers.
#
# Mandatory: no
# Range: 0-1000
# Default:
StartJavaPollers=20 #开启的线程个数
root@Zabbix-server:~# systemctl restart zabbix-server #重启 zabbix-server 服务
查看 java gateway 轮询器进程
root@Zabbix-server:~# ps -ef |grep java
zabbix 17076 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #1 [got 0 values in 0.000072 sec, idle 5 sec]
zabbix 17077 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #2 [got 0 values in 0.000043 sec, idle 5 sec]
zabbix 17078 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #3 [got 0 values in 0.000031 sec, idle 5 sec]
zabbix 17079 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #4 [got 0 values in 0.000025 sec, idle 5 sec]
zabbix 17080 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #5 [got 0 values in 0.000027 sec, idle 5 sec]
zabbix 17081 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #6 [got 0 values in 0.000043 sec, idle 5 sec]
zabbix 17082 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #7 [got 0 values in 0.000012 sec, idle 5 sec]
zabbix 17083 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #8 [got 0 values in 0.000006 sec, idle 5 sec]
zabbix 17084 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #9 [got 0 values in 0.000013 sec, idle 5 sec]
zabbix 17085 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #10 [got 0 values in 0.000029 sec, idle 5 sec]
zabbix 17086 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #11 [got 0 values in 0.000006 sec, idle 5 sec]
zabbix 17087 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #12 [got 0 values in 0.000027 sec, idle 5 sec]
zabbix 17088 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #13 [got 0 values in 0.000027 sec, idle 5 sec]
zabbix 17089 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #14 [got 0 values in 0.000006 sec, idle 5 sec]
zabbix 17090 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #15 [got 0 values in 0.000040 sec, idle 5 sec]
zabbix 17091 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #16 [got 0 values in 0.000026 sec, idle 5 sec]
zabbix 17092 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #17 [got 0 values in 0.000006 sec, idle 5 sec]
zabbix 17093 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #18 [got 0 values in 0.000005 sec, idle 5 sec]
zabbix 17094 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #19 [got 0 values in 0.000027 sec, idle 5 sec]
zabbix 17095 17062 0 15:08 ? 00:00:00 /apps/zabbix_server/sbin/zabbix_server: java poller #20 [got 0 values in 0.000006 sec, idle 5 sec]
root 17213 12003 0 15:09 pts/2 00:00:00 grep --color=auto java
root@Zabbix-server:~# ps -ef |grep java|wc -l
21
配置 tomcat 开启 JMX 监控
JMX 在 Java 编程语言中定义了应用程序以及网络管理和监控的体系结构、设计模式、应用程序接口以及服务,通常使用 JMX 来监控 JAVA 系统的运行状态。
什么是JMX?
所谓 JMX,是 Java Management Extensions(Java 管理扩展)的缩写,是一个为应用程序植入管理功能的框架。用户可以在任何 Java 应用程序中使用这些代理和服务实现管理。
修改启动参数
在机器172.20.32.106 WEB-1
root@WEB-1:~# vim /usr/local/tomcat/bin/catalina.sh
.....
116行
# added catalina_opts
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote # 启用远程监控JMX
-Dcom.sun.management.jmxremote.port=12345 # 默认启动的JMX端口号,要和zabbix添加主机时候的端口一致即可
-Dcom.sun.management.jmxremote.authenticate=false # 不使用用户名密码
-Dcom.sun.management.jmxremote.ssl=false # 不使用ssl认证
-Djava.rmi.server.hostname=x.x.x.x" # tomcat主机自己的IP地址,不要写zabbix服务器的地址
......
这里的tomcat本机机器为172.20.32.106
root@WEB-1:~# 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=172.20.32.106"
root@WEB-1:~# /usr/local/tomcat/bin/catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
root@WEB-1:~# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
root@WEB-1:~# ss -ntl|grep 8080
LISTEN 0 100 *:8080 *:*
root@WEB-1:~# ss -ntl|grep 12345
LISTEN 0 50 *:12345 *:*
通过 jconsole 验证 JMX 数据
Windows 版的 jdk 安装完成之后在安装目录的bin/目录下找到 jconsole.exe
比如 C:\Program Files\Java\jdk1.8.0_241\bin\jconsole.exe,如下:
主界面:
内存:
线程:
zabbix server 添加 JMX 监控
在机器172.20.32.101的前端web界面
zabbix server 关联模板
上图中JMX显示一定是要变成绿色的才表示已经添加关联成功。
验证查看JMX监控项
生产中 JMX 监控模板的使用
生产环境的 JMX 监控模板使用,添加了自定义监控项和阈值的监控模板。
选择 XML 格式的监控模板并导入
浏览要导入的模板:
勾选相应选项后导入:
关联导入的模板到 tomcat 服务器
将上一步骤导入的模板关联至被监控的 tomcat 服务器,然后取消关联并清理之前的JMX 模板,如下:
同时把Template App Generic Java JMX模板取消
最终主机模板:
验证是否有监控数据
使用命令测试 JMX 监控是否有效
这里需要一个cmdline-jmxclient-0.10.3.jar包。
root@Zabbix-server:~# java -jar cmdline-jmxclient-0.10.3.jar - 172.20.32.106:12345 'Catalina:name="http-nio-8080",type=ThreadPool' currentThreadCount
04/30/2020 17:04:56 +0800 org.archive.jmx.Client currentThreadCount: 10
root@Zabbix-server:~# java -jar cmdline-jmxclient-0.10.3.jar - 172.20.32.106:12345 'Catalina:name="http-nio-8080",type=ThreadPool' maxThreads
04/30/2020 17:05:51 +0800 org.archive.jmx.Client maxThreads: 200
推荐阅读: