Ottenere le modifiche delle risorse

Le risorse cambiano durante il corso dell'uso quotidiano, la riconfigurazione e persino la ridistribuzione. La maggior parte delle modifiche è prevista da progettazione, ma non tutte. È possibile:

  • Scoprire quando sono state rilevate modifiche a una proprietà di Azure Resource Manager.
  • Visualizzare i dettagli delle modifiche alle proprietà.
  • Eseguire query sulle modifiche su larga scala tra sottoscrizioni, gruppi di gestione o tenant.

In questo articolo, imparerai a:

  • Aspetto del json del payload.
  • Come eseguire query sulle modifiche delle risorse tramite Resource Graph usando l'interfaccia della riga di comando, PowerShell o il portale di Azure.
  • Esempi di query e procedure consigliate per l'esecuzione di query sulle modifiche delle risorse.

Prerequisiti

  • Per abilitare Azure PowerShell per eseguire query su Azure Resource Graph, aggiungere il modulo.
  • Per abilitare l'interfaccia della riga di comando di Azure per eseguire query su Azure Resource Graph, aggiungere l'estensione .

Informazioni sulle proprietà degli eventi di modifica

Quando una risorsa viene creata, aggiornata o eliminata, viene creata una nuova risorsa di modifica (Microsoft.Resources/changes) per estendere la risorsa modificata e rappresentare le proprietà modificate. I record delle modifiche devono essere disponibili in meno di cinque minuti. Il payload JSON di esempio seguente illustra le proprietà delle risorse di modifica:

{
  "targetResourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/microsoft.compute/virtualmachines/myVM",
  "targetResourceType": "microsoft.compute/virtualmachines",
  "changeType": "Update",
  "changeAttributes": {
    "previousResourceSnapshotId": "08584889383111245807_37592049-3996-ece7-c583-3008aef9e0e1_4043682982_1712668574",
    "newResourceSnapshotId": "08584889377081305807_38788020-eeee-ffff-028f-6121bdac9cfe_4213468768_1712669177",
    "correlationId": "04ff69b3-e162-4583-9cd7-1a14a1ec2c61",
    "changedByType": "User",
    "changesCount": 2,
    "clientType": "ARM Template",
    "changedBy": "john@contoso.com",
    "operation": "microsoft.compute/virtualmachines/write",
    "timestamp": "2024-04-09T13:26:17.347+00:00"
  },
  "changes": {
    "properties.provisioningState": {
      "newValue": "Succeeded",
      "previousValue": "Updating",
      "changeCategory": "System",
      "propertyChangeType": "Update",
      "isTruncated": "true"
    },
    "tags.key1": {
      "newValue": "NewTagValue",
      "previousValue": "null",
      "changeCategory": "User",
      "propertyChangeType": "Insert"
    }
  }
}

Vedere la guida di riferimento completa per modificare le proprietà delle risorse.

Eseguire una query

Provare una query resource graph basata su tenant della resourcechanges tabella. La query restituisce le prime cinque modifiche più recenti delle risorse di Azure con l'ora di modifica, il tipo di modifica, l'ID risorsa di destinazione, il tipo di risorsa di destinazione e i dettagli delle modifiche di ogni record di modifica.

# Login first with az login if not using Cloud Shell
 
# Run Azure Resource Graph query
az graph query -q 'resourcechanges | project properties.changeAttributes.timestamp, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'

È possibile aggiornare questa query per specificare un nome di colonna più descrittivo per la proprietà timestamp .

# Run Azure Resource Graph query with 'extend'
az graph query -q 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'

Per limitare i risultati delle query alle modifiche più recenti, aggiornare la query alla order by proprietà changeTime definita dall'utente.

# Run Azure Resource Graph query with 'order by'
az graph query -q 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | order by changeTime desc | limit 5'

È anche possibile eseguire query in base al gruppo di gestione o alla sottoscrizione rispettivamente con i -ManagementGroup parametri o -Subscription .

Nota

Se la query non restituisce risultati da una sottoscrizione a cui si ha già accesso, per impostazione predefinita il Search-AzGraph cmdlet di PowerShell corrisponde alle sottoscrizioni nel contesto predefinito.

Resource Graph Explorer offre anche un'interfaccia pulita per convertire i risultati di alcune query in un grafico che può essere aggiunto a un dashboard di Azure.

Eseguire query delle modifiche alle risorse

Con Resource Graph è possibile eseguire una query sulle resourcechangestabelle , resourcecontainerchangeso healthresourcechanges per filtrare o ordinare in base a una delle proprietà delle risorse di modifica. Gli esempi seguenti eseguono query sulla resourcechanges tabella, ma possono essere applicati anche alla resourcecontainerchanges tabella o healthresourcechanges .

Nota

Altre informazioni sui healthresourcechanges dati sono disponibili nella documentazione di Project Flash.

Esempi

Prima di eseguire query e analizzare le modifiche nelle risorse, esaminare le procedure consigliate seguenti.

  • Eseguire una query per gli eventi di modifica durante un intervallo di tempo specifico e valutare i dettagli della modifica.
    • Questa query funziona meglio durante la gestione degli eventi imprevisti per comprendere le modifiche potenzialmente correlate.
  • Mantenere un database di gestione della configurazione aggiornato (CMDB).
    • Anziché aggiornare tutte le risorse e i relativi set di proprietà completi in base a una frequenza pianificata, si riceveranno solo le modifiche.
  • Comprendere quali altre proprietà potrebbero essere state modificate quando una risorsa cambia "stato di conformità".
    • La valutazione di queste proprietà aggiuntive può fornire informazioni dettagliate su altre proprietà che potrebbero essere necessarie per essere gestite tramite una definizione di Criteri di Azure.
  • L'ordine dei comandi di query è importante. Negli esempi seguenti l'oggetto order by deve precedere il limit comando .
    • Il order by comando ordina i risultati della query in base all'ora di modifica.
    • Il limit comando limita quindi i risultati ordinati per assicurarsi di ottenere i cinque risultati più recenti.

Tutte le modifiche apportate all'ultimo periodo di 24 ore

resourcechanges 
| extend changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId),
changeType = tostring(properties.changeType), correlationId = properties.changeAttributes.correlationId, 
changedProperties = properties.changes, changeCount = properties.changeAttributes.changesCount
| where changeTime > ago(1d)
| order by changeTime desc
| project changeTime, targetResourceId, changeType, correlationId, changeCount, changedProperties

Risorse eliminate in un gruppo di risorse specifico

resourcechanges
| where resourceGroup == "myResourceGroup"
| extend changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId),
changeType = tostring(properties.changeType), correlationId = properties.changeAttributes.correlationId
| where changeType == "Delete"
| order by changeTime desc
| project changeTime, resourceGroup, targetResourceId, changeType, correlationId

Modifiche a un valore di proprietà specifico

resourcechanges
| extend provisioningStateChange = properties.changes["properties.provisioningState"], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType)
| where isnotempty(provisioningStateChange)and provisioningStateChange.newValue == "Succeeded"
| order by changeTime desc
| project changeTime, targetResourceId, changeType, provisioningStateChange.previousValue, provisioningStateChange.newValue

Ultime modifiche alle risorse create negli ultimi sette giorni

resourcechanges
| extend targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType), changeTime = todatetime(properties.changeAttributes.timestamp)
| where changeTime > ago(7d) and changeType == "Create"
| project  targetResourceId, changeType, changeTime
| join ( Resources | extend targetResourceId=id) on targetResourceId
| order by changeTime desc
| project changeTime, changeType, id, resourceGroup, type, properties

Modifiche alle dimensioni della macchina virtuale

resourcechanges
|extend vmSize = properties.changes["properties.hardwareProfile.vmSize"], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType) 
| where isnotempty(vmSize) 
| order by changeTime desc 
| project changeTime, targetResourceId, changeType, properties.changes, previousSize = vmSize.previousValue, newSize = vmSize.newValue

Numero di modifiche in base al tipo di modifica e al nome della sottoscrizione

resourcechanges  
|extend changeType = tostring(properties.changeType), changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceType=tostring(properties.targetResourceType)  
| summarize count() by changeType, subscriptionId 
| join (resourcecontainers | where type=='microsoft.resources/subscriptions' | project SubscriptionName=name, subscriptionId) on subscriptionId 
| project-away subscriptionId, subscriptionId1
| order by count_ desc  

Modifiche più recenti delle risorse per le risorse create con un determinato tag

resourcechanges 
|extend targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType), createTime = todatetime(properties.changeAttributes.timestamp) 
| where createTime > ago(7d) and changeType == "Create" or changeType == "Update" or changeType == "Delete"
| project  targetResourceId, changeType, createTime 
| join ( resources | extend targetResourceId=id) on targetResourceId
| where tags ['Environment'] =~ 'prod' 
| order by createTime desc 
| project createTime, id, resourceGroup, type

Passaggi successivi