x1[root@node1 zabbix_agentd.d]# cat /etc/zabbix/script/docker_discovery.sh
2
3port=($(/usr/bin/docker ps -a|grep -v "CONTAINER ID"|awk '{print $NF}'))
4printf '{\n'
5printf '\t"data":[\n'
6 for key in ${!port[@]}
7 do
8 if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
9 printf '\t {\n'
10 printf "\t\t\t\"{#CONTAINERNAME}\":\"${port[${key}]}\"},\n"
11
12 else [[ "${key}" -eq "((${#port[@]}-1))" ]]
13 printf '\t {\n'
14 printf "\t\t\t\"{#CONTAINERNAME}\":\"${port[${key}]}\"}\n"
15
16 fi
17 done
18
19 printf '\t ]\n'
20 printf '}\n'
21
22#脚本效果如下:
23[root@node1 zabbix_agentd.d]# sh /etc/zabbix/script/docker_discovery.sh
24{
25 "data":[
26 {
27 "{#CONTAINERNAME}":"k8s_install-cni_kube-flannel-ds-amd64-4cr9d_kube-system_39d5a60d-702c-466c-97e0-85123e1a7869_0"},
28 {
29 "{#CONTAINERNAME}":"k8s_nginx_nginx_default_3f5ffd65-a233-48c5-973d-b43922ccea61_0"},
30 {
31 "{#CONTAINERNAME}":"k8s_POD_nginx_default_3f5ffd65-a233-48c5-973d-b43922ccea61_0"},
32 {
33 "{#CONTAINERNAME}":"k8s_kube-flannel_kube-flannel-ds-amd64-4cr9d_kube-system_39d5a60d-702c-466c-97e0-85123e1a7869_3"},
34 {
35 "{#CONTAINERNAME}":"k8s_kube-proxy_kube-proxy-sqhs4_kube-system_1c7fac8f-5263-4cd8-b008-485f1dd44ad1_3"},
36 {
37 "{#CONTAINERNAME}":"k8s_POD_kube-flannel-ds-amd64-4cr9d_kube-system_39d5a60d-702c-466c-97e0-85123e1a7869_3"},
38 {
39 "{#CONTAINERNAME}":"k8s_POD_kube-proxy-sqhs4_kube-system_1c7fac8f-5263-4cd8-b008-485f1dd44ad1_3"}
40 ]
41}
42
43#注意事项!!!
44zabbix-agent在取值的时候使用的是zabbix用户,在执行docker ps -a会没有权限执行
45解决方法有两种
461:添加sudo授权
472:给docker命令加suid权限
48chmod u+s /usr/bin/docker
49推荐使用第二种,比较简单
xxxxxxxxxx
941#取值来源/usr/bin/docker stats --no-stream
2
3[root@node1 zabbix_agentd.d]# cat /etc/zabbix/script/docker_monitor.sh
4
5
6CACHEFILE="/tmp/docker_monitor_cache.txt"
7CMD="/usr/bin/docker stats --no-stream"
8if [ ! -f $CACHEFILE ];then
9 $CMD >$CACHEFILE
10fi
11# Check and run the script
12TIMEFLM=`stat -c %Y $CACHEFILE`
13TIMENOW=`date +%s`
14if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then
15 rm -f $CACHEFILE
16fi
17if [ ! -f $CACHEFILE ];then
18 $CMD >$CACHEFILE
19fi
20container_live() {
21 grep "$1" $CACHEFILE &>/dev/null
22 if [ $? -eq 1 ];then
23 echo '0'
24 else
25 echo '1'
26 fi
27 exit 0
28}
29container_id() {
30 grep "$1" $CACHEFILE|awk '{print $1}'
31 exit 0
32}
33container_cpu() {
34 grep "$1" $CACHEFILE|awk '{print $3}'|tr -d '%'
35 exit 0
36}
37container_mem_used() {
38 grep "$1" $CACHEFILE|awk '{print $4}'|tr -d 'MiB'
39 exit 0
40}
41container_mem_pused() {
42 grep "$1" $CACHEFILE|awk '{print $7}'|tr -d '%'
43 exit 0
44}
45container_net_in() {
46 grep "$1" $CACHEFILE|awk '{print $8}'|tr -d '%'
47 exit 0
48}
49container_net_out() {
50 grep "$1" $CACHEFILE|awk '{print $10}'|tr -d '%'
51 exit 0
52}
53container_disk_in() {
54 grep "$1" $CACHEFILE|awk '{print $11}'
55 exit 0
56}
57container_disk_out() {
58 grep "$1" $CACHEFILE|awk '{print $13}'
59 exit 0
60}
61
62
63case $1 in
64live)
65container_live $2;
66;;
67id)
68container_id $2;
69;;
70cpu)
71container_cpu $2;
72;;
73mem_used)
74container_mem_used $2;
75;;
76mem_pused)
77container_mem_pused $2;
78;;
79net_in)
80container_net_in $2;
81;;
82net_out)
83container_net_out $2;
84;;
85disk_in)
86container_disk_in $2;
87;;
88disk_out)
89container_disk_out $2;
90;;
91*)
92echo '无效的参数';
93exit 2;
94esac
xxxxxxxxxx
51[root@node1 zabbix_agentd.d]# cat /etc/zabbix/zabbix_agentd.d/zbx_docker.conf
2UserParameter=docker.discovery,/bin/bash /etc/zabbix/script/docker_discovery.sh
3UserParameter=docker_monitor[*],/bin/bash /etc/zabbix/script/docker_monitor.sh $1 $2
4
5systemctl restart docker
xxxxxxxxxx
241#测试1
2[root@oldboy ~]# zabbix_get -s 10.0.0.12 -k docker.discovery
3{
4 "data":[
5 {
6 "{#CONTAINERNAME}":"k8s_install-cni_kube-flannel-ds-amd64-4cr9d_kube-system_39d5a60d-702c-466c-97e0-85123e1a7869_0"},
7 {
8 "{#CONTAINERNAME}":"k8s_nginx_nginx_default_3f5ffd65-a233-48c5-973d-b43922ccea61_0"},
9 {
10 "{#CONTAINERNAME}":"k8s_POD_nginx_default_3f5ffd65-a233-48c5-973d-b43922ccea61_0"},
11 {
12 "{#CONTAINERNAME}":"k8s_kube-flannel_kube-flannel-ds-amd64-4cr9d_kube-system_39d5a60d-702c-466c-97e0-85123e1a7869_3"},
13 {
14 "{#CONTAINERNAME}":"k8s_kube-proxy_kube-proxy-sqhs4_kube-system_1c7fac8f-5263-4cd8-b008-485f1dd44ad1_3"},
15 {
16 "{#CONTAINERNAME}":"k8s_POD_kube-flannel-ds-amd64-4cr9d_kube-system_39d5a60d-702c-466c-97e0-85123e1a7869_3"},
17 {
18 "{#CONTAINERNAME}":"k8s_POD_kube-proxy-sqhs4_kube-system_1c7fac8f-5263-4cd8-b008-485f1dd44ad1_3"}
19 ]
20}
21
22#测试2
23[root@oldboy ~]# zabbix_get -s 10.0.0.12 -k docker_monitor[mem_used,k8s_nginx_nginx_default_3f5ffd65-a233-48c5-973d-b43922ccea61_0]
243.102
模板下载地址http://oldqiang.com/zbx_docker_templates.xml
模板中,我只定义了一个低级自动发现规则,关于其他的监控,后期在完善!