Search code examples
pythonazureazure-resource-managerazure-monitoringazure-sdk-python

Correct arguments to create DiagnosticSettingsOperations object


I want to configure diagnostic settings for Azure resource, as a result I am using, DiagnosticSettingsOperations class. I am able to create a client of MonitorManagementClient Class (first argument of DiagnosticSettingsOperations class). However, I am not able to fetch the other three argument that I need to pass to DiagnosticSettingsOperations.

Constructor:

DiagnosticSettingsOperations(client, config, serializer, deserializer)

Since, I did not know what to pass, I just gave blank string ''.

Below is my sample code:

azureCredential = authenticateToAzureUsingServicePrincipal()
monitorManagerClient = MonitorManagementClient(azureCredential, 'xxx-xxxx-xxxx-xxxx-xxxxxx')

diagnosticSettingsOperationsClient = DiagnosticSettingsOperations(monitorManagerClient, '', '','')
resource_uri = '<AZURE RESOURCE ID OF SQL DATABASE>'
name = 'coemonitoreu'
workspace_id = 'AZURE RESOURCE ID OF LOG ANALYTICS WORKSPACE'
metrics = ["WorkloadManagement"]
logs = ["SQLInsights"]

parameter_list = {
    'workspace_id': workspace_id,
    'metrics': metrics,
    'logs': logs
    }
diagnosticSettingsOperationsClient.create_or_update(resource_uri=resource_uri, name=name, parameters=parameter_list)

I know my class invocation is wrong, Can someone point out the correct way of creating the DiagnosticSettingsOperations client, so I can use its create_or_update method to configure diagnostic settings.

Error when I run the code:

<azure.mgmt.monitor._monitor_management_client.MonitorManagementClient object at 0x000002053F7BD448>
Traceback (most recent call last):
  File "C:\Users\manjug\source\repos\ExtolloConfigDatabase\ExtolloConfigDatabase\anotherPythonFile.py", line 35, in <module>
    diagnosticSettingsOperationsClient.create_or_update(resource_uri=resource_uri, name=name, parameters=parameter_list)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\azure\mgmt\monitor\v2017_05_01_preview\operations\_diagnostic_settings_operations.py", line 139, in create_or_update
    'resourceUri': self._serialize.url("resource_uri", resource_uri, 'str', skip_quote=True),
AttributeError: 'str' object has no attribute 'url'

Solution

  • Please try to set it like below:

    #for log    
    logs = [{"category":"SQLInsights","enabled":true,"retentionPolicy":{"days":0,"enabled":false}}]
    #to set more catetories, you can use a "," symbol in the [],like below:
    logs = [{"category":"SQLInsights","enabled":true,"retentionPolicy":{"days":0,"enabled":false}},{other category}]
    
    #for metrics
    metrics = [{"enabled":true,"retentionPolicy":{"days":0,"enabled":false},"category":"WorkloadManagement"}]