普罗米修斯官方所有的镜像:https://hub.docker.com/search?q=prom/&source=community
官方下载地址 https://prometheus.io/download/
找到 node-export 下载即可
curl -o node-exporter.tar.gz -L https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
node-exporter.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9111 --collector.textfile.directory=/apps/exporterData
KillSignal=SIGQUIT
Restart=always
RestartPreventExitStatus=1 6 SIGABRT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=1048576
LimitNPROC=1048576
[Install]
WantedBy=multi-user.target
/apps/exporterData
中读取通其他脚本程序获取的监控数据,比如使用脚本获取到的节点中的容器运行指标。/apps/exporterData
中可以有任意文件名的文件存在,但是文件中的内容数据格式比须遵循 Prometheus 的要求。实例如下:
container_netwrite{name="mysql3",id="9fa421caef99"} 2583852325273.60
container_blkioread{name="mysql3",id="9fa421caef99"} 619549032448
container_blkiowrite{name="mysql3",id="9fa421caef99"} 206708186021888
生产脚本
#!/bin/bash
outputfile=/tmp/docker_stat.output
outInfoFile=/apps/exporterData/docker_s.prom
while true
do
docker stats --format "{{.Name}} {{.ID}} {{.CPUPerc}} {{.MemPerc}} {{.MemUsage}} {{.NetIO}} {{.BlockIO}}" --no-stream |tr -d '/' > $outputfile
cat $outputfile|while read LINE
do
container_name=$(echo $LINE|awk '{print $1}')
container_id=$(echo $LINE|awk '{print $2}')
container_cpuperc=$(echo $LINE|awk '{print $3}'|tr -d '%')
container_memperc=$(echo $LINE|awk '{print $4}'|tr -d '%')
if [[ "$(echo $LINE|awk '{print $5}')" =~ MiB$ ]];then
container_memusage=$(echo $LINE|awk '{print $5}' |awk -F 'MiB' '{print $1}')
elif [[ "$(echo $LINE|awk '{print $5}')" =~ GiB$ ]];then
num=$(echo $LINE|awk '{print $5}' |awk -F 'GiB' '{print $1}')
container_memusage=$(echo $num \* 1024|bc)
fi
if [[ "$(echo $LINE|awk '{print $6}')" =~ MiB$ ]];then
container_memlimit=$(echo $LINE|awk '{print $6}' |awk -F 'MiB' '{print $1}')
elif [[ "$(echo $LINE|awk '{print $6}')" =~ GiB$ ]];then
num2=$(echo $LINE|awk '{print $6}' |awk -F 'GiB' '{print $1}')
container_memlimit=$(echo $num2 \* 1024|bc)
fi
if [[ "$(echo $LINE|awk '{print $7}')" =~ kB$ ]];then
container_netread=$(echo $LINE|awk '{print $7}'|awk -F 'kB' '{print $1}')
elif [[ "$(echo $LINE|awk '{print $7}')" =~ [0-9]B$ ]];then
container_netread=`echo $(echo $LINE|awk '{print $7}' |awk -F 'B' '{print $1}') \* 1048576|bc`
elif [[ "$(echo $LINE|awk '{print $7}')" =~ MB$ ]];then
container_netread=`echo $(echo $LINE|awk '{print $7}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
elif [[ "$(echo $LINE|awk '{print $7}')" =~ GB$ ]];then
container_netread=`echo $(echo $LINE|awk '{print $7}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
fi
if [[ "$(echo $LINE|awk '{print $8}')" =~ kB$ ]];then
container_netwrite=$(echo $LINE|awk '{print $8}'|awk -F 'kB' '{print $1}')
elif [[ "$(echo $LINE|awk '{print $8}')" =~ [0-9]B$ ]];then
container_netwrite=`echo $(echo $LINE|awk '{print $8}' |awk -F 'B' '{print $1}') \* 1048576|bc`
elif [[ "$(echo $LINE|awk '{print $8}')" =~ MB$ ]];then
container_netwrite=`echo $(echo $LINE|awk '{print $8}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
elif [[ "$(echo $LINE|awk '{print $8}')" =~ GB$ ]];then
container_netwrite=`echo $(echo $LINE|awk '{print $8}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
fi
if [[ "$(echo $LINE|awk '{print $9}')" =~ kB$ ]];then
container_blkioread=$(echo $LINE|awk '{print $9}'|awk -F 'kB' '{print $1}')
elif [[ "$(echo $LINE|awk '{print $9}')" =~ [0-9]B$ ]];then
container_blkioread=`echo $(echo $LINE|awk '{print $9}' |awk -F 'B' '{print $1}') \* 1048576|bc`
elif [[ "$(echo $LINE|awk '{print $9}')" =~ MB$ ]];then
container_blkioread=`echo $(echo $LINE|awk '{print $9}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
elif [[ "$(echo $LINE|awk '{print $9}')" =~ GB$ ]];then
container_blkioread=`echo $(echo $LINE|awk '{print $9}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
fi
if [[ "$(echo $LINE|awk '{print $10}')" =~ kB$ ]];then
container_blkiowrite=$(echo $LINE|awk '{print $10}'|awk -F 'kB' '{print $1}')
elif [[ "$(echo $LINE|awk '{print $10}')" =~ [0-9]B$ ]];then
container_blkiowrite=`echo $(echo $LINE|awk '{print $10}' |awk -F 'B' '{print $1}') \* 1048576|bc`
elif [[ "$(echo $LINE|awk '{print $10}')" =~ MB$ ]];then
container_blkiowrite=`echo $(echo $LINE|awk '{print $10}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
elif [[ "$(echo $LINE|awk '{print $10}')" =~ GB$ ]];then
container_blkiowrite=`echo $(echo $LINE|awk '{print $10}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
fi
echo "container_cpuperc{name=\""$container_name"\",id=\""$container_id"\"} $container_cpuperc" >> $outInfoFile
echo "container_memperc{name=\""$container_name"\",id=\""$container_id"\"} $container_memperc" >> $outInfoFile
echo "container_memusage{name=\""$container_name"\",id=\""$container_id"\"} $container_memusage" >> $outInfoFile
echo "container_memlimit{name=\""$container_name"\",id=\""$container_id"\"} $container_memlimit" >> $outInfoFile
echo "container_netread{name=\""$container_name"\",id=\""$container_id"\"} $container_netread" >> $outInfoFile
echo "container_netwrite{name=\""$container_name"\",id=\""$container_id"\"} $container_netwrite" >> $outInfoFile
echo "container_blkioread{name=\""$container_name"\",id=\""$container_id"\"} $container_blkioread" >> $outInfoFile
echo "container_blkiowrite{name=\""$container_name"\",id=\""$container_id"\"} $container_blkiowrite" >> $outInfoFile
done
sleep 10
echo > $outInfoFile
done
下载地址 https://grafana.com/grafana/dashboards/
node-exporter 推荐 https://grafana.com/grafana/dashboards/8919
占 位 符 描述
.Container 容器名称或 ID(用户输入)
.Name 容器名称
.ID 容器标识
.CPUPerc 中央处理器百分比
.MemUsage 内存使用情况
.NetIO 网络接口
.BlockIO 块 IO
.MemPerc 内存百分比(在 Windows 上不可用)
.PIDs PID 数量(在 Windows 上不可用)