Skip to content

Instantly share code, notes, and snippets.

@jkeam
Last active January 10, 2024 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeam/ec57539980d871ed12f174480112cd16 to your computer and use it in GitHub Desktop.
Save jkeam/ec57539980d871ed12f174480112cd16 to your computer and use it in GitHub Desktop.
CPU Count For All Worker Nodes (Ignoring Infras and Masters)
#!/bin/bash
function help() {
echo "This script finds all the CPU for all worker nodes (ignoring them if they are also infra)"
echo
echo "Syntax: nodes.sh [-g|h]"
echo "options:"
echo "h Print this help."
echo "o Obfuscate node names."
echo
}
function main() {
obfuscate=$1
for c in $(oc get ManagedClusters -o name)
do
echo ""
justname=$(echo $c | sed 's/managedcluster\.cluster\.open-cluster-management\.io\///g' | sed 's/ *$//g')
nodes=$(oc get ManagedClusterInfo $justname -n $justname -o jsonpath='{.status.nodeList}' | jq -c -r '.[]')
uid=$(oc get ManagedClusterInfo $justname -n $justname -o jsonpath='{.status.clusterID}')
echo "Cluster Name: $justname"
echo "Cluster UID: $uid"
sum="0"
worker_num="0"
for n in $nodes
do
name=$(echo $n | jq '.name' | sed 's/\"//g')
worker=$(echo $n | jq '.labels | keys | index("node-role.kubernetes.io/worker")')
infra=$(echo $n | jq '.labels | keys | index("node-role.kubernetes.io/infra")')
master=$(echo $n | jq '.labels | keys | index("node-role.kubernetes.io/master")')
if [[ "$worker" != "null" ]] && [[ "$infra" = "null" ]] && [[ "$master" = "null" ]]; then
cpu=($(echo $n | jq '.capacity.cpu' | sed 's/ *$//g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/,//g' | sed 's/\"//g'))
if [[ $obfuscate -eq 0 ]]; then
echo "$name: $cpu"
else
echo "worker $worker_num: $cpu"
fi
sum=$(( $sum + $cpu ))
worker_num=$(( $worker_num + 1 ))
fi
done
echo "Total cpu count: $sum"
done
}
obfuscate=0
while getopts "ho" option; do
case $option in
h)
help
exit
;;
o)
obfuscate=1
;;
\?)
echo "Error: Invalid option"
exit
;;
esac
done
main $obfuscate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment