【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 Developer】使用 Azure Python 查看 Azure 所有的 Alert rule的更多相关文章
- 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式
问题描述 使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token. 如要了解如何从AAD中获取 client id,client secret,tenant id ...
- 通过Python查看Azure VM的状态
Azure的管理平台采用Restful API的方式实现管理.比如获取VM的管理API的各种操作的文档请参考: https://docs.microsoft.com/en-us/rest/api/co ...
- 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据
问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...
- 【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
问题描述 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connecti ...
- 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...
- 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例
什么是表存储 Azure 表存储是一项用于在云中存储结构化 NoSQL 数据的服务,通过无结构化的设计提供键/属性存储. 因为表存储无固定的数据结构要求,因此可以很容易地随着应用程序需求的发展使数据适 ...
- 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...
- 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...
- 【Azure Developer】使用Microsoft Graph API 批量创建用户,先后遇见的三个错误及解决办法
问题描述 在先前的一篇博文中,介绍了如何使用Microsoft Graph API来创建Azure AD用户(博文参考:[Azure Developer]使用Microsoft Graph API 如 ...
- 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID
问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...
随机推荐
- Notepad++ 显示空格
公司里面用yaml 文件经常会出现一些奇怪的问题... 今天又遇到了//全角空格 显示的长度一样 但是实际上 yaml文件解析的不对..notepad++ ---> 视图--->显示符号- ...
- 【JS 逆向百例】转变思路,少走弯路,X米加密分析
声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 逆向目标 目标:X米账号登 ...
- Vue基础系统文章06---导入和导出
一.导入和导出 如果想要在一个Js文件中用另一个js文件的代码 1.将js文件中的变量和函数导出 let a = "aaaa" function show() { console. ...
- 根据pdf模板文件添加数据生成新的pdf与pdf添加读取二维码
参考文档 :https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html 程序demo 地址:https://github.com/hudean/itex ...
- 剪粘板增强小工具(可多次Ctrl+V)
前言 windows的剪贴板中存储是的最新一次的复制结果,比如先复制A,再复制B,C,在按下粘贴键时粘贴的是最后一次的结果C,在工作时有时候会遇到需要多次复制粘贴的情景,我就在思考有没有一款工具可以保 ...
- 基数排序|RadixSort|C++实现
前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量干货博客汇总https://blog. ...
- h5st 4.3版本代码研究
背景介绍 最近比较悠闲,于是没事研究了一下某东的h5st代码,2024年新鲜出炉的前端加密代码: 最大的惊喜并不是算法的复杂,在逆向破解代码的过程中,对js加密混淆有了新的认识: 于是心血来潮,回到这 ...
- 在OpenGL中使用Dear ImGui
在众多GUI库中,Dear ImGui用起来最简单,它很容易集成到程序中,绘制的窗口看起来也还不错.可以用它画出非常炫酷的GUI界面: 而我则不同:无论使用哪个GUI库,画出来的窗口都惨不忍睹.下面简 ...
- ElasticSearch7.3学习(十二)----type底层结构及弃用原因
1.type是什么 type,是一个index中用来区分类似的数据的.类似的数据,但是可能有不同的fields,而且有不同的属性来控制索引建立.分词器.field的value. 在底层的lucene中 ...
- RabbitMQ基础学习Full版
RabbitMQ 消息队列在软件中的应用场景 异步处理上(优于原先的方式) 为什么优于呢? 首先,通常情况下,如上图我们其实不用消息队列的情况下,其实也可以不用100ms,不用allof即可 那么优势 ...