I am trying to query the activity logs of a specific azure resource. However, I am not sure how to do it. I only found base code on the internet that can only filter up to resource group level.
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
]))
I tried to filter it by resource_id attribute but is met with this error:
Code: BadRequest
Message: The filter property: resource_id is not supported.
Is it possible to narrow down the scope to just a resource? Also is there any documentation on how to modify the filter query? I just found the basic ones in the Microsoft documentation. https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2015_04_01.operations.activitylogsoperations?view=azure-python
The error shows that ResourceID is not supported in the $filter argument.
The
$filterhas restrictions refer MS-Doc for detailed information.The $filter argument is very restricted and allows only the following patterns.
If you need to filter specific resource, you can use Resource URI. Filter through resource group Use ResourceGroupName. Resource ID is not supported in the $filter.
The sample code I followed.