1

I need to trigger a script through vsphere for specific events, say if an ESXi host crashed. Wanted to do it using pyvmomi, prefer not to poll the vcenter and rather have an alarm trigger a script. http://www.vcritical.com/2009/10/powershell-prevents-datastore-emergencies/

I looked at this as well https://pubs.vmware.com/vsphere-4-esx-vcenter/index.jsp#com.vmware.vsphere.dcadmin.doc_41/vc_client_help/working_with_alarms/c_running_commands_as_alarm_actions.html

But I wanted to know if we can achieve using pyvmomi? Thanks

Michael Rice
  • 7,974
  • 1
  • 15
  • 18
jramacha
  • 117
  • 2
  • 13
  • Before I could answer that I would have a couple of questions: 1) What version of vSphere would you be using? 2) Will you be using the vCenter Server App or a Windows box powered vCenter? – Michael Rice Nov 05 '16 at 21:09
  • Thanks. Also am new to vmware and still learning. 1. We will be running 5.5 and 6 2. Prefer to trigger from a linux machine if possible :) – jramacha Nov 07 '16 at 15:35

1 Answers1

0

First a disclaimer: It is not recommended that you add additional software to the VCSA, especially that which would increase the load on the machine. Doing so is not supported by VMWare to my knowledge and it could introduce stability issues into your VCSA so do so at your own risk and if you are worried check with your VMWare account team before making any changes.

That being said... This is possible to do. Since you would want to do this using the VCSA which runs on a SLES Linux box it will be very simple to do because it already has both Python and pyVmomi on it. This will also work on 6.5 once it comes out even though the underlying OS is changing from SLES to Photon. The process I describe below will work on 5.5, 6.0, and 6.5 in the same way.

  1. Write the script you want to run when the alarm you will be creating triggers, and place it on the VCSA in /root Be sure to set the the execution bit on the script using chmod a+x script.py

  2. Create the alarm in vCenter that matches the condition you are trying to monitor for. An existing Alarm Definition may exist but you will need to create your own because you can not modify the default alarms.

  3. In the Actions pane for the Alarm Definition select "Run a command".

  4. In the configuration box put the full path to the executable script that you want to run. /root/script.py and save the alarm.

Now when your alarm is triggered your script will run. If you have problems or think it is not working you can find a log file on the VCSA that can highlight what might be happening: /var/log/vmware/vpxd/vpxd.log

I have created a very crude example to show you how to get started with your script.

#!/usr/bin/python
#   Copyright 2016 Michael Rice <michael@michaelrice.org>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from __future__ import print_function
import os
import ssl
import sys
import requests

# This is where VMWare keeps the pyVmomi and other libraries
sys.path.extend(os.environ['VMWARE_PYTHON_PATH'].split(';'))

from pyVim import connect
from pyVmomi import vim
requests.packages.urllib3.disable_warnings()
# this is to ignore SSL verification which is helpful for self signed certs
try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context
USER_NAME = "YOUR USER"
PASSWORD = "YOUR PASS"
HOST = "YOUR HOST"
PORT = "443"
service_instance = connect.SmartConnect(host=HOST,
                                        user=USER_NAME,
                                        pwd=PASSWORD,
                                        port=int(PORT))

root_folder = service_instance.content.rootFolder
# again crude example here. use the logging module instead
with open("/var/log/my_script_log_file.txt", 'a') as f:
    print(root_folder.name, file=f)
    for var, val in os.environ.items():
        # When an alarm is triggered and run a lot of environment variables are set. 
        # This will list them all with their values.
        if var.startswith("VMWARE_ALARM"):
            print("{} = {}".format(var, val), file=f)
    print("##########", file=f)
connect.Disconnect(service_instance)
Michael Rice
  • 7,974
  • 1
  • 15
  • 18
  • I accepted it. Thanks a lot. So if we don't have access to VCSA I have to trigger it from Vcenter(Windows machine)? – jramacha Nov 07 '16 at 17:59
  • well it looks like you upvoted your question and didnt accept my answer. Also I asked you before I answered the question if you would be using a windows vcenter or a vcenter server appliance so my answer is for what you said which is a VCSA. For a windows powered box the process is not the same. – Michael Rice Nov 07 '16 at 18:08
  • I didn't up vote my question :) I can't vote my own post :) I don't have power to select an answer either. It says once you reach 15 points I can upvote an answer or something of that sort. – jramacha Nov 07 '16 at 19:07
  • @Moderator , please see my comment above. Michael Rice's post answers my question. Thanks – jramacha Nov 08 '16 at 21:38