日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控

發布時間:2025/3/18 Nginx 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Zabbix3.0.4添加對Nginx服務的監控


通過Nginx的http_stub_status_module模塊提供的狀態信息來監控,所以在Agent端需要配置Nginx狀態獲取的腳本,和添加key信息等,然后在Server端配置Nginx的監控模板等。請根據自己情況調整,這里只做簡單的參照。


主要是使用Github這個項目的代碼 zabbix-templates


zabbix-server端:192.168.3.108
系統是 centos7.2 zabbix-server是3.0.4版本


Agent端:192.168.386
系統是Centos6.x, Zabbix-agent是3.0版本, Nginx1.11.3 官方最新版本


1.檢查Nginx是否安裝了 http_stub_status_module 模塊,通過下面的命令可以看到編譯參數。


nginx -V?
nginx version: yaya
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)?
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-debug --pid-path=/var/run/nginx/nginx.pid --with-pcre --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --add-module=../nginx_upstream_check_module-master --add-module=../ngx_cache_purge-2.3 --add-module=../ngx_devel_kit-master/ --add-module=../lua-nginx-module-master/ --with-http_stub_status_module


如果沒有這個模塊,還需要重新編譯Nginx.


2.配置Nginx


Nginx 80端口的server配置增加如下的片段
/etc/nginx/nginx.conf
server{
listen ? ? ? *:80 default_server;
location /nginx_status {
? ? ? ? ? ? stub_status on;
? ? ? ? ? ? access_log off;
? ? ? ? ? ? allow 127.0.0.1;
? ? ? ? ? ? allow 192.168.3.108; ?# zabbix 服務端IP
? ? ? ? ? ? deny all;
? ? ? ? }
}

配置完成之后,redload nginx,然后用簡單測試下


curl http://127.0.0.1/nginx_status
Active connections: 2?
server accepts handled requests
?528924 528924 528953?
Reading: 0 Writing: 1 Waiting: 1


3.zabbix-agent 配置


有3個步驟,首先是編寫獲取Nginx信息腳本,接著配置中增加key信息,然后重啟agent 服務。


①編寫Nginx監控腳本,記住路徑,后面配置需要用到,注意腳本權限問題,agent運行用戶要能執行。
mkdir -p /usr/local/zabbix-agent/scripts
cd /usr/local/zabbix-agent/scripts


vim nginx-check.sh


cat nginx-check.sh


#!/bin/bash
##################################
# Zabbix monitoring script
#
# nginx:
# ?- anything available via nginx stub-status module
#
##################################
# Contact:
# ?vincent.viallet@gmail.com
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_URL="http://127.0.0.1/nginx_status"
WGET_BIN="/usr/bin/wget"
#
# 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
NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
# 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


賦予腳本執行權限
chmod o+x /usr/local/zabbix-agent/scripts/nginx-check.sh


②agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定義了其他key的包含目錄 Include=/etc/zabbix/zabbix_agentd.d/, 如果沒有這個配置請自己添加下。接著在 /etc/zabbix/zabbix_agentd.d/ 目錄新建一個文件 nginx-params.conf, 內容如下
cat /etc/zabbix/zabbix_agentd.d/nginx-params.conf


UserParameter=nginx[*],/usr/local/zabbix-agent/scripts/nginx-check.sh "$1"?


③重啟agent
service zabbix-agent restart


Server 的Web端


首先命令行測試下剛才agent好使不,確認好用之后在web端導入模板,之后就可以給對應主機添加監控嘍。


zabbix_get -s 192.168.3.86 -p 10050 -k "nginx[reading]"
0

登錄Zabbix3.0 的web界面,一次選擇 Configuration > Templates , 在主界面的右上角有個 Import 按鈕,用來導入模板。



模板文件比較長留一個下載地址https://github.com/jizhang/zabbix-templates/blob/master/nginx/nginx-template.xml

導入之后就可以給主機添加監控啦。


為了能快速出圖,可以配合壓力測試
ab -c 10 -n 100000 http://192.168.3.86:80/


轉載于:https://www.cnblogs.com/reblue520/p/6784524.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。