Last active
October 1, 2015 08:53
-
-
Save mkulke/7da9a7f21c43b43e1403 to your computer and use it in GitHub Desktop.
No-frills bash script to get memory utilization of your kubernetes cluster. depends on jq
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 | |
# The MIT License (MIT) | |
# Copyright (c) 2015 Magnus Kulke <mkulke at gmail dot com> | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# USAGE: | |
# | |
# To specify clusters or contexts, set the KUBECTL env before calling: | |
# KUBECTL="kubectl --context=myothercontext" ./show-cluster-utilization.sh | |
set -e | |
set -o pipefail | |
KUBECTL=${KUBECTL:-kubectl} | |
function parse_units { | |
read UNIT_STRING | |
echo ${UNIT_STRING} | sed 's/Gi/ * 1024 Mi/g;s/Mi/ * 1024 Ki/g;s/Ki/ * 1024/g' | bc | |
} | |
function to_mb { | |
read BYTES | |
echo ${BYTES} | awk '{printf "%d", $1 / 1024 / 1024}' | |
} | |
declare -a NODE_NAMES=($(${KUBECTL} get nodes -o json | jq -r \ | |
'.items | map(.metadata.name) | join(" ")')) | |
declare -a CAPACITIES=($(${KUBECTL} get nodes -o json \ | |
| jq -r '.items | map(.status.capacity.memory) | join(" ")')) | |
PODS=$(${KUBECTL} get --all-namespaces pods -o json) | |
TABLE="Node Used Capacity ¯\_(ツ)_/¯ Percentage" | |
for i in "${!NODE_NAMES[@]}" | |
do | |
NODE_NAME=${NODE_NAMES[$i]} | |
CAPACITY=$(echo "${CAPACITIES[$i]}" | parse_units | to_mb) | |
NODE_PODS=$(echo "${PODS}" | jq -r --arg node ${NODE_NAME} \ | |
'.items | map(select(.spec.nodeName == $node or .spec.host == $node))') | |
HAS_NULL_LIMIT=$(echo "${NODE_PODS}" | jq -r \ | |
'map(.spec.containers | map(.resources.limits == null) | any) | any') | |
if [ "${HAS_NULL_LIMIT}" == "true" ] | |
then | |
NA_NOTE="* Some containers do not have memory limits specificed, hence N/A." | |
ROW="${NODE_NAME} N/A N/A N/A N/A" | |
else | |
POD_LIMITS=$(echo "${NODE_PODS}" | jq -r \ | |
'map(.spec.containers | map(.resources.limits.memory) | join(" + ")) | join(" + ")') | |
TOTAL=$(echo "${POD_LIMITS}" | parse_units | to_mb) | |
PERCENTAGE=$(echo "${TOTAL} ${CAPACITY}" | awk '{printf "%d", $1 / $2 * 100}') | |
BAR_FULL=$(echo "(${PERCENTAGE} + 5) / 10" | bc) | |
BAR_EMPTY=$(echo "10 - ${BAR_FULL}" | bc) | |
printf -v full %${BAR_FULL}s | |
printf -v empty %${BAR_EMPTY}s | |
BAR=$(printf '%s%s' "${full// /#}${empty// /-}") | |
ROW="${NODE_NAME} ${TOTAL}Mi ${CAPACITY}Mi [${BAR}] ${PERCENTAGE}%" | |
fi | |
TABLE="${TABLE}\n${ROW}" | |
done | |
echo -e "${TABLE}" | column -t -s ' ' | |
if [ "${NA_NOTE}" != "" ] | |
then | |
echo "${NA_NOTE}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment