阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Zabbix监控Nginx性能状态

173次阅读
没有评论

共计 12998 个字符,预计需要花费 33 分钟才能阅读完成。

Nginx 在生产环境中的应用越来越广泛,所以需要对 Nginx 的性能状态做一些监控来发现出来出现的问题。zabbix 监控 Nginx,首先确认 Nginx 的监控指标,主要有:基本活动指标,错误指标,性能指标。

Nginx 处理流程图具体如下:

Zabbix 监控 Nginx 性能状态

注释:Accepts(接受)、Handled(已处理)、Requests(请求数)是一直在增加的计数器。Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)随着请求量而增减

名称

描述

指标类型

Accepts(接受)

NGINX 所接受的客户端连接数

资源: 功能

Handled(已处理)

成功的客户端连接数

资源: 功能

Active(活跃)

当前活跃的客户端连接数

资源: 功能

Dropped(已丢弃,计算得出)

丢弃的连接数(接受 – 已处理)

工作:错误 *

Requests(请求数)

客户端请求数

工作:吞吐量

NGINX worker 进程接受 OS 的连接请求时 Accepts 计数器增加,而 Handled 是当实际的请求得到连接时(通过建立一个新的连接或重新使用一个空闲的)。这两个计数器的值通常都是相同的,如果它们有差别则表明连接被 Dropped,往往这是由于资源限制,比如已经达到 NGINX 的 worker_connections 的限制。

Zabbix 监控 Nginx 性能状态相关文件可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2017 年资料 / 2 月 / 6 日 /Zabbix 监控 Nginx 性能状态 /

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

首先 nginx 需要配置 nginx_status  具体步骤是:在 zabbix agentd 客户端上,查看 nginx 是否加载了 with-http_stub_status_module。因为 zabbix 监控 nginx 是根据 nginx 的 Stub Status 模块,抓取 Status 模块所提供的数据。假如以前没开启,现在想启用 StubStatus 模块,在编译 nginx 的时候要加上参数 with-http_stub_status_module,执行./configure && make 就可以了,不用 make install,一般情况下都是安装的,具体的安装配置如下

(一)配置 nginx

1, 查看 nginx_status 是否开启,查看已开启。

[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.4.7

built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)

TLS SNI support enabled

configure arguments: –with-http_stub_status_module –with-http_ssl_module –with-pcre

–with-http_realip_module –with-http_image_filter_module

[root@iZ237lzm354Z scripts]#

2,nginx_status 开启的步骤:

[root@iZ237lzm354Z scripts]# vim /usr/local/nginx/conf/nginx.conf

 server {

                        listen      80 ;

                        server_name  www.linuxidc.com;

          rewrite ^/invitejoin/(.*)\.htm[l]?$  /register.shtml?$1 last;

                index index.jsp index.html;

                root /opt/home;

            location = /nginx-status {

                              stub_status on;

                              access_log  off;

                              allow 127.0.0.1;

                              allow 10.253.12.34;

                            ####zabbix 服务器端的 IP 地址一般为内网 IP

                              }

3,测试并启动 nginx

[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -s reload

4, 用 curl 来进行测试:

[root@iZ237lzm354Z scripts]# curl www.linuxidc.com/nginx-status

Active connections: 979

server accepts handled requests

 756072922 756072922 1136799890

Reading: 0 Writing: 4 Waiting: 975

备注:

Active connections –当前活跃的连接数量 
server accepts handled requests — 总共处理了 756072922 个连接 , 成功创建 756072922 次握手, 总共处理了 1136799890 个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下, 这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

一些 Zabbix 相关教程集合

CentOS 7.2 安装部署 Zabbix 3.0.4 详解  http://www.linuxidc.com/Linux/2016-11/137671.htm

Ubuntu 14.04 下 Zabbix2.4.5 源码编译安装  http://www.linuxidc.com/Linux/2015-05/117657.htm

CentOS 6.8 下 Zabbix Server 安装部署详细步骤 http://www.linuxidc.com/Linux/2016-12/138908.htm

Zabbix 基本配置及监控主机 http://www.linuxidc.com/Linux/2016-12/138504.htm

CentOS 7.0 x64 下 Zabbix 3.0 安装笔记  http://www.linuxidc.com/Linux/2016-11/137044.htm

CentOS 下 Zabbix 3.0.4 安装部署 http://www.linuxidc.com/Linux/2017-01/139087.htm

CentOS 7 LNMP 环境搭建 Zabbix3.0 http://www.linuxidc.com/Linux/2017-02/140134.htm

64 位 CentOS 6.2 下安装 Zabbix 2.0.6   http://www.linuxidc.com/Linux/2014-11/109541.htm

(二)配置 zabbix_agentd

1,编写脚步来获取 nginx 的相关信息

[root@ittestserver1 opt]# vim /usr/local/zabbix/scripts/nginx-check_performance.sh

#!/bin/bash

##################################

# Zabbix monitoring script

#

# nginx:

# – anything available via nginx stub-status module

#

##################################

# Contact:

# vincent.viallet@gmail.com

# Zabbix requested parameter

ZBX_REQ_DATA=”$1″

ZBX_REQ_DATA_URL=”$2″

# Nginx defaults

NGINX_STATUS_DEFAULT_URL=”www.linuxidc.com/nginx-status”    #(这里写网站的域名)

WGET_BIN=”/usr/bin/wget”

#

# Error handling:

# – need to be displayable in Zabbix (avoid NOT_SUPPORTED)

# – items need to be of type “float” (allow negative + float)

#

ERROR_NO_ACCESS_FILE=”-0.9900″

ERROR_NO_ACCESS=”-0.9901″

ERROR_WRONG_PARAM=”-0.9902″

ERROR_DATA=”-0.9903″ # either can not connect / bad host / bad port

# Handle host and port if non-default

if [! -z “$ZBX_REQ_DATA_URL”]; then

 URL=”$ZBX_REQ_DATA_URL”

else

 URL=”$NGINX_STATUS_DEFAULT_URL”

fi

# save the nginx stats in a variable for future parsing

NGINX_STATS=$($WGET_BIN -q $URL -O – 2> /dev/null)

# error during retrieve

if [$? -ne 0 -o -z “$NGINX_STATS”]; then

 echo $ERROR_DATA

 exit 1

fi

#

# Extract data from nginx stats

#

case $ZBX_REQ_DATA in

 active_connections) echo “$NGINX_STATS” | head -1 | cut -f3 -d’ ‘;;

 accepted_connections) echo “$NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f2 -d’ ‘;;

 handled_connections) echo “$NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f3 -d’ ‘;;

 handled_requests) echo “$NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f4 -d’ ‘;;

 reading) echo “$NGINX_STATS” | tail -1 | cut -f2 -d’ ‘;;

 writing) echo “$NGINX_STATS” | tail -1 | cut -f4 -d’ ‘;;

 waiting) echo “$NGINX_STATS” | tail -1 | cut -f6 -d’ ‘;;

 *) echo $ERROR_WRONG_PARAM; exit 1;;

esac

exit 0

[root@ittestserver1 opt]# chmod +x /usr/local/zabbix/scripts/nginx-check_performance.sh

-rw-r–r-x1 root root 1645 2 月 4 14:26/usr/local/zabbix/scripts/nginx-check_performance.sh

2, 配置 zabbix_agentd.conf。启用 UserParameter,并配置相关的参数。

[root@ittestserver1 opt]# vim /usr/local/zabbix/etc/zabbix_agentd.conf

####### USER-DEFINED MONITORED PARAMETERS #######

### Option: UnsafeUserParameters

#      Allow all characters to be passed in arguments to user-defined parameters.

#      The following characters are not allowed:

#      \ ‘ ” ` * ? [] {} ~ $ ! & ; () < > | # @

#      Additionally, newline characters are not allowed.

#      0 – do not allow

#      1 – allow

#

# Mandatory: no

# Range: 0-1

# Default:

# UnsafeUserParameters=0

UnsafeUserParameters=1

### Option: UserParameter

#      User-defined parameter to monitor. There can be several user-defined parameters.

#      Format: UserParameter=<key>,<shell command>

#      See ‘zabbix_agentd’ directory for examples.

#

# Mandatory: no

# Default:

# UserParameter=

UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check_performance.sh “$1”

3,重启 zabbix_agentd 客户端

[root@zabbix ~]# /etc/init.d/zabbix_agentd restart

Shutting down zabbix_agentd:                              [OK]

Starting zabbix_agentd:                                    [OK]

[root@zabbix ~]#

4,在 zabbix 服务端(server)进行测试。

[root@zabbix ~]# zabbix_get -s 10.253.17.20 -p 10050 -k “nginx[reading]”

0

[root@zabbix ~]#

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-02/140230p2.htm

(三)在网页上配置 nginx 模板的相关监控

1,登录 zabbix 界面,依次点击:配置(configuration)— 模板(template)— 导入(import)

Zabbix 监控 Nginx 性能状态

2, 给主机添加模板:选择主机 —nginx 服务器主机 — 模板 — 选择(刚刚导入的 nginx 性能状态的模板)— 添加 — 更新

Zabbix 监控 Nginx 性能状态
3,查看 nginx 监控的最新数据:监控中 — 图形 — 选择相应的监控类型。

Zabbix 监控 Nginx 性能状态

备注:

Active:当前活跃的连接数。

Accepts: 接受的请求数

Handled: 处理的请求数(正常服务器响应,这两项应该是可以相等的)

Requests: 客户端处理的请求数。(吞吐量)

Reading: 当接收到请求时,连接离开 Waiting 状态,并且该请求本身使 Reading 状态计数增加。在这种状态下 NGINX 会读取客户端请求首部。请求首部是比较小的,因此这通常是一个快速的操作。

Writing: 请求被读取之后,其使 Writing 状态计数增加,并保持在该状态,直到响应返回给客户端。这意味着,该请求在 Writing 状态时,一方面 NGINX 等待来自上游系统的结果(系统放在 NGINX“后面”),另外一方面,NGINX 也在同时响应。请求往往会在 Writing 状态花费大量的时间。

Waiting: 活跃的连接也可以处 于 Waiting 子状态,如果有在此刻没有活跃请求的话。新连接可以绕过这个状态并直接变为到 Reading 状态,最常见的是在使用“accept filter(接受过滤器)”和“deferred accept(延迟接受)”时,在这种情况下,NGINX 不会接收 worker 进程的通知,直到它具有足够的数据才开始响应。如果连接设置为 keep-alive,那么它在发送响应后将处于等待状态。

ZABBIX 的详细介绍:请点这里
ZABBIX 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140230.htm

Nginx 在生产环境中的应用越来越广泛,所以需要对 Nginx 的性能状态做一些监控来发现出来出现的问题。zabbix 监控 Nginx,首先确认 Nginx 的监控指标,主要有:基本活动指标,错误指标,性能指标。

Nginx 处理流程图具体如下:

Zabbix 监控 Nginx 性能状态

注释:Accepts(接受)、Handled(已处理)、Requests(请求数)是一直在增加的计数器。Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)随着请求量而增减

名称

描述

指标类型

Accepts(接受)

NGINX 所接受的客户端连接数

资源: 功能

Handled(已处理)

成功的客户端连接数

资源: 功能

Active(活跃)

当前活跃的客户端连接数

资源: 功能

Dropped(已丢弃,计算得出)

丢弃的连接数(接受 – 已处理)

工作:错误 *

Requests(请求数)

客户端请求数

工作:吞吐量

NGINX worker 进程接受 OS 的连接请求时 Accepts 计数器增加,而 Handled 是当实际的请求得到连接时(通过建立一个新的连接或重新使用一个空闲的)。这两个计数器的值通常都是相同的,如果它们有差别则表明连接被 Dropped,往往这是由于资源限制,比如已经达到 NGINX 的 worker_connections 的限制。

Zabbix 监控 Nginx 性能状态相关文件可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2017 年资料 / 2 月 / 6 日 /Zabbix 监控 Nginx 性能状态 /

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

首先 nginx 需要配置 nginx_status  具体步骤是:在 zabbix agentd 客户端上,查看 nginx 是否加载了 with-http_stub_status_module。因为 zabbix 监控 nginx 是根据 nginx 的 Stub Status 模块,抓取 Status 模块所提供的数据。假如以前没开启,现在想启用 StubStatus 模块,在编译 nginx 的时候要加上参数 with-http_stub_status_module,执行./configure && make 就可以了,不用 make install,一般情况下都是安装的,具体的安装配置如下

(一)配置 nginx

1, 查看 nginx_status 是否开启,查看已开启。

[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.4.7

built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)

TLS SNI support enabled

configure arguments: –with-http_stub_status_module –with-http_ssl_module –with-pcre

–with-http_realip_module –with-http_image_filter_module

[root@iZ237lzm354Z scripts]#

2,nginx_status 开启的步骤:

[root@iZ237lzm354Z scripts]# vim /usr/local/nginx/conf/nginx.conf

 server {

                        listen      80 ;

                        server_name  www.linuxidc.com;

          rewrite ^/invitejoin/(.*)\.htm[l]?$  /register.shtml?$1 last;

                index index.jsp index.html;

                root /opt/home;

            location = /nginx-status {

                              stub_status on;

                              access_log  off;

                              allow 127.0.0.1;

                              allow 10.253.12.34;

                            ####zabbix 服务器端的 IP 地址一般为内网 IP

                              }

3,测试并启动 nginx

[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -s reload

4, 用 curl 来进行测试:

[root@iZ237lzm354Z scripts]# curl www.linuxidc.com/nginx-status

Active connections: 979

server accepts handled requests

 756072922 756072922 1136799890

Reading: 0 Writing: 4 Waiting: 975

备注:

Active connections –当前活跃的连接数量 
server accepts handled requests — 总共处理了 756072922 个连接 , 成功创建 756072922 次握手, 总共处理了 1136799890 个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下, 这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

一些 Zabbix 相关教程集合

CentOS 7.2 安装部署 Zabbix 3.0.4 详解  http://www.linuxidc.com/Linux/2016-11/137671.htm

Ubuntu 14.04 下 Zabbix2.4.5 源码编译安装  http://www.linuxidc.com/Linux/2015-05/117657.htm

CentOS 6.8 下 Zabbix Server 安装部署详细步骤 http://www.linuxidc.com/Linux/2016-12/138908.htm

Zabbix 基本配置及监控主机 http://www.linuxidc.com/Linux/2016-12/138504.htm

CentOS 7.0 x64 下 Zabbix 3.0 安装笔记  http://www.linuxidc.com/Linux/2016-11/137044.htm

CentOS 下 Zabbix 3.0.4 安装部署 http://www.linuxidc.com/Linux/2017-01/139087.htm

CentOS 7 LNMP 环境搭建 Zabbix3.0 http://www.linuxidc.com/Linux/2017-02/140134.htm

64 位 CentOS 6.2 下安装 Zabbix 2.0.6   http://www.linuxidc.com/Linux/2014-11/109541.htm

(二)配置 zabbix_agentd

1,编写脚步来获取 nginx 的相关信息

[root@ittestserver1 opt]# vim /usr/local/zabbix/scripts/nginx-check_performance.sh

#!/bin/bash

##################################

# Zabbix monitoring script

#

# nginx:

# – anything available via nginx stub-status module

#

##################################

# Contact:

# vincent.viallet@gmail.com

# Zabbix requested parameter

ZBX_REQ_DATA=”$1″

ZBX_REQ_DATA_URL=”$2″

# Nginx defaults

NGINX_STATUS_DEFAULT_URL=”www.linuxidc.com/nginx-status”    #(这里写网站的域名)

WGET_BIN=”/usr/bin/wget”

#

# Error handling:

# – need to be displayable in Zabbix (avoid NOT_SUPPORTED)

# – items need to be of type “float” (allow negative + float)

#

ERROR_NO_ACCESS_FILE=”-0.9900″

ERROR_NO_ACCESS=”-0.9901″

ERROR_WRONG_PARAM=”-0.9902″

ERROR_DATA=”-0.9903″ # either can not connect / bad host / bad port

# Handle host and port if non-default

if [! -z “$ZBX_REQ_DATA_URL”]; then

 URL=”$ZBX_REQ_DATA_URL”

else

 URL=”$NGINX_STATUS_DEFAULT_URL”

fi

# save the nginx stats in a variable for future parsing

NGINX_STATS=$($WGET_BIN -q $URL -O – 2> /dev/null)

# error during retrieve

if [$? -ne 0 -o -z “$NGINX_STATS”]; then

 echo $ERROR_DATA

 exit 1

fi

#

# Extract data from nginx stats

#

case $ZBX_REQ_DATA in

 active_connections) echo “$NGINX_STATS” | head -1 | cut -f3 -d’ ‘;;

 accepted_connections) echo “$NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f2 -d’ ‘;;

 handled_connections) echo “$NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f3 -d’ ‘;;

 handled_requests) echo “$NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f4 -d’ ‘;;

 reading) echo “$NGINX_STATS” | tail -1 | cut -f2 -d’ ‘;;

 writing) echo “$NGINX_STATS” | tail -1 | cut -f4 -d’ ‘;;

 waiting) echo “$NGINX_STATS” | tail -1 | cut -f6 -d’ ‘;;

 *) echo $ERROR_WRONG_PARAM; exit 1;;

esac

exit 0

[root@ittestserver1 opt]# chmod +x /usr/local/zabbix/scripts/nginx-check_performance.sh

-rw-r–r-x1 root root 1645 2 月 4 14:26/usr/local/zabbix/scripts/nginx-check_performance.sh

2, 配置 zabbix_agentd.conf。启用 UserParameter,并配置相关的参数。

[root@ittestserver1 opt]# vim /usr/local/zabbix/etc/zabbix_agentd.conf

####### USER-DEFINED MONITORED PARAMETERS #######

### Option: UnsafeUserParameters

#      Allow all characters to be passed in arguments to user-defined parameters.

#      The following characters are not allowed:

#      \ ‘ ” ` * ? [] {} ~ $ ! & ; () < > | # @

#      Additionally, newline characters are not allowed.

#      0 – do not allow

#      1 – allow

#

# Mandatory: no

# Range: 0-1

# Default:

# UnsafeUserParameters=0

UnsafeUserParameters=1

### Option: UserParameter

#      User-defined parameter to monitor. There can be several user-defined parameters.

#      Format: UserParameter=<key>,<shell command>

#      See ‘zabbix_agentd’ directory for examples.

#

# Mandatory: no

# Default:

# UserParameter=

UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check_performance.sh “$1”

3,重启 zabbix_agentd 客户端

[root@zabbix ~]# /etc/init.d/zabbix_agentd restart

Shutting down zabbix_agentd:                              [OK]

Starting zabbix_agentd:                                    [OK]

[root@zabbix ~]#

4,在 zabbix 服务端(server)进行测试。

[root@zabbix ~]# zabbix_get -s 10.253.17.20 -p 10050 -k “nginx[reading]”

0

[root@zabbix ~]#

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-02/140230p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计12998字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中