你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Azure Monitor 中的数据收集规则 (DCR) 示例

本文包括 Azure Monitor 中常见数据收集方案的示例 数据收集规则(DCR) 定义。 你可以根据需要为自己的环境修改这些 DCR 定义,并参考创建或编辑数据收集规则中的指南创建 DCR。 你也可以结合这些示例中的基本策略,为其他场景创建 DCR。

你需要了解 Azure Monitor 数据收集规则的结构中所述的 DCR 结构,才能充分运用这些示例。 部分内容也许可以使用 Azure 门户进行配置,而无需详细了解 DCR 结构。 如果希望使用 Azure 门户之外的方法(例如 ARM、CLI 和 PowerShell)来管理 DCRs,可以将这些示例作为起点。 可能需要使用这些方法来编辑现有 DCR 来实现高级功能,如 转换

每个示例都侧重于特定的数据源,尽管你可以将不同类型的多个数据源组合到单个 DCR 中。 为每个数据源添加数据流,以便将数据发送到相应的目标位置。 在单个 DCR 中组合多个数据源或为每个数据源创建单独的 DCR 之间没有功能差异。 具体方式的选择,取决于你对管理和监视数据收集的要求。

注释

本文中所示的示例提供了创建 DCR 所需的源 JSON。 创建后,DCR 将具有“Azure Monitor 中数据收集规则结构”中所述的其他属性。

收集 VM 客户端数据

以下示例显示了使用 Azure Monitor 代理从虚拟机收集不同类型的数据的 DCR 定义。 可以使用 Azure 门户创建这些 DCR,如 使用 Azure Monitor 从 VM 客户端收集数据中所述。

Windows 事件

适用于 Windows 事件的 DCR 使用 windowsEventLogs 数据源和 Microsoft-Event 传入流。 此流采用已知结构,因此无需在 dataSources 部分中定义。 在 xPathQueries 属性中指定要收集的事件。 如需进一步了解如何使用 XPath 筛选要收集的特定数据,请参阅使用 Azure Monitor 代理收集 Windows 事件。 若要开始,可以参考该文章中的指南通过 Azure 门户创建 DCR,然后参考 DCR 定义中的指南检查 JSON。

可以为 dataFlows 属性添加转换来处理计算列并进一步筛选数据,但应尽可能在代理处使用 XPaths 来筛选数据,以提高效率并避免产生可能的数据引入费用。

以下示例 DCR 会执行以下操作:

  • 收集 Windows 应用程序和系统中错误级别为“警告”、“错误”或“严重”的事件。
  • 将数据发送到工作区中的“事件”表。
  • 该程序使用了一种简单的source转换,这种转换不会更改传入的数据。
{
    "___location": "eastus",
    "properties": {
      "dataSources": {
        "windowsEventLogs": [
          {
            "name": "eventLogsDataSource",
            "streams": [
              "Microsoft-Event"
            ],
            "xPathQueries": [
              "System!*[System[(Level = 1 or Level = 2 or Level = 3)]]",
              "Application!*[System[(Level = 1 or Level = 2 or Level = 3)]]"
            ]
          }
        ]
    },
      "destinations": {
        "logAnalytics": [
          {
            "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
            "name": "centralWorkspace"
          }
        ]
      },
      "dataFlows": [
        {
          "streams": [
            "Microsoft-Event"
          ],
          "destinations": [
            "centralWorkspace"
          ],
            "transformKql": "source",
            "outputStream": "Microsoft-Event"
        }
      ]
    }
  }

Syslog 事件

适用于 Syslog 事件的 DCR 使用 syslog 数据源和 Microsoft-Syslog 传入流。 此流采用已知结构,因此无需在 dataSources 部分中定义。 在 facilityNameslogLevels 属性中指定要收集的事件。 有关更多详细信息,请参阅使用 Azure Monitor 代理收集 Syslog 事件。 若要开始,可以参考该文章中的指南通过 Azure 门户创建 DCR,然后参考 DCR 定义中的指南检查 JSON。

可以为 dataFlows 属性添加转换来使用其他功能和进一步筛选数据,但应尽可能使用 facilityNameslogLevels 进行筛选,以提高效率并避免产生可能的数据引入费用。

以下示例 DCR 会执行以下操作:

  • 收集来自 cron 工具的所有事件。
  • 收集来自 Warningsyslog 工具的 daemon 和更高级别的事件。
  • 将数据发送到工作区中的 Syslog 表。
  • 该程序使用了一种简单的source转换,这种转换不会更改传入的数据。
{
    "___location": "eastus",
    "properties": {
      "dataSources": {
        "syslog": [
          {
            "name": "cronSyslog",
            "streams": [
              "Microsoft-Syslog"
            ],
            "facilityNames": [
              "cron"
            ],
            "logLevels": [
              "Debug",
              "Info",
              "Notice",
              "Warning",
              "Error",
              "Critical",
              "Alert",
              "Emergency"
            ]
          },
          {
            "name": "syslogBase",
            "streams": [
              "Microsoft-Syslog"
            ],
            "facilityNames": [
              "daemon",
              "syslog"
            ],
            "logLevels": [
              "Warning",
              "Error",
              "Critical",
              "Alert",
              "Emergency"
            ]
          }
        ]
      },
      "destinations": {
        "logAnalytics": [
          {
            "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
            "name": "centralWorkspace"
          }
        ]
      },
      "dataFlows": [
        {
          "streams": [
            "Microsoft-Syslog"
          ],
          "destinations": [
            "centralWorkspace"
          ],
            "transformKql": "source",
            "outputStream": "Microsoft-Syslog"
        }
      ]
    }
  }

性能计数器

用于性能数据的 DCR 使用 performanceCounters 数据源以及 Microsoft-InsightsMetricsMicrosoft-Perf 传入流。 Microsoft-InsightsMetrics 用于将数据发送到 Azure Monitor 指标,而 Microsoft-Perf 用于将数据发送到 Log Analytics 工作区。 如果要将性能数据发送到这两个目标位置,可以将这两种数据源添加到 DCR 中。 这些流采用已知架构,因此无需在 dataSources 部分中定义。

counterSpecifiers 属性中指定要收集的性能计数器。 有关更多详细信息,请参阅使用 Azure Monitor 代理收集性能计数器。 若要开始,可以参考该文章中的指南通过 Azure 门户创建 DCR,然后参考 DCR 定义中的指南检查 JSON。

可以为 dataFlowsMicrosoft-Perf 属性添加转换来增加更多功能并进一步筛选数据,但应仅在 counterSpecifiers 中选择所需的计数器,以提高效率并避免可能产生的数据引入费用。

以下示例 DCR 会执行以下操作:

  • 每隔 60 秒收集一组性能计数器,每隔 30 秒收集另一组性能计数器。
  • 将数据发送到 Azure Monitor 指标和 Log Analytics 工作区。
  • 该程序使用了一种简单的source转换,这种转换不会更改传入的数据。
{
    "___location": "eastus",
    "properties": {
      "dataSources": {
        "performanceCounters": [
          {
            "name": "perfCounterDataSource60",
            "streams": [
              "Microsoft-Perf",
              "Microsoft-InsightsMetrics"
            ],
            "samplingFrequencyInSeconds": 60,
            "counterSpecifiers": [
              "\\Processor(_Total)\\% Processor Time",
              "\\Memory\\Committed Bytes",
              "\\LogicalDisk(_Total)\\Free Megabytes",
              "\\PhysicalDisk(_Total)\\Avg. Disk Queue Length"
            ]
          },
          {
            "name": "perfCounterDataSource30",
            "streams": [
              "Microsoft-Perf"
            ],
            "samplingFrequencyInSeconds": 30,
            "counterSpecifiers": [
              "\\Process(_Total)\\Thread Count"
            ]
          }
        ]
      },
      "destinations": {
        "logAnalytics": [
          {
            "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
            "name": "centralWorkspace"
          }
        ],
        "azureMonitorMetrics": 
        {
            "name": "azureMonitorMetrics-default"
        }
      },
      "dataFlows": [
        {
            "streams": [
                "Microsoft-Perf"
            ],
            "destinations": [
                "centralWorkspace"
            ],
            "transformKql": "source",
            "outputStream": "Microsoft-Perf"
        },
        {
            "streams": [
                "Microsoft-Perf"
            ],
            "destinations": [
                "azureMonitorMetrics-default"
            ],
            "outputStream": "Microsoft-InsightsMetrics"
        }
      ]
    }
}

文本日志

适用于收集文本日志的 DCR 使用 logfiles 数据源,其包含代理应收集的日志文件的详细信息, 这包括必须在 streamDeclarations 中定义的流的名称和传入数据的列。 目前,这是一个固定列表,如使用 Azure Monitor 代理从文本文件收集日志中所述。

dataFlows 属性添加转换,以筛除不想收集的记录,以及设置数据格式以匹配目标表的架构。 常见方案是将带分隔符的日志文件解析为多个列,如带分隔符的日志文件中所述。

以下示例 DCR 会执行以下操作:

  • 收集代理计算机 .txt 文件夹中具有 c:\logs 扩展名的所有文件中的条目。
  • 使用转换根据逗号 (,) 分隔符将传入数据拆分为列。 此转换特定于该日志文件的格式,必须针对其他格式的日志文件进行调整。
  • 将收集的日志发送到名为 MyTable_CL 的自定义表。 此表必须已存在且包含由转换操作产生的列输出内容。
  • 收集文本日志的 FilePathComputer,如传入流中所述。 这些列还必须存在于目标表中。
{
    "___location": "eastus",
    "properties": {
        "dataCollectionEndpointId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.Insights/dataCollectionEndpoints/my-dce",
        "streamDeclarations": {
            "Custom-MyLogFileFormat": {
                "columns": [
                    {
                        "name": "TimeGenerated",
                        "type": "datetime"
                    },
                    {
                        "name": "RawData",
                        "type": "string"
                    },
                    {
                        "name": "FilePath",
                        "type": "string"
                    },
                    {
                        "name": "Computer",
                        "type": "string"
                    }
                ]
            }
        },
        "dataSources": {
            "logFiles": [
                {
                    "streams": [
                        "Custom-MyLogFileFormat"
                    ],
                    "filePatterns": [
                        "C:\\logs\\*.txt"
                    ],
                    "format": "text",
                    "settings": {
                        "text": {
                            "recordStartTimestampFormat": "ISO 8601"
                        }
                    },
                    "name": "myLogFileFormat-Windows"
                }
            ]
        },
        "destinations": {
            "logAnalytics": [
                {
                    "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
                    "name": "MyDestination"
                }
            ]
        },
        "dataFlows": [
            {
                "streams": [
                    "Custom-MyLogFileFormat"
                ],
                "destinations": [
                    "MyDestination"
                ],
                "transformKql": "source | project d = split(RawData,\",\") | project TimeGenerated=todatetime(d[0]), Code=toint(d[1]), Severity=tostring(d[2]), Module=tostring(d[3]), Message=tostring(d[4])",
                "outputStream": "Custom-MyTable_CL"
            }
        ]
    }
}

JSON 日志

适用于收集 JSON 日志的 DCR 使用 logfiles 数据源,其包含代理应收集的日志文件的详细信息, 这包括必须在 streamDeclarations 中定义的流的名称和传入数据的列。 有关更多详细信息,请参阅使用 Azure Monitor 代理从 JSON 文件收集日志

dataFlows 属性添加转换,以筛除不想收集的记录,以及设置数据格式以匹配目标表的架构。

以下示例 DCR 会执行以下操作:

  • 收集代理计算机 .json 文件夹中具有 c:\logs 扩展名的所有文件中的条目。 这些文件必须采用 JSON 格式,并且包含流声明中列出的列。
  • 将收集的日志发送到名为 MyTable_CL 的自定义表。 此表必须已存在,并且包含与传入流相同的列。 如果列不匹配,则需要修改 transformKql 属性中的转换,以将数据设置为目标表的格式。
{
    "___location": "eastus",
    "properties": {
        "dataCollectionEndpointId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.Insights/dataCollectionEndpoints/my-dce",
        "streamDeclarations": {
            "Custom-Json-stream": {
                "columns": [
                    {
                        "name": "TimeGenerated",
                        "type": "datetime"
                    },
                    {
                        "name": "FilePath",
                        "type": "string"
                    },
                    {
                        "name": "Code",
                        "type": "int"
                    },
                    {
                        "name": "Module",
                        "type": "string"
                    },
                    {
                        "name": "Message",
                        "type": "string"
                    }
                ]
            }
        },
        "dataSources": {
            "logFiles": [
                {
                    "streams": [
                        "Custom-Json-stream"
                    ],
                    "filePatterns": [
                        "C:\\logs\\*.json"
                    ],
                    "format": "json",
                    "name": "MyJsonFile"
                }
            ]
        },
        "destinations": {
            "logAnalytics": [
                {
                    "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
                    "name": "MyDestination"
                }
            ]
        },
        "dataFlows": [
            {
                "streams": [
                    "Custom-Json-stream"
                ],
                "destinations": [
                    "MyDestination"
                ],
                "transformKql": "source",
                "outputStream": "Custom-MyTable_CL"
            }
        ]
    }
}

将数据发送到事件中心或存储

向事件中心或存储帐户发送数据的 DCR 使用的数据源与使用 Azure Monitor 代理 (AMA) 收集数据的其他 DCR 相同,但其包含以下一个或多个目标位置。 有关更多详细信息,请参阅将数据发送到事件中心和存储(预览版)

  • eventHubsDirect
  • storageBlobsDirect
  • storageTablesDirect

注释

向事件中心或存储帐户发送数据的 DCR 必须包含 "kind": "AgentDirectToStore"

以下示例 DCR 会执行以下操作:

  • 使用 Azure Monitor 代理 (AMA) 从 Windows 计算机收集性能计数器和 Windows 事件。
  • 将数据发送到事件中心、blob 存储和表存储。
{
    "___location": "eastus",
    "kind": "AgentDirectToStore",
    "properties": {
        "dataSources": {
            "performanceCounters": [
                {
                "streams": [
                    "Microsoft-Perf"
                ],
                "samplingFrequencyInSeconds": 10,
                "counterSpecifiers": [
                    "\\Process(_Total)\\Working Set - Private",
                    "\\Memory\\% Committed Bytes In Use",
                    "\\LogicalDisk(_Total)\\% Free Space",
                    "\\Network Interface(*)\\Bytes Total/sec"
                ],
                "name": "perfCounterDataSource"
                }
            ],
            "windowsEventLogs": [
                {
                "streams": [
                    "Microsoft-Event"
                ],
                "xPathQueries": [
                    "Application!*[System[(Level=2)]]",
                    "System!*[System[(Level=2)]]"
                ],
                "name": "eventLogsDataSource"
                }
            ]
        },
        "destinations": {
            "eventHubsDirect": [
                {
                "eventHubResourceId": "/subscriptions/71b36fb6-4fe4-4664-9a7b-245dc62f2930/resourceGroups/my-resource-group/providers/Microsoft.EventHub/namespaces/my-eventhub-namespace/eventhubs/my-eventhub",
                "name": "myEh"
                }
            ],
            "storageBlobsDirect": [
                {
                "storageAccountResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/my-resource-group/providers/Microsoft.Storage/storageAccounts/mystorageaccount",
                "containerName": "myperfblob",
                "name": "PerfBlob"
                },
                {
                "storageAccountResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/my-resource-group/providers/Microsoft.Storage/storageAccounts/mystorageaccount",
                "containerName": "myeventblob",
                "name": "EventBlob"
                }
            ],
            "storageTablesDirect": [
                {
                "storageAccountResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/my-resource-group/providers/Microsoft.Storage/storageAccounts/mystorageaccount",
                "containerName": "myperftable",
                "name": "PerfTable"
                },
                {
                "storageAccountResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/my-resource-group/providers/Microsoft.Storage/storageAccounts/mystorageaccount",
                "containerName": "mymyeventtable",
                "name": "EventTable"
                }
            ]
        },
        "dataFlows": [
            {
                "streams": [
                "Microsoft-Perf"
                ],
                "destinations": [
                "myEh",
                "PerfBlob",
                "PerfTable"
                ]
            },
            {
                "streams": [
                "Microsoft-Event"
                ],
                "destinations": [
                "myEh",
                "EventBlob",
                "EventTable"
                ]
            },
        ]
    }
}

日志引入 API

适用于收集日志引入 API 的 DCR 必须在 DCR 定义的 streamDeclarations 部分中定义传入流的架构。 传入数据必须采用 JSON 格式,且其架构必须与此定义中的列匹配。 如果此架构与目标表的架构匹配,则无需进行转换。 如果架构不匹配,则必须向 dataFlows 属性添加转换,以设置数据的格式。 有关更多详细信息,请参阅 Azure Monitor 中的日志引入 API

以下示例 DCR 具有以下详细信息:

  • 将数据发送到名为 MyTable_CL 的工作区中名为 my-workspace 的表。 安装此 DCR 之前,需要创建包含以下列的表:
    • TimeGenerated
    • 电脑
    • AdditionalContext
    • ExtendedColumn(在转换中定义)
  • 转换应用于传入数据,以将数据设置为目标表的格式。

重要

此示例不包含 dataCollectionEndpointId 属性,因为创建 DCR 时会自动创建此属性。 你需要此属性的值,因为它是应用程序发送数据的目标位置 URL。 DCR 必须具有 kind:Direct,系统才能创建此属性。 有关更多详细信息,请参阅属性

{
    "___location": "eastus",
    "kind": "Direct",
    "properties": {
        "streamDeclarations": {
            "Custom-MyTable": {
                "columns": [
                    {
                        "name": "Time",
                        "type": "datetime"
                    },
                    {
                        "name": "Computer",
                        "type": "string"
                    },
                    {
                        "name": "AdditionalContext",
                        "type": "string"
                    }
                ]
            }
        },
        "destinations": {
            "logAnalytics": [
                {
                    "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace",
                    "name": "LogAnalyticsDest"
                }
            ]
        },
        "dataFlows": [
            {
                "streams": [
                    "Custom-MyTable"
                ],
                "destinations": [
                    "LogAnalyticsDest"
                ],
                "transformKql": "source | extend jsonContext = parse_json(AdditionalContext) | project TimeGenerated = Time, Computer, AdditionalContext = jsonContext, ExtendedColumn=tostring(jsonContext.CounterName)",
                "outputStream": "Custom-MyTable_CL"
            }
        ]
    }
}

工作区改造 DCR

工作区转换 DCR 的 datasources 部分为空,因为转换将应用于发送到工作区中受支持表的所有数据。 对于每个包含转换的表,必须包含且仅包含一个 workspaceResourceId 条目和一个 dataFlows 条目。 它还必须包含 "kind": "WorkspaceTransforms"

以下示例 DCR 具有以下详细信息:

  • LAQueryLogs 表的转换,该转换会筛除表本身的查询,并添加工作区名称列。
  • Event 表的转换,该转换会筛除信息事件并移除 ParameterXml 列。 这仅适用于来自已弃用 Log Analytics 代理的数据,而不适用于来自 Azure Monitor 代理的数据,如工作区转换 DCR 中所述。
{
    "kind": "WorkspaceTransforms",
    "___location": "eastus",
    "properties": {
        "dataSources": {},
        "destinations": {
            "logAnalytics": [
                {
                    "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
                    "name": "clv2ws1"
                }
            ]
        },
        "dataFlows": [
            {
                "streams": [
                    "Microsoft-Table-LAQueryLogs"
                ],
                "destinations": [
                    "clv2ws1"
                ],
                "transformKql": "source | where QueryText !contains 'LAQueryLogs' | extend Context = parse_json(RequestContext) | extend Workspace_CF = tostring(Context['workspaces'][0]) | project-away RequestContext, Context"
            },
            {
                "streams": [
                    "Microsoft-Table-Event"
                ],
                "destinations": [
                    "clv2ws1"
                ],
                "transformKql": "source | where EventLevelName in ('Error', 'Critical', 'Warning') | project-away ParameterXml"
            }
        ]
    }
}

将数据发送到多个表

你可能希望将单个数据源中的数据发送到同一个 Log Analytics 工作区的多个表中,原因包括以下几种情况:

  • 将用于不定期故障排除的记录发送到基本日志表,可节省数据引入成本。
  • 将包含敏感数据的记录或列发送到具有不同权限或保留设置的表。

若要将单个数据源中的数据发送到多个表,请在 DCR 中创建多个数据流,每个数据流使用唯一的转换查询和输出表,如下图所示。

重要

目前,DCR 中的表必须位于同一 Log Analytics 工作区中。 若要从单个数据源发送到多个工作区,请使用多个 DCR 并将应用程序配置为将数据发送到每个工作区。

显示数据传输至多个表的转换示意图。

以下示例会筛选由 Azure Monitor 代理发送到事件表的记录。 仅向“事件”表发送警告事件和错误事件。 其他事件会发送到名为 Event_CL 的事件表的副本,该副本配置为基本日志。

注释

此示例要求在同一工作区中创建名为 Event_CL 的“事件”表副本。

{
    "___location": "eastus",
    "properties": {
        "dataSources": {
            "windowsEventLogs": [
              {
                "name": "eventLogsDataSource",
                "streams": [
                  "Microsoft-Event"
                ],
                "xPathQueries": [
                  "System!*[System[(Level = 1 or Level = 2 or Level = 3)]]",
                  "Application!*[System[(Level = 1 or Level = 2 or Level = 3)]]"
                ]
              }
            ]
        },
        "destinations": {
            "logAnalytics": [
                {
                    "workspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace",
                    "name": "MyDestination"
                }
            ]
        },
        "dataFlows": [
            {
                "streams": [
                    "Microsoft-Event"
                ],
                "destinations": [
                    "MyDestination"
                ],
                "transformKql": "source | where EventLevelName in ('Error', 'Warning')",
                "outputStream": "Microsoft-Event"
            },
            {
                "streams": [
                    "Microsoft-Event"
                ],
                "destinations": [
                    "MyDestination"
                ],
                "transformKql": "source | where EventLevelName !in ('Error', 'Warning')",
                "outputStream": "Custom-Event_CL"
            }
        ]
    }
}

后续步骤