Zabbix 监控 Nginx 模板
转载:https://blog.csdn.net/mshxuyi/article/details/106667438
1、查看
Nginx 可以通过 with-http_stub_status_module 模块来监控 nginx 的一些状态信息
nginx -V
# 显示
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
2、修改配置
vim /etc/nginx/conf.d/default.conf
# 加入
location /nginx_status {
stub_status on;
}
# 重启
systemctl restart nginx
3、测试
[root@vm73 scripts]# curl http://127.0.0.1/nginx_status
# 显示
Active connections: 1
server accepts handled requests
539 539 539
Reading: 0 Writing: 1 Waiting: 0
# Active connections: 正处理的活动连接数,当前的并发连接数
# server:表示nginx启动到现在共处理了多少个连接
# accepts:表示nginx启动到现在共成功创建了多少次握手(备注:请求丢失数=握手数-连接数)
# handled requests:表示总共处理了多少次请求
# Reading:为nginx读取到客户端的Header信息数
# Writing:为nginx返回给客户端的Header信息数
# Waiting: 为nginx已经处理完正在等待下一次请求指令的驻留连接
4、创建 脚本
vim /etc/zabbix/scripts/nginx_status.sh
#!/bin/bash
# date: 2020-06-10
# Description:zabbix监控nginx性能以及进程状态
# Note:此脚本需要配置在被监控端,否则ping检测将会得到不符合预期的结果
HOST="192.168.1.73"
PORT="80"
# 检测nginx进程是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1
5、测试
[root@vm73 scripts]# bash /etc/zabbix/scripts/nginx_status.sh active
# 成功
1
6、创建 zabbix 监控 key
vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
# 添加
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1
7、Zabbix web 界面操作
添加模板 → 添加监控项