Zabbix self-discovery monitors the status of various services in docker containers and containers in Real Time Online Business display

Source: Internet
Author: User
Tags docker ps
This article consists of three parts:
1. view your environment variables and service paths
(1) JDK environment: jdk1.8

(2) zabbix version: zabbix3.4.5
(3) zabbix script storage path:/data/zabbix/scripts/
(4) path to the. conf file:/data/zabbix/etc/zabbix_agentd.conf.d/

2. Configure scripts, keys, and templates
First, configure Vim/data/zabbix/etc/zabbix_agentd.conf.d/docker. conf in zabbix_agentd.

Userparameter = docker. Discovery,/data/zabbix/scripts/docker. py
Userparameter = docker. [*],/data/zabbix/script/docker. py $1 $2

The following is the docker. py script, which uses the automatic discovery rule to discover the container, and then specifies the container to obtain the status information:

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

IfName='Main':
Try:
Name, Action = SYS. argv [1], SYS. argv [2]
Status (name, Action)
Failed t indexerror:
Discover ()

Here we will discuss how to automatically discover the pitfalls of rules... It took me a long time to find out ..... first, JSON format content must be returned, and second, info ['{# containername}'] The key must be written in this way {# containername}
The returned result is as follows:

{"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"}]}

In addition, the function is very simple, that is, the docker command is called to obtain data.

Then import the template:
The template is as follows:

<? 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 <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 <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 <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 <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 <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>
<RecoveryExpression/>
<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> ~~

Template download link: https://pan.baidu.com/share/init? Surl = 18z9qiksulq3sspqslby2a. Password: 3544
3. Web Operations
After the template is imported
Some people may encounter this problem after importing the template:
Zabbixglobal regular expression "container name" does not exist.

This problem proves that the problem is not serious.
Remove this from this location.


If you want to know what this means, please refer to the official website: zabbix Regular Expression writing, the general meaning is as follows:

To manage, general, click regular inside

You just need to know how to use this and add it.
Next, let's take a look at what is monitored in the template.

Trigger has

Check the latest data.

We can see that the status of each container has been added,
By now, you can turn to your leadership.

Zabbix self-discovery monitors the status of various services in docker containers and containers in Real Time Online Business display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.