Skip to content

Instantly share code, notes, and snippets.

@prombouts
Last active October 6, 2021 13:38
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 prombouts/ed0bd566792abc7d281908a58dbe70b3 to your computer and use it in GitHub Desktop.
Save prombouts/ed0bd566792abc7d281908a58dbe70b3 to your computer and use it in GitHub Desktop.
Check usage limits in Azure
#!/bin/bash
echo "Starting..."
# Get all VM usage as json
vmjson=$(az vm list-usage --location westeurope -o json)
# Loop through all items and check whether limit has been reached
# If so, log to DevOps logger so pipeline will reflect warnings or errors
jq -c '.[]' <<< "$vmjson" | while read i; do
# Get values from JSON
currentValue=`jq -r .currentValue <<< $i`
limit=`jq -r .limit <<< $i`
localName=`jq -r .localName <<< $i`
perc=0
if [ "$limit" -gt "0" ]; then
perc=`echo "scale=2; $currentValue / $limit * 100" | bc`
fi
percint=${perc%.*}
if [ "$percint" -ge "85" ]; then
echo "##vso[task.logissue type=warning;]Running low on free $localName resources. Usage $perc";
fi
if [ "$percint" -ge "95" ]; then
echo "##vso[task.logissue type=error;]Running very low on free $localName resources. Usage $perc";
fi
echo "Usage percentage $perc [$localName]"
done
echo "Finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment