I follow this example from microsoft to get a list of virtual machines: https://github.com/Azure-Samples/virtual-machines-python-manage/blob/master/example.py
My code:
from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
def get_credentials():
subscription_id = AZURE_SUBSCRIPTION_ID
credentials = ServicePrincipalCredentials(
client_id=AZURE_CLIENT_ID,
secret=AZURE_CLIENT_SECRET,
tenant=AZURE_TENANT_ID
)
return credentials, subscription_id
credentials, subscription_id = get_credentials()
compute_client = ComputeManagementClient(credentials, subscription_id)
for vm in compute_client.virtual_machines.list_all():
print(vm)
This works fine and return something like this:
{‘additional_properties’: {}, ‘id’: ‘/subscriptions/17bf586e-6072-4e5f-900d-90467e227f73/resourceGroups/VPN-2-IKSI/providers/Microsoft.Compute/virtualMachines/vpntest02’, ‘name’: ‘vpntest02’, ‘type’: ‘Microsoft.Compute/virtualMachines’, ‘location’: ‘southcentralus’, ‘tags’: None, ‘plan’: None, ‘hardware_profile’: , ‘storage_profile’: , ‘additional_capabilities’: None, ‘os_profile’: , ‘network_profile’: , ‘diagnostics_profile’: , ‘availability_set’: None, ‘provisioning_state’: ‘Succeeded’, ‘instance_view’: None, ‘license_type’: None, ‘vm_id’: ‘8c246fff-22ab-4bd7-9f00-708f3b6e60b3’, ‘resources’: None, ‘identity’: None, ‘zones’: None}
But i need the OS name (like ubuntu, or centos, etc.), total disk, total ram, cpu usage, i find in azure sdk docs but is very complicated (the documentation it’s sucks), Has someone done something similar? how they did it? some link with examples or with a decent documentation
2
Answers
For your issue, you can get the VM OS name from the image reference, it will show the image which it has used. For the total disk, you just can get the disk name and then you can use the disk SDK to get the whole details. For the total ram, you just can get the VM size and find the details in the VM size. And the CPU usage, you also cannot get it from the VM information. To get it, you need to use the Azure Monitor SDK.
Which you can get about the VM from the SDK all shows in VirtualMachine class. For more details about something associated with the VM, such as the disk, you need to use other SDK, just like Managed Disk.
Retrieving OS type image reference will work if you use Azure images, so 99% of the time. But if you have VMs that are moved from other clouds, that won’t work.