Zabbix监控Nginx
首先,大家如果看到有什么不懂的地方,欢迎吐槽!!!
我会在当天或者第二天及时回复,并且改进~~
在添加监控之前,必须要强调的是,Nginx必须支持stub_status状态的访问,不然一切都是空谈!!!
首先zabbix监控nginx 是根据nginx的Stub Status模块,抓取Status模块所提供的数据。
如果想启用Stub Status模块,在编译nginx的时候要加上参数
--with-http_stub_status_module
例如:
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
在Nginx配置档中添加:
location /nginx_status {
stub_status on;
access_log off;
}
一、创建监控Nginx的key
- nginx_status_key
vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_status_key
UserParameter=nginx.accepts,/usr/local/zabbix/shell/nginx_status.sh accepts
UserParameter=nginx.handled,/usr/local/zabbix/shell/nginx_status.sh handled
UserParameter=nginx.requests,/usr/local/zabbix/shell/nginx_status.sh requests
UserParameter=nginx.connections.active,/usr/local/zabbix/shell/nginx_status.sh active
UserParameter=nginx.connections.reading,/usr/local/zabbix/shell/nginx_status.sh reading
UserParameter=nginx.connections.writing,/usr/local/zabbix/shell/nginx_status.sh writing
UserParameter=nginx.connections.waiting,/usr/local/zabbix/shell/nginx_status.sh waiting
二、创建监控Nginx的脚本(记得加执行权限)
- nginx_status.sh
vim /usr/local/zabbix/shell/nginx_status.sh
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# License: GPLv4
# Set Variables
BKUP_DATE=`/bin/date +%Y%m%d`
LOG="/usr/local/zabbix/logs/nginx_status.log"
HOST="192.168.0.217" #HOST非常重要,确保CURL能访问到这个IP地址的Status
PORT="80"
# Functions to return nginx stats
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}'
}
# Run the requested function
$1
三、在Zabbix Web界面中创建Templates App Nginx模版
(1)、先创建三个item,这个store value要求是每秒变化的
- nginx.accepts
- nginx.handled
- nginx.requests
我在这边做一个示例:
模版中添加一个nginx.accepts的item
Name nginx.accepts
Type Zabbix agent
Key nginx.accepts
Type of information Numeric (unsigned)
Data Type Decimal
Units 空
Store value Detal (speed per second)
Show value As is
(2)、而后四个item Store value是不变的
- nginx.connections.active
- nginx.connections.reading
- nginx.connections.writing
- nginx.connections.waiting
Name nginx.connections.active
Type Zabbix agent
Key nginx.accepts
Type of information Numeric (unsigned)
Data Type Decimal
Units 空
Store value As is
Show value As is
创建完大概是这个样子,相信新手也能做出来!!!
四、现在点击要Tempates的Graphs,分别创建两个Graphs
监控Nginx(nginx.accepts、nginx.handled、nginx.requests)
监控Nginx(nginx.connections.active、nginx.connections.reading、nginx.connections.writing、nginx.connections.waiting)
图像1:暂且取名为 Nginx_Interactive
图像2:暂且取名为 Nginx_Connection
五、Hosts-Templates-将刚才创建的Templates App Nginx链接到主机上面
完成之后大概是这个样子
六、在Screen中创建监控Nginx的Screen,将前面创建的两个图像添加进去
七、这时候在Zabbix Web页面中查看Nginx状态就OK~~~
时间: 2024-10-01 10:15:38