【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
- [Azure] 使用Azure Powershell脚本查看账号下所有订阅中的云服务公网IP地址绑定情况
- python里使用dir函数查看一个类所有成员函数
- 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式
- 【Azure Developer】使用 Azure Python SDK时,遇见 The resource principal named https://management.azure.com was not found in the tenant China Azure问题的解决办法
- 使用Python+PIL查看两张相似图形的差异
- python使用requests包爬取Pixiv图片--关注画师的所有作品
- 常用SQL:看全部表的空间大小|tempdb使用情况|清空日志|查看日志|查看数据库表|禁用所有约束|系统表|系统视图
- Python使用inspect查看代码参数
- python os.statvfs() 查看文件系统信息 磁盘使用情况
- SVN使用log,list,cat,diff查看所有及特定文件版本信息
- 一个查看所有用户的磁盘空间使用情况的脚本
- 使用Python程序抓取新浪在国内的所有IP的教程
- 使用python实现打印所有100以内的所有质数
- du 使用详解 linux查看目录大小 linux统计目录大小并排序 查看目录下所有一级子目录文件夹大小 du -h --max-depth=1 |grep [
- [Python 爬虫之路4] 使用selenium爬取知乎任意一个问题下,所有回答中的图片
- Linux 查看某个用户组的所有用户(Python实现)
- python利用dir函数查看类中所有成员函数示例代码
- 使用show ip route查看某个指定网段的所有内容
- sp_MSforeachtable使用方法 查看库中所有表的空间大小
- 查看所有JVM参数,默认值,以及一些常用JVM参数使用