-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-network-metrics.sh
More file actions
executable file
·404 lines (347 loc) · 14.4 KB
/
Copy pathcheck-network-metrics.sh
File metadata and controls
executable file
·404 lines (347 loc) · 14.4 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/bash
# Script to check CloudWatch network metrics for EC2 instances
# Usage: ./check-network-metrics.sh [--instance INDEX] [--start-time TIME] [--end-time TIME]
# Function to display usage
usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --instance INDEX Query specific instance by index (0-based), omit for all instances"
echo " --start-time TIME Start time in UTC format (e.g., 2025-09-25T07:00:00Z)"
echo " --end-time TIME End time in UTC format (e.g., 2025-09-25T08:00:00Z)"
echo " --instance-type TYPE Instance type for limit calculations (default: m5.xlarge)"
echo " --help Show this help message"
echo ""
echo "If --start-time and --end-time are not provided, defaults to last 1 hour"
echo "TIME format: YYYY-MM-DDTHH:MM:SSZ (UTC)"
exit 1
}
# Function to convert bytes to human readable format
human_readable() {
local bytes=$1
local period_seconds=300 # 5 minutes
# Convert to bits per second
local bps=$(echo "scale=2; $bytes * 8 / $period_seconds" | bc -l)
if (( $(echo "$bps >= 1000000000" | bc -l) )); then
echo "$(echo "scale=2; $bps / 1000000000" | bc -l) Gbps"
elif (( $(echo "$bps >= 1000000" | bc -l) )); then
echo "$(echo "scale=2; $bps / 1000000" | bc -l) Mbps"
elif (( $(echo "$bps >= 1000" | bc -l) )); then
echo "$(echo "scale=2; $bps / 1000" | bc -l) Kbps"
else
echo "$(echo "scale=2; $bps" | bc -l) bps"
fi
}
# Function to format IOPS
format_iops() {
local ops=$1
local period_seconds=300 # 5 minutes
# Convert to operations per second
local ops_per_sec=$(echo "scale=1; $ops / $period_seconds" | bc -l)
if (( $(echo "$ops_per_sec >= 1000" | bc -l) )); then
echo "$(echo "scale=1; $ops_per_sec / 1000" | bc -l)K IOPS"
else
echo "$(echo "scale=1; $ops_per_sec" | bc -l) IOPS"
fi
}
# Function to format throughput (bytes to MB/s)
format_throughput() {
local bytes=$1
local period_seconds=300 # 5 minutes
# Convert to MB per second
local mbps=$(echo "scale=2; $bytes / $period_seconds / 1048576" | bc -l)
if (( $(echo "$mbps >= 1000" | bc -l) )); then
echo "$(echo "scale=2; $mbps / 1000" | bc -l) GB/s"
else
echo "$(echo "scale=1; $mbps" | bc -l) MB/s"
fi
}
# Function to get max value from CloudWatch data
get_max_value() {
local json_data="$1"
local value=$(echo "$json_data" | jq -r '.Datapoints | map(.Maximum // .Average) | max // 0')
# Handle null/empty values
if [[ "$value" == "null" ]] || [[ -z "$value" ]] || [[ "$value" == "" ]]; then
echo "0"
else
echo "$value"
fi
}
# Function to get instance type limits from AWS
get_instance_limits() {
local instance_type="$1"
echo "Getting limits for $instance_type..." >&2
local limits_json
limits_json=$(aws ec2 describe-instance-types --instance-types "$instance_type" --query 'InstanceTypes[0].[NetworkInfo.NetworkPerformance,EbsInfo.EbsOptimizedInfo.BaselineIops,EbsInfo.EbsOptimizedInfo.BaselineThroughputInMBps,EbsInfo.EbsOptimizedInfo.MaximumIops,EbsInfo.EbsOptimizedInfo.MaximumThroughputInMBps]' --output json 2>/dev/null)
if [[ $? -ne 0 ]] || [[ -z "$limits_json" ]]; then
echo "Warning: Could not get limits for $instance_type, using defaults" >&2
echo "Up to 10 Gigabit|3000|125"
return
fi
local network_perf=$(echo "$limits_json" | jq -r '.[0] // "Up to 10 Gigabit"')
local baseline_iops=$(echo "$limits_json" | jq -r '.[1] // 3000')
local baseline_throughput_mbps=$(echo "$limits_json" | jq -r '.[2] // 125')
local max_iops=$(echo "$limits_json" | jq -r '.[3] // 20000')
local max_throughput_mbps=$(echo "$limits_json" | jq -r '.[4] // 593.75')
# Use actual baseline values from AWS instead of hardcoded gp3 limits
# If baseline values are null or 0, fall back to reasonable defaults
if [[ "$baseline_iops" == "null" ]] || [[ "$baseline_iops" == "0" ]]; then
baseline_iops=3000
fi
if [[ "$baseline_throughput_mbps" == "null" ]] || [[ "$baseline_throughput_mbps" == "0" ]]; then
baseline_throughput_mbps=125
fi
echo "$network_perf|$baseline_iops|$baseline_throughput_mbps"
}
# Function to parse network performance string to baseline bps
parse_network_performance() {
local network_perf="$1"
# Parse AWS network performance to baseline (sustained) bandwidth
case "$network_perf" in
*"25 Gigabit"*) echo "3125000000" ;; # 25 Gbps dedicated
*"10 Gigabit"*) echo "1250000000" ;; # 10 Gbps dedicated
*"5 Gigabit"*) echo "625000000" ;; # 5 Gbps dedicated
*"Up to 25"*) echo "625000000" ;; # Up to 25 Gbps (assume ~2.5 Gbps baseline)
*"Up to 10"*) echo "125000000" ;; # Up to 10 Gbps (assume ~1 Gbps baseline)
*"Up to 5"*) echo "62500000" ;; # Up to 5 Gbps (assume ~500 Mbps baseline)
*"High"*) echo "125000000" ;; # High = ~1 Gbps baseline
*"Moderate"*) echo "62500000" ;; # Moderate = ~500 Mbps baseline
*"Low"*) echo "31250000" ;; # Low = ~250 Mbps baseline
*) echo "125000000" ;; # Conservative default
esac
}
# Parse command line arguments
INSTANCE_INDEX=""
START_TIME=""
END_TIME=""
INSTANCE_TYPE="m5.xlarge"
while [[ $# -gt 0 ]]; do
case $1 in
--instance)
INSTANCE_INDEX="$2"
shift 2
;;
--start-time)
START_TIME="$2"
shift 2
;;
--end-time)
END_TIME="$2"
shift 2
;;
--instance-type)
INSTANCE_TYPE="$2"
shift 2
;;
--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Check if required tools are installed
for cmd in jq bc aws; do
if ! command -v $cmd &> /dev/null; then
echo "Error: $cmd is required but not installed"
exit 1
fi
done
# Set default times if not provided (last 1 hour)
if [[ -z "$START_TIME" ]]; then
START_TIME=$(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ)
fi
if [[ -z "$END_TIME" ]]; then
END_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
fi
echo "Querying CloudWatch metrics from $START_TIME to $END_TIME"
echo "Period: 300 seconds (5 minutes)"
echo "Instance type: $INSTANCE_TYPE"
echo ""
# Get instance type limits
LIMITS=$(get_instance_limits "$INSTANCE_TYPE")
IFS='|' read -r NETWORK_PERF MAX_IOPS MAX_THROUGHPUT_MBPS <<< "$LIMITS"
BASELINE_BPS=$(parse_network_performance "$NETWORK_PERF")
echo ""
# Get terraform output
echo "Getting instance information from terraform..."
# Change to terraform directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
cd "$SCRIPT_DIR/terraform" || {
echo "Error: Could not change to terraform directory at $SCRIPT_DIR/terraform"
exit 1
}
TERRAFORM_OUTPUT=$(terraform output -json 2>/dev/null)
if [[ $? -ne 0 ]] || [[ -z "$TERRAFORM_OUTPUT" ]]; then
echo "Error: Could not get terraform output. Make sure you're in the terraform directory and terraform has been applied."
exit 1
fi
# Parse instances from terraform output
INSTANCES=$(echo "$TERRAFORM_OUTPUT" | jq -r '
.instances.value[] |
"\(.instance_id)|\(.public_ip)|\(.region | gsub("[abc]$"; ""))"
')
if [[ -z "$INSTANCES" ]]; then
echo "Error: No instances found in terraform output"
exit 1
fi
# Convert to array for indexing
readarray -t INSTANCE_ARRAY <<< "$INSTANCES"
# Determine which instances to query
if [[ -n "$INSTANCE_INDEX" ]]; then
if [[ ! "$INSTANCE_INDEX" =~ ^[0-9]+$ ]] || [[ "$INSTANCE_INDEX" -ge "${#INSTANCE_ARRAY[@]}" ]]; then
echo "Error: Invalid instance index. Valid range: 0-$((${#INSTANCE_ARRAY[@]}-1))"
exit 1
fi
INSTANCES_TO_QUERY=("${INSTANCE_ARRAY[$INSTANCE_INDEX]}")
echo "Querying instance at index $INSTANCE_INDEX"
else
INSTANCES_TO_QUERY=("${INSTANCE_ARRAY[@]}")
echo "Querying all ${#INSTANCE_ARRAY[@]} instances"
fi
echo ""
echo "Instance Limits ($INSTANCE_TYPE) - Baseline/Sustained Performance:"
echo " Network: $NETWORK_PERF (baseline estimated)"
echo " EBS: $MAX_IOPS IOPS baseline, $MAX_THROUGHPUT_MBPS MB/s baseline"
echo "========================================================================="
# Query each instance
for i in "${!INSTANCES_TO_QUERY[@]}"; do
INSTANCE_INFO="${INSTANCES_TO_QUERY[$i]}"
IFS='|' read -r INSTANCE_ID PUBLIC_IP REGION <<< "$INSTANCE_INFO"
if [[ -n "$INSTANCE_INDEX" ]]; then
echo "Instance $INSTANCE_INDEX: $INSTANCE_ID ($PUBLIC_IP) in $REGION"
else
echo "Instance $i: $INSTANCE_ID ($PUBLIC_IP) in $REGION"
fi
echo "----------------------------------------"
# Query NetworkIn
NETWORK_IN=$(aws cloudwatch get-metric-statistics \
--region "$REGION" \
--namespace AWS/EC2 \
--metric-name NetworkIn \
--dimensions Name=InstanceId,Value="$INSTANCE_ID" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period 300 \
--statistics Maximum \
2>/dev/null)
# Query NetworkOut
NETWORK_OUT=$(aws cloudwatch get-metric-statistics \
--region "$REGION" \
--namespace AWS/EC2 \
--metric-name NetworkOut \
--dimensions Name=InstanceId,Value="$INSTANCE_ID" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period 300 \
--statistics Maximum \
2>/dev/null)
# Query EBS Read IOPS
EBS_READ_OPS=$(aws cloudwatch get-metric-statistics \
--region "$REGION" \
--namespace AWS/EC2 \
--metric-name EBSReadOps \
--dimensions Name=InstanceId,Value="$INSTANCE_ID" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period 300 \
--statistics Maximum \
2>/dev/null)
# Query EBS Write IOPS
EBS_WRITE_OPS=$(aws cloudwatch get-metric-statistics \
--region "$REGION" \
--namespace AWS/EC2 \
--metric-name EBSWriteOps \
--dimensions Name=InstanceId,Value="$INSTANCE_ID" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period 300 \
--statistics Maximum \
2>/dev/null)
# Query EBS Read Throughput
EBS_READ_BYTES=$(aws cloudwatch get-metric-statistics \
--region "$REGION" \
--namespace AWS/EC2 \
--metric-name EBSReadBytes \
--dimensions Name=InstanceId,Value="$INSTANCE_ID" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period 300 \
--statistics Maximum \
2>/dev/null)
# Query EBS Write Throughput
EBS_WRITE_BYTES=$(aws cloudwatch get-metric-statistics \
--region "$REGION" \
--namespace AWS/EC2 \
--metric-name EBSWriteBytes \
--dimensions Name=InstanceId,Value="$INSTANCE_ID" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period 300 \
--statistics Maximum \
2>/dev/null)
# Parse results
MAX_IN=$(get_max_value "$NETWORK_IN")
MAX_OUT=$(get_max_value "$NETWORK_OUT")
MAX_READ_OPS=$(get_max_value "$EBS_READ_OPS")
MAX_WRITE_OPS=$(get_max_value "$EBS_WRITE_OPS")
MAX_READ_BYTES=$(get_max_value "$EBS_READ_BYTES")
MAX_WRITE_BYTES=$(get_max_value "$EBS_WRITE_BYTES")
# Check if we have any data (with safe number handling)
HAS_NETWORK_DATA=0
HAS_EBS_DATA=0
# Safely check network data
if [[ "$MAX_IN" != "0" ]] || [[ "$MAX_OUT" != "0" ]]; then
HAS_NETWORK_DATA=1
fi
# Safely check EBS data
if [[ "$MAX_READ_OPS" != "0" ]] || [[ "$MAX_WRITE_OPS" != "0" ]] || [[ "$MAX_READ_BYTES" != "0" ]] || [[ "$MAX_WRITE_BYTES" != "0" ]]; then
HAS_EBS_DATA=1
fi
if [[ "$HAS_NETWORK_DATA" == "0" ]] && [[ "$HAS_EBS_DATA" == "0" ]]; then
echo " No data available for this time period"
else
# Network metrics
if [[ "$HAS_NETWORK_DATA" == "1" ]]; then
echo " Network:"
echo " Peak NetworkIn: $(human_readable $MAX_IN)"
echo " Peak NetworkOut: $(human_readable $MAX_OUT)"
# Calculate network utilization percentage
MAX_TOTAL=$(echo "scale=2; ($MAX_IN + $MAX_OUT) * 8 / 300" | bc -l)
UTILIZATION=$(echo "scale=1; $MAX_TOTAL * 100 / $BASELINE_BPS" | bc -l)
echo " Network utilization: ${UTILIZATION}% of baseline (sustained)"
fi
# EBS metrics
if [[ "$HAS_EBS_DATA" == "1" ]]; then
echo " EBS Storage:"
if (( $(echo "$MAX_READ_OPS > 0" | bc -l) )); then
echo " Peak Read IOPS: $(format_iops $MAX_READ_OPS)"
fi
if (( $(echo "$MAX_WRITE_OPS > 0" | bc -l) )); then
echo " Peak Write IOPS: $(format_iops $MAX_WRITE_OPS)"
fi
if (( $(echo "$MAX_READ_BYTES > 0" | bc -l) )); then
echo " Peak Read Throughput: $(format_throughput $MAX_READ_BYTES)"
fi
if (( $(echo "$MAX_WRITE_BYTES > 0" | bc -l) )); then
echo " Peak Write Throughput: $(format_throughput $MAX_WRITE_BYTES)"
fi
# Calculate EBS utilization using dynamic limits
TOTAL_IOPS=$(echo "scale=1; ($MAX_READ_OPS + $MAX_WRITE_OPS) / 300" | bc -l)
TOTAL_THROUGHPUT=$(echo "scale=1; ($MAX_READ_BYTES + $MAX_WRITE_BYTES) / 300 / 1048576" | bc -l)
if (( $(echo "$TOTAL_IOPS > 0" | bc -l) )); then
IOPS_UTILIZATION=$(echo "scale=1; $TOTAL_IOPS * 100 / $MAX_IOPS" | bc -l)
echo " EBS IOPS utilization: ${IOPS_UTILIZATION}% of ${MAX_IOPS} baseline (sustained)"
fi
if (( $(echo "$TOTAL_THROUGHPUT > 0" | bc -l) )); then
THROUGHPUT_UTILIZATION=$(echo "scale=1; $TOTAL_THROUGHPUT * 100 / $MAX_THROUGHPUT_MBPS" | bc -l)
echo " EBS throughput utilization: ${THROUGHPUT_UTILIZATION}% of ${MAX_THROUGHPUT_MBPS} MB/s baseline (sustained)"
fi
fi
fi
echo ""
done
echo "Note: Peak values are maximum over any 5-minute window in the time range"
echo "Network: Baseline utilization shows peak usage vs estimated baseline (>100% uses burst capacity)"
echo "EBS: Utilization shown vs instance baseline limits. Volume limits may also apply."