【问题标题】:Is there a way to specify target log files for microsoft monitoring agent to listen and pick up the logs from code?有没有办法为微软监控代理指定目标日志文件来监听并从代码中提取日志?
【发布时间】:2023-03-11 15:24:01
【问题描述】:

我正在考虑使用 Microsoft 监控代理从系统上的日志文件中收集一些日志记录并将它们发送到日志分析工作区。 有没有办法指定代理将侦听的目标文件(自定义日志文件)并将日志直接流式传输到 azure 工作区。 我知道这可以通过 azure 门户通过在工作区中添加一个额外的数据源来实现(由这个链接 https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-sources-custom-logs 指定)。 我正在寻找一种从 c# 代码/powershell 脚本配置这些数据源的方法。(可能是我不知道的 api 或 sdk)。

【问题讨论】:

    标签: azure azure-log-analytics azure-monitoring


    【解决方案1】:

    添加自定义日志使用 New-AzOperationalInsightsCustomLogDataSource。

    这里是其他 powershell commandlet,可以方便地查询和创建 LogAnalytics 数据源。

    get-azoperationalinsightsdatasource
    New-AzOperationalInsightsApplicationInsightsDataSource 
    New-AzOperationalInsightsAzureActivityLogDataSource
    New-AzOperationalInsightsComputerGroup
    New-AzOperationalInsightsCustomLogDataSource
    New-AzOperationalInsightsLinuxPerformanceObjectDataSource
    New-AzOperationalInsightsLinuxSyslogDataSource
    New-AzOperationalInsightsSavedSearch
    New-AzOperationalInsightsStorageInsight
    New-AzOperationalInsightsWindowsEventDataSource
    New-AzOperationalInsightsWindowsPerformanceCounterDataSource
    

    https://docs.microsoft.com/en-us/powershell/module/az.operationalinsights/get-azoperationalinsightsdatasource?view=azps-2.7.0

    还可以找到日志分析 Rest API 的链接,该 API 可以轻松地与 C# 代码一起使用。

    https://docs.microsoft.com/en-us/rest/api/loganalytics/ https://docs.microsoft.com/en-us/rest/api/loganalytics/datasources/createorupdate

    Powershell

    要收集的自定义日志

    链接:https://docs.microsoft.com/en-us/azure/azure-monitor/platform/powershell-workspace-configuration

    $CustomLog = @"
    {
        "customLogName": "sampleCustomLog1",
        "description": "Example custom log datasource",
        "inputs": [
            {
                "location": {
                "fileSystemLocations": {
                    "windowsFileTypeLogPaths": [ "e:\\iis5\\*.log" ],
                    "linuxFileTypeLogPaths": [ "/var/logs" ]
                    }
                },
            "recordDelimiter": {
                "regexDelimiter": {
                    "pattern": "\\n",
                    "matchIndex": 0,
                    "matchIndexSpecified": true,
                    "numberedGroup": null
                    }
                }
            }
        ],
        "extractions": [
            {
                "extractionName": "TimeGenerated",
                "extractionType": "DateTime",
                "extractionProperties": {
                    "dateTimeExtraction": {
                        "regex": null,
                        "joinStringRegex": null
                        }
                    }
                }
            ]
        }
    "@
    
    
    
    
    # Custom Logs
    
    New-AzOperationalInsightsCustomLogDataSource -ResourceGroupName $ResourceGroup -WorkspaceName $WorkspaceName -CustomLogRawJson "$CustomLog" -Name "Example Custom Log Collection"
    
    • ARM 模板

    自定义日志的 Arm 模板格式如下。详见链接https://docs.microsoft.com/en-us/azure/azure-monitor/platform/template-workspace-configuration

    
    {
              "apiVersion": "2015-11-01-preview",
              "type": "dataSources",
              "name": "[concat(parameters('workspaceName'), parameters('customlogName'))]",
              "dependsOn": [
                "[concat('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName'))]"
              ],
              "kind": "CustomLog",
              "properties": {
                "customLogName": "[parameters('customlogName')]",
                "description": "this is a description",
                "extractions": [
                  {
                    "extractionName": "TimeGenerated",
                    "extractionProperties": {
                      "dateTimeExtraction": {
                        "regex": [
                          {
                            "matchIndex": 0,
                            "numberdGroup": null,
                            "pattern": "((\\d{2})|(\\d{4}))-([0-1]\\d)-(([0-3]\\d)|(\\d))\\s((\\d)|([0-1]\\d)|(2[0-4])):[0-5][0-9]:[0-5][0-9]"
                          }
                        ]
                      }
                    },
                    "extractionType": "DateTime"
                  }
                ],
                "inputs": [
                  {
                    "location": {
                      "fileSystemLocations": {
                        "linuxFileTypeLogPaths": null,
                        "windowsFileTypeLogPaths": [
                          "[concat('c:\\Windows\\Logs\\',parameters('customlogName'))]"
                        ]
                      }
                    },
                    "recordDelimiter": {
                      "regexDelimiter": {
                        "matchIndex": 0,
                        "numberdGroup": null,
                        "pattern": "(^.*((\\d{2})|(\\d{4}))-([0-1]\\d)-(([0-3]\\d)|(\\d))\\s((\\d)|([0-1]\\d)|(2[0-4])):[0-5][0-9]:[0-5][0-9].*$)"
                      }
                    }
                  }
                ]
              }
            }
    

    【讨论】:

    • 感谢@Arun 的链接,但在将自定义日志添加为数据源类型时,我找不到要定义的属性的任何文档。你知道这方面的事情吗?
    • @Harika :由于评论的限制。在我的答案中发布带有示例的更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 2016-01-11
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多