Skip to content

Instantly share code, notes, and snippets.

@mahrous-amer
Created January 7, 2025 04:31
Show Gist options
  • Save mahrous-amer/d7a4f4fffe03c8bfa6d5088667dde593 to your computer and use it in GitHub Desktop.
Save mahrous-amer/d7a4f4fffe03c8bfa6d5088667dde593 to your computer and use it in GitHub Desktop.
This script calculates total IOPS for the volumes, compares it with the instance's maximum allowed IOPS, and provides a compliance status based on EBS optimization support.
#!/bin/bash
PROFILE=""
INSTANCE_ID=""
echo "Instance ID: $INSTANCE_ID"
echo "Fetching instance details..."
instance_details=$(aws --profile $PROFILE ec2 describe-instances --instance-ids "$INSTANCE_ID" --query 'Reservations[0].Instances[0]' --output json)
echo "Instance details fetched successfully."
instance_type=$(echo "$instance_details" | jq -r '.InstanceType')
echo "Instance type: $instance_type"
ebs_optimized=$(echo "$instance_details" | jq -r '.EbsOptimized')
echo "Ebs Optimized: $ebs_optimized"
block_device_mappings=$(echo "$instance_details" | jq -c '.BlockDeviceMappings')
echo "Block Device Mappings: $block_device_mappings"
echo "Fetching instance type details..."
instance_type_details=$(aws --profile $PROFILE ec2 describe-instance-types --instance-types "$instance_type" --query 'InstanceTypes[0]' --output json)
echo "Instance type details fetched successfully."
ebs_optimized_support=$(echo "$instance_type_details" | jq -r '.EbsInfo.EbsOptimizedSupport')
max_iops=$(echo "$instance_type_details" | jq -r '.EbsInfo.EbsOptimizedInfo.MaximumIops // 0')
echo "EBS Optimized Support: $ebs_optimized_support"
echo "Max IOPS: $max_iops"
volume_ids=$(echo "$block_device_mappings" | jq -r '.[] | select(.Ebs) | .Ebs.VolumeId')
total_iops=0
if [ -z "$volume_ids" ]; then
echo "No volumes attached to the instance."
compliance_value="NOT_APPLICABLE"
annotation="No volumes attached."
else
echo "Processing volumes..."
while IFS= read -r volume_id; do
echo "Fetching details for volume ID: $volume_id"
volume_info=$(aws --profile $PROFILE ec2 describe-volumes --volume-ids "$volume_id" --query 'Volumes[0].{VolumeId:VolumeId, Iops:Iops, VolumeType:VolumeType, Size:Size}' --output json)
iops=$(echo "$volume_info" | jq -r '.Iops // 0')
total_iops=$((total_iops + iops))
echo "Volume ID: $volume_id - IOPS: $iops"
done <<< "$volume_ids"
echo "Total IOPS: $total_iops"
echo "Evaluating compliance..."
compliance_value="COMPLIANT"
annotation=""
if [ "$ebs_optimized_support" == "unsupported" ]; then
compliance_value="NOT_APPLICABLE"
annotation="Ebs Optimized: Unsupported"
elif [ "$ebs_optimized_support" == "supported" ] && [ "$ebs_optimized" == "false" ]; then
compliance_value="NOT_APPLICABLE"
annotation="Ebs Optimized: Supported but not enabled"
else
percent=$(( (total_iops * 100) / max_iops ))
annotation="$percent% - Total IOPS / Max IOPS: $total_iops / $max_iops"
if [ "$total_iops" -gt "$max_iops" ]; then
compliance_value="NON_COMPLIANT"
fi
fi
fi
echo "Compliance: $compliance_value"
echo "Annotation: $annotation"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment