目录
一、实验准备
二、监控nginx
一、实验准备
zabbix-sever(192.168.115.4)
zabbix-agent(192.168.115.5)
添加监控对象
二、监控nginx
安装NGINX在192.168.115.5上安装NGINX,开启status模块
yum -y install epel-release
yum -y install nginx
systemctl start nginx
nginx -V #查看模块
编写nginx的配置文件vim /etc/nginx/nginx.conf
server或者location下插入
location / {
}
location /status { #定义运行状态页面名称,页面为statusstub_status on; #开启模块access_log off; #不开启访问日志记录allow 192.168.115.0/24; #允许此网段的主机访问此网页deny all; #其余网段主机不允许访问}
重启:systemctl restart nginx
验证此模块:curl 192.168.115.151/status
编写脚本、设置key并验证
vim /etc/zabbix/nginx.sh
#!/bin/bash
#定义Nginx status页面
ngx_status="192.168.115.5/status"#判断status页面是否存活
ngx_status_code() {http_code=`curl -o /dev/null -s -w %{http_code} ${ngx_status}`if [ ${http_code} == "200" ];thenreturn 1elseecho "Nginx status is not running."fi
}#获取当前活动的客户端连接数
active() {ngx_status_code || curl -s ${ngx_status} | grep "Active" | awk '{print $NF}'
}#获取接收客户端连接的总数量
accepts() {ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $1}'
}#获取已处理的连接总数量
handled() {ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $2}'
}#获取客户端请求总数量
requests() {ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $3}'
}#获取正在读取请求标头的当前连接数量
reading() {ngx_status_code || curl -s ${ngx_status} | grep "Reading" | awk '{print $2}'
}#获取正在将响应写回到客户端的当前连接数量
writing() {ngx_status_code || curl -s ${ngx_status} | grep "Writing" | awk '{print $2}'
}#获取当前正在等待响应的客户端连接数量
waiting() {ngx_status_code || curl -s ${ngx_status} | grep "Waiting" | awk '{print $2}'
}#使用位置变量控制脚本输出
case $1 inactive)active;;accepts)accepts;;handled)handled;;requests)requests;;reading)reading;;writing)writing;;waiting)waiting;;*)echo "Unknown options"
esac
配置Zabbix Agent
vim /etc/zabbix/zabbix_agentd.conf
将333行UnsafeUserParameters=0解开注释并改成UnsafeUserParameters=1
在底端添加创建自定义监控项
UserParameter=nginx.active,bash /etc/zabbix/nginx.sh active
UserParameter=nginx.accepts,bash /etc/zabbix/nginx.sh accepts
UserParameter=nginx.handled,bash /etc/zabbix/nginx.sh handled
UserParameter=nginx.requests,bash /etc/zabbix/nginx.sh requests
UserParameter=nginx.reading,bash /etc/zabbix/nginx.sh reading
UserParameter=nginx.writing,bash /etc/zabbix/nginx.sh writing
UserParameter=nginx.waiting,bash /etc/zabbix/nginx.sh waiting
重启zabbix-agent:systemctl start zabbix-agent
数值获取测试
zabbix_get -s 192.168.115.5 -k nginx.active
web端添加监控项
图形化