在网上查找了zabbix监控docker的方法,基本上分为两种方法:一是用国外大神写的zabbix_module_docker.so加模板来进行监控,这种方法只能看到容器id很不爽;二是自己写脚本进行监控,在网上下载了别人的脚本进行监控,发现数据总是断断续续的,隔一段时间会没数据,索性就自己来写一个简单的脚本。
我的思路是利用docker stats这个docker自带的命令来查看cpu、内存和网络,反正我要求的也不多。。下面正式开始:
1、给zabbix 免密sudo的权限
echo “zabbix ALL=(root) NOPASSWD:/bin/docker,/usr/bin/python,/usr/local/zabbix/scripts/docker_monitor.py,/usr/local/zabbix/scripts/docker_low_discovery.sh,/usr/local/zabbix/scripts/docker_processmonitor.sh”>>/etc/sudoers
- 并且需要注释掉#Defaults requiretty这一行
- 2、在zabbix安装目录下新建一个发现docker的脚本,这个脚本是网上的没有改直接拿过来用
cat /usr/local/zabbix/scripts/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
!
p
o
r
t
[
@
]
d
o
i
f
[
[
"
{!port[@]} do if [[ "
!port[@]doif[["{#port[@]}" -gt 1 && “
k
e
y
"
−
n
e
"
{key}" -ne "
key"−ne"((KaTeX parse error: Expected '}', got '#' at position 2: {#̲port[@]}-1))" ]…{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
2、新建第二个监控docker的脚本
#!/bin/bash
#cat /usr/local/zabbix/script/docker_statsmonitor.sh
#author wusong 703249072@qq.com
#2018-5-24
unit_change(){
tmp="
1
"
n
u
m
=
0
i
f
[
"
1" num=0 if [ "
1"num=0if["(echo
t
m
p
∣
g
r
e
p
G
B
)
"
!
=
"
"
]
;
t
h
e
n
t
m
p
=
{tmp}|grep GB)" != "" ]; then tmp=
tmp∣grepGB)"!=""];thentmp={tmp%GB*}
num=echo "scale=2;($tmp*1024)/1" | bc
elif [ “$(echo
t
m
p
∣
g
r
e
p
k
B
)
"
!
=
"
"
]
;
t
h
e
n
t
m
p
=
{tmp}|grep kB)" != "" ]; then tmp=
tmp∣grepkB)"!=""];thentmp={tmp%kB*}
num=echo "scale=2;$tmp/1024" | bc | awk '{printf "%.2f", $0}'
else
num=KaTeX parse error: Expected '}', got 'EOF' at end of input: …} fi echo "{num}”
}
docker_info(){
name="$1"
info_type="
2
"
r
e
s
u
l
t
=
"
"
i
f
[
"
2" result="" if [ "
2"result=""if["{info_type}" = “cpu” ];then
result=$(sudo docker stats
n
a
m
e
−
−
n
o
−
s
t
r
e
a
m
∣
g
r
e
p
−
v
"
C
O
N
T
A
I
N
E
R
I
D
"
)
r
e
s
u
l
t
=
{name} --no-stream | grep -v "CONTAINER ID") result=
name−−no−stream∣grep−v"CONTAINERID")result=(echo ${result} |awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲') …{result%%*}"
elif [ “${info_type}” = “mem” ];then
result=sudo docker stats ${name} --no-stream | grep -v "CONTAINER ID" | awk '{print $7}'
echo “KaTeX parse error: Expected '}', got 'EOF' at end of input: … elif [ "{info_type}” = “neti” ];then
tmp=sudo docker stats ${name} --no-stream |grep -v "CONTAINER ID" | awk '{print $8}'
unit_change
t
m
p
e
l
i
f
[
"
{tmp} elif [ "
tmpelif["{info_type}" = “neto” ];then
tem=sudo docker stats ${name} --no-stream |grep -v "CONTAINER ID" |awk '{print $10}'
unit_change ${tem}
else
echo “Error:patameter wrong”
fi
}
if [ $# -ne 2 ]; then
echo “Usage: must be have two parameter to execute ‘containerName & cpu|mem|neti|neto’”
fi
case $2 in
cpu|mem|neti|neto)
docker_info $1 $2
;;
*)
echo “Usage:basename $0
must be have two parameter to execute ‘containerName & cpu|mem|neti|neto’”
;;
esac;
这个脚本作用就是查cpu、内存和网络流量,如果执行的时候报错bc : command not found
的话,yum install bc
安装一下就好了。
3、修改zabbix_agentd.conf
vim zabbix_agentd.conf ,在里面加上两个参数,注意脚本路径改成你的路径
UserParameter=docker_low_discovery[],/bin/bash /usr/local/zabbix_agent/scripts/docker_low_discovery.sh $1
UserParameter=docker_statsmonitor[],/bin/bash /usr/local/zabbix_agent/scripts/docker_statsmonitor.sh $1 $2
4、最后在zabbix web界面导入模板,关联主机,稍等即可
模板在文后。
问题调试:
如果等了很久没有出图,或者报错,可以用zabbix_get进行调试,不过使用zabbix_get时必须开启客户端被动模式,要求暴露客户端监听端口。
使用方法:在zabbix_server机器上进入zabbix/bin目录,执行
zabbix_get -s 客户端ip -p 10050 -k “docker_low_discovery[docker]”
或
zabbix_get -s 客户端ip -p 10050 -k “docker_statsmonitor[容器name,cpu]”
模板下载地址:https://download.csdn.net/download/qq_36961530/10437083
引用:https://blog.csdn.net/qq_36961530/article/details/80448301