dcoker监控首先需要将docker的name拿出来,循环执行docker stats将数据拿出重新排序后数值换算追加到/tmp/下以name命名
[root@localhost scripts]# cat docker_host_status.sh
#!/bin/bash
# -------------------------------------------------------------------------------
# Filename: docker-status
# Revision: 1.1
# Date: 201608016
# Author: mark
# Email: usertzc@163.com
# Website: www.linuxea.com
# -------------------------------------------------------------------------------
# Notice
# After this state will docker stats reorder append to a file
# Auto Discovery docker stats Container Name --no-stream Execution time
###############################################################################
docker_name=`/usr/bin/docker ps -a|grep -v "CONTAINER ID"|awk '{print $NF}'`
for e in ${docker_name};do
/usr/bin/docker stats $e --no-stream |awk 'NR==2{a=$1;b=$2;c=$3$4;d=$6$7;e=$9$10;f=$12$13;g=$14$15;h=$17$18;j=$8}END{print "CONTAINER "a"n""CPU "b"n""MEMUSAGE "c"n""LIMIT "d"n""NETI-0 "e"n""NETI-1 "f"n""BLOCKI-0 "g"n""BLOCKI-1 "h" n""MEM "j}' |awk -F'%' '{print $1}' |awk '{a=/GiB/?$2*1024*1024*1024:(/M[i]?B/?$2*1024*1024:(/[Kk][Bb]/?$2*1024:(/B>/?$2*1:$2)))}{print $1,a}' > /tmp/.$e.txt
done
这个脚本需要循环执行,放在计划任务中每分钟一次
*/1 * * * * /usr/bin/sh /etc/zabbix/scripts/docker_host_status.sh
docker name自动发现脚本
[root@localhost scripts]# cat docker_name.py
#!/usr/bin/python
# -------------------------------------------------------------------------------
# Filename: docker_name
# Revision: 1.1
# Date: 201608018
# Author: mark
# Email: usertzc@163.com
# Website: www.linuxea.com
# -------------------------------------------------------------------------------
# Notice
# Automatic discovery instance name
###############################################################################
import os
import json
t=os.popen("""/usr/bin/docker ps -a|grep -v "CONTAINER ID"|awk '{print $NF}' """)
docknam = []
for dname in t.readlines():
r = os.path.basename(dname.strip())
docknam += [{'{#DOCKERNAME}':r}]
print json.dumps({'data':docknam},sort_keys=True,indent=4,separators=(',',':'))
docker UserParameter
UserParameter=docker_status,/etc/zabbix/scripts/docker_name.py
UserParameter=docker_server[*],/usr/bin/docker top "$1"|grep "$2"|grep -v root |wc -l
UserParameter=docker_host[*],awk '/$2>/{print $$2}' /tmp/."$1".txt
这里需要注意的是,docker默认是用root运行的,在使用docker ps 或者docker stats都需要和docker一样的用户,我在这里修改了配置文件,将zabbix-agent改为root运行,zabbix-agent的防火墙规则需要指点ip和端口到zabbix-server,请知悉AllowRoot=1User=root另外,发现其他服务,可能需要手动添加模板下载 密码:isun
20181119今天我提示大家,没有必要参考本章的思路,尽管是可以用来监控,但是,docker在作为大规模部署的时候,特别是在集群模式下,这种方式是不适用的,也是不合理的,这里仅仅作为一个参考的方式。并不作为任何监控手段的意义。对于docker的监控,推荐prometheus