其实zabbix对nginx的服务状态监控,网上有很多相关模板以及设置方法,根据自己的需求修改即可,后期我会写一些比较详细用于生产环境中的一些教程。

部署环境:

OS:CentOS 7.4
WEB: Nginx 1.3
监控:Zabbix 3.4

先决条件:

Centos 7 源码编译安装 Nginx

注意:主要是 –with-http_stub_status_module 模块。

1.修改 nginx.conf

在 server 段 添加以下内容:

$ vim /usr/local/nginx/conf/nginx.conf

location /stub_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}

2.重启Nginx服务并测试访问

$ systemctl restart nginx.service
$ curl http://127.0.0.1/stub_status
Active connections: 1
server accepts handled requests
304 304 304
Reading: 0 Writing: 1 Waiting: 0

访问返回数据说明Nginx配置正常.

3.创建 Nginx 监控脚本

将以下代码复制保存为 nginx.sh

$ cd /usr/local/zabbix/bin
$ vim nginx.sh
#!/bin/bash

HOST="127.0.0.1"
PORT="80"
stub_status=stub_status
function check() {
/sbin/pidof nginx | wc -l
}
function active() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function accepts() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
function reading() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}

case "$1" in
check)
check
;;
active)
active
;;
accepts)
accepts
;;
handled)
handled
;;
requests)
requests
;;
reading)
reading
;;
writing)
writing
;;
waiting)
waiting
;;

*)
echo $"Usage $0 {check|active|accepts|handled|requests|reading|writing|waiting}"
exit
esac

4.给该脚本执行权限

$ chmod 755 nginx.sh

5.创建 userparameter_nginx.conf 配置文件

$ cd /usr/local/zabbix/etc/zabbix_agentd.conf.d

添加以下内容:

$ vim userparameter_nginx.conf
UserParameter=nginx.status[*],/usr/local/zabbix/bin/nginx.sh $1

6.重启 zabbix

$ systemctl restart zabbix-agent.service

7.导入 zbx_nginx_templates.xml 模板

点此下载模板

导入模板添加主机链接到 Template App Nginx 模板即可。

版权声明:本站原创文章,欢迎任何形式的转载。
转载请注明:Zabbix 监控 Nginx status 性能 | 任我乐

Leave a Reply

Your email address will not be published. Required fields are marked *