1. 程式人生 > >zabbix自發現監控應用app裏的url例子

zabbix自發現監控應用app裏的url例子

zabbix自發現監控服務

需求:


現在有大量url需要監控,形式如http://www.baidu.com ,要求url狀態不為200即報警並且獲得響應時間(url可改成自己應用裏的url)。


需求詳細分析:


大量的url,且url經常變化,現在監控用的是zabbix,如果手動添加模板,會造成大量重復工作,如果利用腳本+mail,無法圖形呈現


解決方案:


zabbix有discovery功能,即可輕松解決此問題


首先我們找一個隨便找一個zabbix客戶端來實現接下來要做的功能


首先我們從頭來,我們來裝一下zabbix客戶端

rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/zabbix-release-3.4-1.el6.noarch.rpm

yum -y install zabbix-agent

vim web_site_code_status.sh

腳本內容為:

#!/bin/bash

# function:monitor tcp connect status from zabbix

source /etc/bashrc >/dev/null 2>&1

source /etc/profile >/dev/null 2>&1

#/usr/bin/curl -o /dev/null -s -w %{http_code} http://$1/

web_site_discovery () {

WEB_SITE=($(cat /etc/zabbix/WEB.txt|grep -v "^#"))

printf '{\n'

printf '\t"data":[\n'

for((i=0;i<${#WEB_SITE[@]};++i))

{

num=$(echo $((${#WEB_SITE[@]}-1)))

if [ "$i" != ${num} ];

then

printf "\t\t{ \n"

printf "\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$i]}\"},\n"

else

printf "\t\t{ \n"

printf "\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$num]}\"}]}\n"

fi

}

}

web_time_total () {

/usr/bin/curl -o /dev/null -s -w %{time_total} http://$1

}

web_site_code () {

/usr/bin/curl -o /dev/null -s -w %{http_code} http://$1

}

case "$1" in

web_site_discovery)

web_site_discovery

;;

web_time_total)

web_time_total $2

;;

web_site_code)

web_site_code $2

;;

*)

echo "Usage:$0 {web_site_discovery|web_site_code [URL]}"

;;

esac



/opt/scripts/WEB.txt 在腳本路徑創建WEB.txt文件,文件內容為要監控url,格式如下(添加自己的實際url):

http://www.baidu.com

http://www.sina.com.cn

http://www.163.com 10.10.10.10

http://www.sohu.com 115.23.16.97:80

http://www.111.com


在zabbix客戶端加配置文件:

vim /etc/zabbix/zabbix_agentd.d/web_site_discovery.conf

UserParameter=web.site.discovery,/etc/zabbix/scripts/web_site_code_status.sh web_site_discovery

UserParameter=web.site.code[*],/etc/zabbix/scripts/web_site_code_status.sh web_site_code $1 $2


測試是否正常:

$ zabbix_get -s 10.0.0.109 -k web.site.discovery

{

"data":[

{

"{#SITENAME}":"www.baidu.com"},

{

"{#SITENAME}":"www.sina.com.cn"},

{

"{#SITENAME}":"www.****.com"},

{

"{#SITENAME}":"www.****.com"}]}

$ zabbix_get -s 10.0.0.109 -k web.site.code[www.163.com]

200

在zabbix server web上添加

技術分享圖片


技術分享圖片技術分享圖片

點擊add


然後去創建發現規則


技術分享圖片


技術分享圖片

技術分享圖片

技術分享圖片


技術分享圖片

技術分享圖片


現在開始創建item

技術分享圖片

技術分享圖片


技術分享圖片



技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片


到此監控完畢!

補充:

curl監控站點響應時間

監控站點首頁下載時間:

curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com

curl -o /dev/null -s -w ‘%{http_code}’ http://www.miotour.com

curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com

結果:2.547

-s 靜默輸出;沒有-s的話就是下面的情況,這是在腳本等情況下不需要的信息。

[ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null -w ‘%{time_total}’ http://www.miotour.com

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 67770 0 67770 0 0 19228 0 –:–:– 0:00:03 –:–:– 20705

結果:3.524

監控首頁各項時間指標:

curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.miotour.com

結果: 0.244: 1.044: 2.672

時間指標解釋 :

time_connect 建立到服務器的 TCP 連接所用的時間

time_starttransfer 在發出請求之後,Web 服務器返回數據的第一個字節所用的時間

time_total 完成請求所用的時間

在 發出請求之後,Web 服務器處理請求並開始發回數據所用的時間是

(time_starttransfer) - (time_connect)0.244 = 0.8 秒

系統運維工程師 : 李超

zabbix自發現監控應用app裏的url例子