Consumption Client Usage Details Using Python SDK for Python

141 Views Asked by At

I am using the below code to fetch Usage details from Azure according to my requiremnet. I want to fetch usage details for last year but it is fetching from Jan 1 2024 by default. Can anyone tell me if I can fetch same details daily? Or else If I fetch the same details monthly when should I fetch it so that I can get Usage details for the whole month.

from azure.mgmt.resource import SubscriptionClient
from azure.mgmt.consumption import ConsumptionManagementClient
from azure.identity import ClientSecretCredential
from datetime import datetime

import pandas as pd

""
tenant_id = "b"
client_id = "9"
client_secret = "x"


# credentials = ClientSecretCredential(tenant_id, client_id, client_secret)
credentials = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)


data = []

startDate = datetime(2023, 12, 1).isoformat()
endDate = datetime(2023, 12, 31).isoformat()

subscription_client = SubscriptionClient(credentials)
subscriptions = subscription_client.subscriptions.list()


for subscription in subscriptions:
    subscription_id = subscription.subscription_id

    consumption_client = ConsumptionManagementClient(credentials, subscription_id)
    scope = "/subscriptions/" + subscription_id
    filters = 'usageEnd le '+ str(endDate)
    usage_details = consumption_client.usage_details.list(scope, filter=filters)
    
    for detail in usage_details:
        # print(detail)
        tags = detail.tags
        if tags is not None:
            department = tags.get('Department', '-')  
            email = tags.get('E-mail', '-')
            owner = tags.get('Owner', '-')
        else:
            department = '-'
            email = '-'
            owner = '-'
        # print(detail)
        data.append({
            "Start": "2023-01-01",
            "End": "2023-01-31",
            "FetchDate": detail.date,
            "Department": department,
            "E-mail": email,
            "Owner": owner,
            "Billing AccountId": detail.billing_account_id,
            "Billing AccountName": detail.billing_account_name,
            "Subscription Id": detail.subscription_guid,
            "Subscription Name": detail.subscription_name,
            "Meter ID": detail.meter_id,
            "Quantity": detail.quantity,
            "Unit Price": detail.unit_price,
            "Currency Code": detail.billing_currency_code,
            "Resource Location": detail.resource_location_normalized,
            "Service": detail.consumed_service,
            "InstanceName": detail. instance_name,
            "Product": detail.product,
            "ProductIdentifier": detail.product_identifier,
            "Product OrderId": detail.product_order_id,
            "Product OrderName": detail.product_order_name,
            "Resource Group Name": detail.resource_group,
            "Charge Type": detail.charge_type,
            "Frequency": detail.frequency,
            "CostInBillingCurrency": detail.cost_in_billing_currency,
            "CostInPricingCurrency": detail.cost_in_pricing_currency,
            "Market Price": detail.market_price,
            "Cost": detail.cost_in_usd,
            "Additional Info": detail.additional_info, 
        })
        # print()
df = pd.DataFrame(data)```
1

There are 1 best solutions below

0
Sampath On

The code below is to fetch and analyze Azure usage data:

  • Acquiring an OAuth Token for authentication with Azure services.

  • Fetching Azure usage data for a specified time range and grouping.

  • Transforming the raw response data into a more structured format.

  • Creating pandas dataframes for resource-wise and total cost analysis.

  • Sending an email report with the analysis results in an attractive table format.

  • Code taken from git for Azure Cost Management Runbook using Python.


# Modify the run_script function
def run_script():
    azure_usage = AzureUsage()

    # Specify the desired time range for daily usage
    start_date = "2023-01-01"  # Change this to the desired start date
    end_date = "2023-01-02"    # Change this to the day after the desired end date

    # Fetch daily usage details
    usage_result = azure_usage.run(start_date, end_date, groupby="ResourceGroup")
    print(*usage_result, sep="\n")

    # Send the daily report
    azure_usage.send_report(usage_result)
    print("Done")

# Rest of the code remains unchanged




 def run(self, startdate, enddate, grain="Daily", groupby = None):
        
        payload = {
            "type": "ActualCost",
            "dataSet": {
                    "granularity": grain,
                    "aggregation": {
                        "totalCost": {
                            "name": "PreTaxCost",
                            "function": "Sum"
                        },
                        "totalCostUSD": {
                            "name": "PreTaxCostUSD",
                            "function": "Sum"
                        }
                    }
            },
            "timeframe": "Custom",
            "timePeriod": {
                "from": startdate,
                "to": enddate
            }
        }
        
        if groupby != None:
            payload['dataSet']['grouping'] = [{
                "type": "Dimension",
                "name": groupby
            }]
        
        payloadjson = json.dumps(payload)
        print("Payload Json: ", payloadjson)
        self.usagedata = []
        response = requests.post(
            self.costmanagementUrl, data=payloadjson, headers=self.headers)
        if response.status_code == 200:
            self.transform(payloadjson, response.text)
        else:
            print("error")
            print("error " + response.text)
        
        return self.usagedata
    

    
    def transform(self, payloadjson, response):
        result = json.loads(response)
        print("Result: ", result)
        for record in result["properties"]["rows"]:
            usageRecord = {}
            for index, val in enumerate(record):
                columnName = result["properties"]["columns"][index]
                if columnName["type"] == "Number":
                    usageRecord[columnName["name"]] = val
                else:
                    usageRecord[columnName["name"]] = val
            
            self.usagedata.append(usageRecord)
        
        nextLink = result["properties"]["nextLink"]
        if nextLink != None:
            nextLinkResponse = requests.post(
                nextLink, data=payloadjson, headers=self.headers)
            if nextLinkResponse.status_code == 200:
                self.transform(payloadjson, nextLinkResponse.text)
            else:
                print("error in fetching next page " + nextLink)
                print("error " + nextLinkResponse.text)
    
    
    
    
    def makeResourceWiseTable(self,usageData):
        data=usageData
        df=pd.DataFrame(data)
        df.sort_values('ResourceGroup', inplace=True)
        df.drop_duplicates(subset='ResourceGroup', keep='first', inplace=True)
        
        return df
    
    
    
    
    def makeTotalCostTable(self, resourceWiseCostData):
        resourceCostData=resourceWiseCostData
        column_sum = resourceCostData['PreTaxCost'].sum()
        
        cost=[]
        totalCost=[column_sum]
        print(type(totalCost))
        for item in totalCost:
            cost.append(item)
        
        df = pd.DataFrame(cost, columns=['TotalCost'])
        
        return df
    
    
    
    def send_mail(self, body1, body2):
        message = MIMEMultipart()
        sender = automationassets.get_automation_variable("FromMail")
        receivers = ['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']
        # receivers = 'xxx.mycompany.com'
        message['Subject'] = 'Daily Cost Analysis Report'
        message['From'] = sender
        message['To'] = ";".join(receivers)
        # message['To'] = sender
        body_content = "<h3>Date: <font color=#33cc33>"+convertToStrpday+" <font color=#000000>to ""<font color=#33cc33>"+convertToStrtday+"</h3><h3><font color=#000000>Subscription: <font color=#0099ff><SubscriptionNamw></h3><h2><font color=#000000>Total Cost of Single Day Usage:</h2>"+body2+"\n"+"<h2><font color=#000000>Cost of Individual Resource Group:</h2>"+body1
        
        message.attach(MIMEText(body_content,"html"))
        msg_body = message.as_string()
        
        smtpObj = smtplib.SMTP('yourcompany.mail.protection.outlook.com',25)
        smtpObj.sendmail(sender, receivers, msg_body)
    
    
    
    
    def send_report(self, usageData):
        tabulate_form1 = self.makeResourceWiseTable(usageData)
        tabulate_form2 = self.makeTotalCostTable(tabulate_form1)
        output1 = build_table(tabulate_form1, 'yellow_dark')
        output2 = build_table(tabulate_form2, 'green_dark')
        try:
            self.send_mail(output1, output2)
            print('Mail sent successfullly!!')
        except:
            print('Unable to send Mail')
        
        
def run_script():
    azure_usage = AzureUsage()
    # usageResult = azure_usage.run("2021-09-27", "2021-09-28")
    usageResult = azure_usage.run(
        start_date, end_date, groupby="ResourceGroup")
    print(*usageResult, sep="\n")
    azure_usage.send_report(usageResult)
    print("Done")



 
today = date.today()
previuosDate = today-timedelta(days=1)
convertToStrtday=today.strftime("%Y-%m-%d")
convertToStrpday=previuosDate.strftime("%Y-%m-%d")