Skip to content

Instantly share code, notes, and snippets.

@myh-st
Last active March 1, 2023 08:30
Show Gist options
  • Save myh-st/c6e96b08418ea868a5bc77d4da16e80d to your computer and use it in GitHub Desktop.
Save myh-st/c6e96b08418ea868a5bc77d4da16e80d to your computer and use it in GitHub Desktop.

Revisions

  1. myh-st revised this gist Mar 1, 2023. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions CalcwSLA.sh
    Original file line number Diff line number Diff line change
    @@ -37,8 +37,6 @@ DATA=$(aws cloudwatch get-metric-data \
    --output 'text')

    # Calculate the SLA percentage
    # SLA = (1 - (sum of all values) / (total number of values)) * 100

    SUM=$(echo $DATA | tr ' ' '\n' | awk '{sum += $1} END {print sum}')
    #echo "SUM=$SUM"
    TOTAL=$(echo $DATA | tr ' ' '\n' | wc -l)
  2. myh-st created this gist Mar 1, 2023.
    50 changes: 50 additions & 0 deletions CalcwSLA.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/bin/bash

    INSTANCE_ID="i-0axxxxxxxxxx"
    NAMESPACE="AWS/EC2"
    METRIC_NAME="StatusCheckFailed_Instance"
    START_TIME=$(date -u -d '30 day ago' +%Y-%m-%dT%H:%M:%SZ)
    END_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
    AWS_PROFILE=my_profile

    # Retrieve the metric data
    DATA=$(aws cloudwatch get-metric-data \
    --metric-data-queries '[
    {
    "Id": "m1",
    "MetricStat": {
    "Metric": {
    "Namespace": "'"$NAMESPACE"'",
    "MetricName": "'"$METRIC_NAME"'",
    "Dimensions": [
    {
    "Name": "InstanceId",
    "Value": "'"$INSTANCE_ID"'"
    }
    ]
    },
    "Period": 300,
    "Stat": "Maximum"
    },
    "ReturnData": true
    }
    ]' \
    --start-time "$START_TIME" \
    --end-time "$END_TIME" \
    --scan-by 'TimestampDescending' \
    --query 'MetricDataResults[0].Values[]' \
    --profile "$AWS_PROFILE" \
    --output 'text')

    # Calculate the SLA percentage
    # SLA = (1 - (sum of all values) / (total number of values)) * 100

    SUM=$(echo $DATA | tr ' ' '\n' | awk '{sum += $1} END {print sum}')
    #echo "SUM=$SUM"
    TOTAL=$(echo $DATA | tr ' ' '\n' | wc -l)
    #echo "TOTAL=$TOTAL"
    SLA=$(echo "scale=2; (100 * ($SUM * 5 - 43200)) / 43200" | bc | awk '{print ($1 < 0) ? -$1 : $1}' )
    echo "SLA=$SLA"

    # Print the result
    echo "SLA for $METRIC_NAME on instance $INSTANCE_ID over the last 30 days: $SLA%"