Configure nginx template on Zabbix

참고 URL
https://github.com/zbal/zabbix

nginx site config 파일 생성
: vi /data/apps/ln/nginx/conf/sites-enabled/nginx_status.conf

server {
    listen 10061;

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

nginx 재시작

[root@10-19-10-10 /]# systemctl restart nginx

nginx check file 추가
: vi /etc/zabbix/bin/nginx_status.bash

#!/bin/bash
##################################
# Zabbix monitoring script
#
# nginx:
#  - anything available via nginx stub-status module
#
##################################
# Contact:
#  vincent.viallet@gmail.com
##################################
# ChangeLog:
#  20100922	VV	initial creation
##################################

# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"

# Nginx defaults
NGINX_STATUS_DEFAULT_URL="http://localhost:10061/nginx_status"
WGET_BIN="/usr/bin/wget"
CURL_BIN="/usr/bin/curl"

if [ ! -f $WGET_BIN ];
then
    USE_CURL=true
fi

#
# Error handling:
#  - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
#  - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.9900"
ERROR_NO_ACCESS="-0.9901"
ERROR_WRONG_PARAM="-0.9902"
ERROR_DATA="-0.9903" # either can not connect /	bad host / bad port

# Handle host and port if non-default
if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
  URL="$ZBX_REQ_DATA_URL"
else
  URL="$NGINX_STATUS_DEFAULT_URL"
fi

# save the nginx stats in a variable for future parsing
if [ ! $USE_CURL = true ]; then
  NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
else
  NGINX_STATS=$($CURL_BIN -S -s $URL)
fi

# error during retrieve
if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
  echo $ERROR_DATA
  exit 1
fi

#
# Extract data from nginx stats
#
case $ZBX_REQ_DATA in
  active_connections)   echo "$NGINX_STATS" | head -1             | cut -f3 -d' ';;
  accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';;
  handled_connections)  echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';;
  handled_requests)     echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';;
  reading)              echo "$NGINX_STATS" | tail -1             | cut -f2 -d' ';;
  writing)              echo "$NGINX_STATS" | tail -1             | cut -f4 -d' ';;
  waiting)              echo "$NGINX_STATS" | tail -1             | cut -f6 -d' ';;
  *) echo $ERROR_WRONG_PARAM; exit 1;;
esac

exit 0

UserParameter 파일 추가
: vi /etc/zabbix/zabbix_agentd.d/nginx_status.conf

UserParameter=nginx[*], bash /etc/zabbix/bin/nginx_status.bash "$1" "$2"

자빅스 재시작

[root@10-19-10-10 /]# systemctl restart zabbix_agentd

템플릿 파일 임포트
Tempalate URL

zabbix-nginx-template1

host에 template 적용

zabbix-nginx-template2

적용 후 그래프 확인

zabbix-nginx-template3

You may also like...