1、环境说明
本zabbix-server系列是docker安装,参考:https://github.com/zabbix/zabbix-docker,为适应本地环境和需求,docker-compose.yml文件有改动,具体内容如下:
docker-compose.yml文件
version: '3.5' services: zabbix-server: image: zabbix/zabbix-server-mysql:centos-4.2-latest ports: - "10051:10051" volumes: - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro - ./zbx_env/usr/lib/zabbix/alertscripts:/usr/lib/zabbix/alertscripts:ro - ./zbx_env/usr/lib/zabbix/externalscripts:/usr/lib/zabbix/externalscripts:ro - ./zbx_env/var/lib/zabbix/modules:/var/lib/zabbix/modules:ro - ./zbx_env/var/lib/zabbix/enc:/var/lib/zabbix/enc:ro - ./zbx_env/var/lib/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro - ./zbx_env/var/lib/zabbix/mibs:/var/lib/zabbix/mibs:ro links: - mysql-server:mysql-server container_name: zabbix-server restart: unless-stopped ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000 env_file: - .env_db_mysql - .env_srv secrets: - MYSQL_USER - MYSQL_PASSWORD - MYSQL_ROOT_PASSWORD user: root depends_on: - mysql-server stop_grace_period: 30s sysctls: - net.ipv4.ip_local_port_range=1024 65000 - net.ipv4.conf.all.accept_redirects=0 - net.ipv4.conf.all.secure_redirects=0 - net.ipv4.conf.all.send_redirects=0 zabbix-web-nginx-mysql: image: zabbix/zabbix-web-nginx-mysql:centos-4.2-latest ports: - "8081:80" - "8443:443" links: - mysql-server:mysql-server - zabbix-server:zabbix-server container_name: zabbix-web-nginx-mysql restart: unless-stopped volumes: - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro - ./zbx_env/etc/ssl/nginx:/etc/ssl/nginx:ro env_file: - .env_db_mysql - .env_web secrets: - MYSQL_USER - MYSQL_PASSWORD user: root depends_on: - mysql-server - zabbix-server healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 10s timeout: 5s retries: 3 start_period: 30s stop_grace_period: 10s sysctls: - net.core.somaxconn=65535 zabbix-agent: image: zabbix/zabbix-agent:centos-4.2-latest ports: - "10050:10050" volumes: - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro - ./zbx_env/etc/zabbix/zabbix_agentd.d:/etc/zabbix/zabbix_agentd.d:ro - ./zbx_env/usr/lib/zabbix/alertscripts:/usr/lib/zabbix/alertscripts:ro - ./zbx_env/usr/lib/zabbix/externalscripts:/usr/lib/zabbix/externalscripts:ro - ./zbx_env/var/lib/zabbix/modules:/var/lib/zabbix/modules:ro - ./zbx_env/var/lib/zabbix/enc:/var/lib/zabbix/enc:ro - ./zbx_env/var/lib/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro links: - zabbix-server:zabbix-server restart: unless-stopped container_name: zabbix-agent env_file: - .env_agent user: root privileged: true pid: "host" stop_grace_period: 5s mysql-server: image: mysql:8.0 ports: - "33060:3306" command: [mysqld, --character-set-server=utf8, --collation-server=utf8_bin, --default-authentication-plugin=mysql_native_password] volumes: - ./zbx_env/var/lib/mysql:/var/lib/mysql:rw restart: unless-stopped container_name: mysql-server env_file: - .env_db_mysql secrets: - MYSQL_USER - MYSQL_PASSWORD - MYSQL_ROOT_PASSWORD user: root stop_grace_period: 1m secrets: MYSQL_USER: file: ./.MYSQL_USER MYSQL_PASSWORD: file: ./.MYSQL_PASSWORD MYSQL_ROOT_PASSWORD: file: ./.MYSQL_ROOT_PASSWORD
2、zabbix添加自定义监控
(1)zabbix_server.conf配置
#脚本路径
AlertScriptsPath=/usr/lib/zabbix/alertscripts ExternalScripts=/usr/lib/zabbix/externalscripts
(2)zabbix_agent.conf配置
##允许使用用户自定义参数 UnsafeUserParameters=1 ##导入该文件下的配置 Include=/etc/zabbix/zabbix_agentd.d/*.conf
(3)自定义监控脚本
/usr/lib/zabbix/externalscripts/docker_discovery.py,搜集正在运行的容器的名称
#!/usr/bin/env python # -*- coding: utf-8 -* import os import simplejson as json t=os.popen("""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=(',',':')))
安装python3 所需要的包
pip3 install simplejson
docker_discovery.py执行结果
python3 docker_discovery.py { "data":[ { "{#CONTAINERNAME}":"nginx" }, { "{#CONTAINERNAME}":"fortune" } ] }
/usr/lib/zabbix/externalscripts/docker_monitor.py,输入容器名称以及监控项,输出数据
#!/usr/bin/env python import docker import sys import subprocess import os def check_container_stats(container_name,collect_item): if collect_item == "ping": cmd = 'docker inspect --format="{{.State.Running}}" %s' % container_name result = os.popen(cmd).read().replace("\n","") if result == "true": return 1 else: return 0 #:docker_client = docker_client.containers.get(container_name) container_collect=docker_client.containers.get(container_name).stats(stream=True) old_result=eval(next(container_collect)) new_result=eval(next(container_collect)) print(new_result) 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') container_name=sys.argv[1] collect_item=sys.argv[2] print(check_container_stats(container_name,collect_item)
安装python3依赖的包
pip3 install docker
docker_monitor.py执行结果
python3 docker_monitor.py nginx mem_percent
0.49
(4)脚本赋执行权限
chmod +x docker_discovery.py
chmod +x docker_monitor.py
(5)自定义监控配置文件
/etc/zabbix/zabbix_agentd.d/docker_discovery.conf UserParameter=docker_discovery,/usr//bin/python3 /usr/lib/zabbix/externalscripts/docker_discovery.py UserParameter=docker_status[*],/usr/bin/python3 /usr/lib/zabbix/externalscripts/docker_monitor.py $1 $2
(6)导入模板
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>4.2</version> <date>2019-09-27T06:35:12Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template Discovery Docker</template> <name>Template Discovery Docker</name> <description/> <groups> <group> <name>Templates</name> </group> </groups> <applications/> <items/> <discovery_rules> <discovery_rule> <name>collect docker container use resource</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>docker:{#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>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#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>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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#CONTAINERNAME}:men_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>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#CONTAINERNAME}:men_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>0</value_type> <allowed_hosts/> <units>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#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>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#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>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#CONTAINERNAME}:is run</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>docker_status[{#CONTAINERNAME},ping]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>0</value_type> <allowed_hosts/> <units>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> <item_prototype> <name>docker:{#CONTAINERNAME}:cpu_system_usage</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>docker_status[{#CONTAINERNAME},system_cpu_usage]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>0</value_type> <allowed_hosts/> <units>%</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/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <application_prototypes/> <master_item/> </item_prototype> </item_prototypes> <trigger_prototypes/> <graph_prototypes> <graph_prototype> <name>docker:{#CONTAINERNAME}:cpu</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>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},cpu_percent]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>F63100</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},system_cpu_usage]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>2774A4</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},cpu_total_usage]</key> </item> </graph_item> </graph_items> </graph_prototype> <graph_prototype> <name>docker:{#CONTAINERNAME}:menory</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>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},mem_percent]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>F63100</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},mem_usage]</key> </item> </graph_item> </graph_items> </graph_prototype> <graph_prototype> <name>docker:{#CONTAINERNAME}:network</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>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},network_rx_bytes]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>F63100</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template Discovery Docker</host> <key>docker_status[{#CONTAINERNAME},network_tx_bytes]</key> </item> </graph_item> </graph_items> </graph_prototype> </graph_prototypes> <host_prototypes/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <lld_macro_paths/> <preprocessing/> <master_item/> </discovery_rule> </discovery_rules> <httptests/> <macros/> <templates/> <screens/> <tags/> </template> </templates> </zabbix_export>
模板导入成功后,查看监控项是否可用,注意客户端版本是否和文章一致,这里的zabbix所有版本均为4.2,若有问题可留言讨论。
3、问题整理
由于原本客户端python是2.7版本,在执行docker_monitor.py时有如下报错,后来知道是python版本问题,升级python3以后执行成功。
# python docker_monitor.py
Traceback (most recent call last): File "docker_monitor.py", line 36, in <module> docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock') AttributeError: 'module' object has no attribute 'DockerClient'
(1)python2升级步骤
#安装python3.6 yum install python36u -y ln -s /usr/bin/python3.6 /bin/python3 #安装pip3 yum install python36u-pip -y ln -s /usr/bin/pip3.6 /bin/pip3 pip3 install --upgrade pip
(2)升级python3后,simplejson和docker-py需要重装
pip3 install simplejson #docker-py最新版改名为docker pip3 install docker
(3)zabbix-serve端测试时报错
zabbix_get -s 10.8.0.22 -p 10050 -k docker_status[reids,cpu_percent] Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 672, in urlopen chunked=chunked, File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request conn.request(method, url, **httplib_request_kw) File "/usr/lib64/python3.6/http/client.py", line 1254, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1300, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1249, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1036, in _send_output self.send(msg) File "/usr/lib64/python3.6/http/client.py", line 974, in send self.connect() File "/usr/local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 43, in connect sock.connect(self.unix_socket) PermissionError: [Errno 13] Permission denied During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send timeout=timeout File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 720, in urlopen method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2] File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 400, in increment raise six.reraise(type(error), error, _stacktrace) File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 734, in reraise raise value.with_traceback(tb) File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 672, in urlopen chunked=chunked, File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request conn.request(method, url, **httplib_request_kw) File "/usr/lib64/python3.6/http/client.py", line 1254, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1300, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1249, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1036, in _send_output self.send(msg) File "/usr/lib64/python3.6/http/client.py", line 974, in send self.connect() File "/usr/local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 43, in connect sock.connect(self.unix_socket) urllib3.exceptions.ProtocolError: ('Connection aborted.', PermissionError(13, 'Permission denied')) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/lib/zabbix/externalscripts/docker_monitor.py", line 39, in <module> print(check_container_stats(container_name,collect_item)) File "/usr/lib/zabbix/externalscripts/docker_monitor.py", line 9, in check_container_stats container_collect=docker_client.containers.get(container_name).stats(stream=True) File "/usr/local/lib/python3.6/site-packages/docker/models/containers.py", line 880, in get resp = self.client.api.inspect_container(container_id) File "/usr/local/lib/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped return f(self, resource_id, *args, **kwargs) File "/usr/local/lib/python3.6/site-packages/docker/api/container.py", line 756, in inspect_container self._get(self._url("/containers/{0}/json", container)), True File "/usr/local/lib/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner return f(self, *args, **kwargs) File "/usr/local/lib/python3.6/site-packages/docker/api/client.py", line 230, in _get return self.get(url, **self._set_request_timeout(kwargs)) File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 546, in get return self.request('GET', url, **kwargs) File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', PermissionError(13, 'Permission denied'))
从错误可以看出是权限问题,解决办法:
#查看docker.sock用户权限 ls /var/run/docker.sock -lh srw-rw---- 1 root docker 0 9月 2 17:04 /var/run/docker.sock #将zabbix用户加入docker所在组 gpasswd -a zabbix docker
zabbix_get测试连接
zabbix_get -s 10.8.0.22 -p 10050 -k docker_status[redis,mem_percent]
0.3
以上所有内容均为亲自测试成功后才写的,那些踩的最深的坑都已经都修改,大可放心使用。