Use zabbix to monitor php-fpm service
1. Turn on php-fpm status monitoring
1.开启php-fpm状态监控
[root@192_168_81_220 ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /php_status
2.配置nginx连接php-fpm
[root@192_168_81_220 ~]# vim /etc/nginx/nginx.conf
location /php_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3.重启服务
[root@192_168_81_220 ~]# systemctl restart nginx
[root@192_168_81_220 ~]# systemctl restart php-fpm
2. Test the monitoring page and visit
[root@192_168_81_220 ~]# curl 127.0.0.1/php_status
pool: www
process manager: dynamic
start time: 02/Nov/2020:17:09:24 +0800
start since: 39
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
accepts 4 //How many requests has been received since nginx started
handled 4 //How many requests has been processed since nginx started
requests //Total number of http requests
Reading: 0 //Number of connection request headers currently read
Writing: 1 //Currently responding to the number of client requests
Waiting: 0 //Current number of waiting requests
The value of writing+waiting must be equal to the value of Active connections
3. Use zabbix to monitor php-fpm status value
Ideas:
1. First write the script to get the value, because it is a web page, it can be grabbed by the curl command
2. A function can be defined in the script. Each value corresponds to a function. Finally, use the case to determine that the $1 passed in as a parameter matches that function. If the match is correct, execute the function, and then define a $2 to display the access url
3. Define the monitoring item key in the form of passing parameters, so that you can define a key value and write a different $1.
3.1. Write a script to obtain monitoring values
Script writing idea: first save the data obtained by the curl command to a file, then define the variables of the current time and the file time, and compare them. If the current time is greater than 60s than the file time, delete the file of the curl command and re-curl Obtain the data and import it into the file, and finally through the function of defining various states, the function is mainly to obtain the state value through the file, and then judge which parameter to pass through the case, and finally execute the command of the corresponding parameter to obtain the monitoring value
Finally, to monitor whether the process is alive, you can check the process through ps and then get the number through wc -l, and finally echo this value, and then create a trigger. The latest value is equal to 0 to indicate that the process does not exist.
#!/bin/bash
#这是一个简单的监控php-fpm状态值的脚本
#20201103 ---jxl
comm_para=$1
PHP_URL=$2
cachefile=/tmp/php_status.txt
port=80
file_time=`stat -c %Y $cachefile`
now_time=`date +%s`
rm_file=$(($now_time-$file_time))
if [ -z $2 ];then
url=http://127.0.0.1:$port/php_status
else
url=$PHP_URL
fi
cmd="/usr/bin/curl $url"
if [ ! -e $cachefile ];then
$cmd > $cachefile 2>/dev/null
fi
if [ $rm_file -gt 60 ];then
rm -rf $cachefile
fi
if [ ! -f $cachefile ];then
$cmd > $cachefile 2>/dev/null
fi
start_since() {
#运行时长
cat $cachefile | awk '/since/{print $3}'
exit 0;
}
accepted_conn() {
cat $cachefile | awk '/accepted/{print $3}'
exit 0;
}
listen_queue(){
cat $cachefile | awk '{if(NR==6){print $3}}'
exit 0;
}
max_listen_queue(){
cat $cachefile | awk '{if(NR==7){print $4}}'
exit 0;
}
listen_queue_len() {
cat $cachefile | awk '{if(NR==8){print $4}}'
exit 0;
}
idle_processes() {
cat $cachefile | awk '/idle/{print $3}'
exit 0;
}
active_processes() {
cat $cachefile | awk '{if(NR==10){print $3}}'
exit 0;
}
total_processes() {
cat $cachefile | awk '{if(NR==11){print $3}}'
exit 0;
}
max_active_processes() {
cat $cachefile | awk '{if(NR==12){print $4}}'
exit 0;
}
max_children_reached() {
cat $cachefile | awk '{if(NR==13){print $4}}'
exit 0;
}
slow_requests() {
cat $cachefile | awk '{if(NR==14){print $3}}'
exit 0;
}
check() {
php_pro_count=`ps aux | grep php | grep -v grep | grep -v php-fpm_status.sh | wc -l`
echo $php_pro_count
}
case "$comm_para" 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
;;
check)
check
;;
*)
echo "invalid status"
exit 2;
esac
3.2. Write a custom monitoring item configuration file
[root@192_168_81_220 ~]# cat /etc/zabbix/zabbix_agentd.d/php-fpm.conf
UserParameter=php_status[*],/bin/bash /etc/zabbix/scripts/php-fpm_status.sh $1 $2
[root@192_168_81_220 ~]# systemctl restart zabbix-agent
zabbix-server测试
[root@zabbix-server ~]# zabbix_get -s 192.168.81.220 -k php_status[max_active_processes,http://192.168.81.220/php_status]
1
3.3. Create a monitoring template
3.3.1. Click to create a template
Setting name: php-fpm status
3.3.2. Set Macro
This macro is actually where zabbix sets variables, and the macro should correspond to the php_url in the script
Macro: {$PHP_URL} Value: http://127.0.0.1/php_status
3.3.3. Create Application Set
3.3.4. Create monitoring items
Name: start_since running time
Key value: php_status[start_since,{$PHP_URL}]
Update interval: 60s, because the judgment time in the script is 60s
Application set selection: php-fpm status
The configuration of other monitoring items is the same, but the parameter key value is different
Key value of all monitoring items
php_status[start_since,{$PHP_URL}]
php_status[accepted_conn,{$PHP_URL}]
php_status[listen_queue,{$PHP_URL}]
php_status[max_listen_queue,{$PHP_URL}]
php_status[listen_queue_len,{$PHP_URL}]
php_status[idle_processes,{$PHP_URL}]
php_status[active_processes,{$PHP_URL}]
php_status[total_processes,{$PHP_URL}]
php_status[active_processes,{$PHP_URL}]
php_status[max_active_processes,{$PHP_URL}]
php_status[max_children_reached,{$PHP_URL}]
php_status[slow_requests,{$PHP_URL}]
php_status[check,{$PHP_URL}]
3.3.5. Create trigger
Mainly make a trigger for the php-fpm process
expression
{php-fpm status:php_status[check,{$PHP_URL}].last()}=0
3.3.6. Create graphics
The graphics are added
3.3.7. Template creation completed
4. Monitoring host application php-fpm monitoring template
Configuration—Host—Template—Select—Add—Update
Template link succeeded
5. View the latest data
Monitoring-latest data-select host-application set
6. Trigger the existence of the php-fpm process and alert
[root@192_168_81_220 ~]# systemctl stop php-fpm
[root@192_168_81_220 ~]# ps aux | grep php | grep -v grep | grep -v php_status.sh | wc -l
0
进程数已经是0,坐等监控报警
Dashboard display
Alarm SMS
7. Use grafana to generate php-fpm status monitoring graphics
7.1. Create graphics
Click to create graphics
Select bar chart
7.2. Add monitoring items
Select the time period within 5 minutes
Do not add the monitoring item of running time to this graph, because it will be infinite
7.3. Set the name of the graph
Third icon
7.4. Save graphics
7.5. View graphics
View the graph of the last 15 minutes