• 【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

  • 相关阅读:
    ORM是什么?及ORM框架是什么?
    Spring与其两大核心
    装箱和拆箱
    ==和equals的比较
    Vue中ESlint配置文件eslintrc.js文件详解
    RESTful API规范
    CORS跨域djangosetting.py 配置
    LDAP
    模拟浏览器发送请求报文
    学HTTP协议所要知道的基础知识(微总结)
  • 原文地址:https://www.cnblogs.com/lulight/p/16187610.html
Copyright © 2020-2023  润新知