Created
September 12, 2018 09:29
-
-
Save andragabr/4089d31d6ea65e85fcd5976999975fa9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| import os | |
| from_email="" | |
| to_email="" | |
| #AWS total last 12 spendings | |
| aws_budget=200 | |
| max_estimated_charge_12_hour=$(aws --region us-east-1 cloudwatch get-metric-statistics \ | |
| --namespace "AWS/Billing" \ | |
| --metric-name "EstimatedCharges" \ | |
| --dimension "Name=Currency,Value=USD" \ | |
| --start-time $(date -v -12H +"%Y-%m-%dT%H:%M:00") --end-time $(date +"%Y-%m-%dT%H:%M:00") \ | |
| --statistic Maximum \ | |
| --period 60 \ | |
| --output text | sort -r -k 3 | head -n 1 | cut -f 2) | |
| max_estimated_charge_before_last_12_hour=$(aws --region us-east-1 cloudwatch get-metric-statistics \ | |
| --namespace "AWS/Billing" \ | |
| --metric-name "EstimatedCharges" \ | |
| --dimension "Name=Currency,Value=USD" \ | |
| --start-time $(date -v -24H +"%Y-%m-%dT%H:%M:00") \ | |
| --end-time $(date -v -12H +"%Y-%m-%dT%H:%M:00") \ | |
| --statistic Maximum \ | |
| --period 60 \ | |
| --output text | sort -r -k 3 | head -n 1 | cut -f 2) | |
| #EC2 last 12 spendings | |
| EC2_max_estimated_charge_12_hour=$(aws --region us-east-1 cloudwatch get-metric-statistics \ | |
| --namespace "AWS/Billing" \ | |
| --metric-name "EstimatedCharges" \ | |
| --dimension "Name=ServiceName,Value=AmazonEC2,Name=Currency,Value=USD" \ | |
| --start-time $(date -v -12H +"%Y-%m-%dT%H:%M:00") --end-time $(date +"%Y-%m-%dT%H:%M:00") \ | |
| --statistic Maximum \ | |
| --period 60 \ | |
| --output text | sort -r -k 3 | head -n 1 | cut -f 2) | |
| EC2_max_estimated_charge_before_last_12_hour=$(aws --region us-east-1 cloudwatch get-metric-statistics \ | |
| --namespace "AWS/Billing" \ | |
| --metric-name "EstimatedCharges" \ | |
| --dimension "Name=ServiceName,Value=AmazonEC2,Name=Currency,Value=USD" \ | |
| --start-time $(date -v -24H +"%Y-%m-%dT%H:%M:00") \ | |
| --end-time $(date -v -12H +"%Y-%m-%dT%H:%M:00") \ | |
| --statistic Maximum \ | |
| --period 60 \ | |
| --output text | sort -r -k 3 | head -n 1 | cut -f 2) | |
| aws_bill=$(echo "$max_estimated_charge_12_hour $max_estimated_charge_before_last_12_hour" | awk '{print $1-$2}') | |
| EC2_bill=$(echo "$EC2_max_estimated_charge_12_hour $EC2_max_estimated_charge_before_last_12_hour" | awk '{print $1-$2}') | |
| if [ $aws_bill > $aws_budget ] | |
| then | |
| #Cu cat la suta a crescut cheltuielile daca bill=300 si a devenit bill2=372.8 | |
| P=$aws_bill*100/$aws_budget | |
| x=$P-100 | |
| aws ses send-email \ | |
| --from "$from_email" \ | |
| --destination "ToAddresses=<$to_email>" \ | |
| --message "Subject={Data=Billing alert,Charset=utf8},Body={Text={Data=AWS last 12 hour bill $`echo $aws_bill` increased with `echo $x`%,Charset=utf8}}" \ | |
| --region "us-east-1" | |
| fi | |
| aws ses send-email \ | |
| --from "$from_email" \ | |
| --destination "ToAddresses=<$to_email>" \ | |
| --message "Subject={Data=Billing alert,Charset=utf8},Body={Text={Data=EC2 last 12 hour bill $`echo $EC2_bill`,Charset=utf8}}" \ | |
| --region "us-east-1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment