【问题标题】:Activity log for specific azure resource特定 Azure 资源的活动日志
【发布时间】:2022-11-02 18:56:45
【问题描述】:

我正在尝试查询特定天蓝色资源的活动日志。但是,我不知道该怎么做。我只在互联网上找到了只能过滤到资源组级别的基本代码。

    from azure.mgmt.monitor import MonitorManagementClient
    import datetime

    # Get a client for Monitor
    credentials = connectSP() # Custom function to get credentials
    client = MonitorManagementClient(
        credentials,
        sub_id
    )


    # Generate query here
    today = datetime.datetime.now().date()
    filter = "eventTimestamp ge {}".format(today)
    select = ",".join([
        "eventTimestamp",
        "eventName",
        "operationName",
        "resourceGroupName",
    ])


    # Grab activity logs
    activity_logs = client.activity_logs.list(
        filter=filter,
        select=select
    )

    # Print the logs
    for log in activity_logs:
        print(" ".join([
            str(log.event_timestamp),
            str(log.resource_group_name),
            log.event_name.localized_value,
            log.operation_name.localized_value
    ]))

我试图通过 resource_id 属性过滤它,但遇到了这个错误:

Code: BadRequest
Message: The filter property: resource_id is not supported.

是否可以将范围缩小到一个资源?还有关于如何修改过滤器查询的任何文档吗?我刚刚在 Microsoft 文档中找到了基本的。 https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2015_04_01.operations.activitylogsoperations?view=azure-python

【问题讨论】:

  • 尝试按 resourceUri 过滤。 filter = "eventTimestamp ge {} and resourceUri eq {}".format(today, resourceUri)

标签: python azure activitylog


【解决方案1】:

代码:错误请求 消息:过滤器属性:resource_id 不受支持。

该错误表明 ResourceID 在$过滤器争论。

$filter有限制请参阅MS-Doc 了解详细信息。

$过滤器参数非常受限制,只允许以下模式。

  1. 列出资源组的事件:
    $filter=eventTimestamp ge '<Start Date Time>' and eventTimestamp le '<End time>' and resourceGroupName eq '<Resource Group Name>'.
    
    1. 列出资源的事件:
    $filter=eventTimestamp ge '<Start Date Time>' and eventTimestamp le '<End Date Time>' and resourceUri eq '<Resource URI>'.
    
    1. 列出时间范围内的订阅事件:
    $filter=eventTimestamp ge '<Start Date Time>' and eventTimestamp le '<End Date Time>'.
    
    1. 列出资源提供者的事件:
    $filter=eventTimestamp ge '<Start Date time>' and eventTimestamp le '<End Date Time>' and resourceProvider eq 'resourceProviderName'.
    
    1. 列出相关 ID 的事件:
    $filter=eventTimestamp ge '<Start Date Time>' and eventTimestamp le '<End Date Time>' and correlationId eq 'correlationID'.`
    

    如果需要过滤特定资源,可以使用资源 URI.通过资源组过滤使用资源组名. $filter 不支持资源 ID。

    我遵循的示例代码。

    Sub_Base_Url = f"https://management.azure.com/subscriptions/{Your_Subscription_Id}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&"
    # in filter you can use either Resource Group Name or Resource URI
    filter = f"$filter=eventTimestamp ge '{Start_Date_time}' and eventTimestamp le '{End_Date_Time}' and resourceGroupName eq '{Your_Resource_Group}'"
    print(filter)
    
    Sub_Base_Url = Sub_Base_Url + filter
    
    headers = {
        "Authorization": 'Bearer ' + credential.token["access_token"]
    }
    
    res = requests.get(Sub_Base_Url, headers=headers)
    output = res.json()
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多