-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (79 loc) · 3.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from __future__ import print_function
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vmodl, vim
from colored import fore, back, style
import time
import sys
import atexit
import ssl
import modules.strings as strs
import modules.config as cfg
from modules.vm_scanner.getProperties import GetProperties
from modules.vm_scanner.getPerf import getVMPerf
def scanVM():
interval = cfg.vspehre['interval']
try:
while True:
si = None
try:
if cfg.vspehre['skipSSL']:
context = ssl._create_unverified_context()
si = SmartConnect(host=cfg.vspehre['host'],
user=cfg.vspehre['user'],
pwd=cfg.vspehre['password'],
port=int("443"),
sslContext=context)
else:
si = SmartConnect(host=cfg.vspehre['host'],
user=cfg.vspehre['user'],
pwd=cfg.vspehre['password'],
port=int("443"))
except IOError as e:
pass
if not si:
print(strs.connError)
return -1
atexit.register(Disconnect, si)
content = si.RetrieveContent()
# Get vCenter date and time for use as baseline when querying
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',
'runtime.connectionState'],
vim.VirtualMachine)
for vm in retProps:
if (vm['runtime.powerState'] == "poweredOn") \
and \
(vm['runtime.connectionState'] != "disconnected"):
print('*' * 20)
print('[x] ' + vm['name'])
getVMPerf(vm['moref'], content, vchtime,
int(interval), perf_dict)
else:
print(strs.vmError.format(fore.LIGHT_BLUE, back.RED,
style.BOLD, vm['name'],
style.RESET))
# time.sleep(2)
except vmodl.MethodFault as e:
print('Caught vmodl fault : ' + e.msg)
return -1
except Exception as e:
print('Caught exception : ' + str(e))
# return -1
return 0
if __name__ == '__main__':
try:
scanVM()
except KeyboardInterrupt:
time.sleep(1)
sys.exit()