zabbix 监控nginx

zabbix  监控nginx

nginx有status状态模块,可以通过这个模块来反馈状态。

流程:

  • nginx增加status状态
  • agent端,自定义key
  • web页面,定义模板(模板-应用集-监控项-触发器)
  • web页面,为客户机添加模板

第一步,nginx增加status状态

对要监控的nginx配置文件进行修改,增加status状态

#在server中增加
location /nginx_status {
	stub_status on;
	access_log off;
	allow 127.0.0.1;
	deny all;
	}

重启nginx:

systemctl restart nginx

查看nginx_status

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-18.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
#状态解释
Active connections  Nginx正处理的活动链接数个数;重要
server              Nginx启动到现在共处理了多少个连接。
accepts             Nginx启动到现在共成功创建几次握手。 
handled requests    Nginx总共处理了几次请求。
Reading             Nginx读取到客户端的 Header 信息数。
Writing             Nginx返回给客户端的 Header 信息数。
Waiting             Nginx已经处理完正在等候下一次请求指令的驻留链接,开启。
Keep-alive的情况下,Waiting这个值等于active-(reading + writing)。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。

nginx状态查询可以编写脚本进行测试:

提前放zabbix目录方便以后使用

vi /etc/zabbix/zabbix_agentd.d/nginx_status.sh

#cd /etc/zabbix/zabbix_agentd.d/
#vim nginx_monitor.sh
HOST=127.0.0.1
NGINX_STATUS='/nginx_status/'
NGINX_PORT=80  
NGINX_COMMAND=$1

nginx_ping(){
    /usr/sbin/pidof nginx| wc -l
}
nginx_active(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk '/Active/ {print $NF}'
}
nginx_reading(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk '/Reading/ {print $2}'
}
nginx_writing(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk '/Writing/ {print $4}'
       }
nginx_waiting(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk '/Waiting/ {print $6}'
       }
nginx_accepts(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk 'NR==3 {print $1}'
       }
nginx_handled(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk 'NR==3 {print $2}'
       }
nginx_requests(){
    /usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk 'NR==3 {print $3}'
       }
  case $NGINX_COMMAND in
ping)
nginx_ping;
;;
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
      *)
echo $"USAGE:$0 {ping|active|reading|writing|waiting|accepts|handled|requests}"
    esac

测试脚本:

#添加执行权限
chmod +x /etc/zabbix/zabbix_agentd.d/nginx_status.sh
#测试ping结果
/etc/zabbix/zabbix_agentd.d/nginx_status.sh ping
返回为1存在,返回0不存在

第二步,agent端,自定义key

查看/etc/zabbix/zabbix_agent.conf,可以看到自定义key注释和include路径。

key语法:UserParameter=<key>,<shell command> (UserParameter=<键值名称>,<要执行的命令或脚本>)

include路径:Include=/etc/zabbix/zabbix_agentd.d/*.conf

得到,key语法和key路径,我们就可以编辑自定义key了。

(key执行的脚本,上面写的nginx_status.sh)

#创建自定义key配置文件
vi /etc/zabbix/zabbix_agentd.d/nginx_status.conf
#配置文件,加入以下内容
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh

(不推荐,尽量使用脚本)nginx状态单行写键值:

UserParameter=nginx_ping,/usr/sbin/pidof nginx| wc -l
UserParameter=nginx_active,/usr/bin/curl -s $HOST:$NGINX_PORT$NGINX_STATUS|awk '/Active/ {print $NF}'
UserParameter=等等等,以此类推很繁琐,所以不是很推荐

key键定义好后,重启agent

systemctl restart zabbix-agent

到server端,可以通过agent-get测试,我们刚写的键值

#-s 地址 -p 端口号 -k '键值名称'
zabbix_get -s 《agent端ip地址》-p 10050 -k 'nginx_status[active]'
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-19.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

第三步,web页面,定义模板(模板-应用集-监控项-触发器)

向web添加东西,都需要从“配置”模块开始!

创建模板,模板可以理解为一个类别。

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-20.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-21.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

模板中添加应用集(应用集,可以理解为子类别或是一个文件夹)

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-22.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-23.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-25.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

创建监控项

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-26.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-27.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

创建触发器

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-28.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-29.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-30.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-31.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

创建图形:

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-32.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-33.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-34.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

ok,一个监控项完成,以此类推,我们需要重复添加监控项的其他状态

web页面,agent客户机添加模板

模板创建好后,我们要返回配置-主机,哪些主机需要使用这个模板,我们就为该主机增加模板即可。

一个主机可以有N个模板,实现监控N个模块。

这里为我们的agent客户机:vm-1添加nginx状态

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-35.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

添加模板(刚刚创建的app模板)

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-36.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

添加完成后,就可以通过仪表盘进行查看了。

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-37.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-38.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
good good study, day day up!

发表评论

textsms
account_circle
email

zabbix 监控nginx
nginx有status状态模块,可以通过这个模块来反馈状态。 流程: nginx增加status状态agent端,自定义keyweb页面,定义模板(模板-应用集-监控项-触发器)web页面,为客户机添加模板 …
扫描二维码继续阅读
2021-11-05