Home >Backend Development >PHP Tutorial >[Zabbix30] Add Nginx monitoring zabbix get zabbix client installation grafana zabbi
通过Nginx的http_stub_status_module模块提供的状态信息来监控,所以在Agent端需要配置Nginx状态获取的脚本,和添加key信息等,然后在Server端配置Nginx的监控模板等。请根据自己情况调整,这里只做简单的参照。
主要是使用Github这个项目的代码 zabbix-templates
Agent端
系统是Centos6.x, Zabbix-agent是3.0版本, Nginx1.9.x 官方版本
首先要检查Nginx是否安装了 http_stub_status_module
模块,通过下面的命令可以看到编译参数。
<code>nginx <span>-V</span></code>
如果没有这个模块,还需要重新编译Nginx.
配置Nginx
Nginx 80端口的server配置增加如下的片段
<code><span>location</span> /nginx_status { <span>stub_status</span><span>on</span>; <span>access_log</span><span>off</span>; <span>allow</span><span>127.0.0.1</span>; <span>deny</span> all; }</code>
配置完成之后,redload nginx,然后用简单测试下
<code>>> curl http:<span>//127.0.0.1/nginx_status</span> Active connections: <span>7</span><span>server</span> accepts handled requests <span>2707</span><span>2707</span><span>12528</span> Reading: <span>0</span> Writing: <span>1</span> Waiting: <span>6</span>?</code>
zabbix-agent 配置
有3个步骤,首先是编写获取Nginx信息脚本,接着配置中增加key信息,然后重启agent 服务。
!!注意脚本权限问题,agent运行用户要能执行。
<code>>><span># mkdir -p /usr/local/zabbix-agent/scripts</span> >><span># cd /usr/local/zabbix-agent/scripts</span> >><span># vim nginx-check.sh</span> >><span># cat nginx-check.sh</span><span>#!/bin/bash</span><span>##################################</span><span># Zabbix monitoring script</span><span>#</span><span># nginx:</span><span># - anything available via nginx stub-status module</span><span>#</span><span>##################################</span><span># Contact:</span><span># vincent.viallet@gmail.com</span><span># Zabbix requested parameter</span> ZBX_REQ_DATA=<span>"<span>$1</span>"</span> ZBX_REQ_DATA_URL=<span>"<span>$2</span>"</span><span># Nginx defaults</span> NGINX_STATUS_DEFAULT_URL=<span>"http://127.0.0.1/nginx_status"</span> WGET_BIN=<span>"/usr/bin/wget"</span><span>#</span><span># Error handling:</span><span># - need to be displayable in Zabbix (avoid NOT_SUPPORTED)</span><span># - items need to be of type "float" (allow negative + float)</span><span>#</span> ERROR_NO_ACCESS_FILE=<span>"-0.9900"</span> ERROR_NO_ACCESS=<span>"-0.9901"</span> ERROR_WR>"-0.9902" ERROR_DATA=<span>"-0.9903"</span><span># either can not connect / bad host / bad port</span><span># Handle host and port if non-default</span><span>if</span> [ ! -z <span>"<span>$ZBX_REQ_DATA_URL</span>"</span> ]; <span>then</span> URL=<span>"<span>$ZBX_REQ_DATA_URL</span>"</span><span>else</span> URL=<span>"<span>$NGINX_STATUS_DEFAULT_URL</span>"</span><span>fi</span><span># save the nginx stats in a variable for future parsing</span> NGINX_STATS=$(<span>$WGET_BIN</span> -q <span>$URL</span> -O - <span>2</span>> /dev/null) <span># error during retrieve</span><span>if</span> [ $? <span>-ne</span><span>0</span> -o -z <span>"<span>$NGINX_STATS</span>"</span> ]; <span>then</span><span>echo</span><span>$ERROR_DATA</span><span>exit</span><span>1</span><span>fi</span><span>#</span><span># Extract data from nginx stats</span><span>#</span><span>case</span><span>$ZBX_REQ_DATA</span><span>in</span> active_connections) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | head -<span>1</span> | cut <span>-f</span>3 <span>-d</span><span>' '</span>;; accepted_connections) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | grep -Ev <span>'[a-zA-Z]'</span> | cut <span>-f</span>2 <span>-d</span><span>' '</span>;; handled_connections) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | grep -Ev <span>'[a-zA-Z]'</span> | cut <span>-f</span>3 <span>-d</span><span>' '</span>;; handled_requests) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | grep -Ev <span>'[a-zA-Z]'</span> | cut <span>-f</span>4 <span>-d</span><span>' '</span>;; reading) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | tail -<span>1</span> | cut <span>-f</span>2 <span>-d</span><span>' '</span>;; writing) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | tail -<span>1</span> | cut <span>-f</span>4 <span>-d</span><span>' '</span>;; waiting) <span>echo</span><span>"<span>$NGINX_STATS</span>"</span> | tail -<span>1</span> | cut <span>-f</span>6 <span>-d</span><span>' '</span>;; *) <span>echo</span><span>$ERROR_WRONG_PARAM</span>; <span>exit</span><span>1</span>;; <span>esac</span><span>exit</span><span>0</span></code>
/etc/zabbix/zabbix_agentd.conf
中定义了其他key的包含目录 Include=/etc/zabbix/zabbix_agentd.d/
, 如果没有这个配置请自己添加下。接着在 /etc/zabbix/zabbix_agentd.d/
目录新建一个文件 nginx-params.conf
, 内容如下<code>UserParameter=nginx[*],/usr/local/zabbix-agent/scripts/nginx-check.sh <span>"<span>$1</span>"</span></code>
<code>>>> <span>/etc/init</span>.d/zabbix-agent restart</code>
Server 的Web端
首先命令行测试下刚才agent好使不,确认好用之后在web端导入模板,之后就可以给对应主机添加监控喽。
<code><span>>>></span> zabbix_get <span>-s</span><span>127.0</span><span>.0</span><span>.1</span><span>-p</span><span>10050</span><span>-k</span><span>"nginx[reading]"</span><span>0</span></code>
登录Zabbix3.0 的web界面,一次选择 Configuration
> Templates
, 在主界面的右上角有个 Import
按钮,用来导入模板。
模板文件比较长留一个下载地址
导入之后就可以给主机添加监控啦。
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });以上就介绍了[Zabbix30 ]添加Nginx监控,包括了zabbix,nginx方面的内容,希望对PHP教程有兴趣的朋友有所帮助。