import subprocess from datetime import datetime, timedelta import json import codecs def get_aws_bills(account_name, start_date, end_date): cmd = 'aws ce get-cost-and-usage --time-period Start=' + start_date + ',End=' + end_date + ' --granularity MONTHLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --profile ' + account_name result = subprocess.check_output(cmd, shell=True) j = json.loads(result) return j # Return 2020-10-17, 2020-10-23 def get_current_week_dates(): today = datetime.now().date() start = today - timedelta(days=today.weekday() + 2) end = start + timedelta(days=6) return str(start), str(end) def get_aws_instances(account_name,region): cmd = "aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[] | [0], Placement.AvailabilityZone,PublicIpAddress,InstanceType,State.Name,KeyName,LaunchTime]' --region " + region + " --output table --profile " + account_name result = subprocess.check_output(cmd, shell=True) decoded = codecs.decode(result, 'unicode_escape') return decoded def get_aws_s3(account_name,region): cmd = "aws s3 ls --region " + region + " --output table --profile " + account_name result = subprocess.check_output(cmd, shell=True) decoded = codecs.decode(result, 'unicode_escape') return decoded def get_aws_rds(account_name,region): cmd = "aws rds describe-db-instances --region " + region + " --output table --profile " + account_name result = subprocess.check_output(cmd, shell=True) decoded = codecs.decode(result, 'unicode_escape') return decoded def get_aws_keypair(account_name,region): cmd = "aws ec2 describe-key-pairs --region " + region + " --output text --profile " + account_name + " | cat | awk '{print $3}'" result = subprocess.check_output(cmd, shell=True) decoded = codecs.decode(result, 'unicode_escape') return decoded def get_gcp_instances(project_name): cmd = "gcloud compute instances list --project " + project_name result = subprocess.check_output(cmd, shell=True) return result # AWS Part ACCOUNT_LIST = ['pingcap','pingcap-dbaas','pingcap-test'] REGION_LIST = ['us-east-2','us-east-1','us-west-1','us-west-2','af-south-1','ap-east-1','ap-south-1','ap-northeast-3','ap-northeast-2','ap-southeast-1','ap-southeast-2','ap-northeast-1','ca-central-1','cn-north-1','cn-northwest-1','eu-central-1','eu-west-1','eu-west-2','eu-south-1','eu-west-3','eu-north-1','me-south-1','sa-east-1'] # GCP Part PROJECT_NAME = ['dbaas-central','dbaas-key','dbaas-oauth-proxy','dbaas-peer','dbaas-prod','dbaas-staging','dbaas-test-268007','pingcap-dbaas-build'] if __name__ == "__main__": start_date, end_date = get_current_week_dates() print("During " + start_date + "~" + end_date) print("") for account in ACCOUNT_LIST: bill = get_aws_bills(account,start_date, end_date) #print(str(bill)) #cost = float(bill['ResultsByTime'][0]['Total']['BlendedCost']['Amount']) + float(bill['ResultsByTime'][0]['Total']['UnblendedCost']['Amount']) cost = float(bill['ResultsByTime'][0]['Total']['UnblendedCost']['Amount']) print("[AWS Account] " + account + " " + str(cost) + " USD") print("==================================================================") print("AWS Resources") for account in ACCOUNT_LIST: print("AWS EC2") print("[AWS Account " + account + "]") print("=================================================") for region in REGION_LIST: try: instances = get_aws_instances(account,region) print("EC2 Region: " + region) print(instances) except: print("Region Empty") print("AWS EC2 Keypair") for region in REGION_LIST: try: instances = get_aws_keypair(account,region) print("EC2 Keypairs: " + region) print(instances) except: print("Region Empty") print("AWS S3") try: instances = get_aws_s3(account,region) print("S3 Region: " + region) print(instances) except: print("Region Empty") print("AWS RDS") for region in REGION_LIST: try: instances = get_aws_rds(account,region) print("RDS Region: " + region) print(instances) except: print("Region Empty") print("==================================================================") print("GCP Resources") for project in PROJECT_NAME: print(project) print("=================================================") print(get_gcp_instances(project))