-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-resources.sh
More file actions
34 lines (27 loc) · 1.18 KB
/
monitor-resources.sh
File metadata and controls
34 lines (27 loc) · 1.18 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
#!/bin/bash
# Monitor container resource usage
CONTAINER_ID=$1
CGROUP_PATH="/sys/fs/cgroup"
while true; do
echo "=== Resource Usage for ${CONTAINER_ID} ==="
echo "Timestamp: $(date)"
# Memory usage
MEMORY_USED=$(cat ${CGROUP_PATH}/memory/sandbox/container_${CONTAINER_ID}/memory.usage_in_bytes)
MEMORY_LIMIT=$(cat ${CGROUP_PATH}/memory/sandbox/container_${CONTAINER_ID}/memory.limit_in_bytes)
MEMORY_PCT=$((MEMORY_USED * 100 / MEMORY_LIMIT))
echo "Memory: ${MEMORY_USED} / ${MEMORY_LIMIT} (${MEMORY_PCT}%)"
# CPU usage
CPU_USAGE=$(cat ${CGROUP_PATH}/cpuacct/sandbox/container_${CONTAINER_ID}/cpuacct.usage)
echo "CPU Usage (nanoseconds): ${CPU_USAGE}"
# PID count
PID_CURRENT=$(cat ${CGROUP_PATH}/pids/sandbox/container_${CONTAINER_ID}/pids.current)
PID_MAX=$(cat ${CGROUP_PATH}/pids/sandbox/container_${CONTAINER_ID}/pids.max)
echo "PIDs: ${PID_CURRENT} / ${PID_MAX}"
# Check for OOM
OOM_COUNT=$(cat ${CGROUP_PATH}/memory/sandbox/container_${CONTAINER_ID}/memory.oom_control | grep "oom_kill" | awk '{print $2}')
if [ "$OOM_COUNT" != "0" ]; then
echo "[WARNING] OOM KILLS: ${OOM_COUNT}"
fi
echo ""
sleep 5
done