【问题标题】:Azure DB sync logs in log analytics workspace日志分析工作区中的 Azure DB 同步日志
【发布时间】:2020-12-09 12:00:13
【问题描述】:

我有一个计划每小时运行的 Azure SQL 数据库同步组。 问题: 我可以通过启用诊断设置将此日志发送到日志分析工作区吗? 如果是,过滤掉它们的最佳方法是什么?

我可以从 powershell 成功获取日志,但我的最终目标是根据同步日志创建警报。

提前谢谢你!

【问题讨论】:

    标签: azure azure-sql-database


    【解决方案1】:

    如果要将 Azure SQL 数据库同步组发送到日志分析工作区,可以使用 HTTP Data Collector API 实现它。

    例如

    $SubscriptionId = "SubscriptionId" 
    $DS_ResourceGroupName = ""
    $DS_ServerName =  "" 
    $DS_DatabaseName = "" 
    $DS_SyncGroupName = "" 
    
    
    # Replace with your OMS Workspace ID
    $CustomerId = "OMSCustomerID"  
    
    # Replace with your OMS Primary Key
    $SharedKey = "SharedKey"
    
    # Specify the name of the record type that you'll be creating
    $LogType = "DataSyncLog"
    
    # Specify a field with the created time for the records
    $TimeStampField = "DateValue"
    
    Connect-AzureRmAccount
    select-azurermsubscription -SubscriptionId $SubscriptionId
    #get log
    $endtime =[System.DateTime]::UtcNow
    $StartTime = ""
    
    $Logs = Get-AzureRmSqlSyncGroupLog -ResourceGroupName $DS_ResourceGroupName `
                                                      -ServerName $DS_ServerName `
                                                      -DatabaseName $DS_DatabaseName `
                                                      -SyncGroupName $DS_SyncGroupName `
                                                      -starttime $StartTime `
                                                      -endtime $EndTime;
    if ($Logs.Length -gt 0)
    {
       foreach ($Log in $Logs)
       {
        $Log | Add-Member -Name "SubscriptionId" -Value $SubscriptionId -MemberType NoteProperty
        $Log | Add-Member -Name "ResourceGroupName" -Value $DS_ResourceGroupName -MemberType NoteProperty
        $Log | Add-Member -Name "ServerName" -Value $DS_ServerName -MemberType NoteProperty
        $Log | Add-Member -Name "HubDatabaseName" -Value $DS_DatabaseName -MemberType NoteProperty
        $Log | Add-Member -Name "SyncGroupName" -Value $DS_SyncGroupName -MemberType NoteProperty 
    
        #Filter out Successes to Reduce Data Volume to OMS
        #Include the 5 commented out line below to enable the filter
        #For($i=0; $i -lt $Log.Length; $i++ ) {
        #    if($Log[$i].LogLevel -eq "Success") {
        #      $Log[$i] =""      
        #    }
        # }
    
    
    
      }
    
    
    $json = ConvertTo-JSON $logs
    
    
    
    $result = Post-OMSData -customerId $customerId -sharedKey $sharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($json)) -logType $logType
    if ($result -eq 200) 
    {
        Write-Host "Success"
    }
    if ($result -ne 200) 
                   {
       throw 
    @"
        Posting to OMS Failed         
        Runbook Name: DataSyncOMSIntegration         
    "@
    }
    Function Build-Signature ($customerId, $sharedKey, $date, $contentLength, $method, $contentType, $resource)
    {
        $xHeaders = "x-ms-date:" + $date
        $stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource
    
        $bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
        $keyBytes = [Convert]::FromBase64String($sharedKey)
    
        $sha256 = New-Object System.Security.Cryptography.HMACSHA256
        $sha256.Key = $keyBytes
        $calculatedHash = $sha256.ComputeHash($bytesToHash)
        $encodedHash = [Convert]::ToBase64String($calculatedHash)
        $authorization = 'SharedKey {0}:{1}' -f $customerId,$encodedHash
        return $authorization
    }
    
    
    # Create the function to create and post the request
    Function Post-OMSData($customerId, $sharedKey, $body, $logType)
    {
        $method = "POST"
        $contentType = "application/json"
        $resource = "/api/logs"
        $rfc1123date = [DateTime]::UtcNow.ToString("r")
        $contentLength = $body.Length
        $signature = Build-Signature `
            -customerId $customerId `
            -sharedKey $sharedKey `
            -date $rfc1123date `
            -contentLength $contentLength `
            -method $method `
            -contentType $contentType `
            -resource $resource
        $uri = "https://" + $customerId + ".ods.opinsights.azure.com" + $resource + "?api-version=2016-04-01"
    
        $headers = @{
            "Authorization" = $signature;
            "Log-Type" = $logType;
            "x-ms-date" = $rfc1123date;
            "time-generated-field" = $TimeStampField;
        }
    
        $response = Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing
        return $response.StatusCode
    
    }
    
    

    完成后,您可以在 Azure 日志分析中提醒 vai 日志搜索。更多详情请参考blog

    【讨论】:

    • 如果对你有用,可以accept it as an answer吗?
    • 它工作得很好,我只需将 cmdlet 从 AzureRM 更改为 AZ,并在身份验证部分更改一些 mods,因为我必须在自动化帐户中使用脚本。但是一旦我这样做了,一切都完美无缺。我什至可以根据生成的日志设置警报。
    • @Dorin,如果您不介意的话,能否与 AZ 而不是 AzureRM 共享新脚本?谢谢!
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2021-09-18
    • 2019-11-02
    相关资源
    最近更新 更多