微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 Python SDK 为 azure 数据库配置诊断设置

我想使用 Python 为 Azure 数据库配置诊断设置。我知道我必须使用 DiagnosticSettingsOperations 类、MonitorManagementClient 客户端和 create_or_update 方法来启动。我对 Python 开发还很陌生,我正在努力将各个部分放在一起。

但是,没有关于要为 DiagnosticSettingsOperations 类传递哪些参数的正确示例。

示例代码

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","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中有一个例子,你可以看看。而如果要选择ALL Metrics and Logs,则需要在上面代码中的metrics中的logs / BODY中一一添加。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。