简体   繁体   English

使用 Azure 计费 API/SDK 显示 Azure 成本分析数据

[英]Show azure cost analysis data using Azure billing API/SDK

In Azure portal ,if one subscription is selected ,the cost analysis can be viewed like the following screenshot在 Azure 门户中,如果选择了一个订阅,则可以像以下屏幕截图一样查看成本分析

Azure 成本分析

I want to programmatically fetch the information the like the one displayed above may be using using some python SDK API/REST API.我想以编程方式获取信息,就像上面显示的信息可能正在使用一些 python SDK API/REST API。 If anybody has any experience/idea on this ,please help.如果有人对此有任何经验/想法,请提供帮助。

After going through replies ,I have gone through the Azure Billing Rest API and I am now able to call the Usage Aggrgate and RateCard related Rest APIs.完成回复后,我完成了 Azure Billing Rest API,现在可以调用 Usage Aggrgate 和 RateCard 相关的 Rest API。

Following are the results of those REST Calls.以下是这些 REST 调用的结果。 . . Azure Billing Usage Aggregate Response Azure 计费使用情况汇总响应

Azure Billing Ratecard Response Azure 计费费率卡响应

But honestly speaking ,I still have not figured out how these to would give me detailed view like the cost analysis does where for each resource how much cost associated can be displayed.Actually I am very new to Azure probably that is why I am missing the link some where .但老实说,我仍然没有弄清楚这些如何给我详细的视图,就像成本分析一样,每个资源可以显示多少相关的成本。实际上我对 Azure 很陌生,这可能就是我错过的原因链接一些地方。

Can somebody give some hint here ?有人可以在这里给出一些提示吗?

If you already have the usage and the ratecard data, then you must combine them.如果您已经拥有使用情况和价目表数据,则必须将它们合并。 Take the meterId of the usage data and get the related ratecard data.获取使用数据的 meterId 并获取相关的价目表数据。 The ratecard data contains the MeterRates and the IncludedQuantity which you must take.价目表数据包含您必须采用的 MeterRates 和 IncludedQuantity。 There are probably multiple meter rates and the included quantity because there are probably different costs per usage (eg first 10 calls for free, 3 GB for free, ...).可能有多种计量费率和包含的数量,因为每次使用可能有不同的成本(例如,前 10 个免费电话,3 GB 免费,...)。 The consumption starts/is reseted at the 14th of the month.消费在当月 14 日开始/重置。 That's the reason why you have to read the data from the whole billing period (begins with 14th of each month), because that's the only way how you get the correct consumption.这就是为什么您必须读取整个计费周期(从每月 14 日开始)的数据的原因,因为这是您获得正确消费的唯一途径。

So, if you are using eg Azure Functions and you have a usage of 100.000 units per day and you want the costs from 20th - 30th, then the calculation works as follows: read data from 14th - 30th.因此,如果您正在使用例如 Azure Functions,并且每天的使用量为 100.000 个单位,并且您希望从 20 日到 30 日计算成本,那么计算方式如下:从 14 日到 30 日读取数据。 These are 17 days and therefore it used 1.700.000 units.这些是 17 天,因此它使用了 1.700.000 个单位。 The first 400.000 are for free = IncludedQuantity (so in this sample the first 4 days).前 400.000 是免费的 = IncludedQuantity(因此在此示例中为前 4 天)。 From the 400.001 unit on, you have to take the meter rate (0,0000134928 €) and calculate the costs.从 400.001 单位开始,您必须采用电表费率 (0,0000134928 €) 并计算成本。 1.300.000 * 0,0000134928 = ~17,54€. 1.300.000 * 0,0000134928 = ~17,54€。 Fortunately, the azure functions have only one rate.幸运的是,azure 函数只有一种速率。 If the rate changes eg after 5.000.000 units, then you also have to take this into account.如果费率发生变化,例如在 5.000.000 单位后发生变化,那么您也必须考虑到这一点。 If you have the whole costs, then you can filter on your date which is 20.-30.如果您有全部费用,那么您可以过滤您的日期,即 20.-30。 and you will get the result.你会得到结果。

That's the short explanation of the calculation.这是计算的简短说明。 I implemented this calculation in C# and published it as a NuGet package.我在 C# 中实现了这个计算并将其发布为 NuGet 包。 The source code is on github - probably it helps.源代码在 github 上 - 可能有帮助。 It also contains a sample console which you could use to export the data.它还包含一个示例控制台,您可以使用它来导出数据。

I have the same issue.我有同样的问题。 But unluckily, python SDK is too hard to use.但不幸的是,python SDK 太难用了。

Moreover, you cannot find an availabale sample or example on Google.此外,您无法在 Google 上找到可用的示例或示例。

So, I choose using restapi rather than python SDK.所以,我选择使用 restapi 而不是 python SDK。

With python code, you can do this firstly,使用python代码,您可以先执行此操作,

import requests
from azure.common.credentials import ServicePrincipalCredentials

and set headers pyload and url.并设置标题 pyload 和 url。

 headers = {
     "Content-Type": "application/json",
     "Authorization": <token> }

you can get token through credentials, which generated by client_id,secret,tenant.您可以通过由 client_id、secret、tenant 生成的凭据获取令牌。 credentials.token() will return the token you can use in headers. credentials.token() 将返回您可以在标头中使用的令牌。

you can find restapi in https://docs.microsoft.com/en-us/rest/api/ or use F12 in Chrome when you access the Azure dashboard.您可以在https://docs.microsoft.com/en-us/rest/api/ 中找到 restapi,或者在访问 Azure 仪表板时在 Chrome 中使用 F12。

There are the offical documents below for retrieving the billing data using Python SDK or REST API in Python.使用Python SDK 或Python 中的REST API 检索计费数据的官方文档如下。

  1. For using Python SDK, please see http://azure-sdk-for-python.readthedocs.io/en/latest/resourcemanagementcommerce.html .有关使用 Python SDK,请参阅http://azure-sdk-for-python.readthedocs.io/en/latest/resourcemanagementcommerce.html
  2. For using Billing REST API, please see https://docs.microsoft.com/en-us/rest/api/billing/ , and you can try to use Python package requests to get these data.使用Billing REST API,请参见https://docs.microsoft.com/en-us/rest/api/billing/ ,您可以尝试使用 Python 包requests来获取这些数据。

However, I think you may have know these above, the key is that you need to follow the tutorial Manage access to billing information for Azure using role-based access control to get the role permission via your account admin.但是,我认为您可能已经知道了上面的这些,关键是您需要按照教程Manage access to billing information for Azure using role-based access control来通过您的帐户管理员获取角色权限。

And then you may also need to register a client app to get the client id for Resource Management Authentication if you want to use Service Principal/ADAL (not AD User/Password) in Python SDK or use REST API to do the same authentication as Azure REST API Reference page said.然后,如果您想在 Python SDK 中使用服务主体/ADAL(而不是 AD 用户/密码)或使用 REST API 进行与Azure相同的身份验证,您可能还需要注册一个客户端应用程序以获取用于资源管理身份验证的客户端 ID REST API 参考页面说。 For the authentication topic, you can refer to the content map of Manage Apps to know more if you have been getting some trouble.对于认证主题,如果您遇到了一些麻烦,您可以参考管理应用程序的内容地图以了解更多信息。

Hope it helps.希望它有帮助。 Any concern, please feel free to let me know.有任何问题,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM