我需要通过 vsphere 针对特定事件触发脚本,例如 ESXi 主机崩溃时。想使用 pyvmomi 来完成此操作,不想轮询 vcenter,而是让警报触发脚本。 http://www.vritic.com/2009/10/powershell-prevents-datastore-emergcies/
但是我想知道我们是否可以使用 pyvmomi 来实现? 谢谢
首先是免责声明: 不建议您向 VCSA 添加其他软件,尤其是会增加计算机负载的软件。据我所知,VMWare 不支持这样做,并且可能会给您的 VCSA 带来稳定性问题,因此您需要自行承担风险,如果您担心,请在进行任何更改之前咨询您的 VMWare 客户团队。
话虽这么说...这是可以做到的。由于您希望使用在 SLES Linux 机器上运行的 VCSA 来执行此操作,因此执行起来非常简单,因为它已经安装了 Python 和 pyVmomi。即使底层操作系统从 SLES 更改为 Photon,这也适用于 6.5 发布后的版本。我在下面描述的过程将以同样的方式适用于 5.5、6.0 和 6.5。
编写要在要创建的警报触发时运行的脚本,并将其放置在
/root
中的 VCSA 上,请务必使用 chmod a+x script.py
在 vCenter 中创建与您尝试监控的条件相匹配的警报。现有的警报定义可能存在,但您需要创建自己的警报定义,因为您无法修改默认警报。
在警报定义的操作窗格中,选择“运行命令”。
在配置框中输入要运行的可执行脚本的完整路径。
/root/script.py
并保存闹钟。现在,当您的警报被触发时,您的脚本将运行。如果您遇到问题或认为它不起作用,您可以在 VCSA 上找到一个日志文件,该文件可以突出显示可能发生的情况:
/var/log/vmware/vpxd/vpxd.log
我创建了一个非常粗略的示例来向您展示如何开始使用脚本。
#!/usr/bin/python
# Copyright 2016 Michael Rice <[email protected]>
#
# 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)
我刚刚使用 vSphere 7 进行了类似的练习。要捕获 vCenter 提供的所有可能的警报变量,请首先使用 bash 脚本。大多数变量不会导出为环境变量,因此它们不容易从 VCSA 上的 python 调用。我最终通过 vSphere 警报定义的脚本触发器执行通用 bash 脚本,然后通过在 bash 脚本末尾调用后续 python 脚本来传递 bash 中的所有警报变量。以下是您可以从 bash shell 捕获的所有变量的列表:https://docs.vmware.com/en/VMware-vSphere/6.7/com.vmware.vsphere.monitoring.doc/GUID-5F5932FA-71FA- 473E-8776-92B00742D566_copy.html
在 python 中,您可以利用 PyVmomi 连接回 vCenter ServiceInstance 以执行修复。 VCSA 将 PyVmomi 模块存储在 VMWARE_PYTHON_PATH 变量中,因此不要启动 python shell,认为无需向 sys 或 init 提供 PyVmomi 包路径即可导入所需的所有内容。
正如上一位发帖人提到的,您可能应该通过其他计算资源(例如 vRO、Linux 服务器,或者更好的是对 AWS API Gateway 的 API 调用来触发 lambda 或其他内容)运行修复脚本。祝你好运!!