一.概述
Dockbix意為docker+zabbix,即使用zabbix來監控docker容器的插件或者模塊,既然有專業的cadvisor、prometheus等容器監控方案,為什么還要用傳統的zabbix呢?
- 在docker剛出現時,還沒有專業的容器監控方案
- 公司已有zabbix的成熟實踐,想直接集成到zabbix中(雖然不太優雅)
使用zabbix來監控docker有幾種方案,比如:
- 自己寫agent,利用docker的api獲取stats信息,暴露api接口給zabbix采集
- 使用zabbix的Module,將docker的采集展示集成到現有的zabbix系統中
如何使用
寫API
python sdk:https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.stats
stats(**kwargs)
Stream statistics for this container. Similar to the docker stats command.
Parameters:
decode (bool) – If set to true, stream will be decoded into dicts on the fly. Only applicable if stream is True. False by default.
stream (bool) – If set to false, only the current stats will be returned instead of a stream. True by default.
Raises:
docker.errors.APIError – If the server returns an error.
如計算cpu:
def calculate_cpu_percent(d):
cpu_count = len(d["cpu_stats"]["cpu_usage"]["percpu_usage"])
cpu_percent = 0.0
cpu_delta = float(d["cpu_stats"]["cpu_usage"]["total_usage"]) - \
float(d["precpu_stats"]["cpu_usage"]["total_usage"])
system_delta = float(d["cpu_stats"]["system_cpu_usage"]) - \
float(d["precpu_stats"]["system_cpu_usage"])
if system_delta > 0.0:
cpu_percent = cpu_delta / system_delta * 100.0 * cpu_count
return cpu_percent
Zabbix Module
通過部署一個zabbix agent的docker容器來監控宿主機器和宿主機器上docker的狀態。
搬運下開源項目:https://github.com/monitoringartist/zabbix-docker-monitoring
1.在需要監控的宿主機器上運行運行Agent容器
docker run \
--name=dockbix-agent-xxl \
--net=host \
--privileged \
-v /:/rootfs \
-v /var/run:/var/run \
--restart unless-stopped \
-e "ZA_Server=<ZABBIX SERVER IP/DNS NAME/IP RANGE>" \
-e "ZA_ServerActive=<ZABBIX SERVER IP/DNS NAME>" \
-d monitoringartist/dockbix-agent-xxl-limited:latest
2.配置監控模板
在zabbix server上導入監控docker的模版,可用模板包括:
- Zabbix-Template-App-Docker.xml - 標準推薦模板:被動
- Zabbix-Template-App-Docker-active.xml - 檢查模板:主動
- Zabbix-Template-App-Docker-Mesos-Marathon-Chronos.xml - Mesos (Marathon/Chronos)集群的docker模板
也可以用docker鏡像直接運行,來導入模板:monitoringartist/zabbix-templates,如:
docker run --rm \
-e XXL_apiurl=http://zabbix.org/zabbix \
-e XXL_apiuser=Admin \
-e XXL_apipass=zabbix \
monitoringartist/zabbix-templates
item為:
image
詳細的metric的值可以參考:https://github.com/monitoringartist/zabbix-docker-monitoring/blob/master/README.md
3.可視化
zabbix的監控圖:
image
grafana中也有zabbix的數據模板
image
本文為容器監控實踐系列文章,完整內容見:container-monitor-book