1. 程式人生 > >【監控】zabbix監控docker容器

【監控】zabbix監控docker容器

一、背景

二、步驟

1.安裝zabbix 服務端與客戶端,詳情可見https://blog.csdn.net/qq_33633013/article/details/81912573,此處不再贅述。
2.zabbix配置
(1)zabbix_server.conf
##指令碼路徑
AlertScriptsPath=/usr/lib/zabbix/alertscripts
(2)zabbix_agentd.conf
##允許使用使用者自定義引數
UnsafeUserParameters=1
##匯入該檔案下的配置
Include=/etc/zabbix/zabbix_agentd.d/*.conf
(3)自定義監控指令碼
##docker_discovery.py,蒐集正在執行的容器的名稱

#!/usr/bin/env python 
import os
import simplejson as json
t=os.popen("""sudo docker ps |grep -v 'CONTAINER ID'|awk {'print $NF'} """)
container_name = []
for container in  t.readlines():
        r = os.path.basename(container.strip())
        container_name += [{'{#CONTAINERNAME}':r}]
print json.dumps({'data':container_name},sort_keys=True,indent=4,separators=(',',':'))

##執行結果
{
    "data":[
        {
            "{#CONTAINERNAME}":"centos2"
        },
        {
            "{#CONTAINERNAME}":"centos1"
        },
        {
            "{#CONTAINERNAME}":"centosv3"
        }
    ]
}
##docker_monitor.py,輸入容器名稱以及監控項,輸出資料

#!/usr/bin/env python
import docker
import sys
import subprocess
import os

def check_container_stats(container_name,collect_item):
    #docker_client = docker_client.containers.get(container_name)
    container_collect=docker_client.containers.get(container_name).stats(stream=True)
    old_result=eval(container_collect.next())
    new_result=eval(container_collect.next())
    container_collect.close()
    if collect_item == 'cpu_total_usage':
        result=new_result['cpu_stats']['cpu_usage']['total_usage'] - old_result['cpu_stats']['cpu_usage']['total_usage']
    elif collect_item == 'cpu_system_usage':
        result=new_result['cpu_stats']['system_cpu_usage'] - old_result['cpu_stats']['system_cpu_usage']
    elif collect_item == 'cpu_percent':
        cpu_total_usage=new_result['cpu_stats']['cpu_usage']['total_usage'] - old_result['cpu_stats']['cpu_usage']['total_usage']
        cpu_system_uasge=new_result['cpu_stats']['system_cpu_usage'] - old_result['cpu_stats']['system_cpu_usage']
        cpu_num=len(old_result['cpu_stats']['cpu_usage']['percpu_usage'])
        result=round((float(cpu_total_usage)/float(cpu_system_uasge))*cpu_num*100.0,2)
    elif collect_item == 'mem_usage':
        result=new_result['memory_stats']['usage']
    elif collect_item == 'mem_limit':
        result=new_result['memory_stats']['limit']
    elif collect_item == 'network_rx_bytes':
        result=new_result['networks']['eth0']['rx_bytes']
    elif collect_item == 'network_tx_bytes':
        result=new_result['networks']['eth0']['tx_bytes']
    elif collect_item == 'mem_percent':
        mem_usage=new_result['memory_stats']['usage']
        mem_limit=new_result['memory_stats']['limit']
        result=round(float(mem_usage)/float(mem_limit)*100.0,2)
    return result
if __name__ == "__main__":
    docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='1.27')
    container_name=sys.argv[1]
    collect_item=sys.argv[2]
    print check_container_stats(container_name,collect_item)

##如果執行報錯,顯示xxx模組未裝,則使用pip安裝即可。
yum install python-pip -y #安裝pip
pip install simplejson #安裝simplejson模組

##執行結果
[
[email protected]
zabbix]# /usr/bin/python /usr/lib/zabbix/alertscripts/docker_monitor.py centosv3 mem_percent 0.02 [[email protected] zabbix]# /usr/bin/python /usr/lib/zabbix/alertscripts/docker_monitor.py centosv3 cpu_percent 0.0
(4)自定義監控配置檔案
##/etc/zabbix/zabbix_agentd.d/docker_discovery.conf
UserParameter=docker_discovery,python /usr/lib/zabbix/alertscripts/docker_discovery.py
UserParameter=docker_status[*],sudo /usr/bin/python /usr/lib/zabbix/alertscripts/docker_monitor.py $1 $2
(5)zabbix介面新增模板以及監控項等
新建模板

在這裡插入圖片描述

點選新建應用集

在這裡插入圖片描述

在這裡插入圖片描述

點選新建自動發現規則

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

點選新建監控項原型

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

點選新建觸發器

在這裡插入圖片描述

在這裡插入圖片描述

新增好之後,點選最新資料檢視資料是否正常

在這裡插入圖片描述

在這裡插入圖片描述

3.可直接使用自制模板
20181030 新增觸發器與圖形
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.4</version>
    <date>2018-10-26T19:25:03Z</date>
    <groups>
        <group>
            <name>Zabbix servers</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Docker Template1</template>
            <name>Docker Template1</name>
            <description/>
            <groups>
                <group>
                    <name>Zabbix servers</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>docker_discovery</name>
                </application>
            </applications>
            <items/>
            <discovery_rules>
                <discovery_rule>
                    <name>docker_discovery</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>docker_discovery</key>
                    <delay>30s</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/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <filter>
                        <evaltype>0</evaltype>
                        <formula/>
                        <conditions>
                            <condition>
                                <macro>{#CONTAINERNAME}</macro>
                                <value/>
                                <operator>8</operator>
                                <formulaid>A</formulaid>
                            </condition>
                        </conditions>
                    </filter>
                    <lifetime>30d</lifetime>
                    <description/>
                    <item_prototypes>
                        <item_prototype>
                            <name>{#CONTAINERNAME} cpu_percent程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} cpu_percent]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} cpu_system_usage程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} cpu_system_usage]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} cpu_total_usage程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} cpu_total_usage]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} mem_limit程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} mem_limit]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} mem_percent程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} mem_percent]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} mem_usage程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} mem_usage]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>3</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} network_rx_bytes程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} network_rx_bytes]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                        <item_prototype>
                            <name>{#CONTAINERNAME} network_tx_bytes程序報警</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>docker_status[{#CONTAINERNAME} network_tx_bytes]</key>
                            <delay>30s</delay>
                            <history>90d</history>
                            <trends>365d</trends>
                            <status>0</status>
                            <value_type>0</value_type>
                            <allowed_hosts/>
                            <units/>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <params/>
                            <ipmi_sensor/>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>docker_discovery</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                    </item_prototypes>
                    <trigger_prototypes>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} cpu_percent].max(5m)}&gt;80</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} cpu_percent報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} cpu_system_usage].max(5m)}&gt;990000000</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} cpu_system_usage程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} cpu_total_usage].max(5m)}&gt;0</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} cpu_total_usage程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} mem_limit].max(5m)}&gt;0</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} mem_limit程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} mem_percent].max(5m)}&gt;0</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} mem_percent程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} mem_usage].max(5m)}&gt;466944</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} mem_usage程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} network_rx_bytes].max(5m)}&gt;2592</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} network_rx_bytes程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Docker Template1:docker_status[{#CONTAINERNAME} network_tx_bytes].max(5m)}&gt;648</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>{#CONTAINERNAME} network_tx_bytes程序報警</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                    </trigger_prototypes>
                    <graph_prototypes>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} cpu_percent程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} cpu_percent]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} cpu_system_usage程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} cpu_system_usage]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} cpu_total_usage程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} cpu_total_usage]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} mem_limit程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} mem_limit]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} mem_percent程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} mem_percent]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} mem_usage程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} mem_usage]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} network_rx_bytes程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} network_rx_bytes]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                        <graph_prototype>
                            <name>{#CONTAINERNAME} network_tx_bytes程序報警</name>
                            <width>900</width>
                            <height>200</height>
                            <yaxismin>0.0000</yaxismin>
                            <yaxismax>100.0000</yaxismax>
                            <show_work_period>1</show_work_period>
                            <show_triggers>1</show_triggers>
                            <type>0</type>
                            <show_legend>1</show_legend>
                            <show_3d>0</show_3d>
                            <percent_left>0.0000</percent_left>
                            <percent_right>0.0000</percent_right>
                            <ymin_type_1>0</ymin_type_1>
                            <ymax_type_1>0</ymax_type_1>
                            <ymin_item_1>0</ymin_item_1>
                            <ymax_item_1>0</ymax_item_1>
                            <graph_items>
                                <graph_item>
                                    <sortorder>0</sortorder>
                                    <drawtype>0</drawtype>
                                    <color>1A7C11</color>
                                    <yaxisside>0</yaxisside>
                                    <calc_fnc>2</calc_fnc>
                                    <type>0</type>
                                    <item>
                                        <host>Docker Template1</host>
                                        <key>docker_status[{#CONTAINERNAME} network_tx_bytes]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                    </graph_prototypes>
                    <host_prototypes/>
                    <jmx_endpoint/>
                </discovery_rule>
            </discovery_rules>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
</zabbix_export>

三、問題總結

(1)配置監控項健值時報錯,docker_status[{#CONTAINERNAME},cpu_percent]
解決方法:/usr/bin/python /usr/lib/zabbix/alertscripts/docker_monitor.py centosv3 cpu_percent,指令碼使用時兩引數為空格分隔,因此健值應為docker_status[{#CONTAINERNAME} cpu_percent]
(2)Value should be a JSON object
解決方法:可能是zabbix沒有sudo許可權和免密執行許可權。在/etc/sudoers 中新增即可,可用zabbix_get -s xxxx -p 10050 -k “docker_status[xxx cpu_percent]” 檢測。