首先声明:
不建议您向 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 <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)