【问题标题】:How can I run Pyvmomi scripts based on vSphere alarms on the VCSA如何在 VCSA 上运行基于 vSphere 警报的 Pyvmomi 脚本
【发布时间】:2017-03-18 18:23:31
【问题描述】:

我需要通过 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 来实现? 谢谢

【问题讨论】:

  • 在回答之前我有几个问题:1) 您将使用哪个版本的 vSphere? 2) 您将使用 vCenter Server 应用程序还是 Windows box 驱动的 vCenter?
  • 谢谢。也是 vmware 的新手,还在学习。 1. 我们将运行 5.5 和 6 2. 如果可能,更喜欢从 linux 机器触发:)

标签: python vmware pyvmomi


【解决方案1】:

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

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

  1. 编写要在您将要创建的警报触发时运行的脚本,并将其放置在/root 中的 VCSA 上确保使用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)

【讨论】:

  • 我接受了。非常感谢。因此,如果我们无法访问 VCSA,我必须从 Vcenter(Windows 机器)触发它?
  • 好吧,看起来你赞成你的问题,但没有接受我的回答。另外,在我回答问题之前,我问过你是使用 Windows vCenter 还是 vCenter Server Appliance,所以我的回答是针对你所说的 VCSA。对于 Windows 驱动的盒子,过程是不一样的。
  • 我没有投票赞成我的问题 :) 我不能投票给自己的帖子 :) 我也无权选择答案。它说一旦你达到 15 分,我就可以投票给一个答案或类似的东西。
  • @Moderator ,请看我上面的评论。 Michael Rice 的帖子回答了我的问题。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 2021-06-21
相关资源
最近更新 更多