首页 > 后端开发 > php教程 > Zabbix监控Nginx

Zabbix监控Nginx

WBOY
发布: 2016-07-29 09:15:36
原创
1006 人浏览过

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>
登录后复制
<code>在Nginx配置档中添加:
     location /nginx_status {
                stub_status <span>on</span>;
                access_log   <span>off</span>;
                }
</code>
登录后复制

一、创建监控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>
登录后复制

二、创建监控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>
登录后复制

三、在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>
登录后复制

(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>
登录后复制

创建完大概是这个样子,相信新手也能做出来!!!
这里写图片描述


四、现在点击要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>
登录后复制

图像1:暂且取名为 Nginx_Interactive
这里写图片描述
图像2:暂且取名为 Nginx_Connection
这里写图片描述


五、Hosts-Templates-将刚才创建的Templates App Nginx链接到主机上面
完成之后大概是这个样子
这里写图片描述


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

这里写图片描述


七、这时候在Zabbix Web页面中查看Nginx状态就OK~~~
这里写图片描述


这里写图片描述

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

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板