1

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

0

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

6 Comments

finally I was able to show the os i using this: virtual_machine.storage_profile.image_reference but i have a question: how can i get GROUP_NAME? in the example we use GROUP_NAME = 'azure-sample-group-virtual-machines' is static or how i get from my acount
what is group_name?
@AbelOlguinChavez What do you mean about the GROUP_NAME? The resource group name that the VM in?
@CharlesXu yep, in this moment i get from this string: 'id': '/subscriptions/17bf586e-6072-4e5f-900d-90467e227f73/resourceGroups/VPN-2-IKSI/providers/Microsoft.Compute/virtualMachines/vpntest02' is just between "resourceGroups" and "providers" in this case my resource_group_name is: VPN-2-IKSI , Is there any other way to get this?
@AbelOlguinChavez I'm afraid you cannot get the group name from the VM through python SDK. All the things you can get for the VM shows in the VirtualMachine class. The group name is a parameter when you get the VM, even you do not set it.
|
0
vm = compute_client.virtual_machines.get(group_name, vm_name)
os = vm.storage_profile.os_disk.os_type

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.