gpt4 book ai didi

python - 使用 Python 获取 Azure Monitor 中订阅的所有事件警报

转载 作者:行者123 更新时间:2023-12-03 01:29:31 26 4
gpt4 key购买 nike

我只需要使用 Python 从 Azure 监视器获取订阅中所有资源的所有事件警报。
出于同样的目的,可以使用 REST API,check this .
我查过this ,但它提供警报/指标定义,而不是警报本身。
使用 Azure python SDK 是否可以使用类似的东西?
如果有人可以提供一些见解,将会很有帮助。提前致谢。

最佳答案

这是doc提供的功能。但它返回这个: enter image description here

新版本似乎没有使用。

get_all用于列出所有现有警报。它返回一个分页容器,用于迭代 Alert 对象列表。

安装包:pip install azure-mgmt-alertsmanagement

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.alertsmanagement import AlertsManagementClient

subscription_id = 'subscription_id '
tenant_id = 'tenant_id '
client_id = 'client_id '
client_secret = 'client_secret'

credentials = ServicePrincipalCredentials(tenant=tenant_id, client_id=client_id, secret=client_secret)

client = AlertsManagementClient(
credentials,
subscription_id
)

for alert in client.alerts.get_all():
print((alert.name))
<小时/>

所以,我尝试调用 REST API使用Python。它有效。

import requests
import json

client_id = ''
client_secret = ''
subscription_id = ''
tenant_id = ''

# authorize with azure
url = "https://login.microsoftonline.com/" + tenant_id + "/oauth2/v2.0/token"
data = "scope=https%3A%2F%2Fmanagement.azure.com%2F.default&client_id=" + client_id + "&grant_type=client_credentials&client_secret=" + client_secret
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(url, data=data, headers=headers)

# create new resource group using Azure REST API
# https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.AlertsManagement/alerts?api-version=2018-05-05
url = "https://management.azure.com/subscriptions/" + subscription_id + "/providers/Microsoft.AlertsManagement/alerts?api-version=2018-05-05"
headers = { 'Authorization': 'Bearer ' + response.json()['access_token']}
response = requests.get(url, headers=headers)

print(response.json())

enter image description here

关于python - 使用 Python 获取 Azure Monitor 中订阅的所有事件警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63611534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com