zabbix监控nginx性能状态
nginx在生产环境中的应用越来越广泛,所以需要对nginx的性能状态做一些监控,来发现出来出现的问题。zabbix监控nginx,首先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是否开启,查看已开启。
1
2
3
4
5
6
7
# /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
#
2,nginx_status开启的步骤:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80 ;
server_namewww.guojinbao.com;
rewrite ^/invitejoin/(.*)\.htm?$/register.shtml?$1 last;
index index.jsp index.html;
root /opt/home;
location = /nginx-status {
stub_status on;
access_logoff;
allow 127.0.0.1;
allow 10.253.12.34;
####zabbix服务器端的IP地址一般为内网IP
}
3,测试并启动nginx
# /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
# /usr/local/nginx/sbin/nginx -s reload
4,用curl来进行测试:
1
2
3
4
5
# curl www.guojinbao.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_agentd
1,编写脚步来获取nginx的相关信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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.guojinbao.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 '' | cut -f2 -d' ';;
handled_connections) echo "$NGINX_STATS" | grep -Ev '' | cut -f3 -d' ';;
handled_requests) echo "$NGINX_STATS" | grep -Ev '' | 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
# 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,并配置相关的参数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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客户端
1
2
3
4
# /etc/init.d/zabbix_agentd restart
Shutting down zabbix_agentd:
Starting zabbix_agentd:
#
4,在zabbix服务端(server)进行测试。
1
2
3
# zabbix_get -s 10.253.17.20 -p 10050 -k "nginx"
0
#
(三)在网页上配置nginx模板的相关监控
1,登录zabbix界面,依次点击:配置(configuration)---模板(template)---导入(import)
2,给主机添加模板:选择主机---nginx服务器主机---模板---选择(刚刚导入的nginx性能状态的模板)---添加---更新
3,查看nginx监控的最新数据:监控中---图形---选择相应的监控类型。
页:
[1]