【Azure Developer】使用 Azure Python 查看 Azure 所有的 Alert rule
问题描述
在Azure Alert 门户中,可以列举出所有Azure资源的Alert rule信息,如下图:
如果像通过Python SDK来获取所有的Alert Rule,有什么可以参考的代码吗?
问题分析
由于Azure Alert门户是把所有的资源中配置了Alert Rule都进行了汇总显示,而SDK中并没有一个方法客户获取到所有Azure资源的Alert。所以需要先知道需要什么资源的Alert,然后再Github Python的开源代码库中寻找正确的Package 和 方法。
1) 可以在Alert Rule页面中发现需要查看Rule的Type,页面上可以过滤的值有: Metrics,Activity Log, Application Insights, Log ...
2) Clone azure-sdk-for-python (https://github.com/Azure/azure-sdk-for-python/tree/azure-monitor-query_1.0.1/sdk/monitor/azure-monitor-query/samples)并在Source中查看对应Provider的示例代码
3)编写自己的Python 代码,如下的示例中,包含了获取获取Classic, Log Search, Activity 和 Metrics的Alert Rule信息。
from azure.mgmt.monitor import MonitorManagementClient from azure.identity import DefaultAzureCredential from msrestazure.azure_cloud import AZURE_CHINA_CLOUD as CLOUD import os os.environ["SUBSCRIPTION_ID"] = "xxxxxxyour-subidxxxxxx" os.environ["AZURE_TENANT_ID"] = "your tenant idxxxxx" os.environ["AZURE_CLIENT_ID"] = "client_id_sp" os.environ["AZURE_CLIENT_SECRET"] = "pw_sp" subscription_id = os.environ["SUBSCRIPTION_ID"] credential = DefaultAzureCredential(authority=CLOUD.endpoints.active_directory) # create client client1 = MonitorManagementClient( credential, subscription_id, base_url=CLOUD.endpoints.resource_manager, credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"] ) #classic my_alerts1 = client1.alert_rules.list_by_subscription() for j in my_alerts1: print(j) #log search alerts client2 = MonitorManagementClient( credential, subscription_id, base_url=CLOUD.endpoints.resource_manager, credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"] ) my_alerts2 = client2.scheduled_query_rules.list_by_subscription() for j in my_alerts2: print(j) #activity alerts client3 = MonitorManagementClient( credential, subscription_id, base_url=CLOUD.endpoints.resource_manager, credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"], api_version="2017-04-01" ) my_alerts3 = client3.activity_log_alerts.list_by_subscription_id() for j in my_alerts3: print(j) #metric alerts client4 = MonitorManagementClient( credential, subscription_id, base_url=CLOUD.endpoints.resource_manager, credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"] ) my_alerts4 = client4.metric_alerts.list_by_subscription() for j in my_alerts4: print(j)
参考资料
Azure Python SDK Sample:
https://github.com/Azure/azure-sdk-for-python/tree/azure-monitor-query_1.0.1/sdk/monitor/azure-monitor-query/samples
https://docs.microsoft.com/zh-cn/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2018_03_01.operations.metricalertsoperations?view=azure-python
https://docs.microsoft.com/zh-cn/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2018_04_16.operations.scheduledqueryrulesoperations?view=azure-python
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-04-24 【Azure 应用服务】App Service/Azure Function的出站连接过多而引起了SNAT端口耗尽,导致一些新的请求出现超时错误(Timeout)