docker 检查容器连接数

检测docker 容器的tcp连接情况,并做统计(window 和mac环境不支持)
用法:./detect.sh

detect.sh 源码如下:

#!/usr/bin/env bash


tmp_result="/tmp/docker_connections";

detectTCPConnection(){

    echo "start detecting...";

    list=$(docker ps -f 'status=running' --format "table {{.ID}}");

    list=${list#CONTAINER ID};

    test -z "${list}" && echo "container id is null" && exit 0;

    timestamp=$(date +"%Y%m%d%H%M%S");
    test ! -d "${tmp_result}" && mkdir ${tmp_result};
    cd ${tmp_result};

    for i in $list; do
        pid=$(docker inspect -f {{.State.Pid}} $i);
    echo "start detect for pid:${pid}";
    tmpFile="${i}_${pid}.txt";
    echo  "make file ${tmpFile}";

    test -a "$tmpFile" && rm -f $tmpFile;

    nsenter -t $pid -n netstat |grep ESTABLISHED > $tmpFile;
    echo "end statistic connection for container  ${i}";
    done

}

doStatisticTCPConection(){

    test ! -d "${tmp_result}" && echo "not data for statistic" && exit 0;
    cd ${tmp_result};

    for i in *.txt; do

        echo "${i} tcp connections:$(cat ${i} | grep "6379"  | wc -l)";

    done


}


select i in 'do detect' 'do statistic' 'do both';do

case $i in
'do detect')
detectTCPConnection;
;;
'do statistic')
doStatisticTCPConection;
;;
'do both')
detectTCPConnection;
doStatisticTCPConection;
;;
*)
echo "usage: ./detect.sh ";
;;
esac
done

使用案例:查看每个容器中redis的连接数


 提示:需要详细资料,可以访问这个

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值