1. 程式人生 > >AWS billing-report處理指令碼

AWS billing-report處理指令碼

#以下程式碼執行前提是取得的aws賬單報告含有ResourceID項和resourceTags/user:Name項,如果生成的賬單報告沒有這兩項可以通過付款賬戶開啟
# 執行以下程式碼可以生成兩個csv檔案,其中new_report.csv為排序後的賬單檔案,篩選項為ARN,Name標籤,混合成本,產品程式碼,用量,AZ,資源型別,核心數,記憶體,作業系統,開始時間,結束時間
# final_report.csv為在new_report.csv的基礎上僅篩選EC2和RDS,並按ResourceID進行排序

import csv
import datetime

#輸入檔案地址
OriginalLocation = billing-report-1.csv'
#輸出檔案目錄
StorageLocation = 'C:billing-report-1/'

def newreport_writer():       #get the csvfile and sort useful item
    with open(StorageLocation + 'new_report.csv','w',encoding='UTF-8',newline='') as newreport:
        newrow = csv.writer(newreport)
        with open(OriginalLocation, 'r',encoding='UTF-8') as billing_report:
            reader = csv.DictReader(billing_report)
            for row in reader:
                #if len(row["resourceTags/user:Name"]) != 0 and row["resourceTags/user:Name"] != "resourceTags/user:Name":
                if row["resourceTags/user:Name"] != "resourceTags/user:Name":
                    # ARN,Name標籤,混合成本,產品程式碼,用量,AZ,資源型別,核心數,記憶體,作業系統,開始時間,結束時間
                    newrow.writerow([row["lineItem/ResourceId"]]+[row["resourceTags/user:Name"]]+[row["lineItem/BlendedCost"]]+[row["lineItem/ProductCode"]]+
                                    [row["lineItem/UsageAmount"]]+[row["product/region"]]+[row["pricing/term"]]+[row["product/vcpu"]]+[row["product/memory"]]+
                                    [row["product/operatingSystem"]]+[row["lineItem/UsageStartDate"]]+[row["lineItem/UsageEndDate"]])
        billing_report.closed
    newreport.closed

def ascending_order(): #sort by Name and ResourceID
    with open(StorageLocation + 'new_report.csv', 'r', encoding='UTF-8', newline='') as newreport:
        data = csv.reader(newreport)
        sortedlist = sorted(data, key=lambda x: (x[1], x[0]), reverse=True)
        with open(StorageLocation + "new_report.csv", "w", newline='') as f:
            fileWriter = csv.writer(f, delimiter=',')
            fileWriter.writerow(["ResourceID"] + ["Name"] + ["Cost"] + ["ProductCode"] + ["UsageAmount"] + ["Region"] + ["Term"] + ["Vcpu"] + ["Memory"] + ["OS"] + ["StartDate"] + ["EndDate"])
            for row in sortedlist:
                fileWriter.writerow(row)
        f.close()
    newreport.closed

def Integrete_Item_ByResourceId():      #new only integrete EC2 and RDS
    with open(StorageLocation + 'new_report.csv', 'r') as newreport:
        cost = ""
        Item_Date = ""
        OperationDate = 0
        # data = [["ResourceID","Name","Cost","ProductCode","UsageAmount","Region","Term","Vcpu","Memory","OS","OperationDate"]]
        data = [["ResourceID", "Name", "Cost", "ProductCode", "UsageAmount", "Region", "Term", "Vcpu", "Memory", "OS"]]
        LastResourceID,ResourceID,Name,ProductCode,UsageAmount,Region,Term,Vcpu,Memory,OS,StartDate,EndDate = "","overlook the null of ResourceID","","","","","","","","","",""
        reader = csv.DictReader(newreport)
        for row in reader:
            if "i-0" in row["ResourceID"] or "arn:aws:rds:" in row["ResourceID"]:
                result = "instance/i-0" in row["ResourceID"]
                if not result:
                    if row["ResourceID"] == LastResourceID:
                        LastResourceID = row["ResourceID"]
                        ResourceID = row["ResourceID"]
                        Name = row["Name"]
                        cost += float(row["Cost"])               #add the cost when the Name same as last Name
                        ProductCode = row["ProductCode"]
                        UsageAmount = row["UsageAmount"]
                        Region = row["Region"]
                        Term = row["Term"]
                        if len(row["Vcpu"]) > 0:                 #judeg the Vcpu/Memory/OS's length and select the useful information
                            Vcpu = row["Vcpu"]
                        if len(row["Memory"]) > 0:
                            Memory = row["Memory"]
                        if len(row["OS"]) > 0:
                            OS = row["OS"]
                        # get the delta-T
                        Item_Date = (datetime.datetime.strptime(row["EndDate"], '%Y-%m-%dT%H:%M:%SZ') - datetime.datetime.strptime(row["StartDate"], '%Y-%m-%dT%H:%M:%SZ')).seconds/3600
                        # add the delta-T to the OpererationDate
                        OperationDate += float(Item_Date)
                        # StartDate = row["StartDate"]
                        # EndDate = row["EndDate"]
                        Item_Date = 0
                    else:
                        LastResourceID = row["ResourceID"]
                        data.append([
                            ResourceID,Name,cost,ProductCode,UsageAmount,Region,Term,Vcpu,Memory,OS
                        ])
                        cost = 0
                        OperationDate = 0
        with open(StorageLocation + 'final_report.csv', 'w', encoding='UTF-8', newline='') as finalreport:
            writer_row = csv.writer(finalreport)
            for row in data:
                writer_row.writerow(row)
        finalreport.closed
    newreport.closed

def Integrete_Item_ByName():
    with open(StorageLocation + 'new_report.csv', 'r') as newreport:
        cost = ""
        data = [["Name","Cost","ProductCode","UsageAmount","Region","Term","Vcpu","Memory","OS","StartDate","EndDate"]]
        Name,ProductCode,UsageAmount,Region,Term,Vcpu,Memory,OS,StartDate,EndDate = "overlook the null of ResourceID and Name","","","","","","","","",""
        reader = csv.DictReader(newreport)
        for row in reader:
            if row["Name"] == Name:
                Name = row["Name"]
                cost += float(row["Cost"])                #add the cost when the Name same as last Name
                ProductCode = row["ProductCode"]
                UsageAmount = row["UsageAmount"]
                Region = row["Region"]
                Term = row["Term"]
                Vcpu = row["Vcpu"]
                Memory = row["Memory"]
                OS = row["OS"]
                StartDate = row["StartDate"]
                EndDate = row["EndDate"]
            else:
                data.append([
                    Name,cost,ProductCode,UsageAmount,Region,Term,Vcpu,Memory,OS,StartDate,EndDate
                ])
                Name = row["Name"]
                cost = 0
        print(data)
        with open(StorageLocation + 'final_report.csv', 'w', encoding='UTF-8', newline='') as finalreport:
            writer_row = csv.writer(finalreport)
            for row in data:
                writer_row.writerow(row)
        finalreport.closed
    newreport.closed

newreport_writer()
ascending_order()
Integrete_Item_ByResourceId()
#Integrete_Item_ByName()