Automate Host Maintenance with the vCenter Event Broker Appliance

2 minute read

The vCenter Event Broker Appliance is a new VMware Fling and open source project that was recently released at VMworld Europe. Using OpenFaaS and its vCenter event connector functions can be run based on vCenter events. A preview of it was also presented at VMworld US which I had the pleasure of attending. If you haven’t seen either of these sessions I highly recommend watching the recording of #CODE1379E “If This Then That” for vSphere - The Power of Event Driven Automation session. I’m quite interested in this project and really excited to see all of the cool automation the community does with it.

If you haven’t deployed the appliance the Getting Started guide has all the details for getting it setup. Also be sure to join the VMware {Code} Slack channel. All of my work is based on the existing samples on GitHub and Opvizor’s excellent article: Audit VM configuration changes using the vCenter Event Broker. A huge thanks to these guys for all of their work and special thanks to OpenFaaS and Alex Ellis for making this all possible.

This example will disable alarm actions on a host while it is in maintenance mode. It is actually two functions using the same script. The first function subscribes to the entered.maintenance.mode event to run when a host is put into maintenance mode and disable its alarms. The second function subscribes to the exit.maintenance.mode event to re-enable alarms when the host exits maintenance mode. The script looks at the event type (or in OpenFaaS speak the “topic”) that called the function and performs the appropriate action to disable/enable alarms. Here is what the stack.yml file looks like with both functions:

version: 1.0
provider:
  name: openfaas
  gateway: https://veba.yourdomain.com
functions:
  powercli-entermaint:
    lang: powercli
    handler: ./handler
    image: doogleit/powercli-hostmaint:latest
    environment:
      write_debug: true
      read_debug: true
      function_debug: false
    secrets:
      - vcconfig
    annotations:
      topic: entered.maintenance.mode
  powercli-exitmaint:
    lang: powercli
    handler: ./handler
    image: doogleit/powercli-hostmaint:latest
    environment:
      write_debug: true
      read_debug: true
      function_debug: false
    secrets:
      - vcconfig
    annotations:
      topic: exit.maintenance.mode

Deploying the functions

To deploy these functions clone the example from GitHub:

git clone https://github.com/doogleit/vcenter-event-broker-appliance
cd vcenter-event-broker-appliance/examples/powercli/hostmaint-alarms

If you’ve already created a secret named ‘vcconfig’ from one of the other PowerCLI examples and want to use the same vCenter server and credentials you can skip this next step. Note that the ‘vcconfig’ secret used in the Python tagging example is different and won’t work for these functions. Configure your vCenter details in the vcconfig.json file and create the secret:

{
    "VC" : "vcenter-hostname",
    "VC_USERNAME" : "veba@vsphere.local",
    "VC_PASSWORD" : "FillMeIn"
}
faas-cli secret create vcconfig --from-file=vcconfig.json --tls-no-verify

Finally modify the gateway in the stack.yml file with your vCenter Event Broker Appliance address and deploy the functions:

provider:
  name: openfaas
  gateway: https://veba.yourdomain.com
...
faas-cli deploy -f stack.yml --tls-no-verify

Have a host enter and exit maintenance mode to test it out. You can check the logs using either:

faas-cli logs powercli-entermaint --follow --tls-no-verify

for a host entering maintenance mode or:

faas-cli logs powercli-exitmaint --follow --tls-no-verify

for a host exiting maintenance mode.

If you use SolarWinds to monitor your ESXi hosts keep an eye out for an example on that as well. I hope to have an example on GitHub soon.

I was previously using VMware Dispatch to experiment with running functions based on vCenter event subscriptions. If your interested you can check out some of my previous articles on using Dispatch here:

Comments