-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-node-memory.sh
More file actions
executable file
·40 lines (30 loc) · 1.33 KB
/
Copy pathmonitor-node-memory.sh
File metadata and controls
executable file
·40 lines (30 loc) · 1.33 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
#!/bin/bash
# Monitor unallocated memory on a node every 2 seconds
# Usage: ./monitor-node-memory.sh [NODE_NAME]
# If no node name provided, uses the first node
NODE=${1:-$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}')}
echo "Monitoring unallocated memory on node: $NODE"
echo "Press Ctrl+C to stop"
echo ""
while true; do
# Get allocatable memory in bytes
ALLOCATABLE=$(kubectl get node "$NODE" -o jsonpath='{.status.allocatable.memory}' | sed 's/Ki$//' | awk '{print $1 * 1024}')
# Get total requested memory in bytes from all pods on the node
REQUESTED=$(kubectl get pods --all-namespaces -o jsonpath="{.items[?(@.spec.nodeName=='$NODE')].spec.containers[].resources.requests.memory}" | \
tr ' ' '\n' | \
grep -v '^$' | \
sed 's/Mi$//' | \
awk '{sum += $1} END {print sum * 1024 * 1024}')
# Handle empty requested (default to 0)
REQUESTED=${REQUESTED:-0}
# Calculate unallocated
UNALLOCATED=$((ALLOCATABLE - REQUESTED))
# Convert to Mi for readability
UNALLOCATED_MI=$((UNALLOCATED / 1024 / 1024))
ALLOCATABLE_MI=$((ALLOCATABLE / 1024 / 1024))
REQUESTED_MI=$((REQUESTED / 1024 / 1024))
# Print with timestamp
TIMESTAMP=$(date '+%H:%M:%S')
echo "[$TIMESTAMP] Allocatable: ${ALLOCATABLE_MI}Mi | Requested: ${REQUESTED_MI}Mi | Unallocated: ${UNALLOCATED_MI}Mi"
sleep 1
done