Monitor Nginx and PHP-FPM performance with Zabbix

Source: Internet
Author: User
Tags fpm

1.2 Software Installation Path

Zabbix is installed under the/usr/local/zabbix path, and its related configuration files and binaries are placed in it.
Nginx installed under/usr/local/nginx, its related configuration files and binary execution program are placed therein.
PHP is installed under/usr/local/php, and its related configuration files and binary execution programs are placed therein.
Please confirm that the service ports of Nginx and PHP-FPM are in listening state before proceeding to the following operation.

Second, the configuration Nginx status2.1 confirm that nginx status has been compiled into the package

Use Nginx-v to see if Nginx has compiled the module http_stub_status_module ' into the package:

[[email protected] zabbix_agentd.conf.d]# nginx -Vnginx version: nginx/1.10.3built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure arguments: --prefix=/usr/local/nginx-1.10.3 --with-http_ssl_module --with-http_stub_status_module --with-pcre   #出现with-http_stub_status_module说明已装载status模块
2.2 Enabling the Nginx status module
    • Modify the Nginx.conf file

In general, add the following location to the default host:

location /nginx_status {  stub_status on;  access_log off;  allow 127.0.0.1;  allow 192.168.249.0/24;  deny all;
    • View Nginx Status

After the Nginx configuration file is modified, use NGINX-T to test the configuration file for obvious logical and grammatical errors, and to overload the configuration file without problems.

nginx -tnginx -s reload

Then use the browser or Curl command to view the Nginx status:

Description of 2.3 nginx status value

Third, configure the status of PHP-FPM

The PHP-FPM comes with a page that queries its working status, which requires two steps to enable this feature.

3.1 Enable PHP-FPM Status page

Modify the php-fpm.conf file, remove the comment from the Status page, and rename it as needed. The operation is as follows:

vim /usr/local/php/etc/php-fpm.confpm.status_path = /php_fpm-status       #去掉了前面的;注释符,并更名为php_fpm-status
3.2 View PHP-FPM's status

After modifying php-fpm.conf, use the service php-fpm reload to reload the configuration file, and then view the status of PHP-FPM in the browser or using the Curl command. The operation is as follows:

[[email protected] zabbix_agentd.conf.d]# curl localhost/php_fpm-statuspool:                 wwwprocess manager:      dynamicstart time:           01/May/2017:15:14:23 +0800start since:          29536accepted conn:        5947listen queue:         0max listen queue:     3listen queue len:     128idle processes:       2active processes:     1total processes:      3max active processes: 3max children reached: 0slow requests:        0

PHP-FPM status can view summary information and details, more information than the summary information about each PHP-FPM process, and support a variety of format output, such as XML, HTML and JSON, by default, if the directive can be used:

Examples for summary status page:http://example.com/statushttp://example.com/status?jsonhttp://example.com/status?htmlhttp://example.com/status?xmlExample for detailed status page:http://example.com/status?fullhttp://example.com/status?json&fullhttp://example.com/status?html&fullhttp://example.com/status?xml&full

Which format to use to view status determines how the status value is subsequently monitored using Zabbix. This paper takes the output of the curl Localhost/php_fpm-status as an example.

Meaning of 3.3 php-fpm status

Iv. writing the Status Data extraction script

In the right place, it is generally/usr/local/zabbix/bin the script is used to extract the value of each field in the status.

4.1 Nginx's Status Value extraction script

This script is/usr/local/zabbix/bin/nginx_status.sh, which reads as follows:

#!/bin/bash#check nginx statusip=127.0.0.1function Ping () {#用于检测nginx进程是否存在/sbin/pidof n Ginx | Wc-l}function Active () {#用于提取status中的active数值/usr/bin/curl/http $ip/nginx_status 2>/dev/null | Sed-n ' 1p ' | awk ' {print $NF} '}function accepts () {#用于提取status中的accepts数值/usr/bin/curl/http $ip/nginx_status 2>/de V/null | Sed-n ' 3p ' | awk ' {print '} '}function handled () {#用于提取status中的handled数值/usr/bin/curl/http $ip/nginx_status 2>/dev/n ull | Sed-n ' 3p ' | awk ' {print $} '}function requests () {#用于提取status中的request数值/usr/bin/curl/http $ip/nginx_status 2>/dev/nu ll | Sed-n ' 3p ' | awk ' {print $} '}function reading () {#用于提取status中的reading数值/usr/bin/curl/http $ip/nginx_status 2>/dev/nul l | Sed-n ' 4p ' | awk ' {print $} '}function writing () {#用于提取status中的writing数值/usr/bin/curl/http $ip/nginx_status 2>/dev/nul l | Sed-n ' 4p ' | awk ' {print $4} '}function WAiting () {#用于提取status中的waiting数值/usr/bin/curl/http $ip/nginx_status 2>/dev/null | sed-n ' 4p ' | awk ' {PRINT   $6} '}$1 #通过第一个位置参数的值来调用相应的函数
4.2 PHP-FPM Status Value extraction script

The PHP-FPM status Value extraction script is/usr/local/zabbix/bin/php_fpm_status.sh and reads as follows:

#!/bin/bash#check php-fpm statuscase in ping) #检测php-fpm process exists/sbin/pidof PHP-FPM |    Wc-l;; start_since) #提取status中的start since value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |    awk ' Nr==4{print $ A} ';; CONN) #提取status中的accepted conn value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |    awk ' Nr==5{print $ A} ';; Listen_queue) #提取status中的listen Queue value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |     awk ' Nr==6{print $ A} ';; Max_listen_queue) #提取status中的max Listen queue value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |    awk ' Nr==7{print $4} ';; Listen_queue_len) #提取status中的listen queue Len/usr/bin/curl Localhost/php_fpm-status 2>/dev/null |    awk ' Nr==8{print $4} ';; idle_processes) #提取status中的idle processes value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |    awk ' Nr==9{print $ A} ';;   active_processes)#提取status中的active processes value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |     awk ' Nr==10{print $ A} ';; total_processes) #提取status中的total processess value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |    awk ' Nr==11{print $ A} ';; max_active_processes) #提取status中的max Active processes numeric/usr/bin/curl localhost/php_fpm-status 2>/dev/null |     awk ' Nr==12{print $4} ';; max_children_reached) #提取status中的max children reached value/usr/bin/curl localhost/php_fpm-status 2>/dev/null |    awk ' Nr==13{print $4} ';; slow_requests) #提取status中的slow requests value/usr/bin/curl Localhost/php_fpm-status 2>/dev/null |    awk ' Nr==14{print $ A} ';; *) echo "Usage: $ conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processess|total_ Processes|max_active_processes|max_children_reached|slow_requests} "exit 1;; Esac
V. Create a ZABBIX_AGENTD configuration file

Create a child profile in/usr/local/zabbix/etc/zabbix_agentd.d/that associates the Nginx status and PHP-FPM status, create one, or add it to an existing configuration file, Here I create a profile for each of them, and the file name is customizable, as long as you ensure that the entire contents of this folder are contained in zabbix_agentd.conf (include).

5.1 userparameter_nginx.conf
[[email protected] zabbix_agentd.conf.d]# cat userparameter_nginx.conf #/usr/local/zabbix/bin/nginx_status.shUserParameter=nginx.status[*],/usr/local/zabbix/bin/nginx_status.sh $1   #这种写法比较简洁,参考zabbix 3.2.4中自带的的userparameter_examples.confUserParameter=nginx.version,/usr/local/nginx/sbin/nginx -v     
5.2 userparameter_php-fpm.conf
[[email protected] zabbix_agentd.conf.d]# cat userparameter_php-fpm.conf #/usr/local/zabbix/bin/php_fpm_status.shUserParameter=php-fpm.status[*],/usr/local/zabbix/bin/php_fpm_status.sh $1UserParameter=php-fpm.version,/usr/local/php/sbin/php-fpm -v | awk ‘NR==1{print $0}‘   #获取php-fpm版本信息
5.3 Confirm that the sub-profile is included in the ZABBIX_AGENTD configuration file

Verify that the/usr/local/zabbix/etc/zabbix_agentd.conf contains: include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/and is not commented.

Six, restart ZABBIX_AGENTD service

Service Zabbix_agentd Restart let ZABBIX_AGENTD load the new configuration file.

Seven, test zabbix_get can fetch the data

Use the following command on Zabbix server to test whether you can get the Nginx and PHP-FPM status values.

[[email protected] zabbix_agentd.conf.d]# zabbix_get -s 127.0.0.1 -k ‘nginx.status[ping]‘      1[[email protected] zabbix_agentd.conf.d]# zabbix_get -s 127.0.0.1 -k ‘nginx.status[active]‘6[[email protected] zabbix_agentd.conf.d]# zabbix_get -s 127.0.0.1 -k ‘nginx.status[requests]‘  1841[[email protected] zabbix_agentd.conf.d]# zabbix_get -s 127.0.0.1 -k ‘php-fpm.status[ping]‘ 1[[email protected] zabbix_agentd.conf.d]# zabbix_get -s 127.0.0.1 -k ‘php-fpm.status[conn]‘1247

Attention:

    • The Zabbix_get directive runs on Zabbix server.
    • The userparameter_*.conf file is located on the Zabbix_agentd side.
    • Nginx_status.sh and php_fpm_status.sh are located on the Zabbix_agentd side.
    • Zabbix-get after-s specifies the IP for the zabbix-agentd of the listening IP.
Viii. Configure the appropriate template on the Zabbix server

The template here contains a series of items, trigger, graphs, and so on that are monitored for nginx status and PHP-FPM status. For the new content to monitor on the Zabbix server, the best way is to start from the template, and then configure the corresponding content in order, do not need to go online to find the existing template import, for the system monitoring on-demand configuration is the best. The following is the appropriate procedure.

8.1 Add Nginx Status Monitor template

New Nginx Status Template

Defining the Nginx status template

New Nginxapplication

Define Nginx status items, here I am using Passive Pro control

Add Nginx Status Code mapping

Nginx Trigger Definition

Defining Nginx Monitoring views

8.2 Add php-fpm Status Monitor template

The PHP-FPM status template is added in the same way as above, not in the following.

8.3 Select the corresponding template on the appropriate host or group association

To associate a template with a host or group

Nine, the actual monitoring effect display

Viewing the monitoring effect can be viewed monitoring-->latest data, filtering out the corresponding host and application name for viewing, the latest collection of the item's monitoring results, any monitoring to the data item will display the data, there is a gray representation of the data is not monitored, Need to troubleshoot the cause. As shown below:

Nginx's monitoring data

You can also view the monitoring effect by defining the graphs:

Nginx Statur Monitoring View

Ten, summary

To implement monitoring of user-defined item in Zabbix, the general process is as follows:

    • Enable the performance statistics feature for the corresponding software
    • Writing performance Statistics extraction scripts
    • Configuring a custom Userparameter conf file
    • Restart ZABBIX_AGENTD Service
    • Add the appropriate template on the Zabbix server Web
    • To invoke a template to a host or group

Monitor Nginx and PHP-FPM performance with Zabbix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.