Tag Archives: Proactive HA

Tutorial: Part-2: How to simulate Proactive HA using pyVmomi?

In part-1, we learned “What is Proactive HA and how to configure Proactive HA using pyVmomi?”. To understand part-2, I highly recommend you to take a look at part-1. In this post, we will learn how to simulate a failure to see Proactive HA in action.

Before going further, let us review what we configured already in last post.
– We registered a health provider.
– Added entities to be monitored i.e. hosts inside cluster
– Verified above 2 steps.
– Enabled Proactive HA on cluster in automated mode and configured remediation.

After performing above steps, you might have observed that all of the hosts are showing one red icon. This red icon indicates that initial health status for hosts is unknown i.e. gray, which is absolutely expected since Proactive HA provider must initialize health status of all ESXi hosts for the first time. In order to initialize health status of all ESXi, first we need to set status to “green”. This is where Proactive HA API i.e. PostHealthUpdates() comes to rescue. In fact, the same API will help us simulate a “red” health status, which will make Proactive HA to take configured action. Let us take a look at below pyVmomi script, which will initialize ESXi hosts to “green” health status.

This script is available on my git-hub repo as well i.e. push_health_updates.py

from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import atexit
import ssl
import sys
import time
#Script to push meaningful health updates to Proactive HA 
s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode=ssl.CERT_NONE
si= SmartConnect(host="10.161.20.30", user="Administrator@vsphere.local", pwd="VMware#123",sslContext=s)
content=si.content

# Below method helps us to get MOR of the object (vim type) that we passed.
def get_obj(content, vimtype, name):
        obj = None
        container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
        for c in container.view:
                if name:
                        if c.name == name:
                                obj = c
                                break

        return obj

#Cluster where ProactiveHA is enabled, this parameter is NOT required for this script
cluster_name="ClusterProHA"
# initialize health status of below host, we need to repeat for all the hosts inside the cluster. 
host_name="10.160.20.15" 
host = get_obj(content,[vim.HostSystem],host_name)
# Managed object that exposes all the methods to configure & manage proactiveHA
heath_update_mgr=content.healthUpdateManager

#Provider ID for the registered provider
providerid="52 b5 17 d2 2f 46 7e 9b-5f 4e 1a 25 a3 db 49 85"
health_update=vim.HealthUpdate()
health_update.entity=host
health_update.healthUpdateInfoId="1000"
health_update.id="vThinkBeyondVM"
health_update.remediation =""
health_update.status="green"
#Below is the array of health updates
updates = [health_update]

#Method to post health updates for Proactive HA to consume
heath_update_mgr.PostHealthUpdates(providerid,updates)

#Disconnecting vCenter session
atexit.register(Disconnect, si)

We must understand line #32 to line #45.
Line #34: This is provider id for the health provider we registered in part 1 using QueryProviderName() API
Line #35: HealthUpdate is the data object to be passed to the Proactive HA API we are talking about i.e. PostHealthUpdates()
Line #36: This is the host object for which we wanted to set status to “green”. For the sake of simplicity, this script changes health status per host. You can either modify this script to have for all the hosts or can run it multiple times.
Line #37: This is the healthUpdateInfoId we passed while registering health provider i.e. register_provider.py
Line #38: This id can be any of our choice but it should be unique per health update being pushed.
Line #39: Note that remediation set here was blank. This is required when we are setting health updates as “green”.
Line #40: Here we set the health status as “green”. Other valid values are “yellow” and “red”.
Line #42: We can have multiple health updates in single API call.
Line #45: This is the API we are interested to call i.e. PostHealthUpdates().

Since we have initialized health status for all ESXi hosts to “green”, now is the time to simulate a fake health update so that Proactive HA will trigger “quarantine mode” on a host. We can use exactly the same script with little change to line #38,39 & 40. Note that simulating fake health update is only for educational purpose . Take a look at the code snippet for pushing “red”(fake) health status a host.

#Provider ID for the registered provider
providerid="52 b5 17 d2 2f 46 7e 9b-5f 4e 1a 25 a3 db 49 85"
health_update=vim.HealthUpdate()
health_update.entity=host
health_update.healthUpdateInfoId="1000"
health_update.id="vThink-BVM"
health_update.remediation ="Please replace power unit"
health_update.status="red"
#Below is the array of health updates
updates = [health_update]

#Method to post health updates for Proactive HA to consume
heath_update_mgr.PostHealthUpdates(providerid,updates)

Once I executed above script, I refreshed web client after 3-4 seconds and below is what I see. How cool is that?

Now you can set the host health status back to “green” the way did above, which will make host to exit from quarantine mode. Finally you can leverage my Part-1 scripts to disable Proactive HA on the cluster and un-register the fake health provider we registered initially. In addition to APIs we explored in part-1 & 2, there are few more utility APIs Proactive HA has exposed. I will leave them for your exercise. Let me know if you have any doubts/comments.

Further learning:

1. If you haven’t started with vSphere python SDK i.e. pyVmomi. Here is my tutorial post on the same.

2.For the sake of simplicity, I use to write individual API scripts & hard-code some parameters but I have a plan to consolidate above scripts to have good Proactive HA pyVmomi module. If you would like to contribute to module, please let me know, we can work together.