106 lines
2.8 KiB
Python
Executable File
106 lines
2.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from pyVmomi import vim
|
|
import pyVim.connect
|
|
import atexit
|
|
import argparse
|
|
from pprint import pprint
|
|
import re
|
|
from datetime import datetime
|
|
from dateutil.tz import tzlocal
|
|
import siteconf
|
|
|
|
site_conf = siteconf.read()
|
|
|
|
|
|
def get_args():
|
|
parser = argparse.ArgumentParser(
|
|
description='Clone VM from template')
|
|
|
|
parser.add_argument('-s', '--site',
|
|
required=False,
|
|
action='store',
|
|
help='name of the site to connect to')
|
|
|
|
parser.add_argument('-H', '--host',
|
|
required=False,
|
|
action='store',
|
|
help='vcenter host name (override site)')
|
|
|
|
parser.add_argument('-u', '--user',
|
|
required=False,
|
|
action='store',
|
|
help='user name')
|
|
|
|
parser.add_argument('-p', '--passwd',
|
|
required=False,
|
|
action='store',
|
|
help='password')
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
|
|
def get_vms(content):
|
|
obj_view = content.viewManager.CreateContainerView(
|
|
content.rootFolder, [vim.VirtualMachine], True)
|
|
vms_list = obj_view.view
|
|
obj_view.Destroy()
|
|
return vms_list
|
|
|
|
|
|
def main():
|
|
args = get_args()
|
|
|
|
host, user, passwd = None, None, None
|
|
if args.site:
|
|
if not args.site in site_conf:
|
|
print(f"site not found: {args.site}")
|
|
exit(1)
|
|
host = site_conf[args.site]['vcenter']
|
|
user = site_conf[args.site]['vcenter_user']
|
|
passwd = site_conf[args.site]['vcenter_passwd']
|
|
if args.host:
|
|
host = args.host
|
|
if args.user:
|
|
host = args.user
|
|
if args.passwd:
|
|
host = args.passwd
|
|
|
|
if not host or not user or not passwd:
|
|
print("Missing site / host, user, passwd.")
|
|
exit(1)
|
|
#print(f"host {host} user {user} passwd {passwd}")
|
|
|
|
si = pyVim.connect.SmartConnect(
|
|
host = host,
|
|
user = user,
|
|
pwd = passwd,
|
|
disableSslCertValidation = True
|
|
)
|
|
atexit.register(pyVim.connect.Disconnect, si)
|
|
content = si.RetrieveContent()
|
|
|
|
vms = get_vms(content)
|
|
for vm in vms:
|
|
uptime = vm.summary.quickStats.uptimeSeconds
|
|
if uptime > 86400:
|
|
days = uptime // 86400
|
|
uptime %= 86400
|
|
uptimestr = f"{days:3}d "
|
|
else:
|
|
uptimestr = ' '
|
|
uptimestr += f"{uptime // 3600:2}h"
|
|
boottime = vm.runtime.bootTime
|
|
if boottime:
|
|
boottime = boottime.strftime('%Y-%m-%d %H:%MZ')
|
|
else:
|
|
boottime = 'off'
|
|
uptimestr = '-'
|
|
print(f"{vm.name:42} {boottime} ({uptimestr})")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# vim: set tabstop=4 shiftwidth=4 expandtab smarttab:
|