zabbix------以自動添加tomcat端口的一條數據爲例,瞭解low level discovery

1 zabbix的low level discovery的作用(官網):

Low-level discovery provides a way to automatically create items, triggers, and graphs for different entities on a computer. For instance, Zabbix can automatically start monitoring file systems or network interfaces on your machine, without the need to create items for each file system or network interface manually. Additionally it is possible to configure Zabbix to remove unneeded entities automatically based on actual results of periodically performed discovery.

我不知道怎麼翻譯這個low level discovery,不過這個和configuration下面的discovery rules肯定不一樣,那個是主機層次的,這個low level我就暫且理解爲基於主機的細節層次的自動發現,不是發現主機,而是發現主機的特殊元素了,然後server根據從agent獲得的數據,自動添加這些items。目前zabbix已經友好地爲我們添加了大概如下的自動LLD,網絡接口,CPU核心,SNMP OID和盤的容量,官網寫的如下:

  • discovery of file systems;(vfs.fs.discovery)

  • discovery of network interfaces;(net.if.discovery)

  • discovery of CPUs and CPU cores;(system.cpu.discovery)

  • discovery of SNMP OIDs.


2 簡單介紹下個人理解的原理

原理有點像key,首先zabbix客戶端和服務端平常交互都是通過JSON式的格式傳輸數據,而且服務端可以認識這些夾雜着變量的數據。以tomcat爲例,我們的目的是想讓zabbix的web主動添加那些監聽端口而不是我們在模板指定的端口,這個端口是根據agent的特殊情況而決定的。所以我們zabbix的agent通過key的方式發送JSON格式的數據,包含LLD需要的這些變量及其數據,server會根據web的discovery的設置對應到discovery的item,並和其他item一樣類似地監控。下面來看看我的步驟,再自己理解下吧。


3 第一步,想辦法輸出你要捕獲的東西,比如tomcat的端口

# 這一步就是我取出的listener的port的

cat $server_xml|grep "\<Connector port"|awk -F"\"" '{print $2}'


4 第二步,把這些內容輸出爲JSON格式,這裏說下,腳本可能會寫錯或有問題,所以建議有問題的時候sh -x SCRIPT來檢測錯誤,整體的腳本爲

# tomcat.sh
#!/bin/sh
# lld about tomcat7.0.53 ports by liuliancao at 2015/12/23 Wed.

# where is your tomcat server.conf 
server_xml="/opt/tomcat/conf/server.xml"


# print json style output for server
tomcat_discovery(){

# try to catch the port
cat $server_xml|grep "\<Connector port"|awk -F"\"" '{print $2}' > /tmp/tomcat_discovery.out

if [ -s /tmp/tomcat_discovery.out ];then
    item=($(cat /tmp/tomcat_discovery.out))
    printf "{\n"
    printf "\t\"data\":[\n"
    for((i=0;i<${#item[@]};i++))
    {
        num=$(echo $((${#item[@]}-1)))
        if [ "$i" != ${num} ];then
            printf "\t\t{\n"
            printf "\t\t\t\"{#TOMCATPORT}\":\"${item[$i]}\"},\n"
        else
            printf "\t\t{\n"
            printf "\t\t\t\"{#TOMCATPORT}\":\"${item[$num]}\"}\n\t]\n}\n"
        fi
   }
else
   echo "check port listened in your server.xml!"
fi
}

case "$1" in 
discovery)
        tomcat_discovery;;
esac


5  把剛剛的腳本做成一個key,然後你懂的

[root@Zabbix-Server ~]# cat /etc/zabbix/zabbix_agentd.conf.d/tomcat.conf
UserParameter=tomcat.discovery,/bin/sh /etc/zabbix/scripts/tomcat.sh discovery
[root@Zabbix-Server ~]# service zabbix_agentd restart

# 必須滿足下面的類似輸出結果才行,保證key可用

[root@Zabbix-Server ~]# zabbix_get -s localhost -k tomcat.discovery
{
        "data":[
                {
                        "{#TOMCATPORT}":"18080"},
                {
                        "{#TOMCATPORT}":"8443"},
                {
                        "{#TOMCATPORT}":"8009"}
        ]
}


6 上面的內容做出來下面就是web的添加了,和item差不多,不過我們要選擇模板的discovery

wKiom1Z6u1iicjUQAAAXU46WdaA612.png

然後

wKiom1Z6vDuR6edcAAAmL2m83ZY971.png

wKioL1Z6vE_gqyESAACHu3egQP4691.png

這個時候也就是我們做到了把客戶端的JSON數據拿到了,下面就是添加item了,但這個怎麼自動呢,在哪兒添加呢?


7 添加萬itemtype,我們看到,是一排

wKioL1Z6vO2iTBS0AABHdPK827E956.png

按照自己想要的添加就行了,就像平常一樣,這裏我舉個例子

添加的itemtypes

wKiom1Z6vTazY38DAAAvfg1eSXg615.png


最終綁定host後出現了下面的

wKioL1Z6vUnh8_zFAAA7yRhIXus001.png

如果你的item對應的key是對的,那麼就會出現數據。

我的18080和8443都沒監聽,而且8009模板中已經有了,所以就沒有這個添加的item,不過通過這個例子,相信大家已經知道怎麼做了吧。


8 總結下:把想要自定義的數據做成key,key返回JSON格式數據,在web上的模板中添加discovery,在discovery中的itemtypes等添加自己的item(帶你的自定義的參數的),最終去host看是否有這個item,看latest data是否有數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章