I am using the azure advisor python SDK to fetch advisor recommendations. It is giving me all the recommendations other than 'security'.
When I first used the same code, it used to give me 'security' recommendation as well, but all of a sudden it stopped giving me 'security' recommendations.
From Azure portal: I can see a total of 33 recommendations (30 are security, and remaining are 1 of each -operationalExcellence, Reliability, performance). But when I run the code, I get only 3 recommendations, other than 'security'. Can some one please help me, if any default configurations has to be changed?
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
from azure.mgmt.advisor import AdvisorManagementClient
import sys
import os
SUBSCRIPTION_ID = "xxxx-xxx-xxxx-xxx"
client = CostManagementClient(credential=DefaultAzureCredential()) # noqa: F841
print(client)
advisor_client = AdvisorManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
print(advisor_client)
def call(response, *args, **kwargs):
return response.http_response
print(call)
response = advisor_client.recommendations.generate(cls=call) # noqa: F841
print(response)
cached_list = advisor_client.recommendations.list()
for data in cached_list:
print(data)
I have tried the Azure REST API for Advisor as well, it is also giving all recommendations BUT skipping 'security' category.
I am using the azure advisor python SDK to fetch advisor recommendations. It is giving me all the recommendations other than 'security'
I tried in my environment and got the below results:
You can use the below code to get the all categories of all advisor recommendations with count.
Code:
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
from azure.mgmt.advisor import AdvisorManagementClient
SUBSCRIPTION_ID = ""
client = CostManagementClient(credential=DefaultAzureCredential())
advisor_client = AdvisorManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
category_count = {}
response = advisor_client.recommendations.list()
for data in response:
category = data.category
if category in category_count:
category_count[category] += 1
else:
category_count[category] = 1
for category, count in category_count.items():
print(f"{category}: {count}")
Output:
Cost: xx
HighAvailability: xx
Security: xx
Performance: xx
OperationalExcellence: xxx
You can check the particular to list the advisor recommendation. For example, I have taken here performance
to check the impacted_fields with the below code.
Code:
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
from azure.mgmt.advisor import AdvisorManagementClient
SUBSCRIPTION_ID = ""
client = CostManagementClient(credential=DefaultAzureCredential())
advisor_client = AdvisorManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
category_count = {}
response = advisor_client.recommendations.list(filter="Category eq 'Performance'")
for data in response:
print(data.impacted_field)
Output:
Microsoft.Compute/virtualMachines
Microsoft.Kusto/Clusters
Microsoft.Kusto/Clusters
Microsoft.Kusto/Clusters
MICROSOFT.SQL/SERVERS/DATABASES
MICROSOFT.SQL/SERVERS/DATABASES
Reference:
Recommendations - List - REST API (Azure Advisor) | Microsoft Learn