-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_events.py
72 lines (57 loc) · 2.37 KB
/
get_events.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
from datetime import datetime, timedelta
import ssl, atexit, os
from pyVim import connect # get VMWare's python sdk at https://github.com/vmware/pyvmomi
from pyVmomi import vim
from dotenv import load_dotenv
load_dotenv()
# HOST = "10.141.42.240"
VC = os.getenv("VCENTER_ADDR")
USER = os.getenv("VCENTER_USER")
PWD = os.getenv("VCENTER_PWD")
period = None
host = None
EVENTS = ["VmMigratedEvent", "VmCreatedEvent", "VmRemovedEvent", "VmBeingClonedEvent", "VmRelocatedEvent"] # example of desired events
def get_host_events(period=None, events=None, host=None):
# Getting the Sevice Instance
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.verify_mode = ssl.CERT_NONE
si = connect.SmartConnect(protocol="https", host=VC, port=443, user=USER, pwd=PWD, sslContext=context)
#Cleanly disconnect
atexit.register(connect.Disconnect, si)
filter_spec = vim.event.EventFilterSpec()
if events: # if list of event types was provided
filter_spec.eventTypeId = events
if period: # if period was provided
time_filter = vim.event.EventFilterSpec.ByTime()
now = datetime.now()
time_filter.beginTime = now - timedelta(hours=period)
time_filter.endTime = now
filter_spec.time = time_filter
content = si.RetrieveServiceContent()
event_manager = content.eventManager
# event_res = event_manager.QueryEvents(filter_spec)
event_collector = event_manager.CreateCollectorForEvents(filter_spec)
page_size = 1000
out_events = []
while True:
events_in_page = event_collector.ReadNextEvents(page_size)
if not events_in_page:
break
if not host:
out_events.extend(events_in_page)
continue
# if desired host was provided
page_events = []
for e in events_in_page:
if e.host and e.host.host.summary.config.name == host:
page_events.append(e)
out_events.extend(page_events)
return out_events
for e in get_host_events(events=EVENTS, period=period, host=host):
event_string = "{} \033[32m@\033[0m {:%Y-%m-%d %H:%M:%S} by {}".format(e.fullFormattedMessage, e.createdTime, e.userName)
try:
event_string += "in datastore{}".format(e.vm.vm.datastore)
except Exception:
print(event_string)
continue
print(event_string)