Skip to content

Instantly share code, notes, and snippets.

@logan2211
Created November 11, 2021 15:55
Show Gist options
  • Save logan2211/4c62deff2d451c8e9b69cdbba54e8ae9 to your computer and use it in GitHub Desktop.
Save logan2211/4c62deff2d451c8e9b69cdbba54e8ae9 to your computer and use it in GitHub Desktop.
Host memory over-commit (placement service)
#!/bin/bash
function get_resource_usage {
local UUID="$1"
local IFS=$'\n'
declare -Ag RESOURCE_USAGE
for i in $(openstack resource provider usage show "$UUID" -f value); do
CLASS=$(echo "$i" | awk '{ print $1 }')
USAGE=$(echo "$i" | awk '{ print $2 }')
RESOURCE_USAGE[${CLASS}]="${USAGE}"
done
}
function get_resource_inventory {
local UUID="$1"
local IFS=$'\n'
declare -Ag RESOURCE_INVENTORY
for i in $(openstack resource provider inventory list "$UUID" -f value); do
CLASS=$(echo "$i" | awk '{ print $1 }')
TOTAL=$(echo "$i" | awk '{ print $7 }')
RESERVED=$(echo "$i" | awk '{ print $5 }')
RESOURCE_INVENTORY["${CLASS}_TOTAL"]="${TOTAL}"
RESOURCE_INVENTORY["${CLASS}_RESERVED"]="${RESERVED}"
done
}
function report_usage {
local UUID="$1"
local HOST="$2"
if [[ ${RESOURCE_USAGE["MEMORY_MB"]} -gt $((RESOURCE_INVENTORY["MEMORY_MB_TOTAL"] - RESOURCE_INVENTORY["MEMORY_MB_RESERVED"])) ]]; then
OVERCOMMIT_MB=$(( (RESOURCE_INVENTORY["MEMORY_MB_TOTAL"] - RESOURCE_INVENTORY["MEMORY_MB_RESERVED"] - RESOURCE_USAGE["MEMORY_MB"]) * -1 ))
echo "$HOST: Over-committed by ${OVERCOMMIT_MB}MB. Total memory: ${RESOURCE_INVENTORY[MEMORY_MB_TOTAL]}MB Reserved: ${RESOURCE_INVENTORY[MEMORY_MB_RESERVED]}MB Used: ${RESOURCE_USAGE[MEMORY_MB]}MB"
else
echo "$HOST: No over-commit"
fi
}
_IFS="$IFS"
IFS=$'\n'
for i in $(openstack resource provider list -f value); do
UUID=$(echo "${i}" | awk '{ print $1 }')
HOST=$(echo "${i}" | awk '{ print $2 }')
get_resource_usage "$UUID"
get_resource_inventory "$UUID"
report_usage "$UUID" "$HOST"
done
IFS="$_IFS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment