Last active
April 25, 2024 10:42
-
-
Save nmofonseca/0497ef87fbc060bc502ce9f5d6884d00 to your computer and use it in GitHub Desktop.
Script to get resources used and available in a K8s cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables to keep track of the total available CPU and memory | |
total_available_cpu=0 | |
total_available_memory=0 | |
for node in $(kubectl get nodes -o jsonpath='{.items[*].metadata.name}'); do | |
echo "Node: $node" | |
# Get the current usage | |
current_usage=$(kubectl top node $node | tail -n 1 | awk '{print $2, $4}') | |
current_cpu_usage=$(echo $current_usage | cut -d' ' -f1 | sed 's/m//') # remove 'm' from millicores | |
current_memory_usage=$(echo $current_usage | cut -d' ' -f2 | sed 's/Mi//') # remove 'Mi' from mebibytes | |
# Get the allocatable resources | |
allocatable_resources=$(kubectl get node $node -o json | jq -r '.status.allocatable | "\(.cpu),\(.memory)"') | |
allocatable_cpu=$(echo $allocatable_resources | cut -d',' -f1) # in cores or millicores | |
allocatable_memory=$(echo $allocatable_resources | cut -d',' -f2 | sed 's/Ki//') # remove 'Ki' from kibibytes | |
# Check if the allocatable CPU is in millicores or cores and convert to millicores if necessary | |
if [[ $allocatable_cpu == *m ]]; then | |
allocatable_cpu_millicores=$(echo "$allocatable_cpu" | sed 's/m//') # remove 'm' from millicores | |
else | |
allocatable_cpu_millicores=$(echo "$allocatable_cpu * 1000" | bc) | |
fi | |
# Get the allocated resources | |
allocated_resources=$(kubectl describe node $node | grep -A 5 "Allocated resources:" | grep -E '(cpu|memory)' | awk '{print $2}') | |
allocated_cpu=$(echo $allocated_resources | cut -d' ' -f1 | sed 's/[^0-9]*//g') # remove non-numeric characters | |
allocated_memory=$(echo $allocated_resources | cut -d' ' -f2 | sed 's/[^0-9]*//g') # remove non-numeric characters | |
# Calculate the available resources | |
available_cpu=$(echo "$allocatable_cpu_millicores - $current_cpu_usage" | bc) | |
available_memory=$(echo "$allocatable_memory - $current_memory_usage * 1024" | bc) # convert mebibytes to kibibytes | |
# Update the total available CPU and memory | |
total_available_cpu=$(echo "$total_available_cpu + $available_cpu" | bc) | |
total_available_memory=$(echo "$total_available_memory + $available_memory" | bc) | |
# Calculate the percentage of allocated resources vs allocatable resources | |
cpu_percentage=$(echo "$allocated_cpu / $allocatable_cpu_millicores * 100" | bc -l) | |
memory_percentage=$(echo "scale=2; ($allocated_memory * 1024 / $allocatable_memory) * 100" | bc -l) | |
# Check if allocated resources are 80% or higher of the allocatable resources | |
if (( $(echo "$cpu_percentage >= 80" | bc -l) )); then | |
echo "WARNING: Node CPU workload requests reaching close to maximum resource available" | |
fi | |
if (( $(echo "$memory_percentage >= 80" | bc -l) )); then | |
echo "WARNING: Node memory workload requests reaching close to maximum resource available" | |
fi | |
echo "Current CPU usage: $current_cpu_usage m" | |
echo "Allocatable CPU: $allocatable_cpu_millicores m" | |
echo "Allocated CPU: $allocated_cpu m" | |
echo "Percentage of CPU allocated: $cpu_percentage%" | |
echo "Available CPU: $available_cpu m" | |
echo "Current memory usage: $current_memory_usage Mi" | |
echo "Allocatable memory: $(echo "$allocatable_memory / 1024" | bc) Mi" # convert kibibytes to mebibytes | |
echo "Allocated memory: $allocated_memory Mi" | |
echo "Percentage of memory allocated: $memory_percentage%" | |
echo "Available memory: $(echo "$available_memory / 1024" | bc) Mi" # convert kibibytes to mebibytes | |
echo | |
done | |
# Print the total available CPU and memory across the cluster | |
echo "Total available CPU across the cluster: $total_available_cpu m" | |
echo "Total available memory across the cluster: $(echo "$total_available_memory / 1024" | bc) Mi" # convert kibibytes to mebibytes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment