Skip to content

Instantly share code, notes, and snippets.

@riblo
Created October 4, 2019 15:47
Show Gist options
  • Save riblo/a95d3e9c6b356289b2d5e3f84628ca39 to your computer and use it in GitHub Desktop.
Save riblo/a95d3e9c6b356289b2d5e3f84628ca39 to your computer and use it in GitHub Desktop.
Kubernetes CLI cluster monitoring
#!/bin/bash
#
# "kubectl top nodes" based -> metric-servers or heapster needed
# possible CSV output w/o line 21
#
# output example:
# worker-1: 2 % ; 5 % memory
# worker-2: 10 % ; 86 % memory
# 2019/10/04,17.43.08,CPU & RAM,6%,45%
#
set -e
KUBECTL=$(which kubectl)
NODES=$($KUBECTL get nodes --no-headers -o custom-columns=NAME:.metadata.name | grep -v master)
MINUTES=60
function mon() {
local node_count=0
local total_percent_cpu=0
local total_percent_mem=0
local readonly nodes=$@
for n in $nodes; do
local requests=$($KUBECTL top node --no-headers $n)
local percent_cpu=$(echo $requests | awk '{print $3}' | tr "%" " " )
local percent_mem=$(echo $requests | awk '{print $5}' | tr "%" " " )
echo "$n: ${percent_cpu}% ; ${percent_mem}% memory"
node_count=$((node_count + 1))
total_percent_cpu=$((total_percent_cpu + percent_cpu))
total_percent_mem=$((total_percent_mem + percent_mem))
done
local readonly avg_percent_cpu=$((total_percent_cpu / node_count))
local readonly avg_percent_mem=$((total_percent_mem / node_count))
echo "$(date +%Y/%m/%d,%H.%M.%S),CPU & RAM,${avg_percent_cpu}%,${avg_percent_mem}%"
sleep 60
}
for i in {1..$MINUTES}
do
mon $NODES
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment