-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcccheck
executable file
·50 lines (40 loc) · 1.3 KB
/
cccheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
## cccheck
##
## Check for connections of a specific container across EC2 (ECS/docker) instances
## Will not fail if no containers are found
##
#
if [ $# -eq 0 -o $# -gt 2 ]; then
echo "Usage: cccheck container-name1[,container-name-2,container-name3...container-name-N] [TIME_WAIT]" 1>&2
exit 1
fi
CONTAINERS="$1" ## container names
ST="$2" ## optional
## program starts here
PAD=45
## get own IP
IPv4=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
if [ -z "$IPv4" ]; then
echo "ERROR: could not get IPv4 of eth0" 1>&2
exit 1
fi
TMPF=$(mktemp)
## access ip_conntrack via sudo only once
if [ ! -z "$ST" ]; then
sudo grep -F "$ST" /proc/net/ip_conntrack >> "$TMPF" || exit $?
else
sudo cat /proc/net/ip_conntrack >> "$TMPF" || exit $?
fi
## split by comma
IFS=',' read -a CONTAINERS <<< "$CONTAINERS"
for CNAME in ${CONTAINERS[@]}; do
for IMGCID in $(docker ps | awk '{ printf "%s=%s\n", $NF, $2}' | grep "^$CNAME"); do
CID=$(echo "$IMGCID" | awk -F= '{ print $1} ')
IMAGE=$(echo "$IMGCID" | awk -F= '{ print $2} '| awk -F/ '{ print $NF}')
CIP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CID) || exit $?
printf "%s\t%.${PAD}s\t\t%s\t" ${IPv4} $IMAGE $CID
grep -F "src=$CIP" "$TMPF" | wc -l
done
done | sort -V -k2
rm "$TMPF"