One addition to part 5 is to get the snapshot create_time. We can do that this way:
Snapshot.get_collection(volUUID,fields="create_time")
Note: I need to think about how we use "create_time" since currently I'm just recording a list of snapshots by name.
Report Fields
The original report idea (back in part 1) had these fields:
1) Snapshot Name, 2) Snapshot ID (if any), 3) Backup Schedule, 4) Volume Name/Path, 5) Backup Results, 6) Time, 7) Success/Failed
For the report version 1, I'm going to change this to:
1) SVM, 2) Volume, 3) Snapshot name, 4) Snapshot policy, 5) Success (exists), 6) Create Time
We have all the information above, now to form it into a report.
Getting the Output
The process:
- for each volume:
- find snapshot policy
- loop through snapPolList
- if we have the snapshot, record in the report as success
- if not, record it as failure
import csv
outputCSV = []
outputFileName = 'snapshotReport.csv'
for u in volUUIDs:
if volByUUID[u]['style'] != 'flexvol':
# Only considering FlexVols (the idea is to skip FlexGroups)
continue
sp = volByUUID[u]['snapshot_policy']
spUUID = snapPolicyNameToUUID[sp]
expectedSnaps = snapPolSnapList[spUUID]
for s in expectedSnaps:
if s in set(snapsByVolUUID[u]):
success = "SUCCESS"
createTime = str(snapDataByVolUUID[u][s]['create_time'])
else:
success = "FAILURE"
createTime = ""
outDict = {}
outDict['SVM'] = volByUUID[u]['svm']
outDict['Volume'] = volUUIDtoName[u]
outDict['Snapshot Name'] = s
outDict['Snapshot Policy'] = volByUUID[u]['snapshot_policy']
outDict['Success (exists)'] = success
outDict['Create Time'] = createTime
outputCSV += [outDict]
with open(outputFileName, 'w', newline='') as csvfile:
fieldnames = ['SVM','Volume','Snapshot Name','Snapshot Policy','Success (exists)','Create Time']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for r in outputCSV:
writer.writerow(r)
I think that is good (need to test.)
Getting Snapshot 'Create Time' Working
Now, how about getting the 'Create Time' working?
## 7) ACQUIRE SNAPSHOTS ##
snapsByVolUUID = {}
snapDataByVolUUID = {}
for u in volUUIDs:
snaps = Snapshot.get_collection(u,fields="create_time")
snapsByVolUUID[u] = []
snapDataByVolUUID[u] = {}
for s in snaps:
snapsByVolUUID[u] += [s.name]
snapDataByVolUUID[u][s.name] = {}
snapDataByVolUUID[u][s.name]['create_time'] = s.create_time
Which allows us to amend the above (getting the output) with 'Create Time'
= str(snapDataByVolUUID[u][s]['create_time'])
Problem
I notice the snapshot naming convention is using the cluster timezone, not UTC which is what I had thought (since my cluster is set to Tasmania timezone and I'm 10 hours away, it is very noticeable). I'll have to fix this...
Image: ONTAP Snapshots Named By Cluster Timezone
Fixing the Time Issue
pip install pytz
import pytz
clus = Cluster()
clus.get()
utcDateTime = clus.statistics.timestamp
local_tz = pytz.timezone(clus.timezone.name)
localDT = utcDateTime.astimezone(local_tz)
With the above, all we'll need to do is change the usage of utcDateTime to localDT.
Next Steps
In Part 9 we'll bring it all together:
- Code from part 5.
- Code from part 7.
- Code from part 8 (including section 7 fixes and time fixes)
- And turn it into a script.
That might be too much for one part.
To be continued ...
Comments
Post a Comment