主頁 > 知識庫 > zabbix監(jiān)控docker容器狀態(tài)【推薦】

zabbix監(jiān)控docker容器狀態(tài)【推薦】

熱門標簽:威海人工智能電銷機器人系統(tǒng) 德州外呼系統(tǒng)排名 好看的地圖標注圖標下載 地圖標注員怎么去做 貴州全自動外呼系統(tǒng)廠家 百度地圖標注備注 機器人電銷有什么用 上海電銷卡外呼系統(tǒng)廠家 福州外呼系統(tǒng)中間件

前言:前段時間在部署zabbix,有個需求就是需要監(jiān)控容器的狀態(tài) 也就是cpu 內存 io的占用,于是就自己寫了一個腳本,以及模板,在這里分享一下 嘿嘿 : )

廢話我也就不多說,直接開始

首選,zabbix_agentd 配置  vim /usr/local/zabbix/etc/zabbix_agentd.conf

UserParameter=docker.discovery,/usr/local/zabbix/script/docker.py
UserParameter=docker.[*],/usr/local/zabbix/script/docker.py $1 $2

下面是docker.py 腳本,采用自動發(fā)現(xiàn)規(guī)則來發(fā)現(xiàn)容器,然后指定容器獲取狀態(tài)信息

#!/usr/bin/python
import sys
import os
import json
def discover():
 d = {}
 d['data'] = []
 with os.popen("docker ps -a --format {{.Names}}") as pipe:
  for line in pipe:
   info = {}
   info['{#CONTAINERNAME}'] = line.replace("\n","")
   d['data'].append(info)
 print json.dumps(d)
def status(name,action):
 if action == "ping":
  cmd = 'docker inspect --format="{{.State.Running}}" %s' %name
  result = os.popen(cmd).read().replace("\n","")
  if result == "true":
   print 1
  else:
   print 0
 else:
  cmd = 'docker stats %s --no-stream --format "{{.%s}}"' % (name,action)
  result = os.popen(cmd).read().replace("\n","")
  if "%" in result:
   print float(result.replace("%",""))
  else:
   print result
if __name__ == '__main__':
  try:
    name, action = sys.argv[1], sys.argv[2]
    status(name,action)
  except IndexError:
    discover()

這里說一下自動發(fā)現(xiàn)規(guī)則的坑。。。我被坑了好久才找出來.....一是必須返回json格式內容,二是 info['{#CONTAINERNAME}' ]  這個key一定要這么寫{#CONTAINERNAME}......

返回結果如下,一定要是這樣的層級關系....

{"data": [{"{#CONTAINERNAME}": "node-3"}, {"{#CONTAINERNAME}": "node-2"}, {"{#CONTAINERNAME}": "node-1"}, {"{#CONTAINERNAME}": "web"}, {"{#CONTAINERNAME}": "cadvisor"}, {"{#CONTAINERNAME}": "updatol"}, {"{#CONTAINERNAME}": "research"}, {"{#CONTAINERNAME}": "services"}, {"{#CONTAINERNAME}": "data"}, {"{#CONTAINERNAME}": "rabbitmq"}, {"{#CONTAINERNAME}": "redis"}, {"{#CONTAINERNAME}": "mysql"}, {"{#CONTAINERNAME}": "ssdb"}]}

另外那個函數(shù)的很簡單了,就是調用docker 命令在獲取數(shù)據(jù)的。

自動發(fā)現(xiàn)規(guī)則呢 也就是這樣

只監(jiān)控的這幾個狀態(tài),以及還有一個觸發(fā)器就是ping 來檢測當前這個容器狀態(tài)是否運行,如果不是就報警。

模板如下

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
 <version>3.2</version>
 <date>2018-06-04T04:12:36Z</date>
 <groups>
  <group>
   <name>Templates</name>
  </group>
 </groups>
 <templates>
  <template>
   <template>docker-status</template>
   <name>docker-status</name>
   <description/>
   <groups>
    <group>
     <name>Templates</name>
    </group>
   </groups>
   <applications>
    <application>
     <name>docker_test</name>
    </application>
   </applications>
   <items/>
   <discovery_rules>
    <discovery_rule>
     <name>docker.discovery</name>
     <type>0</type>
     <snmp_community/>
     <snmp_oid/>
     <key>docker.discovery</key>
     <delay>60</delay>
     <status>0</status>
     <allowed_hosts/>
     <snmpv3_contextname/>
     <snmpv3_securityname/>
     <snmpv3_securitylevel>0</snmpv3_securitylevel>
     <snmpv3_authprotocol>0</snmpv3_authprotocol>
     <snmpv3_authpassphrase/>
     <snmpv3_privprotocol>0</snmpv3_privprotocol>
     <snmpv3_privpassphrase/>
     <delay_flex/>
     <params/>
     <ipmi_sensor/>
     <authtype>0</authtype>
     <username/>
     <password/>
     <publickey/>
     <privatekey/>
     <port/>
     <filter>
      <evaltype>0</evaltype>
      <formula/>
      <conditions>
       <condition>
        <macro>{#CONTAINERNAME}</macro>
        <value>@ CONTAINER NAME</value>
        <operator>8</operator>
        <formulaid>A</formulaid>
       </condition>
      </conditions>
     </filter>
     <lifetime>30</lifetime>
     <description/>
     <item_prototypes>
      <item_prototype>
       <name>Container {#CONTAINERNAME} Diskio usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME} ,BlockIO]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>0</trends>
       <status>0</status>
       <value_type>1</value_type>
       <allowed_hosts/>
       <units/>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container{#CONTAINERNAME} CPU usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME},CPUPerc]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>365</trends>
       <status>0</status>
       <value_type>0</value_type>
       <allowed_hosts/>
       <units>%</units>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container {#CONTAINERNAME} mem usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME},MemPerc]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>365</trends>
       <status>0</status>
       <value_type>0</value_type>
       <allowed_hosts/>
       <units>%</units>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container {#CONTAINERNAME} NETio usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME},NetIO]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>0</trends>
       <status>0</status>
       <value_type>1</value_type>
       <allowed_hosts/>
       <units/>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container{#CONTAINERNAME} is_run :</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME} ,ping]</key>
       <delay>30</delay>
       <history>90</history>
       <trends>365</trends>
       <status>0</status>
       <value_type>3</value_type>
       <allowed_hosts/>
       <units/>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
     </item_prototypes>
     <trigger_prototypes>
      <trigger_prototype>
       <expression>{docker-status:docker.[{#CONTAINERNAME} ,ping].last()}=0</expression>
       <recovery_mode>0</recovery_mode>
       <recovery_expression/>
       <name>docker_{#CONTAINERNAME}_down</name>
       <correlation_mode>0</correlation_mode>
       <correlation_tag/>
       <url/>
       <status>0</status>
       <priority>5</priority>
       <description/>
       <type>0</type>
       <manual_close>0</manual_close>
       <dependencies/>
       <tags/>
      </trigger_prototype>
     </trigger_prototypes>
     <graph_prototypes/>
     <host_prototypes/>
    </discovery_rule>
   </discovery_rules>
   <httptests/>
   <macros/>
   <templates/>
   <screens/>
  </template>
 </templates>
</zabbix_export>

修改Zabbix_agentd 配置,docker.py腳本放在指定路徑下,不要忘了給權限,導入模板,能獲取數(shù)據(jù)就沒問題。獲取不了的,可以zabbix_get 來調試 找到問題出在哪去解決。

總結

以上所述是小編給大家介紹的zabbix監(jiān)控docker容器狀態(tài),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

標簽:南陽 邵陽 岳陽 白城 泉州 南陽 撫州 葫蘆島

巨人網(wǎng)絡通訊聲明:本文標題《zabbix監(jiān)控docker容器狀態(tài)【推薦】》,本文關鍵詞  zabbix,監(jiān)控,docker,容器,狀態(tài),;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《zabbix監(jiān)控docker容器狀態(tài)【推薦】》相關的同類信息!
  • 本頁收集關于zabbix監(jiān)控docker容器狀態(tài)【推薦】的相關信息資訊供網(wǎng)民參考!
  • 推薦文章