使用 Python SDK 配置 azure 数据库的诊断设置

Configure diagnostic setting for azure database using Python SDK

我想使用 Python 配置 Azure 数据库的诊断设置。我知道我必须使用 DiagnosticSettingsOperations Class, and MonitorManagementClient Client, and create_or_update 方法来启动。我是 Python 开发的新手,我正在努力将各个部分组合在一起。

但是,没有关于要为 DiagnosticSettingsOperations 传递哪些参数的适当示例 Class。

示例代码:

from azure.mgmt.monitor import MonitorManagementClient
from azure.identity import ClientSecretCredential

####### FUNCTION TO CREATE AZURE AUTHENTICATION USING SERVICE PRINCIPAL #######
def authenticateToAzureUsingServicePrincipal():

    # Authenticate to Azure using Service Principal credentials
    client_id = 'client_id'
    client_secret = 'client_secret'
    client_tenant_id = 'client_tenant_id'

    # Create Azure credential object
    servicePrincipalCredentialObject = ClientSecretCredential(tenant_id=client_tenant_id, client_id=client_id, client_secret=client_secret)

    return servicePrincipalCredentialObject


azureCredential = authenticateToAzureUsingServicePrincipal()
monitorManagerClient = MonitorManagementClient(azureCredential)

我想为 Azure sql 数据库配置诊断设置,它默认选择所有指标和日志并发送到日志分析工作区。有谁知道如何进一步进行?

代码如下所示:

 #other code

 monitorManagerClient = MonitorManagementClient(azureCredential)

 # Creates or Updates the diagnostic setting[put]
 BODY = {          
          "workspace_id": "the resource id of the log analytics workspace",
           "metrics": [
             {
               "category": "Basic",
               "enabled": True,
               "retention_policy": {
                 "enabled": False,
                 "days": "0"
               }
             }
             #other categories
          ],
          "logs": [
            {
              "category": "SQLInsights",
              "enabled": True,
              "retention_policy": {
                "enabled": False,
                "days": "0"
              }
            }
            #other categories
          ],
          # "log_analytics_destination_type": "Dedicated"
        }
 diagnostic_settings = self.mgmt_client.diagnostic_settings.create_or_update(RESOURCE_URI, INSIGHT_NAME, BODY)

github里面有例子,大家可以看看。而如果你想 select ALL Metrics and Logs,你应该在上面代码中 BODY 中的 metrics / logs 中将它们一一添加。