pythonazureazure-managed-disk

Find when disk became unattached?


I am trying to determine a way to delete unattached disks if they have been unattached for a specific period of time:

Pseudo code

if state = "unattached" & date_unattached > 7 days then DELETE

I currently have the following:

compute_client = ComputeManagementClient(credentials, subscription_id)

for disk in compute_client.disks.list():
    print(disk.as_dict())

However, the payload only gives me a date/time for when the disk was created.

I tried attaching the disk to a VM but the activity logs didn't seem to update (will keep monitoring). I was hoping the disk logs might give this information.

Is there a simple way to find when this disk became unattached?

I am also thinking maybe using a DB to track when the disk was first discovered and then count the number of days between the first time the script ran and the next if the disk state is still the same and get the difference. I would need to run it daily as a user may attach/detach regularly (edge case)


Solution

  • I tried attaching the disk to a VM but the activity logs didn't seem to update (will keep monitoring). I was hoping the disk logs might give this information.

    It will be in the log, but it will just appear as Create or Update Disk, you can just check the unattach disk related information in the portal, it is a preview feature. It is not available via the REST API/Python sdk(sdk also calls the rest api), so I think it is not a simple way to check it.

    enter image description here

    I am also thinking maybe using a DB to track when the disk was first discovered and then count the number of days between the first time the script ran and the next if the disk state is still the same and get the difference. I would need to run it daily as a user may attach/detach regularly (edge case)

    I think this way is feasible, check if the disk_state is Unattached daily with python sdk and count the number of days.