zabbix4.2學習筆記--監控nginx

圖解一個客戶端鏈接開源版本的Nginx狀況
nginx
Accepts(接受)、Handled(已處理)、Requests(請求數)是一直在增長的計數器。Active(活躍)、Waiting(等待)、Reading(讀)、Writing(寫)隨着請求量而增減。nginx

環境

主機 ip zabbix版本 關係
centos7.5 192.168.181.135 zabbix4.2 服務端
centos7.5 192.168.181.136 zabbix4.2 客戶端

Nginx相關指標的收集

首先在nginx配置文件中打開stub_status功能,並經過nginx -s reload命令從新加載配置文件git

vim /usr/local/nginx/conf/nginx.conf

server {
    # zabbix監控nginx基本活躍指標
    location /ngx_status {
        stub_status on;
        access_log off;
        allow 192.168.181.0/24;
        allow 127.0.0.1;
        deny all;
        }

nginx -s reload

zabbix客戶端配置腳本指令

cat /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf

UserParameter=nginx.status[*],/etc/zabbix/scripts/ngx-status.sh $1

建立文件夾github

mkdir /etc/zabbix/scripts

編輯ngx-status.sh腳本vim

#!/bin/bash

#########################################################################
# File Name: nginx.sh
# file_path:
# Author: 浪子塵心
# Mail: 536418286@qq.com
# Created Time: 2019-03-17 16:50:21
# Last Changed: 2019-03-17 16:52:06
# Description: zabbix 經過 status 模塊監控 nginx
# Version:
#########################################################################

ST="127.0.0.1"
PORT="80"

# 檢測nginx相關參數
case $1 in
    ping)
    result=`/bin/pidof nginx 2>/dev/null| wc -l`
    echo $result
    ;;
    active)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'`
    echo $result
    ;;
    reading)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'`
    echo $result
    ;;
    writing)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'`
    echo $result
    ;;
    waiting)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'`
    echo $result
    ;;
    accepts)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'`
    echo $result
    ;;
    handled)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'`
    echo $result
    ;;
    requests)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'`
    echo $result
    ;;
    dropped)
    result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1-$2}'`
    echo $result
    ;;
    *)
     echo "Usage:$0(ping|active|reading|writing|waiting|accepts|handled|requests|dropped)"
    ;;
esac

編輯完成添加執行權限並重啓客戶端centos

chmod +x /etc/zabbix/scripts/ngx-status.sh
systemctl restart zabbix-agent

zabbix頁面導入模板

模板下載
模板下載完成以後,在zabbix頁面點擊導入模板,點擊主機添加模板便可
nignx1
nignx2bash

相關文章
相關標籤/搜索