Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python SDK为azure数据库配置诊断设置_Python_Azure_Azure Monitoring_Azure Diagnostics_Azure Sdk Python - Fatal编程技术网

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

使用Python SDK为azure数据库配置诊断设置,python,azure,azure-monitoring,azure-diagnostics,azure-sdk-python,Python,Azure,Azure Monitoring,Azure Diagnostics,Azure Sdk Python,我想使用Python为Azure数据库配置诊断设置。我知道我必须使用类、客户端和方法来启动。我对Python开发相当陌生,我正在努力将这些部分组合起来 但是,没有关于为DiagnosticsSettings操作类传递哪些参数的适当示例 示例代码: from azure.mgmt.monitor import MonitorManagementClient from azure.identity import ClientSecretCredential ####### FUNCTION TO

我想使用Python为Azure数据库配置诊断设置。我知道我必须使用类、客户端和方法来启动。我对Python开发相当陌生,我正在努力将这些部分组合起来

但是,没有关于为DiagnosticsSettings操作类传递哪些参数的适当示例

示例代码:

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)

在中有一个示例,您可以查看它。如果您想选择所有度量和日志,您应该在上述代码的
正文
中的
度量
/
日志
中逐一添加它们。

感谢您宝贵的回答。这真的很有帮助。如果有一种方法可以默认设置所有指标和日志。这样我就可以有一个函数,我可以调用它为我的所有资源设置诊断设置。我认为这是不可能的:(.我已经检查了,它没有为日志/度量值提供像all一样的值。是否有其他方法/方法可以用于获取Azure资源(如Microsoft.Sql.Servers)的度量值/日志。因此,我可以获取度量值/日志并将其设置为启用。请尝试
mgmt\u客户端。诊断\u设置\u类别。列表(资源\u URI)
根据,我还没有测试它。如果它不起作用,请提出一个新问题并让我知道:)我可以使用以下代码获取资源的度量值列表:metricsObjectsList=(monitorManagerClient.diagnostic_settings.list(resource_uri)).value[0]。metrics