Skip to content

Instantly share code, notes, and snippets.

@zwindler
Created August 8, 2020 13:42
Show Gist options
  • Save zwindler/7ef6ff14218961f9e8af3167fceef584 to your computer and use it in GitHub Desktop.
Save zwindler/7ef6ff14218961f9e8af3167fceef584 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import argparse
import atexit
import getpass
import ssl
vmCount=0
vCPUCount=0
vRAMCount=0
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', required=True, 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', required=True, action='store',
help='User name to use when connecting to host')
parser.add_argument('-p', '--password', required=False, action='store',
help='Password to use when connecting to host')
args = parser.parse_args()
return args
def GetVmInfo(vm, hosts, depth=1):
global vmCount
global vCPUCount
global vRAMCount
maxdepth = 10
if hasattr(vm, 'childEntity'):
if depth > maxdepth:
return
vmList = vm.childEntity
for c in vmList:
GetVmInfo(c, hosts, depth+1)
return
#print(vm.summary)
#print(vm.summary.runtime.maxMemoryUsage)
if (vm.summary.runtime.powerState == 'poweredOn'):
#print(vm.summary.config.name)
if vm.summary.runtime.host in hosts:
#print(vm.summary.config.name)
#print(vm.summary.config.memorySizeMB)
#print(vm.summary.config.numCpu)
vmCount += 1
vCPUCount += vm.summary.config.numCpu
vRAMCount += vm.summary.config.memorySizeMB
def main():
args = GetArgs()
if args.password:
password = args.password
else:
password = getpass.getpass(prompt='Enter password for host %s and '
'user %s: ' % (args.host,args.user))
context = None
if hasattr(ssl, '_create_unverified_context'):
context = ssl._create_unverified_context()
si = SmartConnect(host=args.host,
user=args.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()
for child in content.rootFolder.childEntity:
if hasattr(child, 'hostFolder'):
datacenter = child
clusterFolder = datacenter.hostFolder
clusterList = clusterFolder.childEntity
for cluster in clusterList:
if cluster.name == 'Cluster_zwindler':
#print(cluster)
#print(cluster.name)
#print(cluster.host)
Cluster_zwindler_hosts = cluster.host
#print(cluster.summary)
vcpuTheorical=cluster.summary.numCpuThreads * 2
vramTheorical=cluster.summary.totalMemory / (1024 * 1024 * 1024) / 2
#print(cluster.configuration)
#for vm in cluster.configuration.dasVmConfig:
# print(vm.key)
if hasattr(child, 'vmFolder'):
datacenter = child
vmFolder = datacenter.vmFolder
vmList = vmFolder.childEntity
for vm in vmList:
#print(vm.name)
GetVmInfo(vm,Cluster_zwindler_hosts)
print("Nombre de VMs actives : ", vmCount)
print("Nombre de vCPU alloués : ", vCPUCount, " / ", vcpuTheorical)
print("Nombre de Go de vRAM alloués : ", vRAMCount/1024, " / ", vramTheorical)
return 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