-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathresource-count-abc.sh
executable file
·154 lines (129 loc) · 4.92 KB
/
resource-count-abc.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
#!/bin/bash
# shellcheck disable=SC2102,SC2181,SC2207
# Instructions:
#
# - Go to the Alibaba Cloud Console
#
# - Open Cloud Shell >_
#
# - Click on the cloud on the left side of the Cloud Shell window
#
# - Upload this script
#
# - Make this script executable:
# chmod +x resource-count-abc.sh
#
# - Run this script:
# ./resource-count-abc.sh
#
# This script may generate errors when:
#
# - The API/CLI is not enabled.
# - You don't have permission to make the API/CLI calls.
#
# API/CLI used:
#
# - aliyun ecs DescribeRegions
# - aliyun ecs DescribeInstances
##########################################################################################
##########################################################################################
## Use of jq is required by this script.
##########################################################################################
if ! type "jq" > /dev/null; then
echo "Error: jq not installed or not in execution path, jq is required for script execution."
exit 1
fi
##########################################################################################
## Utility functions.
##########################################################################################
abc_regions_list() {
# shellcheck disable=SC2086
RESULT=$(aliyun ecs DescribeRegions 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
fi
}
abc_compute_instances_list() {
if [ -z "${2}" ]; then
# shellcheck disable=SC2086
RESULT=$(aliyun ecs DescribeInstances --region "${1}" --Status Running --MaxResults 100 2>/dev/null)
else
# shellcheck disable=SC2086
RESULT=$(aliyun ecs DescribeInstances --region "${1}" --Status Running --MaxResults 100 --NextToken "${2}" 2>/dev/null)
fi
if [ $? -eq 0 ]; then
echo "${RESULT}"
fi
}
####
get_regions() {
REGIONS=($(abc_regions_list | jq -r '.Regions.Region[].RegionId' 2>/dev/null | sort))
TOTAL_REGIONS=${#REGIONS[@]}
}
get_instance_count() {
COUNT=0
RESULT=$(abc_compute_instances_list "${1}")
COUNT=$(echo "${RESULT}" | jq -r '.TotalCount' 2>/dev/null)
echo "${COUNT}"
}
get_instance_count_via_pagination() {
COUNT=0
RESULT=$(abc_compute_instances_list "${1}")
INSTANCES=($(echo "${RESULT}" | jq -r '.Instances.Instance[].InstanceId' 2>/dev/null))
COUNT=$((COUNT + ${#INSTANCES[@]}))
NEXTTOKEN=$(echo "${RESULT}" | jq -r '.NextToken' 2>/dev/null)
while [ -n "${NEXTTOKEN}" ]; do
RESULT=$(abc_compute_instances_list "${1}" "${NEXTTOKEN}")
INSTANCES=($(echo "${RESULT}" | jq -r '.Instances.Instance[].InstanceId' 2>/dev/null))
COUNT=$((COUNT + ${#INSTANCES[@]}))
NEXTTOKEN=$(echo "${RESULT}" | jq -r '.NextToken' 2>/dev/null)
done
echo "${COUNT}"
}
##########################################################################################
## Set or reset counters.
##########################################################################################
reset_local_counters() {
COMPUTE_INSTANCES_COUNT=0
WORKLOAD_COUNT=0
}
reset_global_counters() {
COMPUTE_INSTANCES_COUNT_GLOBAL=0
WORKLOAD_COUNT_GLOBAL=0
}
##########################################################################################
## Iterate through the billable resource types.
##########################################################################################
count_resources() {
for ((REGION_INDEX=0; REGION_INDEX<=(TOTAL_REGIONS-1); REGION_INDEX++))
do
REGION="${REGIONS[$REGION_INDEX]}"
echo "###################################################################################"
echo "Processing Region: ${REGION}"
RESOURCE_COUNT=$(get_instance_count "${REGION}")
COMPUTE_INSTANCES_COUNT=$((COMPUTE_INSTANCES_COUNT + RESOURCE_COUNT))
echo " Count of Compute Instances: ${COMPUTE_INSTANCES_COUNT}"
WORKLOAD_COUNT=$((COMPUTE_INSTANCES_COUNT + 0))
echo "Total billable resources for Region: ${WORKLOAD_COUNT}"
echo "###################################################################################"
echo ""
COMPUTE_INSTANCES_COUNT_GLOBAL=$((COMPUTE_INSTANCES_COUNT_GLOBAL + COMPUTE_INSTANCES_COUNT))
reset_local_counters
done
echo "###################################################################################"
echo "Totals"
echo " Count of Compute Instances: ${COMPUTE_INSTANCES_COUNT_GLOBAL}"
WORKLOAD_COUNT_GLOBAL=$((COMPUTE_INSTANCES_COUNT_GLOBAL + 0))
echo "Total billable resources: ${WORKLOAD_COUNT_GLOBAL}"
echo "###################################################################################"
}
##########################################################################################
# Allow shellspec to source this script.
##########################################################################################
${__SOURCED__:+return}
##########################################################################################
# Main.
##########################################################################################
get_regions
reset_global_counters
count_resources