问题描述

在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的更多相关文章

  1. 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式

    问题描述 使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token. 如要了解如何从AAD中获取 client id,client secret,tenant id ...

  2. 通过Python查看Azure VM的状态

    Azure的管理平台采用Restful API的方式实现管理.比如获取VM的管理API的各种操作的文档请参考: https://docs.microsoft.com/en-us/rest/api/co ...

  3. 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据

    问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...

  4. 【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"

    问题描述 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connecti ...

  5. 【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, ...

  6. 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例

    什么是表存储 Azure 表存储是一项用于在云中存储结构化 NoSQL 数据的服务,通过无结构化的设计提供键/属性存储. 因为表存储无固定的数据结构要求,因此可以很容易地随着应用程序需求的发展使数据适 ...

  7. 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例

    问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...

  8. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

  9. 【Azure Developer】使用Microsoft Graph API 批量创建用户,先后遇见的三个错误及解决办法

    问题描述 在先前的一篇博文中,介绍了如何使用Microsoft Graph API来创建Azure AD用户(博文参考:[Azure Developer]使用Microsoft Graph API 如 ...

  10. 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID

    问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...

随机推荐

  1. 将自签名创建的ca证书 添加到linux的授信证书列表的办法

    第一步: 将ca 证书 从cert 格式转换成pem格式 openssl x509 -in ca.crt -out ca.pem -outform PE 第二步: 将ca 证书导入至系统中来 cat ...

  2. 初识VUE响应式原理

    作者:京东零售 吴静 自从Vue发布以来,就受到了广大开发人员的青睐,提到Vue,我们首先想到的就是Vue的响应式系统,那响应式系统到底是怎么回事呢?接下来我就给大家简单介绍一下Vue中的响应式原理. ...

  3. 2020第二届长安杯wp

    1 检材 1 的操作系统版本是 A. CentOS release 6.5 (Final) B. Ubuntu 16.04.3 LTS C. Debian GNU/Linux 7.8 (wheezy) ...

  4. P7031 [NWRRC2016] Anniversary Cake

    题目简述 有一块 \(n \times m\) 的长方形蛋糕.蛋糕上有两个蜡烛,分别用 \((x_1,y_1)\) 和 \((x_2,y_2)\) 表示.现在有一把刀要把蛋糕切成两半,请问切入的终点和 ...

  5. 基于文心大模型套件ERNIEKit实现文本匹配算法,模块化方便应用落地

    文心大模型,产业级知识增强大模型介绍 官网:https://wenxin.baidu.com/ 项目链接见文末 文心大模型开发套件ERNIEKit,面向NLP工程师,提供全流程大模型开发与部署工具集, ...

  6. 19.0 Boost 基于ASIO网络编程技术

    Boost ASIO库是一个基于C++语言的开源网络编程库,该库提供了成熟.高效.跨平台的网络API接口,并同时支持同步与异步两种模式,ASIO库提供了多重I/O对象.异步定时器.可执行队列.信号操作 ...

  7. 【操作系统和计网从入门到深入】(四)基础IO和文件系统

    前言 这个专栏其实是博主在复习操作系统和计算机网络时候的笔记,所以如果是博主比较熟悉的知识点,博主可能就直接跳过了,但是所有重要的知识点,在这个专栏里面都会提到!而且我也一定会保证这个专栏知识点的完整 ...

  8. MarkDown书写语法(常用格式)

    实际上每个 Markdown 应用程序都实现了稍有不同的 Markdown 语法,熟悉MarkDown书写语法常用格式,满足日常文字编辑需求 1.标题 请在单词或短语前面添加井号 (#) .# 的数量 ...

  9. Spring Boot 参数校验注解(自整理,不停的测试更新)

    首先我们只使用java官方的 javax.validation.constraints ,足以使用了,不使用spring boot 自身的,自身的与官方的一致,可能会有扩展,但是还得引入包,麻烦,只用 ...

  10. AT_arc125_c [ARC125C] LIS to Original Sequence 题解

    题目传送门 前置知识 贪心 | 构造 解法 对于任意一个未加入序列 \(P\) 的数 \(x<A_{i}(1 \le i \le k-1)\),如果其放在了 \(A_{i}\) 的前面,会导致最 ...