Başlangıç Kaynak Grafı sorgu örnekleri

Azure Kaynak Grafiği ile sorguları anlamanın il adımı, Sorgu Dili’nin temel bir anlayışıdır. Kusto Sorgu Dili (KQL) hakkında bilginiz yoksa, aradığınız kaynaklar için isteklerin nasıl oluşturabileceğinizi anlamak için KQL öğreticisini gözden geçirmeniz önerilir.

Bu makalede aşağıdaki başlangıç sorguları kullanılır:

Azure aboneliğiniz yoksa başlamadan önce ücretsiz bir hesap oluşturun.

Dil desteği

Azure CLI (bir uzantı yoluyla) ve Azure PowerShell (bir modül yoluyla), Azure Kaynak Grafiği’ni destekler. Aşağıdaki sorgulardan herhangi birini çalıştırmadan önce ortamınızın hazır olduğundan emin olun. Seçtiğiniz kabuk ortamını yükleme ve doğrulama adımları için, bkz. Azure CLI ve Azure PowerShell.

Azure kaynaklarını sayma

Bu sorgu, erişiminiz bulunan aboneliklerde var olan Azure kaynaklarının sayısını döndürür. Ayrıca, seçtiğiniz kabukta uygun Azure Kaynak Grafı bileşenlerinin yüklü ve çalışma düzeninde olduğunu doğrulamak için iyi bir sorgudur.

Resources
| summarize count()

Varsayılan olarak, Azure CLI tüm erişilebilir abonelikleri sorgular, ancak belirli abonelikleri sorgulamak için parametresini belirtebilirsiniz --subscriptions .

az graph query -q "Resources | summarize count()"

Bu örnekte abonelik kimliği için bir değişken kullanılır.

subid=$(az account show --query id --output tsv)
az graph query -q "Resources | summarize count()" --subscriptions $subid

Yönetim grubu ve kiracı kapsamlarına göre de sorgulayabilirsiniz. ve <tenantId> değerlerini değerlerinizle değiştirin<managementGroupId>.

az graph query -q "Resources | summarize count()" --management-groups '<managementGroupId>'
az graph query -q "Resources | summarize count()" --management-groups '<tenantId>'

Kiracı kimliği için bir değişken de kullanabilirsiniz.

tenantid=$(az account show --query tenantId --output tsv)
az graph query -q "Resources | summarize count()" --management-groups $tenantid

Key Vault kaynaklarını sayma

Bu sorgu, döndürülen kayıt sayısını saymak için yerine summarize kullanırcount. Sayıya yalnızca anahtar kasaları dahil edilir.

Resources
| where type =~ 'microsoft.keyvault/vaults'
| count
az graph query -q "Resources | where type =~ 'microsoft.keyvault/vaults' | count"

Ada göre sıralanmış kaynakları listeleme

Bu sorgu herhangi bir türdeki kaynakların yalnızca name, type ve location özelliklerini döndürür. order by kullanarak özellikleri name özelliğine göre artan (asc) düzende sıralar.

Resources
| project name, type, location
| order by name asc
az graph query -q "Resources | project name, type, location | order by name asc"

İsme göre sıralanmış tüm sanal makineleri azalan düzende göster

Yalnızca sanal makineleri (Microsoft.Compute/virtualMachines türündeki) listelemek için sonuçlarda type özelliğini eşleştirebiliriz. Önceki sorguya benzer şekilde desc, order by’yi azalan olması için değiştirir. Eşleme türündeki =~, Kaynak Grafiği’nin büyük/küçük harfe duyarlı olmadığını bildirir.

Resources
| project name, location, type
| where type =~ 'Microsoft.Compute/virtualMachines'
| order by name desc
az graph query -q "Resources | project name, location, type| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc"

Ada ve işletim sistemi türüne göre ilk beş sanal makineyi gösterme

Bu sorgu yalnızca ada göre sıralanmış beş eşleşen kaydı almak için kullanır top . Azure kaynağının türü Microsoft.Compute/virtualMachines’dir. project, Azure Kaynak Grafiği’ne hangi özelliklerin dahil edileceğini bildirir.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| project name, properties.storageProfile.osDisk.osType
| top 5 by name desc
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | project name, properties.storageProfile.osDisk.osType | top 5 by name desc"

Sanal makineleri işletim sistemi türüne göre sayma

Bir önceki sorguyu oluşturmada, hâlâ Microsoft.Compute/virtualMachines türündeki Azure kaynaklarına göre sınırlandırıyoruz, ancak geri gönderilen kayıtların sayısını sınırlandırmıyoruz. Bunu yerine, değerleri (bu örnekte properties.storageProfile.osDisk.osType) özelliğe göre gruplandırma ve toplamayı tanımlamak için summarize ve count() kullandık. Bu dizenin tam nesnede nasıl göründüğüne ilişkin bir örnek için, bkz. kaynakları keşfetme - sanal makine bulma.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| summarize count() by tostring(properties.storageProfile.osDisk.osType)
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)"

Aynı sorguyu yazmanın farklı bir yolu, bir özelliği kullanmak extend ve sorgu içinde kullanmak üzere geçici bir ad vermektir( bu örnekte işletim sistemi). os, önceki örnekte olduğu gibi ve count() tarafından summarize kullanılır.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| extend os = properties.storageProfile.osDisk.osType
| summarize count() by tostring(os)
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | extend os = properties.storageProfile.osDisk.osType | summarize count() by tostring(os)"

Not

=~, büyük/küçük harfe duyarlı olmayan eşleşmeye izin verirken, sorgudaki özelliklerin (örneğin, properties.storageProfile.osDisk.osType gibi) kullanımının, durumun doğru olmasını gerektirdiğini unutmayın. Özellik yanlış durumdaysa, null veya yanlış bir değer döndürülür ve gruplandırma veya özetleme yanlış olur.

Depolama içeren kaynakları göster

Bu örnek sorgu, eşleşecek türü açıkça tanımlamak yerine depolama sözcüğünü içeren contains herhangi bir Azure kaynağını bulur.

Resources
| where type contains 'storage' | distinct type
az graph query -q "Resources | where type contains 'storage' | distinct type"

Tüm Azure sanal ağ alt ağlarını listeleme

Bu sorgu, alt ağ adları ve adres ön ekleri dahil olmak üzere Azure sanal ağlarının (VNet) listesini döndürür.

Resources
| where type == 'microsoft.network/virtualnetworks'
| extend subnets = properties.subnets
| mv-expand subnets
| project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId
az graph query -q "Resources | where type == 'microsoft.network/virtualnetworks' | extend subnets = properties.subnets | mv-expand subnets | project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId"

Tüm genel IP adreslerini listele

Önceki sorguya benzer şekilde, publicIPAddresses sözcüğünü içeren bir tür olan her şeyi bulun. Bu sorgu, yalnızca properties.ipAddress, yalnızca properties.ipAddressisnotempty değerini döndürmek ve ilk 100'e kadar sonuçlara sonuç eklemek için limit bu deseni genişletir. Seçtiğiniz kabuğa bağlı olarak tırnak işaretinden kurtulmanız gerekebilir.

Resources
| where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress)
| project properties.ipAddress
| limit 100
az graph query -q "Resources | where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | project properties.ipAddress | limit 100"

Aboneliğe göre yapılandırılmış IP adreslerine sahip kaynakları sayma

Önceki örnek sorguyu kullanarak ve summarize ile count() ekleyerek, yapılandırılmış IP adreslerine sahip kaynakların aboneliğe göre listesini elde edebiliriz.

Resources
| where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress)
| summarize count () by subscriptionId
az graph query -q "Resources | where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | summarize count () by subscriptionId"

Belirli bir etiket değerine sahip kaynakları listeleme

Sonuçları, etiket gibi Azure kaynak türünden başka özelliklere göre de sınırlandırabiliriz. Bu örnekte, Azure kaynaklarını Dahili değerine sahip Ortam etiket adıyla filtreliyoruz.

Resources
| where tags.environment=~'internal'
| project name
az graph query -q "Resources | where tags.environment=~'internal' | project name"

Kaynağın sahip olduğu etiketleri ve değerlerini de sağlamak için project sözcüğüne tags özelliğini ekleyin.

Resources
| where tags.environment=~'internal'
| project name, tags
az graph query -q "Resources | where tags.environment=~'internal' | project name, tags"

Belirli bir değerine sahip tüm depolama hesaplarını listeleme

Önceki örneğin filtre işleviyle birleştirerek Azure kaynak türünü type özelliğine göre filtreleyin. Bu sorgu da aramayı belirli bir etiket adına ve değerine sahip olan belirli Azure kaynağı türleriyle sınırlar.

Resources
| where type =~ 'Microsoft.Storage/storageAccounts'
| where tags['tag with a space']=='Custom value'
az graph query -q "Resources | where type =~ 'Microsoft.Storage/storageAccounts' | where tags['tag with a space']=='Custom value'"

Not

Bu örnek, eşleştirme için =~ koşullu yerine == kullanır. == büyük küçük harfe duyarlı bir eşleşmedir.

Tüm etiketleri ve bunların değerlerini listeleme

Bu sorguda yönetim grupları, abonelikler ve kaynaklar üzerindeki etiketler ve bunların değerleri listelenir. Sorgu ilk olarak etiketlerin isnotempty()dahil edilen alanları yalnızca içindeki etiketleriprojectekleyerek ve mvexpandextend özellik paketinden eşleştirilmiş verileri alarak sınırladığı kaynaklarla sınırlar. Ardından ResourceContainers sonuçlarını Kaynaklar'dan elde edilen sonuçlarla birleştirmek için kullanır union ve etiketlerin getirildiği geniş kapsamlı bir kapsam sağlar. Son olarak, sonuçları eşleştirilmiş verilerle distinct sınırlar ve sistem tarafından gizlenen etiketleri dışlar.

ResourceContainers
| where isnotempty(tags)
| project tags
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| union (
    resources
    | where isnotempty(tags)
    | project tags
    | mvexpand tags
    | extend tagKey = tostring(bag_keys(tags)[0])
    | extend tagValue = tostring(tags[tagKey])
)
| distinct tagKey, tagValue
| where tagKey !startswith "hidden-"
az graph query -q "ResourceContainers | where isnotempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) | union (resources | where notempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) ) | distinct tagKey, tagValue | where tagKey !startswith "hidden-""

İlişkilendirilmemiş ağ güvenlik gruplarını göster

Bu sorgu, bir ağ arabirimi veya alt ağ ile ilişkilendirilmeyen Ağ Güvenlik Gruplarını (NSG) döndürür.

Resources
| where type =~ "microsoft.network/networksecuritygroups" and isnull(properties.networkInterfaces) and isnull(properties.subnets)
| project name, resourceGroup
| sort by name asc
az graph query -q "Resources | where type =~ 'microsoft.network/networksecuritygroups' and isnull(properties.networkInterfaces) and isnull(properties.subnets) | project name, resourceGroup | sort by name asc"

Önem derecelerine göre sıralanmış Azure İzleyici uyarılarını listeleme

Bu sorgu, önem derecesine alertsmanagementresources göre sıralanmış Azure İzleyici uyarılarını döndürmek için tablosunu kullanır.

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity)
| summarize AlertsCount = count() by Severity
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity) | summarize AlertsCount = count() by Severity"

Önem derecelerine ve uyarı durumuna göre sıralanmış Azure İzleyici uyarılarını listeleme

Bu sorgu, önem derecesine alertsmanagementresources ve uyarı durumuna göre sıralanmış Azure İzleyici uyarılarını döndürmek için tablosunu kullanır.

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity), AlertState = tostring(properties.essentials.alertState)
| summarize AlertsCount = count() by Severity, AlertState
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity), AlertState = tostring(properties.essentials.alertState) | summarize AlertsCount = count() by Severity, AlertState"

Önem derecelerine, izleme hizmetine ve hedef kaynak türüne göre sıralanmış Azure İzleyici uyarılarını listeleme

Bu sorgu önem derecesine alertsmanagementresources , izleme hizmetine ve hedef kaynak türüne göre sıralanmış Azure İzleyici uyarılarını döndürmek için tablosunu kullanır.

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity),
MonitorCondition = tostring(properties.essentials.monitorCondition),
ObjectState = tostring(properties.essentials.alertState),
MonitorService = tostring(properties.essentials.monitorService),
AlertRuleId = tostring(properties.essentials.alertRule),
SignalType = tostring(properties.essentials.signalType),
TargetResource = tostring(properties.essentials.targetResourceName),
TargetResourceType = tostring(properties.essentials.targetResourceName), id
| summarize AlertsCount = count() by Severity, MonitorService , TargetResourceType
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity), MonitorCondition = tostring(properties.essentials.monitorCondition), ObjectState = tostring(properties.essentials.alertState), MonitorService = tostring(properties.essentials.monitorService), AlertRuleId = tostring(properties.essentials.alertRule), SignalType = tostring(properties.essentials.signalType), TargetResource = tostring(properties.essentials.targetResourceName), TargetResourceType = tostring(properties.essentials.targetResourceName), id | summarize AlertsCount = count() by Severity, MonitorService , TargetResourceType"

Sonraki adımlar