欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

zabbix监控docker容器状态【推荐】

程序员文章站 2022-12-06 20:54:54
前言:前段时间在部署zabbix,有个需求就是需要监控容器的状态 也就是cpu 内存 io的占用,于是就自己写了一个脚本,以及模板,在这里分享一下 嘿嘿 : ) 废话我也...

前言:前段时间在部署zabbix,有个需求就是需要监控容器的状态 也就是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 脚本,采用自动发现规则来发现容器,然后指定容器获取状态信息

#!/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()

这里说一下自动发现规则的坑。。。我被坑了好久才找出来.....一是必须返回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"}]}

另外那个函数的很简单了,就是调用docker 命令在获取数据的。

自动发现规则呢 也就是这样

只监控的这几个状态,以及还有一个触发器就是ping 来检测当前这个容器状态是否运行,如果不是就报警。

模板如下

<?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脚本放在指定路径下,不要忘了给权限,导入模板,能获取数据就没问题。获取不了的,可以zabbix_get 来调试 找到问题出在哪去解决。

总结

以上所述是小编给大家介绍的zabbix监控docker容器状态,希望对大家有所帮助