从 table 存储(性能计数器等)中删除旧的 Windows Azure 诊断数据

Delete old Windows Azure Diagnostics data from table storage (performance counters, etc.)

我在 Azure 上有几个 Windows VM 运行,它们被配置为收集性能计数器和事件日志。

所有这些都在 Azure 门户内的 VM 资源上的 "Diagnostic settings..." 中配置。有一个 Windows Azure 诊断代理在 VM 上收集此数据并将其存储到存储帐户(在 Table 存储内)。

所有这些收集的数据(性能计数器、指标、日志等)都没有任何保留策略,而且似乎没有任何设置方法.所以它只会永远累积在存储帐户的 table 存储中。

这就是我的问题所在——现在这些 table 中的数据太多了(在我的情况下是几 TB),而且仅仅为了保留这些数据就需要花费很多钱。而且它只会随着时间的推移而不断增加。

相关存储帐户table是table如:

是否有某种方法如何删除这些 table 中的旧数据,这样它就不会破坏任何东西?或者更好的是,是否有一些配置保留策略的方法或设置它以便它不会永远累积?

Is there some way how to delete old data in these tables so it wouldn't break anything?

您需要手动执行此操作。这样做的方式是,您将首先查询需要删除的数据,然后在获得数据后将其删除。存储在这些 table 中的实体的 PartitionKey 属性实际上表示一个 date/time 值(在刻度前加上零以使其成为一个等长的字符串)所以你需要从和到 date/time 值,将它们转换为刻度,使其成为 19 个字符长的字符串(通过在前面加上适当数量的零)并查询数据。在客户端获取数据后,您会将删除请求发送回 table 存储。

要加快整个过程,您可以做以下几件事:

  • 当您查询数据时,使用查询投影到 return 只有 PartitionKeyRowKey 属性,因为只需要删除这两个属性。
  • 删除可以使用实体批量交易。这可以大大加快删除操作。
  • 为了加快删除速度,您可以在与存储帐户相同的区域启动 VM。这样您就不用支付数据出口费用。

前段时间我写了一篇博客 post,您可能会觉得有帮助:https://gauravmantri.com/2012/02/17/effective-way-of-fetching-diagnostics-data-from-windows-azure-diagnostics-table-hint-use-partitionkey/

Or even better, is there some way to configure retention policy or set it up so that it doesn't keep accumulating forever?

不幸的是,至少到今天为止还没有。有保留设置,但仅适用于 blob。

我在追踪订阅中哪些费用最高时遇到了这个问题。

一个有用的工具是 Azure Storage Explorer。您可以浏览到 table,检查其内容,使用 Table 统计按钮统计 table 行,multi-select 并删除行。

对于一个自 2016 年以来 运行 的小型虚拟机,我发现 WADMetrics table 似乎每 10 天滚动一次,但其他的则不然。样本 WADMetrics table 包含 5724 个条目。 WASWindowsEventLogsTable 包含 10,022 个条目。当它达到 500 万个条目时,我取消了 WADPerformanceCountersTable 计数。存储统计数据比 VM 的 VHD 成本更高。

This article summarizes useful information about PowerShell commands for manipulating tables. Unfortunately, the Azure Cloud Shell doesn't yet support commands for working inside a table e.g. Get-AzTableRow (see this report)。如果您在本地设置最新的 Az PowerShell 命令,我认为这会起作用。然后你可以 select 使用过滤器并使用 Remove-AzTableRow 删除一些行。在我的例子中,机器已经退役,所以我只需要一种方法来删除大量 table,而不必在仪表板中单击每个。以下是一些示例命令:

$location = "uswest"
$resourceGroup = "myRG"
$storageAccountName = "myData"
$storageAccount = get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
# List all tables in storage account
Get-AzStorageTable -Context $ctx
# Count the WADMetrics tables
(Get-AzStorageTable -Context $ctx -Name "WADMetrics*").count
# Count the WADMetrics tables with "2018" in their name
(Get-AzStorageTable -Context $ctx -Name "WADMetrics*2018*").count
# Remove all WADMetrics tables with "2018" in their name without confirmation, then re-count 
# Only Get- supports wilcards, so pipe to Remove-AzStorageTable command
Get-AzStorageTable -Context $ctx -Name "WADMetrics*2018*" | Remove-AzStorageTable -Force
(Get-AzStorageTable -Context $ctx -Name "WADMetrics*2018*").count
# Remove the big tables. Confirmation takes a long time, so suppress it.
Remove-AzStorageTable -Context $ctx -Name "WADWindowsEventLogsTable" -Force
Remove-AzStorageTable -Context $ctx -Name "WADPerformanceCountersTable" -Force

# The following do NOT work in Azure Cloud Shell as of 07/16/2019.  See 
# https://github.com/MicrosoftDocs/azure-docs/issues/28608

# Count the rows in WADWindowsEventLogsTable
$tableName = "WADWindowsEventLogsTable"
$cloudTable = (Get-AzStorageTable -Context $ctx -Name $tableName).CloudTable
$cloudTableResults = Get-AzTableRow -table $cloudTable -columnName "RowKey"
$cloudTableResults.count

另一个解决方案是编写一个小的 C# 程序来擦除 Windows Azure 诊断 (WAD) 数据。

下面的文章为您提供了一个或多或少的开箱即用的解决方案,用于清除 WADMetrics* 表和 WADDiagnosticInfrastructureLogsTableWADPerformanceCountersTable 和 [ 中包含的行的方法=13=]

使用DeleteOldTables()DeleteOldData()这两种方法很容易编写一个可以每月执行的小程序来清理WAD数据。请注意,该代码使用 WindowsAzure.Storage NuGet 包,因此需要在您的项目中安装它。

https://mysharepointlearnings.wordpress.com/2019/08/20/managing-azure-vm-diagnostics-data-in-table-storage/