Skip to content

Instantly share code, notes, and snippets.

@garethahealy
Last active March 14, 2017 12:03
Show Gist options
  • Save garethahealy/b60de450575e9f0094e2d3b2b72a283f to your computer and use it in GitHub Desktop.
Save garethahealy/b60de450575e9f0094e2d3b2b72a283f to your computer and use it in GitHub Desktop.
quota.sh
#!/usr/bin/env bash
projects=$(oc get projects --no-headers | cut -d' ' -f1 | xargs)
projects_array=($projects)
declare -A quota_soft_total
declare -A metrics_to_create
for project in "${projects_array[@]}" ; do
quota_array=$(oc get quota --output=json -n $project)
if [ ! -z ${quota_array+x} ]; then
quota_count=$(echo $quota_array | jq '.items | length')
for ((i = 0; i < $quota_count; i++)); do
quota=$(echo $quota_array | jq --arg i "$i" '.items[$i | tonumber]')
quota_name=$(echo $quota | jq '.metadata.name' | tr -d '"')
quota_namespace=$(echo $quota | jq '.metadata.namespace' | tr -d '"')
quota_hard=$(echo $quota | jq '.status.hard | to_entries')
if [ ! -z ${quota_hard+x} ]; then
quota_hard_count=$(echo $quota_hard | jq length)
for ((j = 0; j < $quota_hard_count; j++)); do
item=$(echo $quota_hard | jq --arg j "$j" '.[$j | tonumber]')
item_key=$(echo $item | jq '.key' | tr -d '"')
item_value=$(echo $item | jq '.value' | tr -d '"')
metric_id="$quota_namespace-$quota_name-$item_key-hard"
metrics_to_create[$metric_id]=$item_value
done
fi
quota_soft=$(echo $quota | jq '.status.used | to_entries')
if [ ! -z ${quota_soft+x} ]; then
quota_soft_count=$(echo $quota_soft | jq length)
for ((k = 0; k < $quota_soft_count; k++)); do
item=$(echo $quota_soft | jq --arg k "$k" '.[$k | tonumber]')
item_key=$(echo $item | jq '.key' | tr -d '"')
item_value=$(echo $item | jq '.value' | tr -d '"')
metric_id="$quota_namespace-$quota_name-$item_key-soft"
metrics_to_create[$metric_id]=$item_value
quota_soft_total[$quota_name-$item_key]=$((quota_soft_total[$quota_name-$item_key] + $item_value))
done
fi
done
fi
done
for key in "${!metrics_to_create[@]}"; do
echo "$key = ${metrics_to_create[$key]}"
./hawkpost.sh $key ${metrics_to_create[$key]} counters
echo ""
done
for key in "${!quota_soft_total[@]}"; do
echo "cluster-$key = ${quota_soft_total[$key]}"
./hawkpost.sh "cluster-$key" ${quota_soft_total[$key]} counters
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment