zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控
生活随笔
收集整理的這篇文章主要介紹了
zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Zabbix3.0.4添加對(duì)Nginx服務(wù)的監(jiān)控
通過(guò)Nginx的http_stub_status_module模塊提供的狀態(tài)信息來(lái)監(jiān)控,所以在Agent端需要配置Nginx狀態(tài)獲取的腳本,和添加key信息等,然后在Server端配置Nginx的監(jiān)控模板等。請(qǐng)根據(jù)自己情況調(diào)整,這里只做簡(jiǎn)單的參照。
主要是使用Github這個(gè)項(xiàng)目的代碼 zabbix-templates
zabbix-server端:192.168.3.108
系統(tǒng)是 centos7.2 zabbix-server是3.0.4版本
Agent端:192.168.386
系統(tǒng)是Centos6.x, Zabbix-agent是3.0版本, Nginx1.11.3 官方最新版本
1.檢查Nginx是否安裝了 http_stub_status_module 模塊,通過(guò)下面的命令可以看到編譯參數(shù)。
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
如果沒(méi)有這個(gè)模塊,還需要重新編譯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 服務(wù)端IP
? ? ? ? ? ? deny all;
? ? ? ? }
}
配置完成之后,redload nginx,然后用簡(jiǎn)單測(cè)試下
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個(gè)步驟,首先是編寫獲取Nginx信息腳本,接著配置中增加key信息,然后重啟agent 服務(wù)。
①編寫Nginx監(jiān)控腳本,記住路徑,后面配置需要用到,注意腳本權(quán)限問(wèn)題,agent運(yùn)行用戶要能執(zhí)行。
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
賦予腳本執(zhí)行權(quán)限
chmod o+x /usr/local/zabbix-agent/scripts/nginx-check.sh
②agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定義了其他key的包含目錄 Include=/etc/zabbix/zabbix_agentd.d/, 如果沒(méi)有這個(gè)配置請(qǐng)自己添加下。接著在 /etc/zabbix/zabbix_agentd.d/ 目錄新建一個(gè)文件 nginx-params.conf, 內(nèi)容如下
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端
首先命令行測(cè)試下剛才agent好使不,確認(rèn)好用之后在web端導(dǎo)入模板,之后就可以給對(duì)應(yīng)主機(jī)添加監(jiān)控嘍。
zabbix_get -s 192.168.3.86 -p 10050 -k "nginx[reading]"
0
登錄Zabbix3.0 的web界面,一次選擇 Configuration > Templates , 在主界面的右上角有個(gè) Import 按鈕,用來(lái)導(dǎo)入模板。
通過(guò)Nginx的http_stub_status_module模塊提供的狀態(tài)信息來(lái)監(jiān)控,所以在Agent端需要配置Nginx狀態(tài)獲取的腳本,和添加key信息等,然后在Server端配置Nginx的監(jiān)控模板等。請(qǐng)根據(jù)自己情況調(diào)整,這里只做簡(jiǎn)單的參照。
主要是使用Github這個(gè)項(xiàng)目的代碼 zabbix-templates
zabbix-server端:192.168.3.108
系統(tǒng)是 centos7.2 zabbix-server是3.0.4版本
Agent端:192.168.386
系統(tǒng)是Centos6.x, Zabbix-agent是3.0版本, Nginx1.11.3 官方最新版本
1.檢查Nginx是否安裝了 http_stub_status_module 模塊,通過(guò)下面的命令可以看到編譯參數(shù)。
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
如果沒(méi)有這個(gè)模塊,還需要重新編譯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 服務(wù)端IP
? ? ? ? ? ? deny all;
? ? ? ? }
}
配置完成之后,redload nginx,然后用簡(jiǎn)單測(cè)試下
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個(gè)步驟,首先是編寫獲取Nginx信息腳本,接著配置中增加key信息,然后重啟agent 服務(wù)。
①編寫Nginx監(jiān)控腳本,記住路徑,后面配置需要用到,注意腳本權(quán)限問(wèn)題,agent運(yùn)行用戶要能執(zhí)行。
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
賦予腳本執(zhí)行權(quán)限
chmod o+x /usr/local/zabbix-agent/scripts/nginx-check.sh
②agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定義了其他key的包含目錄 Include=/etc/zabbix/zabbix_agentd.d/, 如果沒(méi)有這個(gè)配置請(qǐng)自己添加下。接著在 /etc/zabbix/zabbix_agentd.d/ 目錄新建一個(gè)文件 nginx-params.conf, 內(nèi)容如下
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端
首先命令行測(cè)試下剛才agent好使不,確認(rèn)好用之后在web端導(dǎo)入模板,之后就可以給對(duì)應(yīng)主機(jī)添加監(jiān)控嘍。
zabbix_get -s 192.168.3.86 -p 10050 -k "nginx[reading]"
0
登錄Zabbix3.0 的web界面,一次選擇 Configuration > Templates , 在主界面的右上角有個(gè) Import 按鈕,用來(lái)導(dǎo)入模板。
導(dǎo)入之后就可以給主機(jī)添加監(jiān)控啦。
為了能快速出圖,可以配合壓力測(cè)試
ab -c 10 -n 100000 http://192.168.3.86:80/
轉(zhuǎn)載于:https://www.cnblogs.com/reblue520/p/6784524.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Leetcode中Path的题目总结
- 下一篇: 10分钟理解依赖注入