-
Notifications
You must be signed in to change notification settings - Fork 3
/
gcp_resource_count.sh
176 lines (156 loc) · 7.48 KB
/
gcp_resource_count.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
set -e
LOG_FILE='gcp_resource_count.log'
WORKLOAD_VM_UNITS=1
WORKLOAD_FUNCTION_UNITS=50
WORKLOAD_SERVERLESS_CONTAINER_UNITS=10
WORKLOAD_VM_IMAGE_UNITS=1
WORKLOAD_CONTAINER_IMAGE_UNITS=10
WORKLOAD_CONTAINER_HOST_UNITS=1
_tmp_files=$(mktemp)
cleanup() {
rm -f $(< "${_tmp_files}") "${_tmp_files}"
}
trap cleanup EXIT
_make_temp_file() {
local tmp_file=$(mktemp)
echo "${tmp_file}" >> "${_tmp_files}"
echo "${tmp_file}"
}
PROJECT_ID=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-p|--project)
PROJECT_ID="$2"
shift # past argument
shift # past value
;;
*) # unknown option
shift # past argument
;;
esac
done
if [ -z "$PROJECT_ID" ]; then
echo "[+] Getting projects list"
PROJECTS_FILE=$(mktemp)
gcloud -q projects list --filter="lifecycleState:ACTIVE" --format json > "${PROJECTS_FILE}"
PROJECT_LEN="$(cat ${PROJECTS_FILE} | jq -r '. | length')"
echo "Found ${PROJECT_LEN} projects"
PROJECTS="$(cat ${PROJECTS_FILE} | jq -r '.[].projectId')"
echo "[+] Iterating projects and counting resources"
else
PROJECTS="$PROJECT_ID"
echo "[+] counting resources for project ${PROJECT_ID}"
fi
echo
total_vms=0
total_functions=0
total_cloud_run=0
total_container_images=0
total_vm_images=0
total_gke_nodes=0
counter=0
_temp_project_output=$(_make_temp_file)
for project in $PROJECTS; do
echo "Processing Project: $project"
gcloud -q compute instances list --project "${project}" --format json > ${_temp_project_output} 2>> $LOG_FILE || echo "Failed to get Virtual Machines for project ${project}"
project_vm_count=$(cat "${_temp_project_output}" | jq -r '. | length')
if [ -n "$project_vm_count" ]; then
total_vms=$((total_vms + project_vm_count))
echo "Virtual Machines Count: $project_vm_count"
fi
gcloud -q functions list --project "${project}" --format json > ${_temp_project_output} 2>> $LOG_FILE || echo "Failed to get Serverless Functions for project ${project}"
project_function_count=$(cat "${_temp_project_output}" | jq -r '. | length')
if [ -n "$project_function_count" ]; then
total_functions=$((total_functions + project_function_count))
echo "Serverless Functions Count: $project_function_count"
fi
gcloud -q run services list --project "${project}" --format='json(spec.template.spec.containers)' > ${_temp_project_output} 2>> $LOG_FILE || echo "Failed to get Serverless Containers for project ${project}"
containers_group=$(cat "${_temp_project_output}" | jq '.[].spec.template.spec.containers | length')
project_cloud_run_count=0
for containers in $containers_group; do
project_cloud_run_count=$((project_cloud_run_count + $containers))
done
if [[ -n "$project_cloud_run_count" && "$project_cloud_run_count" -ne 0 ]]; then
total_cloud_run=$((total_cloud_run + project_cloud_run_count))
echo "Serverless Containers Count: $project_cloud_run_count"
fi
gcrHosts=("gcr.io" "us.gcr.io" "eu.gcr.io" "asia.gcr.io")
project_container_images_count=0
for host in "${gcrHosts[@]}"; do
gcloud -q container images list --repository=${host}/"${project}" --format json > ${_temp_project_output} 2>> $LOG_FILE || echo "Failed to get Container Images for project ${project}, host: [${host}]"
host_container_images_count=$(cat "${_temp_project_output}" | jq -r '. | length')
if [ -n "$host_container_images_count" ]; then
project_container_images_count=$((project_container_images_count + host_container_images_count))
fi
done
if [[ -n "$project_container_images_count" && "$project_container_images_count" -ne 0 ]]; then
project_container_images_count=$(echo "$project_container_images_count*1.1" | awk '{printf "%.0f", $0}') # we scan 2 images per one repository and we decided to multiply the count by 1.1 based on production statistics
total_container_images=$((total_container_images + project_container_images_count))
echo "Container Images Count: $project_container_images_count"
fi
gcloud -q compute images list --no-standard-images --project "${project}" --format json > ${_temp_project_output} 2>> $LOG_FILE || echo "Failed to get VM images for project ${project}"
project_vm_images_count=$(cat "${_temp_project_output}" | jq -r '. | length')
if [ -n "$project_vm_images_count" ]; then
total_vm_images=$((total_vm_images + project_vm_images_count))
echo "VM images Count: $project_vm_images_count"
fi
gcloud -q container clusters list --project "${project}" --format='get(currentNodeCount)' --filter="NOT autopilot.enabled:true"> ${_temp_project_output} 2>> $LOG_FILE || echo "Failed to get Container Hosts for project ${project}"
clusters_node_counts=$(cat "${_temp_project_output}")
project_nodes_count=0
for node_count in $clusters_node_counts; do
project_nodes_count=$((project_nodes_count + node_count))
done
if [[ -n "$project_nodes_count" && "$project_nodes_count" -ne 0 ]]; then
total_gke_nodes=$((total_gke_nodes + project_nodes_count))
echo "Container Hosts Count: $project_nodes_count"
fi
#Increment counter
counter=$((counter+1))
if [ -n "$PROJECT_LEN" ]; then
echo -n "Progress: $counter/$PROJECT_LEN projects"
fi
# Add a line break
echo -e "\n"
done;
# Workloads calculation
vm_workloads=$(( ( total_vms + WORKLOAD_VM_UNITS / 2 ) / WORKLOAD_VM_UNITS ))
if [[ $vm_workloads -eq 0 && $total_vms -gt 0 ]]; then
vm_workloads=1
fi
function_workloads=$(( ( total_functions + WORKLOAD_FUNCTION_UNITS / 2 ) / WORKLOAD_FUNCTION_UNITS ))
if [[ $function_workloads -eq 0 && $total_functions -gt 0 ]]; then
function_workloads=1
fi
container_workloads=$(( ( total_cloud_run + WORKLOAD_SERVERLESS_CONTAINER_UNITS / 2 ) / WORKLOAD_SERVERLESS_CONTAINER_UNITS ))
if [[ $container_workloads -eq 0 && $total_cloud_run -gt 0 ]]; then
container_workloads=1
fi
container_image_workloads=$(( ( total_container_images + WORKLOAD_CONTAINER_IMAGE_UNITS / 2 ) / WORKLOAD_CONTAINER_IMAGE_UNITS ))
if [[ $container_image_workloads -eq 0 && $total_container_images -gt 0 ]]; then
container_image_workloads=1
fi
vm_image_workloads=$(( ( total_vm_images + WORKLOAD_VM_IMAGE_UNITS / 2 ) / WORKLOAD_VM_IMAGE_UNITS ))
if [[ $vm_image_workloads -eq 0 && $total_vm_images -gt 0 ]]; then
vm_image_workloads=1
fi
container_host_workloads=$(( ( total_gke_nodes + WORKLOAD_CONTAINER_HOST_UNITS / 2 ) / WORKLOAD_CONTAINER_HOST_UNITS ))
if [[ $container_host_workloads -eq 0 && $total_gke_nodes -gt 0 ]]; then
container_host_workloads=1
fi
total_workloads=$(( vm_workloads + function_workloads + container_workloads + container_image_workloads + vm_image_workloads + container_host_workloads ))
echo "=============="
echo "Total results:"
echo "=============="
echo "Virtual Machines Count: $total_vms (Workload Units: ${vm_workloads})"
echo "Serverless Functions Count: $total_functions (Workload Units: ${function_workloads})"
echo "Serverless Containers Count: $total_cloud_run (Workload Units: ${container_workloads})"
echo "Container Images Count: $total_container_images (Workload Units: ${container_image_workloads})"
echo "VM Images Count: $total_vm_images (Workload Units: ${vm_image_workloads})"
echo "Container Hosts Count: $total_gke_nodes (Workload Units: ${container_host_workloads})"
echo "--------------------------------------"
echo "TOTAL Estimated Workload Units: ${total_workloads}"
echo
echo "Please verify if errors were encountered during the resource enumeration in the log file: ${LOG_FILE}"