練習 - 由電腦摘要可用空間統計資料

已完成

在這裡,您將使用 KQL 查詢從 Perf 資料表擷取和轉換資料,以分析機器記錄資料到 [記錄分析] 工作區的可用空間。

1. 設定目標

回想一下,您的 IT 小組注意到虛擬機器上可用空間不足的週期性問題。

若要分析 IT 環境中執行的機器可用空間使用量,您需要下列相關資訊:

  • 每部機器上可用的可用空間總計。
  • 每部機器上已使用的空間百分比。

2. 評估記錄

如前一個練習所見,Perf 資料表提供硬體元件、作業系統和應用程式效能的相關資訊。

我們注意到 Perf 資料表的 ObjectName 資料行會列出所監視之所有物件的名稱,而 CounterName 資料行會保存 Azure 監視器所收集之各種效能計數器的名稱。 我們也看到這兩個資料行都保存許多值,其中許多值出現多次。

讓我們在 Perf 資料表上執行查詢,來列出相異 ObjectName 值:

點擊以在 [記錄分析] 示範環境中執行查詢

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

此查詢的結果集包含資料表中目前的所有 ObjectName 值:

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

在我們的案例中,我們有興趣分析虛擬機器,因此我們想要查看的物件是 LogicalDiskLogical Disk (若要監視實體機器中的記憶體,您需查看 memory 物件)。 在 Linux 記錄中使用 Logical Disk 時,之所以有兩個類似的具名物件是因為 LogicalDisk 是 Windows 記錄中的物件名稱。

若要列出 Azure 監視器針對 LogicalDiskLogical Disk 物件所收集的計數器相異名稱,請執行:

點擊以在 [記錄分析] 示範環境中執行查詢

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

此查詢的結果集包含針對 LogicalDiskLogical 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.

提供已使用和可用空間相關資訊的效能計數器為 % Used Space% Free SpaceFree Megabytes。 我們有兩個類似的計數器, % Free Space% Used Space,分別從 Windows 和 Linux 記錄收集而來。

讓我們評估如何使用這項資料,以及哪些 KQL 作業可協助擷取和轉換資料:

資料行 描述 分析目標 相關的 KQL 作業
TimeGenerated 指出虛擬機器產生每個記錄的時機。 定義分析的時間範圍。 where TimeGenerated > ago(1d)
如需詳細資訊,請參閱 ago()where 運算子數值運算子
Computer 從中收集事件的電腦。 將 CPU 使用率與特定電腦產生關聯。 summarize... by Computer
如需詳細資訊,請參閱 where 運算子
ObjectName 保存資料表保存效能資料的所有物件名稱。 針對您的分析,您對 LogicalDiskLogical Disk 物件感興趣。 監視虛擬機器中的邏輯磁碟。 where ObjectName == "LogicalDisk" or ObjectName == "Logical Disk"
如需詳細資訊,請參閱 where 運算子== (等於) 運算子
CounterName 保存資料表中所有效能計數器的名稱。
  • 監視與可用空間相關的計數器。
  • 重新命名 % Used Space% Free Space (以平行方式,轉換相關的 CounterValue)
.
where CounterName == "Free Megabytes" or CounterName =="% Free Space" or CounterName == "% Used Space"
若要簡化結果並協助進一步分析:
  • 變更 % Used Space% Free Space (CounterName = iff(CounterName=="% Used Space", "% Free Space", CounterName))。
  • 變更 Free MegabytesOverallFreeSpaceInGB (CounterName= iff(CounterName=="Free Megabytes", "OverallFreeSpaceInGB", CounterName))
如需詳細資訊,請參閱 where 運算子== (等於) 運算子
InstanceName 列出受監視物件的受監視實例。 監視虛擬機器上的所有磁碟機。 InstanceName == "_Total"
如需詳細資訊,請參閱 where 運算子== (等於) 運算子
CounterValue 針對計數器收集的量值。 擷取 % Used Space% Free SpaceFree Megabytes 效能計數器的效能量值。
  • CounterValue = iff(CounterName=="% Used Space", 100-CounterValue, CounterValue)
  • CounterValue = iff(CounterName=="Free Megabytes", (CounterValue)*0.001, CounterValue)
如需詳細資訊,請參閱 where 運算子== (等於) 運算子

3. 撰寫查詢

  1. 擷取過去一天所產生的所有記錄,這些記錄會報告 % Used Space% Free Space 物件的 、 Free MegabytesLogicalDisk 效能計數器 Logical Disk

    點擊以在 [記錄分析] 示範環境中執行查詢

    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  
    

    此查詢的結果集可能會包含您用于收集與可用空間相關的效能計數器的每部機器的多個記錄。

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

  2. 針對每個虛擬機器報告的每個計數器收集的最後一個計數器值進行篩選:

    點擊以在 [記錄分析] 示範環境中執行查詢

    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
    

    針對每部機器的每個可用空間相關計數器,您現在已擁有最後回報的計數器值。

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

  3. 為了方便分析:

    1. % Used Space 計數器值從轉換為 % Free Space(從 100% 剪去 % Used Space 值) 並將 % Used Space 資料行的名稱變更為 % Free Space:

      點擊以在 [記錄分析] 示範環境中執行查詢

      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
      

      此查詢的結果集會以相同方式呈現 Windows 和 Linux 機器上可用空間的百分比,這可讓進一步分析更清楚且更容易。

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

    2. 轉換 Free Megabytes 為 GB (Free Megabytes 值 *0.001 = 可用 GB) 並重新標籤 Free MegabytesOverallFreeSpaceInGB:

      點擊以在 [記錄分析] 示範環境中執行查詢

      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
      

      您現在可以清楚瞭解每部機器上以 GB 為單位的總可用空間,以及電腦總記憶體的百分比。

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

挑戰: 將每部電腦的可用空間統計資料組合在一起

到目前為止,查詢的結果集包含每部電腦兩行: 一行顯示 GB 的整體可用空間,另一行則顯示可用空間的百分比。

您可以建立一個字典,將這兩個可用空間統計資料組合在一起,以供每部虛擬機器使用嗎?

提示:

解決方案:

  1. CounterName, CounterValue 機碼值組分組在一起:

    點擊以在 [記錄分析] 示範環境中執行查詢

    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
    

    CounterName, CounterValue 機碼值組分組在一起,可讓您在下一個步驟中建立每部電腦的可用空間統計資料字典。

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

  2. 建立屬性包 (字典),稱為 SpaceStats,針對每部機器收集的所有可用空間統計資料、依電腦摘要,以及篩選小於 50% 可用空間的電腦:

    點擊以在 [記錄分析] 示範環境中執行查詢

    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
    

    此查詢的結果集會依電腦摘要說明可用空間統計資料,這是可用空間分析的目標!

    查詢的最後一行會篩選少於 50% 可用空間的機器。 您可能想要更仔細地監視或分析,或重新設定它們以確保它們不會用盡空間。

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