· 6 years ago · Apr 28, 2019, 03:20 AM
1#!/usr/bin/env python
2# Author: Germain LEFEBVRE [Ineat]
3
4# Prerequisites
5# 00 - Create API Access Key in AWS Accounts to monitor
6# 00 - Fill ~/.aws/credentials with API Access Keys
7# 01 - Fill ./config.yml 'aws_account' dict with aws profiles or access/secret key as following:
8# aws_accounts:
9# - name: Ineat iLab
10# profile: INEAT_ILAB
11# - name: Ineat iLab
12# access_key: ACCESS_KEY
13# secret_key: SECRET_KEY
14# 04 - Create InfluxDB database 'aws'
15# $ influx -execute 'create databases aws'
16# 05 - Run the python script
17# 06 - Stalk your AWS billing costs in Grafana
18
19import yaml, boto3
20from datetime import date,datetime
21from dateutil.relativedelta import relativedelta
22from influxdb import InfluxDBClient
23
24
25
26# AWS type cost
27aws_cost_type=['UnblendedCost']
28
29# Load configuration file
30with open("config.yml", 'r') as ymlfile:
31 cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader)
32
33# InfluxDB connexion
34inf_cl = InfluxDBClient('localhost', 8086, '', '', 'aws')
35
36
37# Browse AWS Accounts Profiles
38# Located in ~/.aws/credentials
39for account in cfg['aws_accounts']:
40 print(account['name'])
41
42 # Force region to work
43 region_name="eu-west-1"
44
45 # Open AWS Cost Explore
46 # When use Profile or Access Key
47 if 'profile' in account.keys():
48 session = boto3.Session(region_name=region_name,
49 profile_name=account['profile'])
50 elif 'access_key' in account.keys():
51 session = boto3.Session(region_name=region_name,
52 aws_access_key_id=account['access_key'],
53 aws_secret_access_key=account['secret_key'])
54
55 aws_cl_ce = session.client('ce')
56
57 # Load Costs dataset
58 cost_usage = aws_cl_ce.get_cost_and_usage(
59 TimePeriod={
60 #'Start': (date.today() + relativedelta(days=-1)).strftime('%Y-%m-%d'),
61 'Start': (date.today() + relativedelta(months=-1)).strftime('%Y-%m-01'),
62 'End': (date.today() + relativedelta(months=+1)).strftime('%Y-%m-01'),
63 },
64 Granularity='MONTHLY',
65 Metrics=[
66 ",".join(aws_cost_type)
67 ]
68 )
69
70 # Print dateset
71 for type in aws_cost_type:
72 for metric in cost_usage['ResultsByTime']:
73 account_name = account['name']
74 metric_time_type = "monthly"
75 metric_start = metric['TimePeriod']['Start']
76 metric_amount = metric['Total'][type]['Amount']
77 metric_unit = metric['Total'][type]['Unit']
78 print("%s %s %s" % (metric_start, metric_amount, metric_unit) )
79
80 json_body = [
81 {
82 "measurement": "billing",
83 "tags": {
84 "account": account_name,
85 "time type": metric_time_type,
86 },
87 #"time": datetime.now().isoformat(),
88 "time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
89 "fields": {
90 "value": metric['Total'][type]['Amount']
91 }
92 }
93 ]
94 inf_cl.write_points(json_body)
95
96result = inf_cl.query('select value from billing;')
97print("Result: {0}".format(result))