zabbix监控Nginx

环境说明

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

IP地址

角色

安装包

192.168.99.128

server

zabbix-server-mysql

zabbix-web-mysql

zabbix-agent

192.168.99.129

agent

zabbix_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