Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx services are integrated into zabbix monitoring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx is useful for load balancing at work, so I'd like to try to monitor the Nginx service with zabbix.

Write down the steps in case you forget it next time.

The experimental environment involved:

Server operating system: CentOS 7.2

Zabbix version: zabbix-2.2.5

Nginx version: nginx-1.10.1

Before you do the monitoring step, check to see if the module of with-http_stub_status_module is loaded in the current Nginx.

# / data0/nginx/sbin/nginx-Vnginx version: nginx/1.10.1built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure arguments:-- prefix=/data0/nginx-- pid-path=/data0/nginx/logs/nginx.pid-- user=nginx-- group=nginx-- with-http_ssl_module-- with-http_flv_module-- with-http_stub_status_module -with-http_gzip_static_module-- http-client-body-temp-path=/var/tmp/nginx/client/-- http-proxy-temp-path=/var/tmp/nginx/proxy/-- http-fastcgi-temp-path=/var/tmp/nginx/fcgi/-- http-uwsgi-temp-path=/var/tmp/nginx/uwsgi-- http-scgi-temp-path=/var/tmp/nginx/scgi-- with-pcre-- add-module=/root/nginx_upstream _ check_module-master/-- add-module=/data0/soft/ngx_cache_purge-master/

This is because the zabbix grabs the data provided by the Status module according to the Stub Status module of nginx.

If you have not opened this module before, and now want to enable the StubStatus module, you can recompile the nginx source installation package, and then remember to add the parameter-with-http_stub_status_module, execute. / configure & & make at the time of compilation, without make install, if make install remembers to back up the configuration file nginx.conf, to prevent the contents of the configuration file from being lost.

Because I always load this module when I configure Nginx, I won't cover it any more.

To reload the nginx module, you can refer to my previous blog.

Http://10803382.blog.51cto.com/10793382/1924871

Under your nginx.conf confirmation, is there any of the following? if not, you can add to the nginx.conf to view the nginxstatus configuration and restart the nginx service.

# vim / data0/nginx/conf/nginx.conf## find the server module, and add the following content in server {}: # 20170627 Nginx monitoring settings location / nginxstatus {stub_status on; access_log / data0/nginx/logs/status.log; allow 10.60.0.71; # # allow zabbix monitoring host private network IP to access allow 10.60.0.163 # # allow nginx local private network IP to access deny all; auth_basic "nginxstatus";} # 20170627 END

Because the limited access to IP helps to protect the security of the URL

# / data0/nginx/sbin/nginx-tnginx: the configuration file / data0/nginx/conf/nginx.conf syntax is oknginx: configuration file / data0/nginx/conf/nginx.conf test is successful# / data0/nginx/sbin/nginx-s reload

Check the nginxstatus connection status you have set up

Http:// URL / nginxstatus

# curl http://127.0.0.1/nginxstatus301 Moved Permanently301 Moved Permanentlynginx/1.10.1

You will notice an error indicating that the 301 website has been set for permanent redirection

If you and I are in the same situation, the URL is https, then please use the URL on the nginx host and zabbix monitoring host to access it.

# curl https://www.sss.com/nginxstatusActive connections: 9 server accepts handled requests 1453770 1453770 2718730 Reading: 0 Writing: 4 Waiting: 5

Note:

Activeconnections: the number of active connections initiated to the backend

Server accepts: nginx handled a total of 1453770 connections

Handled: successfully created 1453770 handshakes

Requests: a total of 2718730 requests were processed.

Number of header for Reading:nginx read client

Writing: the number of header returned by nginx to the client

Waiting: the nginx request processing is complete and is waiting for the connection of the next request instruction.

After the above preparations are done, we will begin to edit a script on the nginx machine that allows the zabbix server to get the data.

Note: because my website has done https, so it is not like others to directly access the IP and port, I can directly access the URL

# vim / data0Zabixserver scripts HOST=www.sss.comexport HOST# Function used to check the process of nginxfunction ping nginxcards status.shemake binbinbinpool bash # Script is userd to fetch nginx statuses for zabbix monitoring systems# Set VariablesBKUP_DATE= `/ bin/date +% Y% m% d`LOG = "/ data0/zabbix/log/webstatus.log" HOST=www.sss.comexport HOST# Function used to check the process of nginxfunction ping {ps-ef | grep nginx | wc-l} # Functions to return nginx statsfunction active {/ usr/bin/curl "status2 > / dev/null | grep 'Active' | | awk'{print $NF}'} function reading {/ usr/bin/curl "https://$HOST/nginxstatus" 2 > / dev/null | grep 'Reading' | awk' {print $2}'} function writing {/ usr/bin/curl" https://$HOST/nginxstatus" 2 > / dev/null | grep 'Writing' | awk' {print $4}'} function waiting {/ usr/bin/curl "https://$HOST/nginxstatus" 2 > / dev/null | grep 'Waiting' | awk' {print $6}'} function accepts { / usr/bin/curl "https://$HOST/nginxstatus" 2 > / dev/null | awk NR==3 | awk'{print $1}'} function handled {/ usr/bin/curl" https://$HOST/nginxstatus" 2 > / dev/null | awk NR==3 | awk'{print $2}'} function requests {/ usr/bin/curl "https://$HOST/nginxstatus" 2 > / dev/null | awk NR==3 | awk'{print $3}'} # Run the requested function$1

# the above is set according to my situation, of course, there are other more concise ways, but because you can achieve the desired functionality, so do not make changes.

Give executable permissions to the script

# chmod + x nginx_status.sh

Change the script to zabbix user and group management

# chown zabbix:zabbix nginx_status.sh# ll nginx_status.sh-rwxr-xr-x 1 zabbix zabbix 1168 Jun 27 13:58 nginx_status.sh

Check locally to see if your settings can produce results, otherwise something goes wrong, but you don't find it, you may not be able to get the data later, and you can troubleshoot it in time.

# / usr/bin/curl "https://www.sss.com/nginxstatus" 2 > / dev/null | grep 'Active' | awk' {print $NF}'12

The above data have been tried one by one, and if the data can be produced as above, it means there is no problem. Then you can go to the zabbix client to add the Nginx service module to get the settings.

# grep-v "^ [#;]" / data0/zabbix/etc/zabbix_agentd.conf | grep-v "^ $" LogFile=/tmp/zabbix_agentd.logServer=10.60.0.71 # # zabbix server monitoring host IP address ServerActive=10.60.0.71 # # zabbix server monitoring host IP address Hostname=10.60.0.163 # # zabbix client, that is, the IP address UnsafeUserParameters=1 # # of the native nginx may not be enabled by default, then you can remove the previous comments

Then you can add the following settings anywhere in zabbix_agentd.conf, which I put under UnsafeUserParameters.

# vim / data0/zabbix/etc/zabbix_agentd.confUserParameter=nginx.accepts,/data0/zabbix/scripts/nginx_status.sh accepts UserParameter=nginx.handled,/data0/zabbix/scripts/nginx_status.sh handled UserParameter=nginx.requests,/data0/zabbix/scripts/nginx_status.sh requests UserParameter=nginx.connections.active,/data0/zabbix/scripts/nginx_status.sh active UserParameter=nginx.connections.reading,/data0/zabbix/scripts/nginx_status.sh reading UserParameter=nginx.connections.writing,/data0/zabbix/scripts/nginx_status.sh writing UserParameter=nginx.connections.waiting / data0/zabbix/scripts/nginx_status.sh waiting

Restart the zabbix agent service to make the configuration settings we have just set take effect

# pkill-9 zabbix_agentd# / data0/zabbix/sbin/zabbix_agentd

Now the settings for the zabbix client side are all finished. Transfer zabbix server monitoring host

First test whether zabbix server can obtain the data of the Nginx service on the zabbix client side through zabbix_get.

# / usr/local/zabbix/bin/zabbix_get-s 10.60.0.163-p 10050-k "nginx.accepts" 142257miles / usr/local/zabbix/bin/zabbix_get-s 10.60.163-p 10050-k "nginx.handled" 142280legs / usr/local/zabbix/bin/zabbix_get-s 10.60.0.163-p 10050-k "nginx.requests" 265408cycles / usr/local/zabbix/bin/zabbix_get-s 10. 60.0.163-p 10050-k "nginx.connections.active" eggs / usr/local/zabbix/bin/zabbix_get-s 10.60.0.163-p 10050-k "nginx.connections.reading" bands / usr/local/zabbix/bin/zabbix_get-s 10.60.0.163-p 10050-k "nginx.connections.writing" bands / usr/local/zabbix/bin/zabbix_get-s 10.60.0.163-p 10050-k "nginx.connections.waiting" 9

From the above, there is no problem with the monitored items of the Nginx service.

Search the Internet for the zabbix template that is suitable for you to use Nginx status, and then import the template into zabbix web. Here is the template of Nginx status that I exported on my own zabbix web. If you find it appropriate, you can also download my template at the bottom of the article.

-2.0 2017-06-27T13:30:39Z-- Freetrade-- Nginx Status Nginx Status-- Freetrade-- Nginx-- Nginx Accepts 7 0 nginx.accepts 30 365 03 localhost 0 000 000-- Nginx- Nginx Connections Active 7 0 nginx.connections.active 30 365 03 localhost 0 000 000-- Nginx-Nginx Connections Reading 7 0 nginx.connections.reading 30 365 03 localhost 0 000 0 0 0 000-- Nginx-Nginx Connections Waiting 7 0 nginx.connections.waiting 30 365 03 localhost 0 000 000-- Nginx-Nginx Connections Writing 7 0 nginx.connections.writing 30 365 365 03 localhost 0 000 000 0-- Nginx-Nginx Handled 7 0 nginx.handled 30 365 365 03 localhost 0 000 000 0-- Nginx -Nginx Requests 7 0 nginx.requests 30 365 0 3 localhost 0000 0000-- Nginx-- Nginx Clients Status 900 0.0000 100.0000 1 10 100.0000 0 .0000 0000-- 00 0000EE 0 20-Nginx Status nginx.connections.active-1 0 EE0000 0 20-Nginx Status nginx.connections.writing-20 EEEE00 0 20-Nginx Status nginx.connections.waiting-30 00EE00 0 20-Nginx Status nginx.connections.reading-Nginx Socket Status 900200 0.0000 100.0000 1 10 100.0000 0.0000 0000-00 00EE00 0 20-Nginx Status nginx.accepts-00 EE0000 0 20-Nginx Status nginx.handled-10 EEEE00 0 20-Nginx Status nginx.requests

First of all, you need to create an empty templates on the zabbix web side, as shown below:

"configuration-template"-> to "create template" in the upper right corner, enter the name of the template you want and the host that belongs to the template, and finally save it.

Download and modify the template that suits you and import it into the original empty templates. Of course, as long as the import is successful, you can later enter templates to modify it to suit you.

Select "Import"-- "Select File"-- "Import" in the upper right corner.

You have to be prompted for a successful import to show that your template is appropriate, but it may not be useful.

Then put the Nginx host you want to monitor into the template, and then go to the monitored Nginx host to see if the module is linked.

You should be able to see some more graphics and projects on your Nginx host, and the template has been linked to the new template you set.

If you click on it, in the monitoring items of nginx hosts, the monitoring items related to Nginx services are Zabbix endpoint agents (active).

Of course, if you do not import the template successfully, then you can honestly create it one by one, but it is also very simple! Just take it one step at a time, make more impression and make it easy to understand.

Create a new template first

Add an application set to the new template

Add monitoring items to the new template, and add the content item by item, basically the same, except for the key value and name

Add graphical monitoring. Here, as long as you have created the monitoring project, you only need to select the monitoring project to which you want to render the graph.

Go to the monitoring host to enter the template link, directly select the existing template, and then add and save

From above, the monitored host has been loaded with a new template and the project set exists.

Finally, check through "Monitoring"-"Graphics" to select the graphic name of the monitored host to see if the diagram has loaded Nginx Client Status,Nginx Socket Status;. If so, congratulations, the monitoring settings are successful!

If you want to do some alarms and so on, you can set triggers in the rules, because everyone's needs are different, so I won't say any more here.

Attachment: http://down.51cto.com/data/2366804

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report