简言

我们要用zabbix来获取nginx的状态首先我们的nginx要支持-with-http_stub_status_module模块。可以通过nginx -V来查看,如果没有配置需要我们平滑升级,这里不在描述。

如果有这个模块那么我们就开始启用nginx status配置在默认主机里面加上location或者你希望能访问到的主机里面。

步骤

1.首先我们在被监控端的服务器中nginx里面的server模块加入下面的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server{
server_name 0.0.0.0;
listen 12222;
access_log off;
location /nginx_status {
# Turn on nginx stats
stub_status on;
# I do not need logs for stats
access_log off;
# Security: Only allow access from 116.229.215.247
#allow 116.229.215.247;
allow all;
# Send rest of the world to /dev/null
#deny all;
}
}

加入之后重新加载nginx配置文件,之后用浏览器访问

curl http://10.120.255.200/nginx_status

2.然后将下面的脚本上传至服务器目录,目录自己选择,下面的nginx-params中的路径要和这个保持一致

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
59
60
61
62
63
64
#!/bin/bash
##################################
# Zabbix monitoring script
#
# nginx:
# - anything available via nginx stub-status module
#
##################################
# Contact:
# vincent.viallet@gmail.com
##################################
# ChangeLog:
# 20100922 VV initial creation
##################################

# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"

# Nginx defaults
NGINX_STATUS_DEFAULT_URL="http://10.120.255.200:80/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

3.将此脚本赋予权限

1
chmod a+x nginx-check.sh

4.进入到zabbix-agent的配置文件找到此目录,进入到里面新建一个配置文件nginx-params.conf

配置文件的内容

1
UserParameter=nginx[*],/usr/local/zabbix-agent-ops/bin/nginx-check.sh "$1" "$2"

5.重启zabbix-agentd服务

1
systemctl restart zabbix-agent.service

6.zabbix前端加入模板

7.获得监控数据

结尾

至此我们又用zabbix监控了nginx的性能,但是还有个问题,就是我们知道nginx作为前端服务器通常上面挂着很多机器和很多域名,比如我们有A域名和B域名,我们如何用zabbix来分别监控出A域名此时有多少链接,B域名有多少连接,这时我们应该怎么办呢?

https://www.cnblogs.com/huangweimin/articles/10166387.html