zabbix监控nginx

本文介绍如何在Zabbix 4.0.3环境下,通过编写shell脚本实现对Nginx服务器的实时监控,包括活动连接数、请求处理情况等关键指标,并详细展示了从脚本编写到配置zabbix-agent的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

zabbix监控Nginx

环境说明

  • 已关闭防火墙、selinux;
  • 所用zabbix版本4.0.3(使用YUM安装);
  • 开启Nginx状态监测,开启配置详情请见Nginx状态监测;
  • 所用IP地址及安装包如下表:
IP地址角色安装包
192.168.99.128serverzabbix-server-mysql
zabbix-web-mysql
zabbix-agent
192.168.99.129agentzabbix_agent
nginx

配置

  • 编写脚本
[root@129 scripts]# vim check_zabbix.sh
#!/bin/bash

NGINX_PORT=80
NGINX_COMMON=$1

function Active_connection () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk '/Active/ {print NF}'
}
function server () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk 'NR==3 {print $1}'
}
function accepts () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk 'NR==3 {print $2}'
}
function handled_requests () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk 'NR==3 {print $3}'
}
function Reading () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk '/Reading/ {print $2}'
}
function Writing () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk '/Writing/ {print $4}'
}
function Waiting () {
curl -s http://127.0.0.1:$NGINX_PORT/status | awk '/Waiting/ {print $6}'
}

case $1 in
	Active_connection)
		Active_connection;
	;;
	server)
		server;
	;;
	accepts)
		acepts;
	;;
	handled_reqursts)
		handled_reqursts;
	;;
	Reading)
		Reading;
	;;
	Writing)
		Writing;
	;;
	Waiting)
		Waiting;
	;;
	*)
		echo "Usage:$0 {Active_connection|server|accepts|handles_reqursts|Reading|Writing|Waiting}"
esac
  • 给脚本赋予执行权限
[root@129 scripts]# chmod a+x check_zabbix.sh
  • etc/zabbix/zabbix.agent.d/目录下创建一个配置文件
[root@129 ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=nginx_status[*],/bin/bash /scripts/check_zabbix.sh $1
  • 重启agent端zabbix-agent服务
[root@129 scripts]# pkill zabbix
[root@129 scripts]# zabbix_agent
  • 在server端进行测试
[root@128 ~]# zabbix_get -s 192.168.99.129 -k nginx_status[server]
61
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值