-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-project-quota-usage-s11.sh
executable file
·160 lines (146 loc) · 6.46 KB
/
check-project-quota-usage-s11.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
#
# 2018…2021 M.Bernhardt, SysEleven GmbH, Berlin, Germany
#
# Check and show quota and usage for all regions conveniently for comparison with OPENSTACK_QUOTA_COMPUTE_STACK
#
# Project xxx-xxx-stack-k8s (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
# https://smith.syseleven.de/cloud/projects/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/show
# quota: CBK: xx vCPUs / xx GiB RAM / xx FIPs / xx GiB VS / xx GiB OS
# quota: DBL: xx vCPUs / xx GiB RAM / xx FIPs / xx GiB VS / xx GiB OS
# usage: CBK: xx vCPUs (x Inst.) / xx GiB RAM / xx FIPs / xx GiB VS / xx GiB OS
# usage: DBL: xx vCPUs (x Inst.) / xx GiB RAM / xx FIPs / xx GiB VS / xx GiB OS
basename="$(basename "$0")"
os_quota=~/repos/openstack/s11stack-manager/client/quota.py
openstack_version="$(openstack --version 2>&1)"
case "$openstack_version" in
"openstack 3.16.0") ;;
"openstack 3.17.0") ;;
"openstack 4.0.0") ;;
"openstack 6.0.0") ;;
*)
echo "WARNING: script not tested with version $openstack_version"
esac
bytes_to_gib() {
local bytes="$1"
local gib="?"
if [[ "$bytes" == -1 ]] ; then
gib="∞"
elif [[ "$bytes" == 0 ]] ; then
gib="0"
elif [[ "$bytes" == 1 ]] ; then
gib="0*"
elif [[ "$bytes" =~ ^[0-9]+$ ]] ; then
gib=$((bytes/2**30))
local rest=$((bytes-gib*2**30))
if [[ $rest -gt 0 ]] ; then
gib="<$((gib+1))"
fi
fi
echo "$gib"
}
# for bytes in null "" -2 -1 0 1 2 $((2**30-1)) $((2**30)) $((2**30+1)) ; do bytes_to_gib $bytes ; done
quota_check() {
local SAVE_OS_REGION_NAME=$OS_REGION_NAME
local delimiter="quota "
local denotion="$(echo "$@")"
local warn_instances=""
local warn_cpu=""
local warn_ram=""
local quota_yml="$($os_quota show $query_project_id)"
test -z "$quota_yml" && echo "quota check failed" && exit 1
result_regions=($(echo "$quota_yml" | yq e 'keys | .[]' -))
for region in "${result_regions[@]}" ; do
quota_cores="$(echo "$quota_yml" | yq e ".$region"'."compute.cores"' -)"
quota_fips="$(echo "$quota_yml" | yq e ".$region"'."network.floatingips"' -)"
quota_vs="$(echo "$quota_yml" | yq e ".$region"'."volume.space_gb"' -)"
quota_instances="$(echo "$quota_yml" | yq e ".$region"'."compute.instances"' -)"
quota_ram="$(($(echo "$quota_yml" | yq e ".$region"'."compute.ram_mb"' -) /1024))"
quota_os_bytes_q="$(echo "$quota_yml" | yq e ".$region"'.objectstorage[] | select( .type == "quobyte" ) | .space_bytes' -)"
quota_os_bytes_c="$(echo "$quota_yml" | yq e ".$region"'.objectstorage[] | select( .type == "ceph" ) | .space_bytes' -)"
quota_os_q="$(bytes_to_gib $quota_os_bytes_q)"
quota_os_c="$(bytes_to_gib $quota_os_bytes_c)"
if [[ "$quota_os_q" == "?" ]] ; then
quota_os="$quota_os_c"
elif [[ "$quota_os_c" == "?" ]] ; then
quota_os="$quota_os_q"
else
quota_os="$quota_os_q+$quota_os_c"
fi
if [[ ${quota_cores} != ${quota_instances} ]] ; then warn_instances=" (${quota_instances} Inst.)" ; fi
if [[ ${quota_cores} -gt $((quota_ram/4)) ]] ; then warn_cpu="!!!" ; fi
if [[ ${quota_cores} -lt $((quota_ram/4)) ]] ; then warn_ram="!!!" ; fi
echo -e "$delimiter$(echo -n "$region" | tr "[:lower:]" "[:upper:]"): ${quota_cores} vCPUs${warn_cpu}${warn_instances} / ${quota_ram} GiB RAM${warn_ram} / ${quota_fips} FIPs / ${quota_vs} GiB VS / ${quota_os} GiB OS" "$@"
warn_instances=""
warn_cpu=""
warn_ram=""
done
export OS_REGION_NAME="$SAVE_OS_REGION_NAME"
}
usage_check() {
local SAVE_OS_REGION_NAME=$OS_REGION_NAME
local delimiter="usage "
local denotion="$(echo "$@")"
local warn_cpu=""
local warn_ram=""
local usage_yml="$($os_quota usage --filter compute,network,s3,objectstorage,volume $query_project_id)"
test -z "$usage_yml" && echo "usage check failed" && exit 1
#result_regions=($(echo "$usage_yml" | yq r -j -- - | jq -r 'keys | .[]'))
result_regions=($(echo "$usage_yml" | yq e 'keys | .[]' -))
for region in "${result_regions[@]}" ; do
usage_cores="$(echo "$usage_yml" | yq e ".$region"'."compute.cores"' -)"
usage_fips="$(echo "$usage_yml" | yq e ".$region"'."network.floatingips"' -)"
usage_vs="$(echo "$usage_yml" | yq e ".$region"'."volume.space_gb"' -)"
usage_instances="$(echo "$usage_yml" | yq e ".$region"'."compute.instances"' -)"
usage_ram="$(($(echo "$usage_yml" | yq e ".$region"'."compute.ram_mb"' -) /1024))"
usage_os_bytes_q="$(echo "$usage_yml" | yq e ".$region"'.objectstorage[] | select( .type == "quobyte" ) | .space_bytes' -)"
usage_os_bytes_c="$(echo "$usage_yml" | yq e ".$region"'.objectstorage[] | select( .type == "ceph" ) | .space_bytes' -)"
usage_os_q="$(bytes_to_gib $usage_os_bytes_q)"
usage_os_c="$(bytes_to_gib $usage_os_bytes_c)"
if [[ "$usage_os_q" == "?" ]] ; then
usage_os="$usage_os_c"
elif [[ "$usage_os_c" == "?" ]] ; then
usage_os="$usage_os_q"
else
usage_os="$usage_os_q+$usage_os_c"
fi
if [[ ${usage_cores} -gt $((usage_ram/4)) ]] ; then warn_cpu="!!!" ; fi
if [[ ${usage_cores} -lt $((usage_ram/4)) ]] ; then warn_ram="!!!" ; fi
echo -e "$delimiter$(echo -n "$region" | tr "[:lower:]" "[:upper:]"): ${usage_cores} vCPUs${warn_cpu} (${usage_instances} Inst.) / ${usage_ram} GiB RAM${warn_ram} / ${usage_fips} FIPs / ${usage_vs} GiB VS / ${usage_os} GiB OS"
warn_cpu=""
warn_ram=""
done
#echo
export OS_REGION_NAME="$SAVE_OS_REGION_NAME"
}
#query_project="${1:-$OS_PROJECT_ID}"
for query_project in $@ ; do
query_project_json="$(openstack project show -f json $query_project)"
query_project_id="$(echo "$query_project_json" | jq -r ".id")"
query_project_name="$(echo "$query_project_json" | jq -r ".name")"
query_project_parent_id="$(echo "$query_project_json" | jq -r ".parent_id")"
query_project_description="$(echo "$query_project_json" | jq -r ".description")"
if [[ "$query_project_id" == "$query_project_name" && "$query_project_parent_id" == "ccd6a18cd67945d7b6a637711a02b5d2" ]] ; then
# syseleven-openstack-cloud / Keystone domain for customer projects (NCS)
query_project_name="$query_project_description"
fi
echo "https://smith.syseleven.de/cloud/projects/${query_project_id}/show"
echo '```'
echo "# Project $query_project_id ($query_project_name)"
if [ -z "$query_project_id" ] ; then continue ; fi
if [[ $basename =~ "change" ]] ; then
quota_check '# before'
usage_check
while true ; do
read -sp '```' keypress ; printf '\r'
quota_check '# after'
done
fi
if [[ $basename =~ "quota" ]] ; then
quota_check
fi
if [[ $basename =~ "usage" ]] ; then
usage_check
fi
echo '```'
done