监控PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 1.修改php配置文件
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /php_status

# 2.修改nginx配置文件
[root@web01 conf.d]# vim webapp.conf
server{
listen 80;
server_name _;
location / {
root /code/webapp;
index index.html;
}
location /status {
stub_status on;
}
location ~ ^/(php_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
}
# 3.重启nginx和php
[root@web01 conf.d]# systemctl restart nginx php-fpm

image-20240807085016358

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pool: www
process manager: dynamic
start time: 02/Aug/2024:10:44:26 +0800
start since: 44
accepted conn: 1
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 4
active processes: 1
total processes: 5
max active processes: 1
max children reached: 0
slow requests: 0

#PHP-FPM状态解释:
pool #fpm池名称,大多数为www
process manager #进程管理方式dynamic或者static
start time #启动日志,如果reload了fpm,时间会更新
start since #运行时间
accepted conn #当前池接受的请求数
listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量
max listen queue #请求等待队列最高的数量
listen queue len #socket等待队列长度
idle processes #空闲进程数量
active processes #活跃进程数量
total processes #总进程数量
max active processes #最大的活跃进程数量(FPM启动开始计算)
max children reached #程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。

## 监控脚本
[root@web01 scripts]# cat php_status.sh
#!/bin/bash
PHPFPM_COMMAND=$1
PHPFPM_URL="http://172.16.1.7/php_status"
start_since(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^start since:/ {print $NF}'
}
accepted_conn(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^accepted conn:/ {print $NF}'
}
listen_queue(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^listen queue:/ {print $NF}'
}
max_listen_queue(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^max listen queue:/ {print $NF}'
}
listen_queue_len(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^listen queue len:/ {print $NF}'
}
idle_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^idle processes:/ {print $NF}'
}
active_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^active processes:/ {print $NF}'
}
total_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^total processes:/ {print $NF}'
}
max_active_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^max active processes:/ {print $NF}'
}
max_children_reached(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^max children reached:/ {print $NF}'
}
slow_requests(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^slow requests:/ {print $NF}'
}

case $PHPFPM_COMMAND in
start_since)
start_since;
;;
accepted_conn)
accepted_conn;
;;
listen_queue)
listen_queue;
;;
max_listen_queue)
max_listen_queue;
;;
listen_queue_len)
listen_queue_len;
;;
idle_processes)
idle_processes;
;;
active_processes)
active_processes;
;;
total_processes)
total_processes;
;;
max_active_processes)
max_active_processes;
;;
max_children_reached)
max_children_reached;
;;
slow_requests)
slow_requests;
;;
*)
echo $"USAGE:$0
{start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_p
rocesses|active_processes|total_processes|max_active_processes|max_children_reac
hed}"
esac

# 授权执行权限
[root@web01 scripts]# chmod +x php_status.sh

# 修改客户端配置文件
[root@web01 zabbix_agentd.d]# vim php_status.conf
UserParameter=php.status[*],/etc/zabbix/zabbix_agentd.d/scripts/php_status.sh $1

# 重启
[root@web01 zabbix_agentd.d]# systemctl restart zabbix-agent

监控JAVA进程JVM

在Zabbix中,JMX监控数据的获取由专门的代理程序来实现,即Zabbix-Java-Gateway来负责数据的采 集,Zabbix-Java-Gateway和JMX的Java程序之间通信获取数据

JMX在Zabbix中的运行流程

1
2
3
4
5
1.Zabbix-Server找Zabbix-Java-Gateway获取Java数据
2.Zabbix-Java-Gateway找Java程序(zabbix-agent)获取数据
3.Java程序返回数据给Zabbix-Java-Gateway
4.Zabbix-Java-Gateway返回数据给Zabbix-Server
5.Zabbix-Server进行数据展示

安装JAVA-gateway

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/
# 安装
[root@web02 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-java-gateway-5.0.42-1.el7.x86_64.rpm

# 启动java-gateway
[root@web02 ~]# systemctl start zabbix-java-gateway

#zabbix服务端
# 修改zabbix_server配置文件
[root@zabbix5-server ~]# vim /etc/zabbix/zabbix_server.conf
JavaGateway=172.16.1.8
JavaGatewayPort=10052
StartJavaPollers=5

# 重启zabbix-server
[root@zabbix5-server ~]# systemctl restart zabbix-server

tomcat开启JMX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 修改tomcat启动脚本
[root@web02 ~]# vim /usr/libexec/tomcat/server
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.16.1.8"

## 完整脚本
[root@web02 ~]# cat /usr/libexec/tomcat/server
#!/bin/bash

. /usr/libexec/tomcat/preamble

MAIN_CLASS=org.apache.catalina.startup.Bootstrap

FLAGS="$JAVA_OPTS"
OPTIONS="-Dcatalina.base=$CATALINA_BASE \
-Dcatalina.home=$CATALINA_HOME \
-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS \
-Djava.io.tmpdir=$CATALINA_TMPDIR \
-Djava.util.logging.config.file=${LOGGING_PROPERTIES} \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"

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.16.1.8"

if [ "$1" = "start" ] ; then
FLAGS="${FLAGS} $CATALINA_OPTS"
if [ "${SECURITY_MANAGER}" = "true" ] ; then
OPTIONS="${OPTIONS} \
-Djava.security.manager \
-Djava.security.policy==${CATALINA_BASE}/conf/catalina.policy"
fi
run start
elif [ "$1" = "stop" ] ; then
run stop
fi

# 重启tomcat
systemctl daemon-reload
systemctl restart tomcat

web界面设置-新建主机

image-20240807103619686

链接模板

1
搜jmx选择两个模板--添加

image-20240807102022640

image-20240807104224895