使用Python监视Azure虚拟机规模集(VMSS)中的实例

时间:2018-07-25 18:55:50

标签: azure azure-virtual-machine autoscaling azure-monitoring azure-autoscaling-block

我想监视并获取有关Azure虚拟机规模集(VMSS)中不同实例的信息。

我使用了命令(Python):

vmss = compute_client.virtual_machine_scale_sets.list(资源组,scale_set_name)

但是我无法获得预期的结果。

任何建议怎么办?

3 个答案:

答案 0 :(得分:0)

如果要获取VM信息,请使用以下代码。

subscription_id = 'subscription Id'
credentials = ServicePrincipalCredentials(client_id=CLIENT, secret=KEY, tenant=TENANT_ID)
client = ComputeManagementClient(credentials, subscription_id)
vmss = client.virtual_machine_scale_set_vms.list("resourcegroup Name","VMSS name")
for item in vmss:  
    print("id:",item.id)
    print("name",item.name)

测试结果:

enter image description here

答案 1 :(得分:0)

您可以使用以下代码获取ip和powerstate。

compute_client = ComputeManagementClient(credentials, subscription_id)
vmss = compute_client.virtual_machine_scale_set_vms.list(resource_group_name="", vmss="")
for item in vmss:
    print("name: ", item.name)
    ni_reference = item.network_profile.network_interfaces[0].id
    resource_client = ResourceManagementClient(credentials, subscription_id)
    nic = resource_client.resources.get_by_id(
        ni_reference,
        api_version='2017-12-01')
    ip_reference = nic.properties['ipConfigurations'][0]['properties']
    print("ip info: ", ip_reference)

    instance_view = compute_client.virtual_machine_scale_set_vms.get_instance_view(resource_group_name="", vmss="", instance_id=item.instance_id)
    print(instance_view.statuses[1].code)

结果:

name:  yangtestvmss_1
ip info:  {'provisioningState': 'Succeeded', 'privateIPAddress': '10.0.0.5', 'privateIPAllocationMethod': 'Dynamic', 'subnet': {'id': '/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/yangtestvmss/providers/Microsoft.Network/virtualNetworks/yangtestvmssVnet/subnets/default'}, 'primary': True, 'privateIPAddressVersion': 'IPv4', 'isInUseWithService': False}
PowerState/running
name:  yangtestvmss_3
ip info:  {'provisioningState': 'Succeeded', 'privateIPAddress': '10.0.0.7', 'privateIPAllocationMethod': 'Dynamic', 'subnet': {'id': '/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/yangtestvmss/providers/Microsoft.Network/virtualNetworks/yangtestvmssVnet/subnets/default'}, 'primary': True, 'privateIPAddressVersion': 'IPv4', 'isInUseWithService': False}
PowerState/running

答案 2 :(得分:0)

有一个很酷的工具,来自Microsoft的家伙已经构建出来,可以监视VMSS 看到此链接VMSS Dashboard

提到的工具可帮助您查看规模集中的VM状态:您可以查看VM的更新域和故障域分组。它使您可以启动或取消分配VM。该代码已使用两年多了。