📈

Azure リソースのメトリックを REST API で取得

に公開

はじめに

Azure リソースのメトリックはポータルから確認できますが、端数まで確認したかったため、REST API で取得してみました。

PowerShell スクリプト

# パラメータ
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$resourceGroupName = "myrg"
$resourceProviderNamespace = "Microsoft.Network"
$resourceType = "loadBalancers"
$resourceName = "myloadbalancer"

# Azure PowerSHell ログイン
Connect-AzAccount -Tenant $tenantId

# メトリック定義の取得
$uri = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/$resourceProviderNamespace/$resourceType/$resourceName/providers/microsoft.insights/metricDefinitions?api-version=2018-01-01"
Invoke-AzRestMethod -Path $uri -Method GET

# 取得したいメトリックの定義
$metric = "ByteCount"
$startTime = "2025-03-17T03:00:00Z"
$endTime = "2025-03-17T03:30:00Z"
$interval = "PT1M"
$aggregation = "Total"

# メトリックの取得
$uri = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/$resourceProviderNamespace/$resourceType/$resourceName/providers/microsoft.insights/metrics?metricnames=$metric&timespan=$startTime/$endTIme&interval=$interval&aggregation=$aggregation&api-version=2019-07-01"
Invoke-AzRestMethod -Path $uri -Method GET

実行結果例

メトリック定義
{
	"value": [
		{
			"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer/providers/microsoft.insights/metricdefinitions/VipAvailability",
			"resourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer",
			"namespace": "Microsoft.Network/loadBalancers",
			"name": {
				"value": "VipAvailability",
				"localizedValue": "Data Path Availability"
			},
			"displayDescription": "Average Load Balancer data path availability per time duration",
			"isDimensionRequired": false,
			"unit": "Count",
			"primaryAggregationType": "Average",
			"supportedAggregationTypes": [
				"None",
				"Average",
				"Minimum",
				"Maximum",
				"Total",
				"Count"
			],
			"metricAvailabilities": [
				{
					"timeGrain": "PT1M",
					"retention": "P93D"
				},
				{
					"timeGrain": "PT5M",
					"retention": "P93D"
				},
				... (省略) ... 
				{
					"timeGrain": "P1D",
					"retention": "P93D"
				}
			],
			"dimensions": [
				{
					"value": "FrontendIPAddress",
					"localizedValue": "Frontend IP Address"
				},
				{
					"value": "FrontendPort",
					"localizedValue": "Frontend Port"
				}
			]
		},
		{
			"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer/providers/microsoft.insights/metricdefinitions/VipAvailability",
			"resourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer",
			"namespace": "Microsoft.Network/loadBalancers",
			"name": {
				"value": "DipAvailability",
				"localizedValue": "Health Probe Status"
			},
			"displayDescription": "Average Load Balancer health probe status per time duration",
			"isDimensionRequired": false,
			"unit": "Count",
			"primaryAggregationType": "Average",
			... (省略) ...
		},
		{
			"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer/providers/microsoft.insights/metricdefinitions/VipAvailability",
			"resourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer",
			"namespace": "Microsoft.Network/loadBalancers",
			"name": {
				"value": "UsedSnatPorts",
				"localizedValue": "Used SNAT Ports"
			},
			"displayDescription": "Total number of SNAT ports used within time period",
			... (省略) ... 
		}
	]
}
メトリック取得結果
{
	"cost": 29,
	"timespan": "2025-03-17T03:00:00Z/2025-03-17T03:30:00Z",
	"interval": "PT1M",
	"value": [
		{
			"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Network/loadBalancers/myloadbalancer/providers/Microsoft.Insights/metrics/ByteCount",
			"type": "Microsoft.Insights/metrics",
			"name": {
				"value": "ByteCount",
				"localizedValue": "Byte Count"
			},
			"displayDescription": "Total number of Bytes transmitted within time period",
			"unit": "Bytes",
			"timeseries": [
				{
					"metadatavalues": [],
					"data": [
						{
							"timeStamp": "2025-03-17T03:00:00Z",
							"total": 60898
						},
						{
							"timeStamp": "2025-03-17T03:01:00Z",
							"total": 529517
						},
						... (省略) ... 
						{
							"timeStamp": "2025-03-17T03:28:00Z",
							"total": 60794
						},
						{
							"timeStamp": "2025-03-17T03:29:00Z",
							"total": 60795
						}
					]
				}
			],
			"errorCode": "Success"
		}
	],
	"namespace": "Microsoft.Network/loadBalancers",
	"resourceregion": "centralus"
}

参考 URL

https://learn.microsoft.com/ja-jp/azure/azure-monitor/essentials/rest-api-walkthrough?tabs=rest%2Cportal

Microsoft (有志)

Discussion