python 用于解析详细的AWS计费CSV文件的简单脚本。脚本显示按区域,存储桶和我们分解的S3操作的成本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于解析详细的AWS计费CSV文件的简单脚本。脚本显示按区域,存储桶和我们分解的S3操作的成本相关的知识,希望对你有一定的参考价值。

# -*- coding:utf-8 -*-
'''
Simplistic script to parse the detailed AWS billing CSV file.

Script displays cost of S3 operations broken down per region, bucket and usage 
type (either storage or network). It also sums up the amount of storage used per bucket. 
Output is filtered wrt to costs < 1$.

See http://docs.aws.amazon.com/awsaccountbilling/latest/about/programaccess.html for 
how to set up programmatic access to your billing.

Should be simple enough to enhance this script and use it for other AWS resources 
(EC2, EMR, etc)

@author: @oddskool <https://github.com/oddskool>
@license: BSD 3 clauses
'''

import sys
import csv
from collections import defaultdict

def add_type(d):
    if d['RecordType'] == 'UsageQuantity':
        return None
    for field in ('Cost', 'UsageQuantity'):
        d[field] = float(d[field])
    for field in ('LinkedAccountId', 'InvoiceID', 'RecordType', 'RecordId',
                  'PayerAccountId', 'SubscriptionId'):
        del d[field]
    return d

def parse(stats, d):
    d = add_type(d)
    if not d:
        return
    if d['ProductName'] != 'Amazon Simple Storage Service':
        return
    stats[(d['AvailabilityZone'] or 'N/A')+' * '+d['ResourceId']+' * '+d['UsageType']]['Cost'] += d['Cost']
    stats[(d['AvailabilityZone'] or 'N/A')+' * '+d['ResourceId']+' * '+d['UsageType']]['UsageQuantity'] += d['UsageQuantity']

if __name__ == '__main__':
    fd = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
    reader = csv.reader(fd, delimiter=',', quotechar='"')
    legend = None
    stats = defaultdict(lambda: defaultdict(int))
    for row in reader:
        if not legend:
            legend = row    
            continue
        d = dict(zip(legend, row))
        try:
            parse(stats, d)
        except Exception as e:
            print e
            print row
            print d
    data = [ (resource, cost_usage) for resource, cost_usage in 
             stats.iteritems() if cost_usage['Cost'] > 1.0 ]
    data.sort(key=lambda x:x[-1]['Cost'], reverse=True)
    for d in data:
        print "%50s : $%.2f - %.2f GB" % (d[0],d[1]['Cost'],d[1]['UsageQuantity'])

以上是关于python 用于解析详细的AWS计费CSV文件的简单脚本。脚本显示按区域,存储桶和我们分解的S3操作的成本的主要内容,如果未能解决你的问题,请参考以下文章

使用 boto3 和 python 将 AWS EC2 详细信息导出到 xlsx/csv

AWS 使用 python 计费

python 解析用于KnowBe4电子邮件上传的csv文件

用于获取 aws 堆栈层实例和标签详细信息的 Python 脚本

如何使用 Python 在 myBucket 中上传 CSV 文件并在 S3 AWS 中读取文件

使用 python 从 AWS S3 到 PostgreSQL Amazon RDS 的 CSV 文件