pyVmomi - vmware api - 可用于zabbix监控

介绍

1. vmware的官方python sdk类型项目,可以通过此sdk简单实现以api的方式接入到vcenter来操作vmware集群(就是说此sdk实际上是以api的形式接入到vcenter的)

 

相关文档

一个使用pyVmomi的例子:https://www.jianshu.com/p/843782e454a8

官方项目地址:https://github.com/vmware/pyvmomi

官方samples地址:https://github.com/vmware/pyvmomi-community-samples/tree/master/samples

官方document地址:https://github.com/vmware/pyvmomi/blob/master/docs/vim/Datastore/Summary.rst

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

安装pyvmomi

pip install pyVmomi # 默认会安装最新版本的pyVmomi

 

登录到vcenter的例子

# 登录信息部分

host = 'your vcenter ip address'

user = 'user'

pwd = 'password'

port = 443



# 代码主体

try:

# 登录到vcenter,这里使用NoSSL验证,直接使用SmartConnect的话会报错

service_instance = connect.SmartConnectNoSSL(host=host,

user=user,

pwd=pwd,

port=port)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

例子

查看vmware集群所有存储的总容量和剩余空间(这里通过代码判断排除了ESXi的本地存储)

#!/usr/local/python3/bin/python3

# -*- coding:utf-8 -*-



"""

vSphere Python SDK program for listing all ESXi datastores and their

associated devices

"""

import argparse

import atexit,sys

import json

import ssl



from pyVim import connect

from pyVmomi import vmodl

from pyVmomi import vim





# http://stackoverflow.com/questions/1094841/

def sizeof_fmt(num):

"""

Returns the human readable version of a file size

:param num:

:return:

"""

for item in ['bytes', 'KB', 'MB', 'GB']:

if num < 1024.0:

return "%3.1f%s" % (num, item)

num /= 1024.0

return "%3.1f%s" % (num, 'TB')







def main():

"""

Simple command-line program for listing all ESXi datastores and their

associated devices

"""



# 登录信息部分

host = 'your vcenter ip address'

user = 'user'

pwd = 'password'

port = 443



# 代码主体

try:

# 登录到vcenter,这里使用NoSSL验证,直接使用SmartConnect的话会报错

service_instance = connect.SmartConnectNoSSL(host=host,

user=user,

pwd=pwd,

port=port)



# 如果登录失败则报错

if not service_instance:

print("Could not connect to the specified host using specified "

"username and password")

return -1



atexit.register(connect.Disconnect, service_instance)



content = service_instance.RetrieveContent()

# 获取vmware集群某资源的对象,vim.Datastore表示存储的对象,其他资源类型请见官方文档

objview = content.viewManager.CreateContainerView(content.rootFolder,

[vim.Datastore],

True)

datastores = objview.view



# 初始化容量大小和剩余空间大小

total_capacity = 0

total_freeSpace = 0

for datastore in datastores:

# ESXi的本地存储则跳过,我这里的集群所有的ESXi本地存储都带有LocalStorage字样

if 'LocalStorage' in datastore.name:continue

# datasotre.name可以输出存储的名称

# print(datastore.name)

# 初始化summary变量,datastore.summary可以查看存储的汇总信息,summary里有哪些变量 datasotre除了summary还有那些变量,都请见官方文档

datastore_summary = None

datastore_summary = datastore.summary

# 统计总容量和总剩余空间

total_capacity = total_capacity + datastore_summary.capacity

total_freeSpace = total_freeSpace + datastore_summary.freeSpace



# 输出整个集群所有存储的总容量和剩余空间

print(sizeof_fmt(total_capacity))

print(sizeof_fmt(total_freeSpace))





except vmodl.MethodFault as error:

print("Caught vmodl fault : " + error.msg)

return -1



return 0



# Start program

if __name__ == "__main__":

main()

 

需要真正给Zabbix用的监控脚本 和 Zabbix监控模板(监控所有共享存储的总容量 和 每个共享存储的容量),可与我联系

 

你可能感兴趣的:(zabbix)