Esercizio - Sintetizza le statistiche sullo spazio disponibile in base al computer

Completato

Qui, recupererai e trasformerai i dati dalla tabella Perf, usando query KQL per analizzare lo spazio disponibile dei computer che registrano i dati sulla tua area di lavoro Log Analytics.

1. Imposta obiettivi

Tieni presente che il tuo team IT ha notato problemi ricorrenti correlati allo spazio libero insufficiente nelle macchine virtuali.

Per analizzare l'utilizzo dello spazio disponibile dei computer in esecuzione nel tuo ambiente IT, hai bisogno di informazioni su:

  • Spazio disponibile totale in ogni computer.
  • Percentuale di spazio usato in ogni computer.

2. Valuta i registri

Come illustrato nell'esercizio precedente, la tabella Perf fornisce informazioni sulle prestazioni dei componenti hardware, dei sistemi operativi e delle applicazioni.

Abbiamo notato che la colonna ObjectName della tabella Perf elenca i nomi di tutti gli oggetti monitorati e la colonna CounterName contiene i nomi dei vari contatori delle prestazioni raccolti da Monitoraggio di Azure. Si è anche visto che entrambe queste colonne contengono molti valori, molti dei quali compaiono più volte.

Esegui una query nella tabella Perf per elencare valori ObjectName distinti :

Fai clic per eseguire il query nell'ambiente demo di analisi dei log

Perf // The table you’re querying
| distinct ObjectName // Lists distinct ObjectName values

Il set di risultati di questa query include tutti i valori ObjectName attualmente presenti nella tabella:

Screenshot that shows the results of the Distinct Object Name query on the Perf table with the Logical Disk values highlighted.

Nel nostro scenario, siamo interessati all'analisi delle macchine virtuali, quindi gli oggetti da esaminare sono LogicalDisk e Logical Disk (per monitorare la memoria in un computer fisico, esaminerai l'oggetto memory ). Il motivo per cui esistono due oggetti denominati in modo simile è che LogicalDisk è il nome dell'oggetto nei record di Windows mentre Logical Disk viene usato nei record Linux.

Per elencare i nomi distinti dei contatori raccolti da Monitoraggio di Azure per gli oggetti LogicalDisk e Logical Disk, esegui:

Fai clic per eseguire il query nell'ambiente demo di analisi dei log

Perf // The table you’re querying  
| where ObjectName == "LogicalDisk" or // The object name used in Windows records
ObjectName == "Logical Disk" // The object name used in Linux records
| distinct CounterName // Lists distinct CounterName values

Il set di risultati di questa query include tutti i contatori delle prestazioni raccolti per gli oggettiLogicalDisk e Logical Disk :

Screenshot that shows the results of a query that lists the distinct names of the counters Azure Monitor collects for the LogicalDisk (written as one word) and Logical Disk (written as two words) objects.

I contatori delle prestazioni che forniscono informazioni sull'uso e sullo spazio disponibile sono % Used Space, % Free Space e Free Megabytes. Abbiamo disponibili due contatori simili, % Free Space e % Used Space raccolti rispettivamente dai record di Windows e Linux.

Valutiamo ora come possiamo usare questi dati e quali operazioni KQL consentono di estrarre e trasformare i dati:

Colonna Descrizione Obiettivo di analisi Operazioni KQL correlate
TimeGenerated Indica quando la macchina virtuale ha generato ogni log. Definisci l'ambito temporale dell'analisi. where TimeGenerated > ago(1d)
Per ulteriori informazioni, vedi ago(), dove operatori e Operatori numerici.
Computer Computer da cui è stato raccolto l'evento. Associa l'utilizzo della CPU a un computer specifico. summarize... by Computer
Per ulteriori informazioni, vedi Operatore di riepilogo.
ObjectName Contiene i nomi di tutti gli oggetti per cui la tabella contiene dati sulle prestazioni. Per il tuo analisi, ti interessano gli oggetti LogicalDisk e Logical Disk. Monitora i dischi logici nelle macchine virtuali. where ObjectName == "LogicalDisk" or ObjectName == "Logical Disk"
Per ulteriori informazioni, vedi dove l'operatore e == (uguali) l'operatore.
CounterName Contiene i nomi di tutti i contatori delle prestazioni nella tabella.
  • Monitora i contatori correlati allo spazio disponibile.
  • Rinomina % Used Spaceal % Free Space (in parallelo, convertire il relativo CounterValue)
.
where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space"
Per semplificare i risultati e facilitare un'ulteriore analisi:
  • Modifica % Used Space al % Free Space (CounterName = iff(CounterName=="% Used Space", "% Free Space", CounterName)).
  • Modifica Free Megabytes al OverallFreeSpaceInGB (CounterName= iff(CounterName=="Free Megabytes", "OverallFreeSpaceInGB", CounterName))
Per ulteriori informazioni, vedi dove l'operatore e == (uguali) l'operatore.
InstanceName Elenca le istanze monitorate dell'oggetto monitorato. Monitora tutte le unità nella macchina virtuale. InstanceName == "_Total"
Per ulteriori informazioni, vedi dove l'operatore e == (uguali) l'operatore.
CounterValue La misura raccolta per il contatore. Recupera le misurazioni delle prestazioni per i contatori delle prestazioni % Used Space, % Free Space e Free Megabytes.
  • CounterValue = iff(CounterName=="% Used Space", 100-CounterValue, CounterValue)
  • CounterValue = iff(CounterName=="Free Megabytes", (CounterValue)*0.001, CounterValue)
Per ulteriori informazioni, vedi dove l'operatore e == (uguali) l'operatore.

3. Scrivi la tua query

  1. Recuperare tutti i log generati nell'ultimo giorno che hanno segnalato i contatori delle prestazioni % Used Space, % Free Space e Free Megabytes per gli oggetti LogicalDisk e Logical Disk:

    Fai clic per eseguire la query nell'ambiente demo di analisi dei log

    Perf
    | where TimeGenerated > ago(1d)
    | where ObjectName == "LogicalDisk" or // The object name used in Windows records
    ObjectName == "Logical Disk" // The object name used in Linux records
    | where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space" // Filters for the performance counters Free Megabytes, % Free Space, and % Used Space performance counters
    | where InstanceName == "_Total"  // Retrieves data related to free space for all drives on a virtual machine  
    

    Il set di risultati di questa query include probabilmente più record per ogni computer da cui raccogli i contatori delle prestazioni correlati allo spazio libero.

    Screenshot that shows the results of a query for logs generated in the past day that report on virtual machine free space.

  2. Filtrare l'ultimo valore del contatore raccolto per ogni contatore segnalato da ogni macchina virtuale:

    Fai clic per eseguire la query nell'ambiente demo di analisi dei log

    Perf
    | where TimeGenerated > ago(1d)
    | where ObjectName == "LogicalDisk" or // The object name used in Windows records
    ObjectName == "Logical Disk" // The object name used in Linux records
    | where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space" // Filters for the performance counters Free Megabytes, % Free Space, and % Used Space performance counters
    | where InstanceName == "_Total"  // Retrieves data related to free space for all drives on a virtual disk  
    | summarize arg_max(TimeGenerated, CounterValue) by Computer, CounterName // Retrieves the last counter value collected for each counter for every virtual machine
    

    Ora hai l'ultimo valore del contatore segnalato per ogni contatore correlato allo spazio disponibile di ogni computer.

    Screenshot that shows the results of a query that filters for the last counter value collected for each counter every virtual machine.

  3. Per facilitare l'analisi:

    1. Converti il valore del contatore % Used Space in % Free Space (sottraendo il % Used Space valore dal 100%) e modificando il nome della colonna % Used Space in % Free Space:

      Fai clic per eseguire la query nell'ambiente demo di analisi dei log

      Perf
      | where TimeGenerated > ago(1d)
      | where ObjectName == "LogicalDisk" or // The object name used in Windows records
      ObjectName == "Logical Disk" // The object name used in Linux records
      | where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space" // Filters for the performance counters Free Megabytes, % Free Space, and % Used Space performance counters
      | where InstanceName == "_Total"  // Retrieves data related to free space for all drives on a virtual disk  
      | summarize arg_max(TimeGenerated, CounterValue) by Computer, CounterName // Retrieves the last counter value collected for each counter for every virtual machine
      | extend CounterValue = iff(CounterName=="% Used Space", 100-CounterValue, CounterValue) // Converts % Used Space to % Free Space
      | extend CounterName = iff(CounterName=="% Used Space", "% Free Space", CounterName) // Changes the column name from % Used Space to % Free Space
      

      Il set di risultati di questa query presenta la percentuale di spazio disponibile nei computer Windows e Linux nello stesso modo, in modo da rendere più chiara e semplice l'analisi.

      Screenshot that shows the results of a query that converts the Percentage Used Space counter value to Percentage Free Space.

    2. Converti Free Megabytes in Gigabyte (Free Megabytes valore * 0,001 = Gigabyte gratuiti) e etichettare nuovamente Free Megabytes in OverallFreeSpaceInGB:

      Fai clic per eseguire la query nell'ambiente demo di analisi dei log

      Perf
      | where TimeGenerated > ago(1d)
      | where ObjectName == "LogicalDisk" or // The object name used in Windows records
      ObjectName == "Logical Disk" // The object name used in Linux records
      | where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space" // Filters for the performance counters Free Megabytes, % Free Space, and % Used Space performance counters
      | where InstanceName == "_Total"  // Retrieves data related to free space for all drives on a virtual disk  
      | summarize arg_max(TimeGenerated, CounterValue) by Computer, CounterName // Retrieves the last counter value collected for each counter for every virtual machine
      | extend CounterValue = iff(CounterName=="% Used Space", 100-CounterValue, CounterValue) // Converts % Used Space to % Free Space
      | extend CounterName = iff(CounterName=="% Used Space", "% Free Space", CounterName) // Changes the column name from % Used Space to % Free Space
      | extend CounterValue = iff(CounterName=="Free Megabytes", (CounterValue)*0.001, CounterValue) // Converts megabytes to gigabytes
      | extend CounterName= iff(CounterName=="Free Megabytes", "OverallFreeSpaceInGB", CounterName) // Changes the column name fromFree Megabytes to OverallFreeSpaceInGB
      

      Adesso puoi ottenere un'immagine chiara dello spazio disponibile totale in ogni computer in gigabyte e anche il percentuale della memoria totale del computer.

      Screenshot that shows the results of a query that converts the Free Megabytes column to Overall Free Space In Gigabytes.

Sfida: raggruppare le statistiche sullo spazio libero per ogni computer

Il set di risultati della nostra query finora include due righe per ogni computer: una riga mostra lo spazio disponibile complessivo in Gigabyte e l'altra mostra la percentuale di spazio disponibile.

Puoi creare un dizionario che raggruppa queste due statistiche di spazio libero per ogni macchina virtuale?

Suggerimento:

  • Usa la funzione bag_pack() per creare coppie chiave-valore per ognuno dei due contatori delle prestazioni.
  • Usa la funzione di aggregazione make_bag() per aggregare entrambi i valori chiave-valore per ogni computer.

Soluzione:

  1. Raggruppa coppie CounterName, CounterValue chiave-valore:

    Fai clic per eseguire la query nell'ambiente demo di analisi dei log

    Perf
    | where TimeGenerated > ago(1d)
    | where ObjectName == "LogicalDisk" or // The object name used in Windows records
    ObjectName == "Logical Disk" // The object name used in Linux records
    | where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space" // Filters for the performance counters Free Megabytes, % Free Space, and % Used Space performance counters
    | where InstanceName == "_Total"  // Retrieves data related to free space for all drives on a virtual disk  
    | summarize arg_max(TimeGenerated, CounterValue) by Computer, CounterName // Retrieves the last counter value collected for each counter for every virtual machine
    | extend CounterValue = iff(CounterName=="% Used Space", 100-CounterValue, CounterValue) // Converts % Used Space to % Free Space
    | extend CounterName = iff(CounterName=="% Used Space", "% Free Space", CounterName) // Changes the column name from % Used Space to % Free Space
    | extend CounterValue = iff(CounterName=="Free Megabytes", (CounterValue)*0.001, CounterValue) // Converts megabytes to gigabytes
    | extend CounterName= iff(CounterName=="Free Megabytes", "OverallFreeSpaceInGB", CounterName) // Changes the column name fromFree Megabytes to OverallFreeSpaceInGB
    | extend packed = pack(CounterName, CounterValue) // Groups together CounterName-CounterValue key-value pairs
    

    Raggruppare CounterName, CounterValue coppie chiave-valore ti consente di creare un dizionario di statistiche di spazio libero per ogni computer nel passaggio successivo.

    Screenshot that shows the results of a query that groups together Counter Name and Counter Value key-value pairs.

  2. Crea un contenitore di proprietà (dizionario), denominato SpaceStats, di tutte le statistiche di spazio libero raccolte per ogni computer, riepilogare in base al computer e filtrare i computer con meno del 50% di spazio libero:

    Fai clic per eseguire la query nell'ambiente demo di analisi dei log

    Perf
    | where TimeGenerated > ago(1d)
    | where ObjectName == "LogicalDisk" or // The object name used in Windows records
    ObjectName == "Logical Disk" // The object name used in Linux records
    | where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space" // Filters for the performance counters Free Megabytes, % Free Space, and % Used Space performance counters
    | where InstanceName == "_Total"  // Retrieves data related to free space for all drives on a virtual disk  
    | summarize arg_max(TimeGenerated, CounterValue) by Computer, CounterName // Retrieves the last counter value collected for each counter for every virtual machine
    | extend CounterValue = iff(CounterName=="% Used Space", 100-CounterValue, CounterValue) // Converts % Used Space to % Free Space
    | extend CounterName = iff(CounterName=="% Used Space", "% Free Space", CounterName) // Changes the column name from % Used Space to % Free Space
    | extend CounterValue = iff(CounterName=="Free Megabytes", (CounterValue)*0.001, CounterValue) // Converts megabytes to gigabytes
    | extend CounterName= iff(CounterName=="Free Megabytes", "OverallFreeSpaceInGB", CounterName) // Changes the column name fromFree Megabytes to OverallFreeSpaceInGB
    | extend packed = pack(CounterName, CounterValue) // Groups together CounterName-CounterValue key-value pairs
    | summarize SpaceStats = make_bag(packed) by Computer // Summarizes free space statstics by computer
    | where SpaceStats.["% Free Space"]<= 50
    

    Il set di risultati di questa query riepiloga le statistiche sullo spazio libero in base al computer, che è stato il tuo obiettivo dell'analisi dello spazio libero!

    L'ultima riga della query filtra i computer con meno del 50% di spazio disponibile. Potresti voler monitorare o analizzare più attentamente o riconfigurarli per assicurarti di non esaurire lo spazio.

    Screenshot that shows the results of a query that summarizes free space statistics by machine.