azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Subscription Diagnostic Settings for Azure Monitor

Open nkiest opened this issue 6 years ago • 10 comments

Description of the new feature

Need matching feature for Powershell that we have in CLI: https://github.com/Azure/azure-cli/issues/13692

Support Subscription Variant of Diagnostic Settings to export Activity Log, as documented at https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-05-01-preview/subscriptionDiagnosticsSettings_API.json

Proposed implementation details (optional)

This should look like Diagnostic Settings command (Set-AzDiagnosticSetting), but with Subscription as target, rather than resource ID. Note, retention setting is not supported. https://docs.microsoft.com/en-us/azure/azure-monitor/platform/diagnostic-settings This feature will eventually replace Add-AzLogProfile

nkiest avatar Sep 24 '20 18:09 nkiest

Hi @nkiest ,

Az.Monitor depends on Dotnet management SDK generated from swagger specs, this feature was not included (SDK_Info) in the current SDK version 0.25.2-preview

We will plan this feature once SDK was ready.

Thanks

VeryEarly avatar Sep 27 '20 02:09 VeryEarly

From @asheniam I was able to use the latest Azure Monitor SDK and list/create subscription diagnostic setting. https://www.nuget.org/packages/Microsoft.Azure.Management.Monitor/0.25.2-preview

Note: There isn't any 1st class SDK support for subscription diagnostic settings so from the SDK side, it looks like resource level diagnostic settings.

Example:


MonitorManagementClient client = new MonitorManagementClient(serviceCreds); 
            client.SubscriptionId = "c0b5f462-6f71-4a0e-9916-6250aae92ae1";

            DiagnosticSettingsResourceCollection resource = client.DiagnosticSettings.List("/subscriptions/" + client.SubscriptionId);
            client.DiagnosticSettings.CreateOrUpdate(
                resourceUri: "/subscriptions/" + client.SubscriptionId,
                parameters: resource.Value[1],
                name: resource.Value[1].Name);

The PowerShell team should be unblocked.

nkiest avatar Oct 08 '20 22:10 nkiest

Hi

Hi @nkiest ,

Az.Monitor depends on Dotnet management SDK generated from swagger specs, this feature was not included (SDK_Info) in the current SDK version 0.25.2-preview

We will plan this feature once SDK was ready.

Thanks

Hi, Please see the response above from nkiest and advise on the next steps. Thanks

shilpasharmaAM avatar Oct 13 '20 17:10 shilpasharmaAM

From @asheniam I was able to use the latest Azure Monitor SDK and list/create subscription diagnostic setting. https://www.nuget.org/packages/Microsoft.Azure.Management.Monitor/0.25.2-preview

Note: There isn't any 1st class SDK support for subscription diagnostic settings so from the SDK side, it looks like resource level diagnostic settings.

Example:


MonitorManagementClient client = new MonitorManagementClient(serviceCreds); 
            client.SubscriptionId = "c0b5f462-6f71-4a0e-9916-6250aae92ae1";

            DiagnosticSettingsResourceCollection resource = client.DiagnosticSettings.List("/subscriptions/" + client.SubscriptionId);
            client.DiagnosticSettings.CreateOrUpdate(
                resourceUri: "/subscriptions/" + client.SubscriptionId,
                parameters: resource.Value[1],
                name: resource.Value[1].Name);

The PowerShell team should be unblocked.

The SDK method referenced above "client.DiagnosticSettings.List" is from API: https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-05-01-preview/diagnosticsSettings_API.json

which is different from: https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-05-01-preview/subscriptionDiagnosticsSettings_API.json

Subscription level diagnostic setting cannot be created from Set-AzDiagnosticSetting is because API: https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-05-01-preview/diagnosticsSettingsCategories_API.json is not able to return the category for subscription.

VeryEarly avatar Oct 22 '20 01:10 VeryEarly

Invoke-AzRestMethod -Path subscriptions/XXXXXXXXXXXX/providers/microsoft.insights/diagnosticSettingsCategories?api-version=2017-05-01-preview -Method GET

Headers : {[Cache-Control, System.String[]], [Pragma, System.String[]], [Strict-Transport-Security, System.String[]], [x-ms-request-id, System.String[]]…} Version : 1.1 StatusCode : 400 Method : GET Content : {"code":"BadRequest","message":""}

VeryEarly avatar Oct 22 '20 02:10 VeryEarly

This preexisting API for activity log (was called event service, hence bad API name) provides a way to get valid categories for Subscription Diagnostic Settings: https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/stable/2015-04-01/eventCategories_API.json

nkiest avatar Oct 22 '20 20:10 nkiest

This preexisting API for activity log (was called event service, hence bad API name) provides a way to get valid categories for Subscription Diagnostic Settings: https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/stable/2015-04-01/eventCategories_API.json

Hi @nkiest ,

I'll say it is possible to add some specific logic to handle subscription in powershell side:

  • get categories for subscription using event categories API

  • explicitly convert categories from above API to the type needed by https://github.com/Azure/azure-rest-api-specs/blob/master/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-05-01-preview/diagnosticsSettings_API.json

However, it doesn't look appropriate to have this kind of logic in powershell side, ideal solution would be let API /{resourceUri}/providers/microsoft.insights/diagnosticSettingsCategories support subscription as resource.

Let me know if you have any concern.

VeryEarly avatar Oct 28 '20 08:10 VeryEarly

@asheniam Any input on how categories gets returned? CLI team did some version of the first set of logic, so we thought we were good here.

nkiest avatar Oct 29 '20 21:10 nkiest

this feature will be released with S185

you can set subscription diagnostic settings by: $list = @() Get-AzSubscriptionDiagnosticSettingCategory | ForEach-Object { $list += (New-AzDiagnosticDetailSetting -Log -Category $_.Name -Enabled) } $DiagnosticSettingName = 'please use your setting name here' $SubscriptionId = 'please use your subscription Id here' $WorkspaceId = 'please use your workspace Id here' $setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -SubscriptionId $SubscriptionId -WorkspaceId $WorkspaceId -Setting $list Set-AzDiagnosticSetting -InputObject $setting

VeryEarly avatar Mar 25 '21 02:03 VeryEarly

In case anyone comes along and has trouble finding the cmdlet, Get-AzSubscriptionDiagnosticSettingCategory replaced by Get-AzEventCategory

Az.Monitor Version 4.2.0 https://github.com/Azure/azure-powershell/blob/f67aaf4dbe62a9b9ed08bbb1fe885a1d39d4fce5/src/Monitor/Monitor/ChangeLog.md?plain=1#L115

brianstringfellow avatar Jun 12 '24 23:06 brianstringfellow