1、从https://pypi.python.org/pypi/docker-py#downloads 这里下载docker-py-xxx.tar.gz到本地再传进去linux系统里面进行解压
zabbix监控容器情况和磁盘IO
python setup.py install
zabbix监控容器情况和磁盘IO

https://pypi.python.org/packages/6c/54/f7e9cea6897636a04e74c3954f0d8335cc38f7d01e27eec98026b049a300/setuptools-38.5.1.zip#md5=1705ae74b04d1637f604c336bb565720

2、在agentd那里加入自定义模块
UserParameter=docker_low_discovery[],/bin/bash /etc/zabbix/scripts/docker_low_discovery.sh $1
UserParameter=docker_stats[
],sudo /usr/bin/python /etc/zabbix/scripts/docker_monitor.py $1 $2
UserParameter=docker_process[],/bin/bash /etc/zabbix/scripts/docker_processmonitor.sh $1 $2 $3
UserParameter=custom.vfs.dev.read.ops[
],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}' #磁盘读的次数
UserParameter=custom.vfs.dev.read.ms[],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}' #磁盘读的毫秒数
UserParameter=custom.vfs.dev.write.ops[
],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}' #磁盘写的次数
UserParameter=custom.vfs.dev.write.ms[],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}' #磁盘写的毫秒数
UserParameter=custom.vfs.dev.io.active[
],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'
UserParameter=custom.vfs.dev.io.ms[],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}' #花费在IO操作上的毫秒数
UserParameter=custom.vfs.dev.read.sectors[
],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}' #读扇区的次数(一个扇区的等于512B)
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}' #写扇区的次数(一个扇区的等于512B)
zabbix监控容器情况和磁盘IO

3、把两个shell脚本和一个python脚本放进去zabbix-agent的目录下
zabbix监控容器情况和磁盘IO

docker_monitor.py
[root@spark scripts]# cat docker_monitor.py

#!/usr/bin/env python
#-*- coding: utf-8 -*-
from docker import Client
import sys
import subprocess
import os
global null
null=''

def check_container_stats(container_name,collect_item):
    container_collect=docker_client.stats(container_name)
    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 == '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)
    elif collect_item == 'network_rx_bytes':
        network_check_command="""sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk '{print $2,$10}'"""%container_name
        result=os.popen(network_check_command).read().split()[0]
    elif collect_item == 'network_tx_bytes':
        network_check_command="""sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk '{print $2,$10}'"""%container_name
        result=os.popen(network_check_command).read().split()[1]
    return result
if __name__ == "__main__":
    docker_client = Client(base_url='unix://var/run/docker.sock', version='1.17')
    container_name=sys.argv[1]
    collect_item=sys.argv[2]
    print check_container_stats(container_name,collect_item)

zabbix监控容器情况和磁盘IO

docker_low_discovery.sh
[root@spark scripts]# cat docker_low_discovery.sh

#!/bin/bash
#Fucation:docker low-level discovery
docker() {
            port=($(sudo docker ps -a|grep -v "CONTAINER ID"|awk '{print $NF}'))
            printf '{\n'
            printf '\t"data":[\n'
               for key in ${!port[@]}
                   do
                       if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
                          printf '\t {\n'
                          printf "\t\t\t\"{#CONTAINERNAME}\":\"${port[${key}]}\"},\n"

                     else [[ "${key}" -eq "((${#port[@]}-1))" ]]
                          printf '\t {\n'
                          printf "\t\t\t\"{#CONTAINERNAME}\":\"${port[${key}]}\"}\n"

                       fi
               done

                          printf '\t ]\n'
                          printf '}\n'
}
case $1 in
docker)
docker
;;
*)
echo "Usage:`basename $0` {docker}"
;;
esac

zabbix监控容器情况和磁盘IO

docker_processmonitor.sh
[root@spark scripts]# cat docker_processmonitor.sh

#!/bin/bash
#license:GPL
processmem(){
        sudo /usr/bin/docker exec $1 ps aux|grep $2|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
processcpu(){
        sudo /usr/bin/docker exec $1 ps aux|grep $2|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
processport(){
        sudo /usr/bin/docker exec $1 ss -antlp|grep $2|grep LISTEN|wc -l
}
case "$3" in
mem)
processmem $1 $2
;;
cpu)
processcpu $1 $2
;;
port)
processport $1 $2
;;
*)
echo "Usage: $0 {docker_containername}{processname}{mem|cpu|port}"
;;
esac

zabbix监控容器情况和磁盘IO

3、然后可以在zabbix_server端测试是否能够获取到相应的数据:
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_tx_bytes]"
343128
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_rx_bytes]"
10617111
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_rx_bytes]"
9716403
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_tx_bytes]"
137637
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_low_discovery[docker]"
{
"data":[
{
"{#CONTAINERNAME}":"docker_signage_1"}
]
}
zabbix监控容器情况和磁盘IO

4、在zabbix导入模板
http://down.51cto.com/data/2366161 从这里下载到windows本地,然后在web页面导入后,很快就加入自动发现了
zabbix监控容器情况和磁盘IO

zabbix监控容器情况和磁盘IO
zabbix监控容器情况和磁盘IO

5、磁盘IO
然后添加grahs(图形)和添加对应的自定义监控项

zabbix监控容器情况和磁盘IO

zabbix监控容器情况和磁盘IO