Skip to main content
 首页 » 编程设计

python之如何验证你在 Azure 上是否有足够的资源

2025年05月04日25bjzhanghao

背景

如果这看起来像是一个信息不完整的问题,那确实是。那是因为它与一个大型项目有关,该项目使用 Azure 来自动配置和缩放和缩减大量 VM,该项目由第三方移交给我们,我们在理解一些问题时遇到了困难。但我会尽力解释

我们正在使用 python 的 Azure SDK启动 Azure 虚拟机,杀死它们等。请参阅启动 azure 虚拟机的方法中使用的代码:

    #Get VMs info. do it via minimum calls to speed up things 
    started_on = time.time() 
    while True: 
        try: 
            time.sleep(5) 
            vm = compute_client.virtual_machines.get(self.resource_group, vm_name) 
            break 

问题

有时当我们运行这个命令时,我们会得到这个错误:

aioc.logic.connectors.azure:2017-08-17 08:39:45,709 | ERROR | Stop waiting for VM auto-acfinH-25 to finish 
Traceback (most recent call last): 
  File "/home/aioc/aioc/aioc/logic/connectors/azure.py", line 126, in start 
    vm = compute_client.virtual_machines.get(self.resource_group, vm_name) 
  File "/home/aioc/venv/lib/python3.4/site-packages/azure/mgmt/compute/compute/v2016_04_30_preview/operations/virtual_machines_operations.py", line 369, in get 
    raise exp 
msrestazure.azure_exceptions.CloudError: Azure Error: ResourceNotFound 
Message: The Resource 'Microsoft.Compute/virtualMachines/auto-acfinH-25' under resource group 'AIOCBot' was not found. 
aioc.logic.main_controller_logic:2017-08-17 08:39:45,978 | ERROR | An error occurred while checking Vm with id '599553cdc1462e3a828c66da' machine id '328' 
Traceback (most recent call last): 
  File "/home/aioc/aioc/aioc/logic/main_controller_logic.py", line 41, in run_vm_controller 
    started_vms = vmc.start(vm.machine_type, 1) 
  File "/home/aioc/aioc/aioc/logic/connectors/azure.py", line 135, in start 
    vm_net_interface = network_client.network_interfaces.get(self.resource_group, vm_name) 
  File "/home/aioc/venv/lib/python3.4/site-packages/azure/mgmt/network/v2017_03_01/operations/network_interfaces_operations.py", line 171, in get 
    raise exp 
msrestazure.azure_exceptions.CloudError: Azure Error: ResourceNotFound 

经调查,事实证明这是因为我们在 azure 的资源已用尽(?)。解决此问题的方法是使用以下方法清除上述资源:

def cleanup_all(self): 
    """ 
    Clean up all auto-created resources 
    """ 
    compute_client = ComputeManagementClient(self.credentials, self.subscription_id) 
    network_client = NetworkManagementClient(self.credentials, self.subscription_id) 
    resource_client = ResourceManagementClient(self.credentials, self.subscription_id) 
 
    l = resource_client.resources.list() 
    for r in [r for r in l if r.name.startswith('auto-')]: 
        try: 
            if 'publicIPAddresses' in r.type: 
                rs = network_client.public_ip_addresses.delete(self.resource_group, r.name) 
                rs.wait() 
            elif 'Microsoft.Network' in r.type:  
                rs = network_client.network_interfaces.delete(self.resource_group, r.name) 
                rs.wait() 
            elif 'Microsoft.Compute/virtualMachines' in r.type: 
                rs = compute_client.virtual_machines.delete(self.resource_group, r.name) 
                rs.wait() 
        except: 
            log.warn("Failed to stop resource: %s with type: %s", r.name, r.type, exc_info=1) 

这一切都很棒。但是,出于商业原因,我们不能简单地 - 创建一个定期运行此命令的 cron 作业 - 不能以任何自动化方式运行它 b/c 它会同时影响许多不同的环境(即 prod/demo/stage/dev),这是一个太大的副作用,无法理解。

这意味着我们必须定期运行此命令,每隔一段时间,一旦我们同意所有环境都已清除并准备就绪。

问题

我想看看我的 Azure 控制台中的资源部分

并有一种方法 来找出我消耗了多少允许的 资源。我需要有一个想法,例如:哦,顺便说一句,你已经消耗了你允许的公共(public) ips 的 45% 等,这样我就知道我是否安全或者我是否需要再次运行清除命令。

想法?

更新

This page详细讨论了可用的限制,例如:

但它没有讨论如何衡量这些资源中有多少当前正在使用或剩余多少..这就是我想要找出的内容

谁能解释一下这是怎么回事?

请您参考如下方法:

您问题的部分答案。对于手册,您可以在 Azure 门户本身上找到此信息。点击“订阅”,然后从订阅列表中选择您的订阅,然后选择“使用量 + 配额”。