【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接口. 如果是直接调 ...
随机推荐
- Linux 内核参数
/proc/sys/net/ipv4: ip_local_port_range:定义了TCP或UDP对目标发起连接所选择的本地端口范围(除ip_local_reserved_ports之外),其定义受 ...
- 基于javaPoet的缓存key优化实践
一. 背景 在一次系统opsreview中,发现了一些服务配置了@Cacheable注解.@cacheable 来源于spring cache框架中,作用是使用aop的方式将数据库中的热数据缓存在re ...
- git查看自己是从那个分支建的分支
可能发生的情况 很多时候,开始建分支的时候, 能够确认自己是那个分支建的,但是当写完功能之后, 再去回想,有时是忘记自己基于那个分支建的分支. 这时有一个命令的话就可以很快的定位了. 查看创建的分支来 ...
- 玩一玩 golang 1.21 的 pgo 编译优化
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 1.下载镜像 暂时不想替换本机的 golang 版本,于是 ...
- Flask 实现简单的图片上传
通过使用Flask框架实现一个简单的图片上传工具,当用户提交图片后会自动将图片保存到upload目录下,代码如下 import os from flask import Flask, request ...
- maven打包报错
* 系统:macOS* 开发工具:Idea* 问题描述:在idea中执行mvn clean install时报No compiler is provided in this environment. ...
- Asp.net平台常用的框架整理
分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架. Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度. Redis:是一个高性能的K ...
- workman在线五子棋
一.下载安装workman,地址:https://github.com/walkor/workerman composer require workerman/workerman 二.cd到worke ...
- Flutter聊天室|dart+flutter仿微信App界面|flutter聊天实例
一.项目概述 flutter-chatroom是采用基于flutter+dart+chewie+image_picker+photo_view等技术跨端开发仿微信app界面聊天室项目.实现了消息发送/ ...
- AI热点概念解读:一文搞懂这些热词
自 ChatGPT 问世以来,AI的风口就来了. AI是一门研究如何使计算机具有类似人类智能的学科. 自从ChatGPT-3.5给大家带来了极大的震惊之后,全民都在谈论AI,在这个AI大时代背景之下, ...