有没有办法使用azure的python-sdk在azure中创建资源的时间?

时间:2018-02-10 11:58:50

标签: python azure

我正在尝试编写一个python脚本来获取azure中超过30天的资源列表。而且我无法找到任何可以告诉我创建该特定实例的时间的模块或包,例如创建它时的vm的时间戳。

1 个答案:

答案 0 :(得分:0)

import os
import datetime
from pprint import pprint
from azure.monitor import MonitorClient
from azure.common.credentials import ServicePrincipalCredentials

subscription_id = '***'
client_id='***'
secret='***'
tenant='***'

today = datetime.datetime.now().date()
filter = " and ".join([ "eventTimestamp le '{}T00:00:00Z'".format(today), "resourceGroupName eq 'jay'" ])

credentials = ServicePrincipalCredentials(client_id=client_id, secret=secret, tenant=tenant)

client = MonitorClient(credentials, subscription_id)
select = ",".join([ "eventName", "operationName" ])

print select
print filter
activity_logs = client.activity_logs.list( filter=filter, select=select )

for log in activity_logs:
    # assert isinstance(log, azure.monitor.models.EventData)
    print(" ".join([
        log.event_name.localized_value,
        log.operation_name.localized_value
    ]))

重新评估A zure Monitor Python SDK之后,我发现了差异。 这是ge,而不是le。

修改代码中的关键字(ge,not le)

eventName,operationName
eventTimestamp ge '2017-10-17T00:00:00Z' and resourceGroupName eq 'jay'
End request Microsoft.Compute/virtualMachines/delete
End request Microsoft.Compute/virtualMachines/delete
End request Microsoft.Compute/virtualMachines/delete
Begin request Microsoft.Compute/virtualMachines/delete
End request Microsoft.Compute/virtualMachines/deallocate/action
End request Microsoft.Compute/virtualMachines/deallocate/action
Begin request Microsoft.Compute/virtualMachines/deallocate/action
End request Microsoft.Compute/virtualMachines/write
End request Microsoft.Compute/disks/write
End request Microsoft.Compute/virtualMachines/write
End request Microsoft.Network/networkSecurityGroups/write
End request Microsoft.Network/networkInterfaces/write
End request Microsoft.Network/publicIPAddresses/write
根据文档,您的日期似乎应该被转义。此外,他们似乎需要一个日期时间(而不是日期):https://docs.microsoft.com/en-us/rest/api/monitor/activitylogs

有关其他信息:not getting azure vm metric data values

https://www.microsoft.com/en-us/research/wp-content/uploads/2016/04/building-scalable-services-in-microsoft-azure-with-python.pdf