通过vSphere API获取对象Statistics
预备知识点:
1、vim.PerformanceManager.MetricId() 通过counter_id获取到metric_id
2、vim.PerformanceManager.QuerySpec() 通过对象、metric_id、起始时间进行装配
3、content.perfManager.QueryPerf() 通过QuerySpec获取对象的Statistics
4、content.perfManager.perfCounter 获取所有Performance Counter相关信息
示例1: 获取ESXI主机的网络利用率
- from pyVim.connect import SmartConnectNoSSL, Disconnect
- from pyVmomi import vim
- from datetime import timedelta
- import atexit
- def connect_vc(host, user, pwd, port):
- si = SmartConnectNoSSL(host=host, user=user, pwd=pwd, port=port)
- # disconnect this thing
- atexit.register(Disconnect, si)
- return si
- def build_query(content, vc_time, counter_id, obj, interval):
- metric_id = vim.PerformanceManager.MetricId(counterId=counter_id, instance="")
- start_time = vc_time - timedelta(minutes=(interval + 1))
- end_time = vc_time - timedelta(minutes=1)
- query = vim.PerformanceManager.QuerySpec(intervalId=20,
- entity=obj,
- metricId=[metric_id],
- startTime=start_time,
- endTime=end_time)
- perf_results = content.perfManager.QueryPerf(querySpec=[query])
- if perf_results:
- return perf_results
- else:
- pass
- def print_statistics(obj, content, vc_time, interval, perf_dict, ):
- stat_interval = interval * 3 # There are 3per 20s samples in each minute
- # Network usage (Tx/Rx)
- # statNetworkTx = BuildQuery(content, vchtime, (stat_check(perf_dict, 'net.usage.maximum')), obj, interval)
- # networkTx = (float(sum(statNetworkTx[0].value[0].value) * 8 / 1024) / statInt)
- # statNetworkRx = BuildQuery(content, vchtime, (stat_check(perf_dict, 'net.usage.minimum')), obj, interval)
- # networkRx = (float(sum(statNetworkRx[0].value[0].value) * 8 / 1024) / statInt)
- # Network utilization (combined transmit-rates and receive-rates) during the interval = 145
- network_usage = build_query(content, vc_time, stat_check(perf_dict, 'net.usage.average'), obj, interval)
- try:
- print('statNetworkThroughput:%sMB' % (round((((sum(network_usage[0].value[0].value)) / 1024) / stat_interval), 2)))
- except TypeError:
- # 关机的ESXi主机无法获取到数据
- pass
- def stat_check(perf_dict, counter_name):
- """通过performance counter名称获取counter id"""
- counter_id = perf_dict[counter_name]
- return counter_id
- def main():
- username = 'administrator@vsphere.local'
- password = 'xxxxxx'
- vc_ip = '172.16.65.99'
- vc_port = ''
- statistics_interval_time = 10 # 分钟为单位
- si = connect_vc(host=vc_ip, user=username, pwd=password, port=vc_port)
- content = si.RetrieveContent()
- # Get vCenter date and time for use as baseline when querying for counters
- vc_time = si.CurrentTime()
- # 获取所有performance counter,并放入字典中
- perf_dict = {}
- perf_list = content.perfManager.perfCounter
- for counter in perf_list:
- counter_full = "{}.{}.{}".format(counter.groupInfo.key, counter.nameInfo.key, counter.rollupType)
- perf_dict[counter_full] = counter.key # perf_dict包含了所有的perfCounter
- # 获取ESXi主机对象
- container_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.HostSystem], True)
- for obj in container_view.view:
- print_statistics(obj, content, vc_time, statistics_interval_time, perf_dict)
- # Start program
- if __name__ == "__main__":
- main()
示例2: 获取VM相关的performance
- """
- Python program that generates various statistics for one or more virtual machines
- A list of virtual machines can be provided as a comma separated list.
- """
- from __future__ import print_function
- from pyVim.connect import SmartConnectNoSSL, Disconnect
- from pyVmomi import vmodl, vim
- from datetime import timedelta, datetime
- import atexit
- def connect_vc(host, user, pwd, port):
- si = SmartConnectNoSSL(host=host, user=user, pwd=pwd, port=port)
- # disconnect this thing
- atexit.register(Disconnect, si)
- return si
- def BuildQuery(content, vchtime, counterId, instance, vm, interval):
- perfManager = content.perfManager
- metricId = vim.PerformanceManager.MetricId(counterId=counterId, instance=instance)
- startTime = vchtime - timedelta(minutes=(interval + 1))
- endTime = vchtime - timedelta(minutes=1)
- query = vim.PerformanceManager.QuerySpec(intervalId=20, entity=vm, metricId=[metricId], startTime=startTime,
- endTime=endTime)
- perfResults = perfManager.QueryPerf(querySpec=[query])
- if perfResults:
- return perfResults
- else:
- print('ERROR: Performance results empty. TIP: Check time drift on source and vCenter server')
- print('Troubleshooting info:')
- print('vCenter/host date and time: {}'.format(vchtime))
- print('Start perf counter time : {}'.format(startTime))
- print('End perf counter time : {}'.format(endTime))
- print(query)
- exit()
- def PrintVmInfo(vm, content, vchtime, interval, perf_dict, ):
- statInt = interval * 3 # There are 3 20s samples in each minute
- summary = vm.summary
- disk_list = []
- network_list = []
- # Convert limit and reservation values from -1 to None
- if vm.resourceConfig.cpuAllocation.limit == -1:
- vmcpulimit = "None"
- else:
- vmcpulimit = "{} Mhz".format(vm.resourceConfig.cpuAllocation.limit)
- if vm.resourceConfig.memoryAllocation.limit == -1:
- vmmemlimit = "None"
- else:
- vmmemlimit = "{} MB".format(vm.resourceConfig.cpuAllocation.limit)
- if vm.resourceConfig.cpuAllocation.reservation == 0:
- vmcpures = "None"
- else:
- vmcpures = "{} Mhz".format(vm.resourceConfig.cpuAllocation.reservation)
- if vm.resourceConfig.memoryAllocation.reservation == 0:
- vmmemres = "None"
- else:
- vmmemres = "{} MB".format(vm.resourceConfig.memoryAllocation.reservation)
- vm_hardware = vm.config.hardware
- for each_vm_hardware in vm_hardware.device:
- if (each_vm_hardware.key >= 2000) and (each_vm_hardware.key < 3000):
- disk_list.append('{} | {:.1f}GB | Thin: {} | {}'.format(each_vm_hardware.deviceInfo.label,
- each_vm_hardware.capacityInKB/1024/1024,
- each_vm_hardware.backing.thinProvisioned,
- each_vm_hardware.backing.fileName))
- elif (each_vm_hardware.key >= 4000) and (each_vm_hardware.key < 5000):
- network_list.append('{} | {} | {}'.format(each_vm_hardware.deviceInfo.label,
- each_vm_hardware.deviceInfo.summary,
- each_vm_hardware.macAddress))
- #CPU Ready Average
- statCpuReady = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'cpu.ready.summation')), "", vm, interval)
- cpuReady = (float(sum(statCpuReady[0].value[0].value)) / statInt)
- #CPU Usage Average % - NOTE: values are type LONG so needs divided by 100 for percentage
- statCpuUsage = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'cpu.usage.average')), "", vm, interval)
- cpuUsage = ((float(sum(statCpuUsage[0].value[0].value)) / statInt) / 100)
- #Memory Active Average MB
- statMemoryActive = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'mem.active.average')), "", vm, interval)
- memoryActive = (float(sum(statMemoryActive[0].value[0].value) / 1024) / statInt)
- #Memory Shared
- statMemoryShared = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'mem.shared.average')), "", vm, interval)
- memoryShared = (float(sum(statMemoryShared[0].value[0].value) / 1024) / statInt)
- #Memory Balloon
- statMemoryBalloon = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'mem.vmmemctl.average')), "", vm, interval)
- memoryBalloon = (float(sum(statMemoryBalloon[0].value[0].value) / 1024) / statInt)
- #Memory Swapped
- statMemorySwapped = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'mem.swapped.average')), "", vm, interval)
- memorySwapped = (float(sum(statMemorySwapped[0].value[0].value) / 1024) / statInt)
- #Datastore Average IO
- statDatastoreIoRead = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'datastore.numberReadAveraged.average')),
- "*", vm, interval)
- DatastoreIoRead = (float(sum(statDatastoreIoRead[0].value[0].value)) / statInt)
- statDatastoreIoWrite = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'datastore.numberWriteAveraged.average')),
- "*", vm, interval)
- DatastoreIoWrite = (float(sum(statDatastoreIoWrite[0].value[0].value)) / statInt)
- #Datastore Average Latency
- statDatastoreLatRead = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'datastore.totalReadLatency.average')),
- "*", vm, interval)
- DatastoreLatRead = (float(sum(statDatastoreLatRead[0].value[0].value)) / statInt)
- statDatastoreLatWrite = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'datastore.totalWriteLatency.average')),
- "*", vm, interval)
- DatastoreLatWrite = (float(sum(statDatastoreLatWrite[0].value[0].value)) / statInt)
- #Network usage (Tx/Rx)
- statNetworkTx = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'net.transmitted.average')), "", vm, interval)
- networkTx = (float(sum(statNetworkTx[0].value[0].value) * 8 / 1024) / statInt)
- statNetworkRx = BuildQuery(content, vchtime, (StatCheck(perf_dict, 'net.received.average')), "", vm, interval)
- networkRx = (float(sum(statNetworkRx[0].value[0].value) * 8 / 1024) / statInt)
- print('networkRx:', networkRx)
- print('networkTx:', networkTx)
- print('\nNOTE: Any VM statistics are averages of the last {} minutes\n'.format(statInt / 3))
- print('Server Name :', summary.config.name)
- print('Description :', summary.config.annotation)
- print('Guest :', summary.config.guestFullName)
- if vm.rootSnapshot:
- print('Snapshot Status : Snapshots present')
- else:
- print('Snapshot Status : No Snapshots')
- print('VM .vmx Path :', summary.config.vmPathName)
- try:
- print('Virtual Disks :', disk_list[0])
- if len(disk_list) > 1:
- disk_list.pop(0)
- for each_disk in disk_list:
- print(' ', each_disk)
- except IndexError:
- pass
- print('Virtual NIC(s) :', network_list[0])
- if len(network_list) > 1:
- network_list.pop(0)
- for each_vnic in network_list:
- print(' ', each_vnic)
- print('[VM] Limits : CPU: {}, Memory: {}'.format(vmcpulimit, vmmemlimit))
- print('[VM] Reservations : CPU: {}, Memory: {}'.format(vmcpures, vmmemres))
- print('[VM] Number of vCPUs :', summary.config.numCpu)
- print('[VM] CPU Ready : Average {:.1f} %, Maximum {:.1f} %'.format((cpuReady / 20000 * 100),
- ((float(max(
- statCpuReady[0].value[
- 0].value)) / 20000 * 100))))
- print('[VM] CPU (%) : {:.0f} %'.format(cpuUsage))
- print('[VM] Memory : {} MB ({:.1f} GB)'.format(summary.config.memorySizeMB, (float(summary.config.memorySizeMB) / 1024)))
- print('[VM] Memory Shared : {:.0f} %, {:.0f} MB'.format(
- ((memoryShared / summary.config.memorySizeMB) * 100), memoryShared))
- print('[VM] Memory Balloon : {:.0f} %, {:.0f} MB'.format(
- ((memoryBalloon / summary.config.memorySizeMB) * 100), memoryBalloon))
- print('[VM] Memory Swapped : {:.0f} %, {:.0f} MB'.format(
- ((memorySwapped / summary.config.memorySizeMB) * 100), memorySwapped))
- print('[VM] Memory Active : {:.0f} %, {:.0f} MB'.format(
- ((memoryActive / summary.config.memorySizeMB) * 100), memoryActive))
- print('[VM] Datastore Average IO : Read: {:.0f} IOPS, Write: {:.0f} IOPS'.format(DatastoreIoRead,
- DatastoreIoWrite))
- print('[VM] Datastore Average Latency : Read: {:.0f} ms, Write: {:.0f} ms'.format(DatastoreLatRead,
- DatastoreLatWrite))
- print('[VM] Overall Network Usage : Transmitted {:.3f} Mbps, Received {:.3f} Mbps'.format(networkTx, networkRx))
- print('[Host] Name : {}'.format(summary.runtime.host.name))
- print('[Host] CPU Detail : Processor Sockets: {}, Cores per Socket {}'.format(
- summary.runtime.host.summary.hardware.numCpuPkgs,
- (summary.runtime.host.summary.hardware.numCpuCores / summary.runtime.host.summary.hardware.numCpuPkgs)))
- print('[Host] CPU Type : {}'.format(summary.runtime.host.summary.hardware.cpuModel))
- print('[Host] CPU Usage : Used: {} Mhz, Total: {} Mhz'.format(
- summary.runtime.host.summary.quickStats.overallCpuUsage,
- (summary.runtime.host.summary.hardware.cpuMhz * summary.runtime.host.summary.hardware.numCpuCores)))
- print('[Host] Memory Usage : Used: {:.0f} GB, Total: {:.0f} GB\n'.format(
- (float(summary.runtime.host.summary.quickStats.overallMemoryUsage) / 1024),
- (float(summary.runtime.host.summary.hardware.memorySize) / 1024 / 1024 / 1024)))
- def StatCheck(perf_dict, counter_name):
- counter_key = perf_dict[counter_name]
- return counter_key
- def GetProperties(content, viewType, props, specType):
- # Build a view and get basic properties for all Virtual Machines
- objView = content.viewManager.CreateContainerView(content.rootFolder, viewType, True)
- tSpec = vim.PropertyCollector.TraversalSpec(name='tSpecName', path='view', skip=False, type=vim.view.ContainerView)
- pSpec = vim.PropertyCollector.PropertySpec(all=False, pathSet=props, type=specType)
- oSpec = vim.PropertyCollector.ObjectSpec(obj=objView, selectSet=[tSpec], skip=False)
- pfSpec = vim.PropertyCollector.FilterSpec(objectSet=[oSpec], propSet=[pSpec], reportMissingObjectsInResults=False)
- retOptions = vim.PropertyCollector.RetrieveOptions()
- totalProps = []
- retProps = content.propertyCollector.RetrievePropertiesEx(specSet=[pfSpec], options=retOptions)
- totalProps += retProps.objects
- while retProps.token:
- retProps = content.propertyCollector.ContinueRetrievePropertiesEx(token=retProps.token)
- totalProps += retProps.objects
- objView.Destroy()
- # Turn the output in retProps into a usable dictionary of values
- gpOutput = []
- for eachProp in totalProps:
- propDic = {}
- for prop in eachProp.propSet:
- propDic[prop.name] = prop.val
- propDic['moref'] = eachProp.obj
- gpOutput.append(propDic)
- return gpOutput
- def main():
- username = 'administrator@vsphere.local'
- password = 'xxxxxx'
- vc_ip = '172.16.65.99'
- vc_port = ''
- customization_spec_name = 'Ubuntu_Customization'
- si = connect_vc(host=vc_ip, user=username, pwd=password, port=vc_port)
- content = si.RetrieveContent()
- # Get vCenter date and time for use as baseline when querying for counters
- vchtime = si.CurrentTime()
- # Get all the performance counters
- perf_dict = {}
- perfList = content.perfManager.perfCounter
- for counter in perfList:
- counter_full = "{}.{}.{}".format(counter.groupInfo.key, counter.nameInfo.key, counter.rollupType)
- perf_dict[counter_full] = counter.key
- retProps = GetProperties(content, [vim.VirtualMachine], ['name', 'runtime.powerState'], vim.VirtualMachine)
- #Find VM supplied as arg and use Managed Object Reference (moref) for the PrintVmInfo
- for vm in retProps:
- PrintVmInfo(vm['moref'], content, vchtime, 20, perf_dict)
- break
- # Start program
- if __name__ == "__main__":
- main()
通过vSphere API获取对象Statistics的更多相关文章
- 利用腾讯企业邮箱开放API获取账户未读邮件数初探
公司一直使用腾讯提供的免费企业邮箱服务,今天用管理员帐户登录后发现,原来现在腾讯的企业邮箱也开放了部分API 你可以通过开放接口实现以下功能: 数据同步 数据同步可以帮助你同步部门成员信息,你还可以创 ...
- JavaSE_ API常用对象 总目录(11~14)
JavaSE学习总结第11天_开发工具 & API常用对象111.01 常见开发工具介绍11.02 Eclipse和MyEclipse的概述11.03 Eclipse的下载安装及卸载11.04 ...
- 在C#中调用API获取网络信息和流量
原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...
- C# 通过豆瓣网络编程API获取图书信息
这篇文章主要是关于如何通过豆瓣API获取信息的书籍,起初,我看到了原来的想法的内容是"C# 网络编程之网页简单下载实现"中通过HttpWebResponse类下载源代码,再通过正則 ...
- 【转】百度API获取城市名地名(附源码)
在做一个软件时,用到了定位功能.网上有很多关于google 的GPS定位,但网上关于google定位都没有用, 搜索下原因:(这里建议大家在中国就尽量不使用系统自带的定位) 因为Google的服务器不 ...
- 通过SDK和API获取阿里云RDS的监控数据
阿里云的RDS自带的监控系统获取数据不怎么直观,想要通过API获取数据通过zabbix显示,因为网上资料缺乏和其他一些原因,获取API签名很困难,但使用阿里云的SDK可以完美避开获取签名的步骤. 阿里 ...
- 使用Vue.js和Axios从第三方API获取数据 — SitePoint
更多的往往不是,建立你的JavaScript应用程序时,你会想把数据从远程源或消耗一个[ API ](https:/ /恩.维基百科.org /维基/ application_programming_ ...
- zabbix通过SDK和API获取阿里云RDS的监控数据
阿里云的RDS自带的监控系统获取数据不怎么直观,想要通过API获取数据通过zabbix显示,因为网上资料缺乏和其他一些原因,获取API签名很困难,但使用阿里云的SDK可以完美避开获取签名的步骤. 阿里 ...
- ASP.NET Web API 路由对象介绍
ASP.NET Web API 路由对象介绍 前言 在ASP.NET.ASP.NET MVC和ASP.NET Web API这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了 ...
随机推荐
- ASP.NET CORE RAZOR :个性化显示
https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/da1 我们的电影应用有个不错的开始,但是展示效果还不够理想. 我 ...
- file_put_contents执行返回false,file_put_contents false(linux服务器httpd)
file_put_contents执行返回false,file_put_contents false(linux服务器httpd) 默认下selinux是开启的查看SELinux状态:1./usr/s ...
- php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法
1.认识 __set (在给不可访问属性赋值时,__set() 会被调用) 也就是说你再访问一个类里面没有的属性,会出发这个方法 class A{ private $aa = '11'; publ ...
- Amzaon EC2虚拟化技术演进:从 Xen 到 Nitro
今年2月,由光环新网运营的 AWS 中国(北京)区域和由西云数据运营的 AWS 中国 (宁夏)区域发布新的实例类型,新的实例类型包括 C5.C5d.R5.R5d.除了这四种之外,在AWS国外部分区 ...
- GNU LD 脚本学习笔记
LD脚本(linker script)是什么 GNU ld是链接器,ld实际并不是GCC的一部分,ld属于binutils软件包.但是嵌入式开发时,下载的linaro GCC工具集中是包含 arm-l ...
- mybatis 单一参数时的动态语句
public void getBookList(String publisher,String author){ Map<String,Object> maps = new HashMap ...
- 经常使用的eclipse快捷键-也适合sts
非常多内容来自互联网和已知文件,本人仅整理. 1. ctrl+shift+r:打开资源 这可能是全部快捷键组合中最省时间的了. 这组快捷键能够让你打开你的工作区中不论什么一个文件.而你仅仅须要按下文件 ...
- ios 10 sticker pack application
看了WWDC2016直播,我们发现变得谨慎而开放的苹果在新一版四大平台系统中展示了很多变化,当然重中之重还是伟大的iOS.通过试用iOS10beta版,除了长大了的更强大的Siri主要感受到iMess ...
- Laravel开发:多用户登录验证(1)
之前实现了一次,后来代码忘记放哪了,所以有跳了一次坑. 先贴上Laravel自带的验证代码: 路由:routes/web.php // Authentication Routes... $this-& ...
- DevExpress控件显示前弹出到期时间解决办法
方法是,删除Properties下的license.licx文件,重新生成即可.