Dockbix意爲docker+zabbix,即便用zabbix來監控docker容器的插件或者模塊,既然有專業的cadvisor、prometheus等容器監控方案,爲何還要用傳統的zabbix呢?html
使用zabbix來監控docker有幾種方案,好比:python
python sdk:https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.statsgit
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:github
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 agent的docker容器來監控宿主機器和宿主機器上docker的狀態。docker
搬運下開源項目:https://github.com/monitoringartist/zabbix-docker-monitoringapi
1.在須要監控的宿主機器上運行運行Agent容器bash
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.配置監控模板app
在zabbix server上導入監控docker的模版,可用模板包括:less
也能夠用docker鏡像直接運行,來導入模板:monitoringartist/zabbix-templates,如:this
docker run --rm \ -e XXL_apiurl=http://zabbix.org/zabbix \ -e XXL_apiuser=Admin \ -e XXL_apipass=zabbix \ monitoringartist/zabbix-templates
item爲:
詳細的metric的值能夠參考:https://github.com/monitoringartist/zabbix-docker-monitoring/blob/master/README.md
3.可視化
zabbix的監控圖:
grafana中也有zabbix的數據模板
本文爲容器監控實踐系列文章,完整內容見:container-monitor-book