目录
简介
配置yum仓库
Web Zabbix端添加主机
启用之前自动发现的规则及动作
nginx在生产环境中的应用越来越广泛,所以需要对nginx的性能状态做一些监控来发现出来出现的问题。zabbix监控nginx,首先确认nginx的监控指标,主要有:基本活动指标,错误指标,性能指标。
nginx处理流程图具体如下:
注释:Accepts(接受)、Handled(已处理)、Requests(请求数)是一直在增加的计数器。Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)随着请求量而增减
名称
描述
指标类型
Accepts(接受)
NGINX 所接受的客户端连接数
资源: 功能
Handled(已处理)
成功的客户端连接数
资源: 功能
Active(活跃)
当前活跃的客户端连接数
资源: 功能
Dropped(已丢弃,计算得出)
丢弃的连接数(接受 - 已处理)
工作:错误*
Requests(请求数)
客户端请求数
工作:吞吐量
NGINX worker 进程接受 OS 的连接请求时 Accepts 计数器增加,而Handled 是当实际的请求得到连接时(通过建立一个新的连接或重新使用一个空闲的)。这两个计数器的值通常都是相同的,如果它们有差别则表明连接被Dropped, 往往这是由于资源限制,比如已经达到 NGINX 的worker_connections的限制。
[root@agent ~]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base epel extras updates
Cleaning up list of fastest mirrors
[root@agent ~]# yum makecache
已加载插件:fastestmirror
Determining fastest mirrors
epel/x86_64/metalink
安装nginx
[root@agent ~]# yum install -y nginx
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
正在解决依赖关系
查看nginix_status是否开启
[root@agent ~]# nginx -V
nginx version: nginx/1.20.1
配置nginx
[root@agent ~]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.html;
stub_status on; #开启stub模块
access_log off; #关闭access_log
allow 127.0.0.1; #允许自身ip
allow 192.168.50.50; #此处设置的是zabbix server的ip>地址
}
#添加如下信息
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
[root@agent ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@agent ~]# systemctl start nginx 开启nginx
[root@agent ~]# curl 192.168.50.50/nginx-status 获取nginx信息
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
注释:
Active connections –当前活跃的连接数量
server accepts handled requests — 总共处理了756072922个连接 , 成功创建 756072922次握手, 总共处理了1136799890个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
创建脚本存放目录
[root@agent ~]# mkdir /usr/local/zabbix/scripts
编写zabbix监控nginx脚本
[root@agent ~]# vim /usr/local/zabbix/scripts/nginx-check.sh
#!/bin/bash
###########################
#zabbix monitoring script
#
# nginx:
# - anything available via nginx stub-status module
#
##################################
# Contact:
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_URL="192.168.50.50/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
配置zabbix_agentd.conf
[root@agent ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
UnsafeUserParameters=1 启用自定义
UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check.sh "$1" #引用nginx脚本
查看端口
[root@agent ~]# netstat -anpt | grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1736/zabbix_agentd
server测试获取数据
[root@server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.200.117 -p 10050 -k "nginx[reading]"
0
先禁用
导入模板
创建主机添加导入模板
等一会查看效果