1. 程式人生 > >zabbix增加手機端4個url地址的返回值

zabbix增加手機端4個url地址的返回值

curl zabbix url

由同事提供4個需要監控的url地址

GET類型:

http://10.15.24.61:809/UserCenterService.svc/getAccountInfo/563/9638

POST類型:

http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/4/0/563/1/9638/1.0

http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/3/0/563/1/9638/1.0

http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/2/0/563/1/9638/1.0


url的返回值

  以下操作,統一是在

10.25.4.7服務器上操作。由於curl的是SLB IP地址,下面的操作,其實在任何1CentOS服務器都可以進行。

  其中,通過執行curl命令,獲取的返回值,GET類型正確的結果如下:

[[email protected] ~]# curl http://10.15.24.61:809/UserCenterService.svc/getAccountInfo/563/9638
"{\"state\":\"1\",\"data\":{\"AccountInfo\":{\"Uid\":\"563\",\"Recieving\":\"0\",\"Collect\":\"0\",\"Reading\":\"0\",\"Recommends\":\"0\",\"Bankcards\":\"0\",\"userName\":\"加加林\",\"IsQuickReg\":\"0\",\"UNameComplete\":\"0\",\"PwdComplete\":\"0\",\"IsOwner\":\"1\",\"IsRecommended\":\"1\",\"IsBindBankCard\":\"1\",\"MyBanks\":[{\"CardPadRightNum\":\"7471\",\"CardStatus\":\"2\"}],\"Email\":\"[email protected]
/* */\",\"EmailVerify\":\"\",\"isVip\":0}},\"errorInfo\":\"\",\"versionCode\":\"\"}"

POST類型的正確結果如下:

[[email protected] ~]# curl -d -XPOST http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/4/0/563/1/9638/1.0
"{\"state\":\"1\",\"data\":{\"productlist\":null,\"rowCount\":0,\"ProSource\":4},\"errorInfo\":\"\",\"versionCode\":\"\"}"

  3POST類型的url返回結果是一致的。

  其中,以上4url的返回結果,只需要關註返回值前面的“state”值後面為“1”,就說明接口是正常的。

  因此,後面的監控都是圍繞這個結果進行。



awk命令獲取url返回值

GET類型:

[[email protected] ~]# curl http://10.15.24.61:809/UserCenterService.svc/getAccountInfo/563/9638  2>/dev/null|grep state|cut -d":" -f 2|cut -d "\"" -f 2|cut -d "\\" -f 1
1

POST類型:

[[email protected] ~]# curl -d-XPOST http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/4/0/563/1/9638/1.02>/dev/null|grep state|cut -d ":" -f 2|cut -d "\""-f 2|cut -d "\\" -f 1
1

  雖然同樣是curl命令,但是,不同類型,參數不太一樣。

  只要這條命令的返回值不是1,就讓zabbix報警。

確認自定義監控腳本的位置

  創建自定義監控項,需要確認自定義監控腳本的位置。有的使用者會把自定義監控命令直接寫到配置文件裏,但是,推薦把監控腳本放在配置文件的include目錄裏。

[[email protected] ~]# head -12 /etc/zabbix/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=10.25.4.8
#ServerActive=10.25.4.6
ServerActive=10.25.4.8
Hostname=10.25.4.8
#Hostname=iZ23kdZ
RefreshActiveChecks=300
BufferSend=5
BufferSize=1000
MaxLinesPerSecond=200
Timeout=10
Include=/etc/zabbix/zabbix_agentd.d/*.conf

  就是上面最後1行配置文件指定的目錄。註意,監控腳本的後綴要求必須是.conf,否則,zabbix不會識別。

相關自定義監控腳本

[[email protected] ~]# ll -tr /etc/zabbix/zabbix_agentd.d/curl*
-rw-r--r-- 1 root root 215Jun 23 14:20 /etc/zabbix/zabbix_agentd.d/curl_post_1.conf
-rw-r--r-- 1 root root 215Jun 23 15:23 /etc/zabbix/zabbix_agentd.d/curl_post_2.conf
-rw-r--r-- 1 root root 215Jun 23 15:28 /etc/zabbix/zabbix_agentd.d/curl_post_3.conf
-rw-r--r-- 1 root root 190 Jun 23 16:11 /etc/zabbix/zabbix_agentd.d/curl_get.conf

  如下是自定義的監控腳本,curl_post_3.conf沒有列出,和前2個幾乎一樣。

[[email protected] ~]# cat /etc/zabbix/zabbix_agentd.d/curl_get.conf 
UserParameter=curl_get,/usr/bin/curl http://10.15.24.61:809/UserCenterService.svc/getAccountInfo/563/9638  2>/dev/null|grep state|cut -d":" -f 2|cut -d "\"" -f 2|cut -d "\\" -f 1
[[email protected] ~]# cat /etc/zabbix/zabbix_agentd.d/curl_post_1.conf 
UserParameter=curl_post_1,/usr/bin/curl -d -XPOST http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/4/0/563/1/9638/1.02>/dev/null|grep state|cut -d ":" -f 2|cut -d "\""-f 2|cut -d "\\" -f 1
[[email protected] ~]# cat /etc/zabbix/zabbix_agentd.d/curl_post_2.conf 
UserParameter=curl_post_2,/usr/bin/curl -d -XPOST http://10.15.24.61:809/ProductService/userInvestVarietyYjsList/3/0/563/1/9638/1.02>/dev/null|grep state|cut -d ":" -f 2|cut -d "\""-f 2|cut -d "\\" -f 1


自定義腳本重啟zabbix agent服務才能生效

#檢查zabbix agent服務進程

[[email protected] ~]# ps -ef|grep zabbix|grep -v "zabbix_server"
root      7847 26915  0 16:57 pts/0    00:00:00 grep --color=auto zabbix
root     18956    1  0 16:12 ?        00:00:00 zabbix_agentd -c/etc/zabbix/zabbix_agentd.conf
root     18957 18956  0 16:12 ?        00:00:00 zabbix_agentd: collector [idle1 sec]
root     18958 18956  0 16:12 ?        00:00:00 zabbix_agentd: listener #1[waiting for connection]
root     18959 18956  0 16:12 ?        00:00:00 zabbix_agentd: listener #2[waiting for connection]
root     18960 18956  0 16:12 ?       00:00:00 zabbix_agentd:listener #3 [waiting for connection]
root     18961 18956  0 16:12 ?        00:00:00 zabbix_agentd: active checks#1 [idle 1 sec]

#殺死zabbix agent服務

[[email protected] ~]# pkill -f /etc/zabbix/zabbix_agentd.conf

#查看進程結果

[[email protected] ~]# ps -ef|grep zabbix|grep -v "zabbix_server"
root     7934 26915  0 16:57 pts/0    00:00:00 grep --color=auto zabbix

#啟動zabbix agent服務

[[email protected] ~]#zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf

#查看啟動服務結果

[[email protected] ~]# ps -ef|grep zabbix|grep -v "zabbix_server"
root      7941    1  0 16:57 ?        00:00:00 zabbix_agentd -c/etc/zabbix/zabbix_agentd.conf
root      7942 7941  0 16:57 ?        00:00:00 zabbix_agentd: collector [idle1 sec]
root      7943 7941  0 16:57 ?        00:00:00 zabbix_agentd: listener #1[waiting for connection]
root      7944 7941  0 16:57 ?        00:00:00 zabbix_agentd: listener #2[waiting for connection]
root      7945 7941  0 16:57 ?        00:00:00 zabbix_agentd: listener #3[waiting for connection]
root      7946 7941  0 16:57 ?        00:00:00 zabbix_agentd: active checks#1 [idle 1 sec]
root     7967 26915  0 16:58 pts/0    00:00:00 grep --color=auto zabbix

  請按照文檔的殺死進程和啟動服務的命令來執行,否則,後果自負。

zabbix_get命令測試結果

[[email protected] ~]# zabbix_get -s 10.25.4.8 -p 10050 -k "curl_post_1"
1
[[email protected] ~]# zabbix_get -s 10.25.4.8 -p 10050 -k "curl_get"
1

  現在,就可以去zabbixweb頁面添加監控項了。


增加Item

  依次點擊“Configuration”→“Host

技術分享

  在頁面下面找到“10.253.4.8”服務,點擊“Items

技術分享

  4個監控項已經添加

技術分享

  4個監控項的任意1個,點擊查看的結果:

技術分享

  只有4個位置需要修改。其中,“Key”值就是“zabbix_get -s 10.25.4.7 -p 10050 -k "curl_get"”命令最後的參數“-k”的值。

  “Name”值建議和“Key”值保持一致。

  “Description”的值是由前面提到的同事提供,就是這個4url的中文說明。

  “Applications”值是因為前面創建過這個監控組。

  再列出1個作為參考:

技術分享

觸發值的創建

  在zabbixWEB界面,很多位置都可以找到這個觸發值,只要是同一臺需要監控的服務器。

技術分享

  “Triggers”創建後的列表:

技術分享

  每個“Triggers”項基本一致:

技術分享

  “Expression”框裏的表達式,需要點擊右邊的“Add”按紐:

技術分享

  在彈出的對話框點擊“Item”右邊的“Select”按紐:

技術分享

  找到對應的監控項,點擊確認。

技術分享

  在返回的對話框裏,點擊“Function”項,選中“Last (most recent) T value is NOT N”項。

  最後的“N”項,輸入“1”。

  點擊“Insert”按紐完成表達式的創建。

技術分享

  在“Trigger”配置頁面裏,Severity項的意思是報警級別。

  截圖中最下面的“Clone”按紐,可以通過它來創建參數相近的觸發值。前面的Items監控項,也有這樣的按紐。



圖形的創建

  點擊“Graphs”按紐,註意是不是對應的服務器。

技術分享

  我把4個監控項的圖形頁面放在了一起,其實,分開也可以。

技術分享

  細節如下:

技術分享

  “Graph type”是圖形類型,一般選擇默認。

  每1個監控項是通過“Add”按紐來添加。

  點擊“Preview”可以看到預覽圖。

  註意:所有修改,必須點擊“Update”按紐才能生效。

  目前已經把4url地址的監控分開配置圖形:

技術分享


curl_get配置:

技術分享

curl_post_1配置:

技術分享

curl_post_2配置:

技術分享

  最後1個也差不多。

  現在,要把4張圖放在一個界面顯示:

  依次點擊“Monitoring”→“Screens

技術分享

  點擊“Create screen”按紐

技術分享

  配置的內容如下:

技術分享

  “Columns”:縱列數

  “Rows”:排數

  點擊“Add”按紐後,在列表裏點擊剛創建的“curl_4

技術分享

  點擊右上角的“Edit screen”按紐

技術分享

  在彈出的頁面裏點擊左上角的“Change”按紐。

技術分享

  在彈出的頁面裏,點擊“Graph”選項的“Select”按紐

技術分享

  在彈出的對話框裏,選擇“curl_get

技術分享

  “Resource”就是“Graph”即可。

  “Dynamic item”的意思是“動態獲取”,應該勾選。

  點擊“Add”按紐完成添加。

  其它3個添加方法相同。

技術分享

  回到zabbix主界面,找到左側的“Favorite screens”,點擊“Action”按紐,在彈出的快捷菜單點擊“Add

技術分享

  在彈出的對話框選中剛才創建的“curl_4”,點擊“Select”按紐

技術分享

  最後的結果:

技術分享

  這樣,就可以在zabbix主界面通過點擊“Favorite screens”添加的screens,同時展示4url的監控狀態。


參考文檔

https://www.abcdocker.com/abcdocker/1453——Zabbix 3.0 部署監控 []


本文出自 “dark-matter” 博客,謝絕轉載!

zabbix增加手機端4個url地址的返回值