Created
November 20, 2018 14:44
-
-
Save rockaut/8045aea1e1c19639ff26c886b63740c3 to your computer and use it in GitHub Desktop.
pyvmomi.tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# VMware vSphere Python SDK | |
# Copyright (c) 2008-2015 VMware, Inc. All Rights Reserved. | |
# | |
# 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. | |
""" | |
Python program for listing the vms on an ESX / vCenter host | |
""" | |
from __future__ import print_function | |
from pyVim.connect import SmartConnect, Disconnect | |
from pyVmomi import vim | |
from datetime import datetime, timedelta | |
from os import environ | |
import argparse | |
import atexit | |
import getpass | |
import ssl | |
def GetArgs(): | |
""" | |
Supports the command-line arguments listed below. | |
""" | |
parser = argparse.ArgumentParser( | |
description='Process args for retrieving all the Virtual Machines') | |
parser.add_argument('-s', '--host', action='store', | |
help='Remote host to connect to') | |
parser.add_argument('-o', '--port', type=int, default=443, action='store', | |
help='Port to connect on') | |
parser.add_argument('-u', '--user', action='store', | |
help='User name to use when connecting to host') | |
parser.add_argument('-p', '--password', action='store', | |
help='Password to use when connecting to host') | |
args = parser.parse_args() | |
return args | |
def main(): | |
""" | |
Simple command-line program for listing the virtual machines on a system. | |
""" | |
args = GetArgs() | |
if args.password: | |
password = args.password | |
else: | |
password = environ['VC_PASSWORD'] | |
if args.user: | |
user = args.user | |
else: | |
user = environ['VC_USER'] | |
if args.host: | |
host = args.host | |
else: | |
host = environ['VC_HOST'] | |
context = None | |
if hasattr(ssl, '_create_unverified_context'): | |
context = ssl._create_unverified_context() | |
si = SmartConnect(host=host, | |
user=user, | |
pwd=password, | |
port=int(args.port), | |
sslContext=context) | |
if not si: | |
print("Could not connect to the specified host using specified " | |
"username and password") | |
return -1 | |
atexit.register(Disconnect, si) | |
content = si.RetrieveContent() | |
perfManager = content.perfManager | |
counterN2I = {} | |
counterI2N = {} | |
for c in perfManager.perfCounter: | |
prefix = c.groupInfo.key | |
fullName = c.groupInfo.key + "." + c.nameInfo.key + "." + c.rollupType | |
counterN2I[fullName] = c.key | |
counterI2N[c.key] = fullName | |
container = content.rootFolder | |
viewType = [vim.VirtualMachine] | |
recursive = True | |
containerView = content.viewManager.CreateContainerView(container,viewType,recursive) | |
children = containerView.view | |
metricIDs = [vim.PerformanceManager.MetricId(counterId=2,instance="")] | |
querySpecs = [] | |
queryResults = [] | |
maxQueryChunk = 128 | |
end=datetime.now() | |
start=(end -timedelta(seconds=300)) | |
for vm in children: | |
spec = vim.PerformanceManager.QuerySpec(entity=vm,metricId=metricIDs,intervalId=300,endTime=end,startTime=start,format='normal') | |
querySpecs.append(spec) | |
for specChunk in ([querySpecs[i:i + maxQueryChunk] for i in range(0, len(querySpecs), maxQueryChunk)]): | |
queryResults += perfManager.QueryStats(querySpec=specChunk) | |
print(len(queryResults)) | |
# (vim.PerformanceManager.EntityMetric) { | |
# dynamicType = <unset>, | |
# dynamicProperty = (vmodl.DynamicProperty) [], | |
# entity = 'vim.VirtualMachine:vm-308', | |
# sampleInfo = (vim.PerformanceManager.SampleInfo) [ | |
# (vim.PerformanceManager.SampleInfo) { | |
# dynamicType = <unset>, | |
# dynamicProperty = (vmodl.DynamicProperty) [], | |
# timestamp = 2018-11-20T14:35:00Z, | |
# interval = 300 | |
# } | |
# ], | |
# value = (vim.PerformanceManager.MetricSeries) [ | |
# (vim.PerformanceManager.IntSeries) { | |
# dynamicType = <unset>, | |
# dynamicProperty = (vmodl.DynamicProperty) [], | |
# id = (vim.PerformanceManager.MetricId) { | |
# dynamicType = <unset>, | |
# dynamicProperty = (vmodl.DynamicProperty) [], | |
# counterId = 2, | |
# instance = '' | |
# }, | |
# value = (long) [ | |
# 361 | |
# ] | |
# } | |
# ] | |
# } | |
#print(queryResults[0].entity.config.uuid) | |
#print(counterI2N[queryResults[0].value[0].id.counterId]) | |
for qr in queryResults: | |
print('"{}" "{}" "{}"'.format(qr.entity.config.uuid, counterI2N[qr.value[0].id.counterId], qr.value[0].value[0])) | |
#vm = children[4] | |
#spec = vim.PerformanceManager.QuerySpec(entity=vm,metricId=metricIDs,intervalId=300,endTime=end,startTime=start,format='normal') | |
#querySpecs.append(spec) | |
#print(len(querySpecs)) | |
#print(vm.name) | |
#print(querySpecs) | |
#result = perfManager.QueryPerf(querySpec=querySpecs) | |
#print(result[0].entity.config.uuid) | |
return 0 | |
counterN2I = 0 | |
# Start program | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment