Unified nginx monitoring for zabbix applications

Source: Internet
Author: User

"Unified nginx monitoring" means to centrally monitor the status of all nginx sites on a Linux server.We call this server "nginx monitoring server". It can be a zabbix server with zabbix agent or a monitored client with other agents installed.

Idea: Write the URLs of all nginx sites to a configuration file on the "nginx monitoring server. To add or delete nginx monitoring sites, you only need to modify this configuration file and use zabbix's low-level dislevel to dynamically monitor these sites. The "nginx monitoring server" zabbix-agent calls the nginx monitoring script. After obtaining the nginx_status, zabbix-sender is used to send the monitoring data to the zabbix server.

The configuration steps are as follows:

1. Configure the stub_status module on the monitored nginx site

Enable the stub_status module monitoring and add location to the server block.

Assume that the IP address of the "nginx monitoring server" is 192.168.0.1. The configuration is as follows:

Location/nginx_status {stub_status on; access_log off; allow 127.0.0.1; # Allow "nginx monitoring server" to access nginx status allow 192.168.0.1; deny all ;}

Restart nginx and request http: // domain/nginx_status. The result is as follows:

Active connections: 1 server accepts handled requests 16672 16672 16769 Reading: 0 Writing: 1 Waiting: 0

2. Configure the monitoring script on the nginx Monitoring Server

First, install zabbix-agent and zabbix-sender on the nginx monitoring server. We recommend that you install yum.

Note: set timeout = 30 in zabbix_agentd.conf. Reduce the situation where the agent cannot obtain data because the script execution times out.

Then, create the directory of the script file/etc/zabbix/monitor_scripts。

shell# mkdir -p /etc/zabbix/monitor_scripts

Put the monitoring script nginx_monitor.sh/etc/zabbix/monitor_scriptsDirectory.

The content of the nginx_monitor.sh script is as follows. You need to modify the value of the agent_conf variable:

#! /Bin/bash # filename: nginx_monitor.sh # revision: 1.0 # Date: 2014/09/24 # Author: qicheng # Email: # Website: http://qicheng0211.blog.51cto.com # Description: nginx unified monitoring script # Notes: # change the value of agent_conf to the configuration file path of the local zabbix agent: agent_conf = "/etc/zabbix/ingress" # nginx_site_conf = "/etc/zabbix/monitor_scripts /nginx_site.conf "# zabbix_sender path zbx_sender ="/usr/bin/zabbix_sender "function = $ 1host_name = $ 2nginx_site = $3 curl ="/usr/bin/curl "timeout = 30 # nginx site low-level discoveryfunction nginxsitediscovery () {nginx_site = ($ (grep '^ [^ #]' $ {nginx_site_conf })) max_index = $ [$ {# nginx_site [@]}-1] printf' {\ n 'printf' \ t "data ": ['for key in 'seq-S' 0 $ max_index 'do printf' \ n \ t {'printf "\" {# nginx_site }\": \ "$ {nginx_site [$ {key}]} \"} "If [$ key-ne $ max_index]; then printf ", "fi done printf' \ n \ t] \ n' printf'} \ n'} # obtain the nginx status and send the data to zabbix serverfunction getnginxstatus () {nginx_status_url = "$ {nginx_site}/nginx_status" # After obtaining nginx_status, save it to the following file: nginx_status_file = "/tmp/nginx_status _ $ (echo $ {nginx_site} | SED's # ^ HTTP. *: // ###; s #/#_ # G '). log ":> "$ nginx_status_file" # curl: Obtain nginx_status $ {curl}-s -- connect-Timeout $ {timeout} "$ nginx_status_url" 2> & 1 | Tee "$ nginx_status_file" line_num = $ (cat "$ nginx_status_file" | WC-l) # determine if nginx_status is correctly obtained [$ line_num-ne 4] & {echo "error: $ nginx_status_file is not correct. "; Exit 1;} active = $ (Cat" $ nginx_status_file "| grep 'active' | awk '{print $ NF }') reading = $ (Cat "$ nginx_status_file" | grep 'reading' | awk '{print $2 }') writing = $ (Cat "$ nginx_status_file" | grep 'writing' | awk '{print $4 }') waiting = $ (Cat "$ nginx_status_file" | grep 'waiting' | awk '{print $6 }') accepts = $ (Cat "$ nginx_status_file" | awk Nr = 3 | awk '{print $1 }') handled = $ (Cat "$ nginx_status_file" | awk Nr = 3 | awk '{print $2 }') requests = $ (Cat "$ nginx_status_file" | awk Nr = 3 | awk '{print $3}') echo "sending the data to zabbix server... "# send data in a specific format to zabbix server. The format of each line is: 

Create nginx site configuration file nginx_site.conf:

shell# touch /etc/zabbix/monitor_scripts/nginx_site.confshell# chown -R zabbix:zabbix /etc/zabbix/monitor_scripts

Write the URLs of all monitored nginx sites to the nginx_site.conf file. Each line has a URL, such:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4A/15/wKiom1QiRf2RCGflAACIA9vwXvo605.jpg "Title =" conf.png "alt =" wkiom1qirf2rcgflaacia9vwxvo605.jpg "/>

Add custom parameters at the end of zabbix_agentd.conf:

UserParameter=nginxSiteDiscovery,bash /etc/zabbix/monitor_scripts/nginx_monitor.sh nginxSiteDiscoveryUserParameter=getNginxStatus[*],bash /etc/zabbix/monitor_scripts/nginx_monitor.sh getNginxStatus "$1" "$2"

Restart zabbix agent.

Run the zabbix_get command on the zabbix server to test:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4A/17/wKioL1QiTd6TTqiMAAMEdIpr0fA635.jpg "Title =" qq20140924124624.png "alt =" wkiol1qitd6ttqimaamedipr0fa635.jpg "/>

As shown in, the environment has been deployed.

Iii. zabbix Web Client

1. Import a template

Import the template app nginx. XML template. the attachment is downloaded.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4A/17/wKioL1QiVXHx-YAdAAGcPqGMVhA261.jpg "Title =" import.png "alt =" wKioL1QiVXHx-YAdAAGcPqGMVhA261.jpg "/>

The template's item prototypes are as follows: 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4A/16/wKiom1QiXPPijFunAAQ_V-haHlU404.jpg "Title =" item.png "alt =" wKiom1QiXPPijFunAAQ_V-haHlU404.jpg "/>

The "Get nginx status of {# nginx_site}" Metric item is of the zabbix agent type and is used to call the script to obtain nginx_status. Other items are of the zabbix trapper type and are used to receive nginx_status from zabbix-sender.

A trigger is configured in the template. If nginx_status cannot be obtained, an alarm is triggered.

2. Create an nginx monitoring host and link the Template

In the "nginx monitoring host" configuration, enter the agent interfaces and link the template app nginx.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4A/17/wKioL1QiVdrSUqcTAAJOp49C1RI625.jpg "Title =" zhuji.png "alt =" wkiol1qivdrsuqctaajop49c1ri625.jpg "/>

3. Data presentation

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4A/17/wKioL1QiV5HQQZ06AAewCq9WxeA362.jpg "Title =" his.png "alt =" wkiol1qiv5hqqz06aaewcq9wxea362.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4A/18/wKioL1QiYXezJTIsAAanVXgwlIg508.jpg "Title =" qq20140924140936.png "alt =" wkiol1qiyxezjtisaaanvxgwlig508.jpg "/>

This article is from the "departure-Linux technology blog" blog, please be sure to keep this source http://qicheng0211.blog.51cto.com/3958621/1557695

Unified nginx monitoring for zabbix applications

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.