Azure Resource GraphにてKQLを使用するとリソースのパラメーターを容易に取得することが出来ます。
まずは複数台のパラメーターをまとめて取る機会が多そうな、仮想マシン(以下、VM)のパラメーターを集める事が出来るか試してみました。
VMに付随するリソースのパラメータについて
VMのパラメーターとして欲しい情報は、Disk、ネットワーク設定、バックアップ設定(RecoveryServiceコンテナー情報)などになりますが、これらは別の情報として保持されています。
そのため、これらの情報を一度に取得する場合はテーブル結合を行います。
各情報のテーブルについて
以下の方法で、VMやVMに付随する情報を取得しています。
- VM情報
- Resources
| where type =~ "microsoft.compute/virtualmachines"
- Resources
- Disk情報取得
- Resources
| where type == "microsoft.compute/disks"
- Resources
- ネットワーク情報取得
- Resources
| where type contains "networkinterfaces"
- Resources
- RecoveryServiceコンテナー情報
- RecoveryServicesResources
| where type =~ ('Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems')
- RecoveryServicesResources
クエリ
必要な情報の取得・結合が出来るように作成したクエリがこちらとなります。
Resources
| where type =~ "microsoft.compute/virtualmachines"
| extend
MachineName = toupper(name),
JoinID = toupper(id),
SKUs = tostring(strcat(properties.storageProfile.imageReference.publisher," ",properties.storageProfile.imageReference.sku)),
ImagedOS = tostring(split(properties.storageProfile.imageReference.id,"/")[-1]),
VMSize = tostring(properties.hardwareProfile.vmSize),
UserName = tostring(properties.osProfile.adminUsername),
Password = "********",
VMGeneration = tostring(properties.extended.instanceView.hyperVGeneration),
Zone = tostring(zones),
ExtendedLocation = tostring(extendedLocation)
| extend BootDiagnostics = tostring(properties.diagnosticsProfile.bootDiagnostics.enabled)
| extend BootDiagnostics_Strage = iff(strlen(properties.diagnosticsProfile.bootDiagnostics.storageUri)==1,"",properties.diagnosticsProfile.bootDiagnostics.storageUri)
| extend OSImage = iff(strlen(SKUs)==1,ImagedOS,SKUs)
| extend Str_ID = tostring(identity.userAssignedIdentities)
| parse kind=regex Str_ID with '/userAssignedIdentities/' IDCheck '":{'
| extend UserManagedID = iff(strlen(identity.userAssignedIdentities) < 1,"Off",IDCheck),
SystemManagedID = iff(strlen(identity.principalId) < 1,"OFF",tostring(identity.principalId))
//拡張機能取得
| join kind=leftouter(
Resources
| where type == 'microsoft.compute/virtualmachines/extensions'
| extend
VMId = toupper(substring(id, 0, indexof(id, '/extensions'))),
ExtensionName = name
) on $left.JoinID == $right.VMId
//Disk情報取得
| join kind=leftouter(
Resources
| where type == "microsoft.compute/disks"
| extend
ManagedBy = toupper(managedBy),
DiskKind = tostring(sku.name),
DiskEncypion = tostring(properties.encryption.type),
DiskEncypionSet = "-"
) on $left.JoinID == $right.ManagedBy
//ネットワーク情報取得
| join kind=leftouter(
Resources
| where type contains "networkinterfaces"
| extend
VMname = tostring(toupper(split(properties.virtualMachine.id,"/")[-1])),
PrivateIPType = tostring(properties.ipConfigurations[0].properties.privateIPAllocationMethod),
PrivateIP = tostring(properties.ipConfigurations[0].properties.privateIPAddress),
SubnetName = tostring(split(properties.ipConfigurations[0].properties.subnet.id,"/")[-1]),
ipConfigurations = properties.ipConfigurations
| mvexpand ipConfigurations
| parse kind=regex ipConfigurations with '/virtualNetworks/' VirtualNetwork '/subnets/'
| extend InherVNet = iff(array_length(properties.dnsSettings.dnsServers)<1,"inherited from virtual networks",tostring(properties.dnsSettings.dnsServers)),
AccelerateNetWork = tostring(properties.enableAcceleratedNetworking)
) on $left.MachineName == $right.VMname
//RecoveryServiceコンテナー情報
| join kind=leftouter(
RecoveryServicesResources
| where type =~ ('Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems')
| extend RCname = tostring(split(properties.containerName,";")[1])
| extend PolicyName = case(type =~ 'Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems',properties.policyName, type =~ 'microsoft.dataprotection/backupVaults/backupInstances', properties.policyInfo.name, '--')
| parse kind=regex id with '/vaults/' RecoveryServicesContainer '/backupFabrics/'
| extend BackupEnabled = iff(isnull(RecoveryServicesContainer),"Off","On")
| extend friendlyName = toupper(properties.friendlyName)
) on $left.MachineName == $right.friendlyName
//指定列のみ出力
| summarize Extensions = make_list(ExtensionName) by
name,resourceGroup,subscriptionId,location,
OSImage,VMSize,UserName,Password,Zone,
DiskKind,DiskEncypion,
VirtualNetwork,SubnetName,PrivateIPType,PrivateIP,InherVNet,AccelerateNetWork,
BootDiagnostics,BootDiagnostics_Strage,SystemManagedID,UserManagedID,
BackupEnabled,RecoveryServicesContainer,PolicyName,
ExtendedLocation,VMGeneration
最後に
今回は仮想マシンのリソースについて、仮想マシンだけでなく、仮想マシン作成に付随して作成する頻度の高いリソースも併せてパラメーターを取得するクエリを作成してみました。
今後は他のリソースや今回のクエリのブラッシュアップもやっていけたらと思っています。