gpt4 book ai didi

python - 使用 Python 从 Azure Monitor 获取特定警报

转载 作者:行者123 更新时间:2023-12-03 06:14:13 25 4
gpt4 key购买 nike

我想使用 python(通过 Azure 函数)从 Azure Monitor 获取特定警报。Azure Monitor 将为每个事件触发 az 函数。

目前我正在使用get_all从 azure.mgmt.alertsmanagement.operations 模块,这使我能够获取所有警报。也已经测试了 get_by_id,但当我希望自动获取它时,我不得不指定alert_id。

import logging
import urllib3
import os
import json
import requests
from azure.identity import ClientSecretCredential
from azure.mgmt.alertsmanagement import AlertsManagementClient


subscription_id =""
client_id =""
client_secret =""
tenant_id = ""

credential = ClientSecretCredential(
tenant_id=tenant_id,
client_id=client_id,
client_secret=client_secret
)

print("===Auth Azure Monitor===")
client = AlertsManagementClient(
credential,
subscription_id
)

print("=== Get alert event from Az Monitor & Post it to monitoring platform === ")
headers = {'Authorization': f'authtoken {token}'}

for alert in client.alerts.get_all():
if alert.name == "alert_rule_name" :
attributes = {'CLASS': 'EVENT',
'severity': 'CRITICAL',
'msg': alert.name,
'lastModifiedDateTime': json.dumps(alert.properties.essentials.last_modified_date_time, indent=4, sort_keys=True, default=str)
}
payload = [{'eventSourceHostName': alert.properties.essentials.target_resource_name, 'attributes': attributes}]
print("JSON_PAYLOAD :", payload)
## Some code here to push the Alert to a monitoring platform ..

请在下面找到 Azure Monitor 使用 get_all 发送的 json:

{'value': [{'properties': {'essentials': {
'severity': 'Sev2',
'signalType': 'Metric',
'alertState': 'New',
'monitorCondition': 'Fired',
'monitorService': 'Platform',
'targetResource': '/subscriptions/sub_id/resourcegroups/rsg_name/providers/microsoft.compute/virtualmachines/vm_name',
'targetResourceName': 'vm_name',
'targetResourceGroup': 'rsg_name',
'targetResourceType': 'virtualmachines',
'sourceCreatedId': '5f33r_rsg_name_microsoft.insights_metricAlerts_alert_rule_name-1899618006',
'alertRule': '/subscriptions/sub_id/resourceGroups/rsg_name/providers/microsoft.insights/metricAlerts/alert_rule_name',
'startDateTime': '2023-05-09T13:32:28.1880147Z',
'lastModifiedDateTime': '2023-05-09T13:32:28.1880147Z',
'lastModifiedUserName': 'System',
'actionStatus': {'isSuppressed': False}, 'description': ''}
},
'id': '/subscriptions/sub_id/providers/Microsoft.AlertsManagement/alerts/2222-5555-88888',
'type': 'Microsoft.AlertsManagement/alerts',
'name': 'alert_rule_name'},

如您所见,我正在按 [if alert.name == "alert_rule_name"] 进行过滤,但这不是我要查找的内容(我得到了事件列表)。

当 Azure Monitor 调用我的函数时,有没有办法从负载中获取警报 ID?这是利用这个ID来获取特定的alert(事件)。

提前致谢

最佳答案

Is there a way to get the alert ID from the payload when Azure Monitor

您可以使用以下代码通过 python 获取带有有效负载的警报 ID。

您需要在属性中添加 alert.id 才能获取特定警报的警报 ID。

代码:

import os
import json
import requests
from azure.identity import DefaultAzureCredential
from azure.mgmt.alertsmanagement import AlertsManagementClient


subscription_id ="your subscription id"
client_id =""
client_secret =""
tenant_id = ""

credential = ClientSecretCredential(
tenant_id=tenant_id,
client_id=client_id,
client_secret=client_secret
)


print("===Auth Azure Monitor===")
client = AlertsManagementClient(
credential,
subscription_id
)
print("=== Get alert event from Az Monitor & Post it to monitoring platform === ")

for alert in client.alerts.get_all():
if alert.name == "Backup Failure" :
attributes = {'CLASS': 'EVENT',
'severity': 'CRITICAL',
'msg': alert.name,
'id': alert.id,
'lastModifiedDateTime': json.dumps(alert.properties.essentials.last_modified_date_time, indent=4, sort_keys=True, default=str)
}

payload = [{'eventSourceHostName': alert.properties.essentials.target_resource_name,'attributes': attributes}]
print("JSON_PAYLOAD :", payload)

输出:

===Auth Azure Monitor===
=== Get alert event from Az Monitor & Post it to monitoring platform ===
JSON_PAYLOAD : [{'eventSourceHostName': 'mm-automation-runas-account-2', 'attributes': {'CLASS': 'EVENT', 'severity': 'CRITICAL', 'msg': 'aa-test-1', 'id': '/subscriptions/bxxxxf/resourcegroups/management_migration-resources/providers/microsoft.automation/automationaccounts/mm-automation-runas-account-2/providers/Microsoft.AlertsManagement/alerts/3f481155-b808-a188-6exxxxxx', 'lastModifiedDateTime': '"2023-06-14 05:35:14.747028+00:00"'}}]
JSON_PAYLOAD : [{'eventSourceHostName': 'mm-automation-runas-account-2', 'attributes': {'CLASS': 'EVENT', 'severity': 'CRITICAL', 'msg': 'aa-test-1', 'id': '/subscriptions/bxxxxxf/resourcegroups/management_migration-resources/providers/microsoft.automation/automationaccounts/mm-automation-runas-account-2/providers/Microsoft.AlertsManagement/alerts/8cba3e70-c957-4xxxxxxxx', 'lastModifiedDateTime': '"2023-06-13 12:35:13.840749+00:00"'}}]

enter image description here

关于python - 使用 Python 从 Azure Monitor 获取特定警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76437598/

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