Skip to content

Instantly share code, notes, and snippets.

@iaon
Last active February 22, 2018 18:08
Show Gist options
  • Save iaon/cfcdd32031d93aab7120f4408a3efa9d to your computer and use it in GitHub Desktop.
Save iaon/cfcdd32031d93aab7120f4408a3efa9d to your computer and use it in GitHub Desktop.
Docker Netstat counts tcp connections by state
#!/bin/bash
while :
do
date
containers=$(docker ps --format "{{.Names}}:{{.ID}}")
for c in $containers
do
echo $c
CID=$(echo $c | awk -F ":" '{print $2}')
nsenter -t $(docker inspect -f '{{.State.Pid}}' $CID) -n netstat -ant |tail -n +3|awk '{print $6}' | sort | uniq -c
echo ----------------------
done
echo ==================
sleep 30
done
@iaon
Copy link
Author

iaon commented Feb 22, 2018

output example

Чт фев 22 18:26:15 CET 2018
grafana_cadvisor.uk0vabia0n4a4oqsentz4n82u.yhc6xv7vbg8yyxbgg722tbwh8:a58deb1d53e3
      1 ESTABLISHED
      2 LISTEN
----------------------
ucp-agent.uk0vabia0n4a4oqsentz4n82u.uh4yk8n08q63kedzqyq98l8i4:dfd2901ca11c
----------------------
ucp-controller:e05b4609c678
      5 CLOSE_WAIT
     47 ESTABLISHED
      1 LAST_ACK
      1 LISTEN
    390 TIME_WAIT
----------------------
ucp-metrics:3def2b8433a0
     11 ESTABLISHED
      2 LISTEN
      3 TIME_WAIT
----------------------
ucp-auth-api:e43487d3eae8
     16 ESTABLISHED
      1 LISTEN
----------------------
ucp-auth-worker:336afb043cdf
      5 ESTABLISHED
      1 LISTEN
----------------------
ucp-swarm-manager:45b1bc32ec2e
     64 ESTABLISHED
      1 LISTEN
----------------------
ucp-auth-store:4f48e1c85c8e
     22 ESTABLISHED
      2 LISTEN
     40 TIME_WAIT
----------------------
ucp-kv:5d8820f63dc2
     20 ESTABLISHED
      2 LISTEN
----------------------
ucp-client-root-ca:af612f10c531
      1 LISTEN
----------------------
ucp-cluster-root-ca:7019e3898873
      1 ESTABLISHED
      1 LISTEN
----------------------
ucp-proxy:d88faa8a1022
     13 ESTABLISHED
      1 LISTEN
      1 TIME_WAIT
----------------------
modest_borg:ad7e21ff3bf2
      1 LISTEN
----------------------
==================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment