Querying Activity Log via python sdk says, "$filter=eventTimestamp is not supported." error
- Package Name: monitor
- Package Version:
- Operating System: linux
- Python Version: 3.6
Describe the bug I am trying to read activity logs from portal and trying to dump into xls sheet while i am following the documentation provided for listing activity log. I am sorry how ever I try i am not able to format below code properly using code... :( .
d_date = datetime.datetime.now()
today = d_date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
print(today)
N = 10
date_N_days_ago = datetime.datetime.now() - timedelta(days=N)
start_date = date_N_days_ago.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
print(start_date)
for vm_general in vm_list:
general_view = vm_general.id.split("/")
resource_group = general_view[4]
BASE_URL = f"https://management.azure.com/subscriptions/{subscription_id}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge {start_date} and eventTimestamp le {today} and resourceGroupName eq {resource_group}&$select=eventName,id,resourceGroupName,resourceProviderName,operationName,status,eventTimestamp,correlationId,submissionTimestamp,level"
myfilter = f"$filter=eventTimestamp ge '{start_date}' and eventTimestamp le '{today}' and resourceGroupName eq '{resource_group}'"``
BASE_URL = BASE_URL + myfilter
headers = {
"Authorization": 'Bearer ' + credential.token["access_token"]
}
response = requests.get(BASE_URL, headers=headers)
However i am keep getting error saying $filter=eventTimestamp is not supported. Eventhough i am giving timezone format in correct format.. can some one help with full python code example to acheive this please...
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @SameergMS, @dadunl.
You should try to use the monitor SDK, and not a direct call to the service (this repo is about the SDK for Python). https://docs.microsoft.com/en-us/python/api/overview/azure/monitoring?view=azure-python
Though the RestAPI doc is here: https://docs.microsoft.com/en-us/rest/api/monitor/activitylogs/list#uri-parameters
And it seems the format should be:
The $filter argument is very restricted and allows only the following patterns.
- List events for a resource group: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceGroupName eq 'resourceGroupName'.
- List events for resource: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceUri eq 'resourceURI'.
- List events for a subscription in a time range: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z'.
- List events for a resource provider: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceProvider eq 'resourceProviderName'.
- List events for a correlation Id: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and correlationId eq 'correlationID'.
as mentioned, pls use monitor python sdk
You should try to use the monitor SDK, and not a direct call to the service (this repo is about the SDK for Python). https://docs.microsoft.com/en-us/python/api/overview/azure/monitoring?view=azure-python
Though the RestAPI doc is here: https://docs.microsoft.com/en-us/rest/api/monitor/activitylogs/list#uri-parameters
And it seems the format should be:
The $filter argument is very restricted and allows only the following patterns. - List events for a resource group: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceGroupName eq 'resourceGroupName'. - List events for resource: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceUri eq 'resourceURI'. - List events for a subscription in a time range: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z'. - List events for a resource provider: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceProvider eq 'resourceProviderName'. - List events for a correlation Id: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and correlationId eq 'correlationID'.
Hi @lmazuel @chenjianfei2017
I am using sdk only. here my code that i am using
from azure.monitor import MonitorClient
from azure.mgmt.monitor import MonitorManagementClient
......
.......
# i am getting keys dynmically substituted below
credential = ServicePrincipalCredentials(tenant=tenant_var, client_id=result[0], secret=result[1])
subscription_client = SubscriptionClient(credential)
subscription = next(subscription_client.subscriptions.list())
client = MonitorManagementClient(credential, subscription.subscription_id)
compute_client = ComputeManagementClient(credential, subscription.subscription_id)
subscription_id = subscription.subscription_id
d_date = datetime.datetime.now()
today = d_date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
print(today)
N = 10
date_N_days_ago = datetime.datetime.now() - timedelta(days=N)
start_date = date_N_days_ago.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
print(start_date)
vm_list = compute_client.virtual_machines.list_all()
for vm_general in vm_list:
general_view = vm_general.id.split("/")
resource_group = general_view[4]
BASE_URL = f"https://management.azure.com/subscriptions/{subscription_id}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&"
myfilter = f"$filter=eventTimestamp ge '{start_date}' and eventTimestamp le '{today}' and resourceGroupName eq '{resource_group}'"
print(myfilter)
BASE_URL = BASE_URL + myfilter
headers = {
"Authorization": 'Bearer ' + credential.token["access_token"]
}
response = requests.get(BASE_URL, headers=headers)
output = response.json()
print(output)
my filter looks like this: 2020-08-06T12:12:11.189195Z 2020-07-27T12:12:11.190139Z $filter=eventTimestamp ge '2020-07-27T12:12:11.190139Z' and eventTimestamp le '2020-08-06T12:12:11.189195Z' and resourceGroupName eq 'xxxxxxxxxxxx'
I am getting json output but its not filtering on date and also i am getting json which is not parasable. ? can you check from your end and let me know any correction in above code ?
Can you please give it a try on some use case and see if this should work properly ?
looks you may use MonitorManagementClient to send request, here is an example
def test_activity_log(self, resource_group):
# Need to freeze the date for the recorded tests
today = datetime.date(2016,11,17)
filter = " and ".join([
"eventTimestamp ge {}".format(today),
"eventChannels eq 'Admin, Operation'"
])
select = "eventName,operationName"
filter = filter.format(resource_group.name)
# self.client is:
# self.client = self.create_mgmt_client(
# azure.mgmt.monitor.MonitorManagementClient
# )
activity_logs = list(self.client.activity_logs.list(
filter=filter,
select=select
))
for log in activity_logs:
self.assertIsNotNone(log.event_name.localized_value)
self.assertIsNotNone(log.operation_name.localized_value)
Hi, we're sending this friendly reminder because we haven't heard back from you in a while. We need more information about this issue to help address it. Please be sure to give us your input within the next 7 days. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!