-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerps.sh
executable file
·63 lines (57 loc) · 1.6 KB
/
dockerps.sh
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
#
# dockerpsns - proof of concept for a "docker ps --namespaces".
#
# USAGE: ./dockerpsns.sh
#
# This lists containers, their init PIDs, and namespace IDs. If container
# namespaces equal the host namespace, they are colored red (this can be
# disabled by setting color=0 below).
#
# Copyright 2017 Netflix, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 10-Apr-2017 Brendan Gregg Created this.
namespaces="cgroup ipc mnt net pid user uts"
color=1
declare -A hostns
printf "%-14s %-20s %6s %-16s" "CONTAINER" "NAME" "PID" "PATH"
for n in $namespaces; do
printf " %-10s" $(echo $n | tr a-z A-Z)
done
echo
# print host details
pid=1
read name < /proc/$pid/comm
printf "%-14s %-20.20s %6d %-16.16s" "host" $(hostname) $pid $name
for n in $namespaces; do
id=$(stat --format="%N" /proc/$pid/ns/$n)
id=${id#*[}
id=${id%]*}
hostns[$n]=$id
printf " %-10s" "$id"
done
echo
# print containers
for UUID in $(docker ps -q); do
# docker info:
pid=$(docker inspect -f '{{.State.Pid}}' $UUID)
name=$(docker inspect -f '{{.Name}}' $UUID)
path=$(docker inspect -f '{{.Path}}' $UUID)
name=${name#/}
printf "%-14s %-20.20s %6d %-16.16s" $UUID $name $pid $path
# namespace info:
for n in $namespaces; do
id=$(stat --format="%N" /proc/$pid/ns/$n)
id=${id#*[}
id=${id%]*}
docolor=0
if (( color )); then
[[ "${hostns[$n]}" == "$id" ]] && docolor=1
fi
(( docolor )) && echo -e "\e[31;1m\c"
printf " %-10s" "$id"
(( docolor )) && echo -e "\e[0m\c"
done
echo
done