Automating VM Reconfiguration with Zabbix Upon Azure Spot VM Eviction

KindCompute-6524 95 Reputation points
2024-07-21T04:29:03.53+00:00

Hello community,

I am looking for a method and example code to modify a virtual machine's properties monitored by Zabbix when a specific event occurs. For instance, if a spot virtual machine on Azure is evicted, Zabbix should detect the unavailability, execute a script to change the VM size to another type or size with a lower eviction rate, and then automatically restart the VM. Could you please share your ideas, advice, or sample code?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
38,521 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Udayashankar K.N 80 Reputation points Microsoft Employee
    2024-07-22T05:21:23.2666667+00:00

    hi

    Just see the below if it helps and make the below as a executable one .

    import os

    from azure.identity import DefaultAzureCredential

    from azure.mgmt.compute import ComputeManagementClient

    from datetime import datetime, timedelta

    subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', '<your-subscription-id>')

    resource_group_name = '<your-resource-group-name>'

    vm_name = '<your-vm-name>'

    credential = DefaultAzureCredential()

    compute_client = ComputeManagementClient(credential, subscription_id)

    def check_spot_vm_eviction():

    try:
    
        vm_instance_view = compute_client.virtual_machines.instance_view(resource_group_name, vm_name)
    
        for status in vm_instance_view.statuses:
    
            if status.code == 'ProvisioningState/succeeded' and 'eviction' in status.display_status.lower():
    
                return 1
    
        return 0
    
    except Exception as e:
    
        print(f"Error checking Spot VM eviction: {e}")
    
        return -1
    

    if name == "main":

    eviction_status = check_spot_vm_eviction()
    
    print(eviction_status)
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.