如何查看Azure中所有Log Analytics工作区的每日数据用量(GB)?
多Log Analytics工作区每日数据用量跟踪实现方法
一、Azure门户直接查看
- 登录Azure门户,搜索成本管理 + 计费
- 进入后选择成本分析,在顶部范围选择框里选中目标订阅
- 在页面右侧的分组依据选项中,选择资源类型,然后筛选出
Log Analytics Workspaces - 调整聚合选项为使用量,将单位切换为GB,时间范围设置为每日,就能看到所有工作区的每日数据引入用量汇总
- 点击页面顶部的下载按钮,可将数据导出为CSV或Excel格式保存
二、Azure CLI脚本批量获取
使用Azure Consumption命令批量拉取数据,示例脚本如下:
# 替换为你的目标订阅ID SUBSCRIPTION_ID="your-subscription-id" az account set --subscription $SUBSCRIPTION_ID # 获取过去7天的每日用量数据,仅筛选Log Analytics工作区 az consumption usage list \ --subscription $SUBSCRIPTION_ID \ --start-date "$(date -d '-7 days' +%Y-%m-%d)" \ --end-date "$(date +%Y-%m-%d)" \ --filter "properties/resourceType eq 'Microsoft.OperationalInsights/workspaces'" \ --query "[].{工作区名称:properties/resourceName, 日期:properties/usageStart, 用量(GB):properties/quantity}" \ --output table
- 调整
--start-date和--end-date的日期值,可自定义查询时间范围 - 若需要导出为CSV格式,将
--output table替换为--output csv即可
三、Azure REST API调用获取
通过Consumption Usage Details API查询数据,示例curl请求如下:
# 获取Azure访问令牌 TOKEN=$(az account get-access-token --resource https://management.azure.com --query accessToken -o tsv) # 发送API请求,替换订阅ID和日期范围 curl -X GET "https://management.azure.com/subscriptions/your-subscription-id/providers/Microsoft.Consumption/usageDetails?api-version=2023-03-01&startDate=2024-05-01&endDate=2024-05-08&\$filter=properties/resourceType eq 'Microsoft.OperationalInsights/workspaces'" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json"
返回的JSON响应中,properties/quantity为用量数值,properties/usageStart对应日期,properties/resourceName是工作区名称,可自行解析这些字段得到每日用量数据。
内容的提问来源于stack exchange,提问作者PrayingMantis
相关产品推荐
相关产品推荐

