-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcapture_docker_stats.sh
More file actions
executable file
·88 lines (74 loc) · 2.47 KB
/
capture_docker_stats.sh
File metadata and controls
executable file
·88 lines (74 loc) · 2.47 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Initialize variables
CONTAINER_NAME=""
VERBOSE=false
STOPPED=false
# Parse arguments
for arg in "$@"; do
if [ "$arg" == "-v" ]; then
VERBOSE=true
else
CONTAINER_NAME="$arg"
fi
done
# Validate if the container name is provided and exists
if [ ! -z "$CONTAINER_NAME" ]; then
if ! docker ps --format '{{.Names}}' | grep -wq "$CONTAINER_NAME"; then
echo "Error: Container '$CONTAINER_NAME' is not running or does not exist."
exit 1
fi
fi
# Define the log file based on the container name
if [ -z "$CONTAINER_NAME" ]; then
LOG_FILE="stats_all_containers.log"
else
LOG_FILE="stats_${CONTAINER_NAME}.log"
fi
# Let the user know we're running
if [ -z "$CONTAINER_NAME" ]; then
echo "Capturing all container stats to $LOG_FILE"
echo " - add container name as argument to capture for specific container"
else
echo "Capturing Container '${CONTAINER_NAME}' stats to $LOG_FILE"
fi
# If verbose mode is enabled, also print to the screen
if [ "$VERBOSE" = false ]; then
echo " - add -v parameter to log to screen as well"
fi
echo "CTRL-C to finish"
# Capture CTRL-C to gracefully exit on the next iteration instead of abending
trap 'STOPPED=true' SIGINT
# Loop to capture CPU usage continually
while [[ $STOPPED = false ]]; do
# Capture the current timestamp
TIMESTAMP=$(date --iso-8601=seconds)
# Run docker stats in a subshell where SIGINT is ignored
if [ -z "$CONTAINER_NAME" ]; then
JSON=$(setsid docker stats --format json --no-stream)
else
JSON=$(setsid docker stats "$CONTAINER_NAME" --format json --no-stream)
fi
# Add the current timestamp to the JSON and format fields for logging
UPDATED_JSON=$(echo "$JSON" | jq -c --arg time "$TIMESTAMP" '
.dateTime = $time |
del(.BlockIO, .NetIO, .PIDs) |
.CPUPerc = (.CPUPerc | gsub("%"; "") | tonumber) |
.MemPerc = (.MemPerc | gsub("%"; "") | tonumber) |
.MemUsage |= (split(" / ") | {
Memory: .[0] | capture("(?<value>[-0-9.]+)(?<unit>[a-zA-Z]+)") | {Memory: .value | tonumber, Units: .unit},
Max: .[1] | capture("(?<value>[-0-9.]+)(?<unit>[a-zA-Z]+)") | {Memory: .value | tonumber, Units: .unit}
}) |
.MemMax = .MemUsage.Max |
.MemUsage = .MemUsage.Memory |
{dateTime, ID, Name, Container, CPUPerc, MemPerc, MemUsage, MemMax}
')
# Write the JSON to the log file
echo "$UPDATED_JSON" >> "$LOG_FILE"
# If verbose mode is enabled, also print to the screen
if [ "$VERBOSE" = true ]; then
echo "$UPDATED_JSON"
fi
# Sleep for a desired interval (seconds)
sleep 0.25
done
echo "CTRL-C / SIGINT - Stopping."