zabbix监控——Nginx监控

环境说明

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

配置

  • 编写脚本
[root@localhost scripts]# vim nginx_status.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@localhost scripts]# chmod +x nginx_status.sh
  • 在/etc/zabbix/zabbix.agent.d/目录下创建一个配置文件
[root@localhost scripts]# vim /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /scripts/nginx_status.sh "$1"
  • 重启agent端zabbix-agent服务
[root@localhost scripts]# systemctl restart zabbix-agent
  • 在server端进行测试
[root@localhost init.d]# zabbix_get -s 192.168.91.134 -k "nginx_status[server]"
61

你可能感兴趣的:(zabbix监控)