1

我需要通过 vsphere 触发特定事件的脚本,例如 ESXi 主机是否崩溃。想要使用 pyvmomi 来完成,不想轮询 vCenter,而是让警报触发脚本。 http://www.vcritical.com/2009/10/powershell-prevents-datastore-emergencies/

我也看了这个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

但我想知道我们是否可以使用 pyvmomi 来实现?谢谢

4

1 回答 1

0

首先声明:不建议您向 VCSA 添加其他软件,尤其是那些会增加机器负载的软件。据我所知,VMWare 不支持这样做,它可能会在您的 VCSA 中引入稳定性问题,因此请您自担风险,如果您担心在进行任何更改之前请与您的 VMWare 客户团队联系。

话虽这么说......这是可能的。由于您希望使用在 SLES Linux 机器上运行的 VCSA 来执行此操作,因此执行起来非常简单,因为它上面已经有 Python 和 pyVmomi。即使底层操作系统正在从 SLES 更改为 Photon,这也将在 6.5 上运行。我在下面描述的过程将以相同的方式在 5.5、6.0 和 6.5 上运行。

  1. 编写要在要创建触发器的警报时运行的脚本,并将其放置在 VCSA 中/root确保使用脚本设置执行位chmod a+x script.py

  2. 在 vCenter 中创建与您尝试监控的条件相匹配的警报。现有的警报定义可能存在,但您需要创建自己的警报定义,因为您无法修改默认警报。

  3. 在警报定义的操作窗格中,选择“运行命令”。

  4. 在配置框中输入要运行的可执行脚本的完整路径。/root/script.py并保存警报。

现在,当您的警报被触发时,您的脚本将运行。如果您有问题或认为它不起作用,您可以在 VCSA 上找到一个日志文件,该文件可以突出显示可能发生的情况:/var/log/vmware/vpxd/vpxd.log

我创建了一个非常粗略的示例来向您展示如何开始使用您的脚本。

#!/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)
于 2016-11-07T17:18:46.050 回答