Home > Backend Development > PHP Tutorial > Zabbix monitors Nginx

Zabbix monitors Nginx

WBOY
Release: 2016-07-29 09:15:36
Original
1006 people have browsed it

Zabbix监控Nginx

在添加监控之前,必须要强调的是,Nginx必须支持stub_status状态的访问,不然一切都是空谈!!!

<code>首先zabbix监控nginx 是根据nginx的Stub Status模块,抓取Status模块所提供的数据。
如果想启用Stub Status模块,在编译nginx的时候要加上参数 
<span>--</span><span>with</span><span>-http_stub_status_module</span>例如:
<span>.</span>/configure <span>--</span>user<span>=</span>www <span>--</span><span>group</span><span>=</span>www <span>--</span>prefix<span>=</span>/usr/<span>local</span>/nginx <span>--</span><span>with</span><span>-http_stub_status_module</span><span>--</span><span>with</span><span>-http_ssl_module</span></code>
Copy after login
<code>在Nginx配置档中添加:
     location /nginx_status {
                stub_status <span>on</span>;
                access_log   <span>off</span>;
                }
</code>
Copy after login

一、创建监控Nginx的key

  • nginx_status_key
<code>vim /usr/local/zabbix/etc/zabbix_agentd<span>.conf</span><span>.d</span>/nginx_status_key

UserParameter=nginx<span>.accepts</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> accepts 
UserParameter=nginx<span>.handled</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> handled 
UserParameter=nginx<span>.requests</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> requests 
UserParameter=nginx<span>.connections</span><span>.active</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> active 
UserParameter=nginx<span>.connections</span><span>.reading</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> reading 
UserParameter=nginx<span>.connections</span><span>.writing</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> writing 
UserParameter=nginx<span>.connections</span><span>.waiting</span>,/usr/local/zabbix/shell/nginx_status<span>.sh</span> waiting</code>
Copy after login

二、创建监控Nginx的脚本(记得加执行权限)

  • nginx_status.sh
<code>vim /usr/local/zabbix/shell/nginx_status.sh

#!<span>/bin/</span>bash 
# Script to fetch nginx statuses <span>for</span> tribily monitoring systems 
# License: GPLv4 
# Set Variables 
BKUP_DATE=`/bin/date +%Y%m%d` 
LOG=<span>"/usr/local/zabbix/logs/nginx_status.log"</span>HOST=<span>"192.168.0.217"</span>        #HOST非常重要,确保CURL能访问到这个IP地址的Status
PORT=<span>"80"</span># Functions to <span>return</span> nginx stats 
<span><span>function</span><span>active</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| grep <span>'Active'</span> | awk <span>'{print $NF}'</span>    }     
<span><span>function</span><span>reading</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| grep <span>'Reading'</span> | awk <span>'{print $2}'</span>    }     
<span><span>function</span><span>writing</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| grep <span>'Writing'</span> | awk <span>'{print $4}'</span>    }     
<span><span>function</span><span>waiting</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| grep <span>'Waiting'</span> | awk <span>'{print $6}'</span>    }     
<span><span>function</span><span>accepts</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| awk NR==<span>3</span> | awk <span>'{print $1}'</span>    }     
<span><span>function</span><span>handled</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| awk NR==<span>3</span> | awk <span>'{print $2}'</span>    }       
<span><span>function</span><span>requests</span> {</span>    /usr/bin/curl <span>"http://$HOST:$PORT/nginx_status"</span><span>2</span>><span>/dev/</span><span>null</span>| awk NR==<span>3</span> | awk <span>'{print $3}'</span>    } 
# Run the requested <span><span>function</span><span>$1</span></span></code>
Copy after login

三、在Zabbix Web界面中创建Templates App Nginx模版
(1)、先创建三个item,这个store value要求是每秒变化的

  • nginx.accepts
  • nginx.handled
  • nginx.requests
<code>我在这边做一个示例:
模版中添加一个nginx.accepts的item

Name                nginx.accepts  
<span>Type</span>                Zabbix agent
Key                 nginx.accepts
<span>Type</span><span>of</span> information Numeric (<span>unsigned</span>)
Data <span>Type</span>           Decimal
<span>Units</span>               空
Store value         Detal (speed per second)
Show  value         As <span>is</span></code>
Copy after login

(2)、而后四个item Store value是不变的

  • nginx.connections.active
  • nginx.connections.reading
  • nginx.connections.writing
  • nginx.connections.waiting
<code>Name                nginx.connections.active
<span>Type</span>                Zabbix agent
Key                 nginx.accepts
<span>Type</span><span>of</span> information Numeric (<span>unsigned</span>)
Data <span>Type</span>           Decimal
<span>Units</span>               空
Store value         As <span>is</span>
Show  value         As <span>is</span></code>
Copy after login

创建完大概是这个样子,相信新手也能做出来!!!
Zabbix monitors Nginx


四、现在点击要Tempates的Graphs,分别创建两个Graphs

<code>监控Nginx(nginx<span>.accepts</span>、nginx<span>.handled</span>、nginx<span>.requests</span>)
监控Nginx(nginx<span>.connections</span><span>.active</span>、nginx<span>.connections</span><span>.reading</span>、nginx<span>.connections</span><span>.writing</span>、nginx<span>.connections</span><span>.waiting</span>)</code>
Copy after login

图像1:暂且取名为 Nginx_Interactive
Zabbix monitors Nginx
图像2:暂且取名为 Nginx_Connection
Zabbix monitors Nginx


五、Hosts-Templates-将刚才创建的Templates App Nginx链接到主机上面
完成之后大概是这个样子
Zabbix monitors Nginx


六、在Screen中创建监控Nginx的Screen,将前面创建的两个图像添加进去

Zabbix monitors Nginx


七、这时候在Zabbix Web页面中查看Nginx状态就OK~~~
Zabbix monitors Nginx


Zabbix monitors Nginx

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了 Zabbix监控Nginx,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template