有没有一种方法可以为Microsoft监视代理指定目标日志文件,以侦听并从代码中提取日志?

时间:2019-10-11 09:47:27

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

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

1 个答案:

答案 0 :(得分:1)

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

以下是其他powershell命令行工具,可以方便地查询和创建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

还找到了Log Analytics Rest 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].*$)"
                  }
                }
              }
            ]
          }
        }