標籤: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的話就是下面的情況,這是在指令碼等情況下不需要的資訊。
[[email protected] ~]$ 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例子