牛下载:绿色软件官方软件免费下载基地!
所在位置:首页 > 新闻资讯 > Zabbix3监控Nginx性能状态

Zabbix3监控Nginx性能状态

发布时间:2020-04-09 21:16:36来源:阅读:

本文主要介绍使用zabbix来监控nginx的性能状态,包括当前活动的连接数,已经收到的请求数,已经处理的请求数等。

编辑nginx配置文件

vim /etc/nginx/nginx.conf
server {
        listen  8082;
        location /nginx_status {
                stub_status on;
                access_log   off;
        }
}

重启nginx

/etc/init.d/nginx restart

创建监控脚本

mkdir /etc/zabbix/scripts/
chown zabbix:zabbix -R /etc/zabbix/scripts/
chmod 750 /etc/zabbix/scripts/
vim /etc/zabbix/scripts/nginx-stats.sh
#!/bin/bash
##### OPTIONS VERIFICATION #####
if [[ -z "$1" || -z "$2" || -z "$3" ]]; then
  exit 1
fi
##### PARAMETERS #####
RESERVED="$1"
METRIC="$2"
STATSURL="$3"
CURL="/usr/bin/curl"
CACHE_TTL="55"
CACHE_FILE="/tmp/zabbix.nginx.`echo $STATSURL | md5sum | cut -d" " -f1`.cache"
EXEC_TIMEOUT="1"
NOW_TIME=`date '+%s'`
##### RUN #####
if [ -s "${CACHE_FILE}" ]; then
  CACHE_TIME=`stat -c"%Y" "${CACHE_FILE}"`
else
  CACHE_TIME=0
fi
DELTA_TIME=$((${NOW_TIME} - ${CACHE_TIME}))
#
if [ ${DELTA_TIME} -lt ${EXEC_TIMEOUT} ]; then
  sleep $((${EXEC_TIMEOUT} - ${DELTA_TIME}))
elif [ ${DELTA_TIME} -gt ${CACHE_TTL} ]; then
  echo "" >> "${CACHE_FILE}" # !!!
  DATACACHE=`${CURL} --insecure -s "${STATSURL}" 2>&1`
  echo "${DATACACHE}" > "${CACHE_FILE}" # !!!
  chmod 640 "${CACHE_FILE}"
fi
#
if [ "${METRIC}" = "active" ]; then
  cat "${CACHE_FILE}" | grep "Active connections" | cut -d':' -f2
fi
if [ "${METRIC}" = "accepts" ]; then
  cat "${CACHE_FILE}" | sed -n '3p' | cut -d" " -f2
fi
if [ "${METRIC}" = "handled" ]; then
  cat "${CACHE_FILE}" | sed -n '3p' | cut -d" " -f3
fi
if [ "${METRIC}" = "requests" ]; then
  cat "${CACHE_FILE}" | sed -n '3p' | cut -d" " -f4
fi
if [ "${METRIC}" = "reading" ]; then
  cat "${CACHE_FILE}" | grep "Reading" | cut -d':' -f2 | cut -d' ' -f2
fi
if [ "${METRIC}" = "writing" ]; then
  cat "${CACHE_FILE}" | grep "Writing" | cut -d':' -f3 | cut -d' ' -f2
fi
if [ "${METRIC}" = "waiting" ]; then
  cat "${CACHE_FILE}" | grep "Waiting" | cut -d':' -f4 | cut -d' ' -f2
fi
#
exit 0

增加执行权限

chown zabbix:zabbix /etc/zabbix/scripts/nginx-stats.sh
chmod 540 /etc/zabbix/scripts/nginx-stats.sh

测试脚本

sudo -u zabbix /etc/zabbix/scripts/nginx-stats.sh none active http://192.168.42.70:8082/nginx_status

到zabbix agent添加nginx监控

vim /etc/zabbix/zabbix_agentd.conf
UserParameter=nginx[*],/etc/zabbix/scripts/nginx-stats.sh "none" "$1" "$2"

重启zabbix agent

/etc/init.d/zabbix-agentd restart

测试监控

zabbix_get -s HOST -k "nginx[active,http://192.168.42.70:8082/nginx_status]"

添加监控项

在zabbix web中依次添加监控项,如添加active

nginx[active,http://192.168.42.70:8082/nginx_status]

完成后依次添加accepts,handled,requests,reading,writing,waiting的监控

反对
收藏
  • 热门资讯
  • 最新资讯
  • 应用排行榜
  • 游戏排行榜