-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpy-snaplist.py
executable file
·178 lines (149 loc) · 7.07 KB
/
py-snaplist.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Python program that generates a snapshot report for up to three levels below the root Snapshot
"""
from __future__ import print_function
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vmodl, vim
from datetime import datetime
import argparse
import atexit
import pytz
import getpass
# Alter this to change the number of days for aged snapshots to display a warning in the output
warning_age = 7
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')
parser.add_argument('--insecure', required=False, action="store_true",
default=False,
help='Ignore ssl certificate')
args = parser.parse_args()
return args
def get_properties(content, viewType, props, specType):
# Build a view and get basic properties for all Virtual Machines
"""
Obtains a list of specific properties for a particular Managed Object Reference data object.
:param content: ServiceInstance Managed Object
:param viewType: Type of Managed Object Reference that should populate the View
:param props: A list of properties that should be retrieved for the entity
:param specType: Type of Managed Object Reference that should be used for the Property Specification
"""
objView = content.viewManager.CreateContainerView(content.rootFolder, viewType, True)
tSpec = vim.PropertyCollector.TraversalSpec(name='tSpecName', path='view', skip=False, type=vim.view.ContainerView)
pSpec = vim.PropertyCollector.PropertySpec(all=False, pathSet=props, type=specType)
oSpec = vim.PropertyCollector.ObjectSpec(obj=objView, selectSet=[tSpec], skip=False)
pfSpec = vim.PropertyCollector.FilterSpec(objectSet=[oSpec], propSet=[pSpec], reportMissingObjectsInResults=False)
retOptions = vim.PropertyCollector.RetrieveOptions()
totalProps = []
retProps = content.propertyCollector.RetrievePropertiesEx(specSet=[pfSpec], options=retOptions)
totalProps += retProps.objects
while retProps.token:
retProps = content.propertyCollector.ContinueRetrievePropertiesEx(token=retProps.token)
totalProps += retProps.objects
objView.Destroy()
# Turn the output in retProps into a usable dictionary of values
gpOutput = []
for eachProp in totalProps:
propDic = {}
for prop in eachProp.propSet:
propDic[prop.name] = prop.val
propDic['moref'] = eachProp.obj
gpOutput.append(propDic)
return gpOutput
def print_snap_info(vm_snap):
"""
This function will loop through 3 levels of snapshots and print out the name, description and
age in a tree-type view
:param vm_snap: The snapshot property and all values
"""
current_snap = vm_snap.currentSnapshot
print(vm_snap.rootSnapshotList[0].name + ' : ' + vm_snap.rootSnapshotList[0].description + ' : '
+ snap_age_check(vm_snap.rootSnapshotList[0])
+ current_snap_check(current_snap, vm_snap.rootSnapshotList[0].snapshot))
if (vm_snap.rootSnapshotList[0].childSnapshotList):
for snapshot in vm_snap.rootSnapshotList[0].childSnapshotList:
print('\t|- ' + snapshot.name + ' : ' + snapshot.description + ' : ' + snap_age_check(snapshot)
+ current_snap_check(current_snap, snapshot.snapshot))
snap = snapshot
if (snap.childSnapshotList):
for snapshot in snap.childSnapshotList:
print('\t\t|-- ' + snapshot.name + ' : ' + snapshot.description + ' : ' + snap_age_check(snapshot)
+ current_snap_check(current_snap, snapshot.snapshot))
snap = snapshot
if (snap.childSnapshotList):
for snapshot in snap.childSnapshotList:
print('\t\t\t|--- ' + snapshot.name + ' : ' + snapshot.description + ' : '
+ snap_age_check(snapshot) + current_snap_check(current_snap, snapshot.snapshot))
snap = snapshot
if (snap.childSnapshotList):
print('\t\t\tWARNING: Only three levels of snapshots supported, but this Virtual Machine has more.')
def snap_age_check(snapshot):
"""
This function checks the age of each snapshot.
:param snapshot: The current snapshot property and value in the tree
:return: Return WARNING text with the age or just the age.
"""
snap_age = datetime.utcnow().replace(tzinfo=pytz.utc) - snapshot.createTime
if snap_age.days > warning_age:
return '!WARNING! Snapshot is ' + str(snap_age.days) + ' days old'
else:
return str(snap_age.days) + ' days old'
def current_snap_check(current_snap, tree_snap):
"""
:param current_snap: The current live snapshot state MORef of the Virtual Machine
:param tree_snap: The current tree snapshot state MORef of the Virtual Machine
:return: Return text stating where the live VM currently is in the tree or return nothing.
"""
if current_snap == tree_snap:
return ' : *You are here*'
else:
return ''
import ssl
def main():
args = GetArgs()
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
if(args.insecure):
context.verify_mode = ssl.CERT_NONE
else:
context.verify_mode = ssl.CERT_REQUIRED
try:
si = None
if args.password:
password = args.password
else:
password = getpass.getpass(prompt="Enter password for host {} and user {}: ".format(args.host, args.user))
try:
si = SmartConnect(host=args.host,
user=args.user,
pwd=password,
port=int(args.port),
sslContext=context)
except IOError, e:
pass
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()
retProps = get_properties(content, [vim.VirtualMachine], ['name', 'snapshot'], vim.VirtualMachine)
for vm in retProps:
if ('snapshot' in vm):
print('\n' + vm['name'])
print_snap_info(vm['snapshot'])
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
# Start program
if __name__ == "__main__":
main()