import datetime
from datetime import timedelta
### getSnapshots(datetime ,str ,int ,list ,list ,list,list ,list)
def getSnapshots(utcdatetime,prefix,count,minutes,hours,days,weekdays,months):
## Variables ##
xTime = utcdatetime + datetime.timedelta(minutes=1)
go = True
snapCount = 0
snapList = []
## The Go Back In A Minute at a Time Loop ##
while go:
match = False
xTime = xTime - timedelta(minutes=1)
xYear = xTime.year
xMonth = xTime.month
xDay = xTime.day
xWeekDay = xTime.weekday()
xHour = xTime.hour
xMinute = xTime.minute # xMin
## Checking months ##
if months != ['X']:
if xMonth not in set(months):
xTime = datetime.datetime(xYear,xMonth,1,0,0)
continue
# otherwise we might have a match ...
## Checking day/weekday ##
if days != ['X'] and weekdays != ['X']:
if xDay not in set(days) or xWeekDay not in set(weekdays):
xTime = datetime.datetime(xYear,xMonth,xDay,0,0)
continue
# otherwise we might have a match ...
## Checking hour ##
if hours != ['X']:
if xHour not in set(hours):
xTime = datetime.datetime(xYear,xMonth,xDay,xHour,0)
continue
# otherwise we might have a match ...
## Checking minute ##
if xMinute in set(minutes):
if hours == ['X'] and days == ['X'] and weekdays == ['X'] and months == ['X']:
match = True
elif xHour in set(hours):
if days == ['X'] and weekdays == ['X'] and months == ['X']:
match = True
elif xDay in set(days) or xWeekDay in set(weekdays):
if months == ['X']:
match = True
elif xMonth in set(months):
match = True
## The snapshot ##
if match:
## Snap Name at time x (PREFIX.YYYY-MM-DD_HHmm) ##
xSnapName = prefix + "."
xSnapName += str(xYear) + "-"
xSnapName += "{:02d}".format(xMonth) + "-"
xSnapName += "{:02d}".format(xDay) + "_"
xSnapName += "{:02d}".format(xHour)
xSnapName += "{:02d}".format(xMinute)
snapList += [xSnapName]
snapCount += 1
## Count check ##
if snapCount >= count:
go = False
## Return the list of snaps!
return snapList
Example usage and output:
Generating All Expected Snapshots for All Snapshot Policies
Okay, so we can construct all the snapshot names from one schedule in a Snapshot Policy. But, snapshot policies can have multiple schedules, so how do we construct all a list of for every schedule in one list!?
This is straightforward, we just loop through the schedules running the function above.
snapPolSnapList = {}
for UUID in snapPolicyUUIDs:
snapPolSnapList[UUID] = []
try:
for s in snapPoliciesByUUID[UUID].copies:
prefix = s.prefix
count = s.count
zCronUUID = s.schedule.uuid
minutes = schedByUUID[zCronUUID]['minutes']
hours = schedByUUID[zCronUUID]['hours']
days = schedByUUID[zCronUUID]['days']
weekdays = schedByUUID[zCronUUID]['weekdays']
months = schedByUUID[zCronUUID]['months']
snapPolSnapList[UUID] += getSnapshots(utcDateTime,prefix,count,minutes,hours,days,weekdays,months)
except AttributeError:
void = "The snapshot policy has no schedules."
I did notice a problem whilst running this. The default weekly schedule uses Sunday but instead the snapshots had Mondays date. Do we have a difference somewhere of where day 0 is!?
The output of schedByUUID for the weekly cron schedule shows this:
weekly {'minutes': [15], 'hours': [0], 'days': ['X'], 'weekdays': [0], 'months': ['X']}
Sunday is 0. But Python datetime Monday is 0. We'll fix by adding some extra code to modify schedByUUID[UUID]['weekdays'].
## Changing ONTAP day 0 = Sunday, to Python datetime day 0 = Monday ##
for u in schedUUIDs:
if schedByUUID[u]['weekdays'] != ['X']:
newWeekdays = []
for w in schedByUUID[u]['weekdays']:
x = w - 1
if x == -1:
x = 6
newWeekdays += [x]
schedByUUID[u]['weekdays'] = newWeekdays
Fixed! I'll add this modification to part 5.
To be continued ...
Comments
Post a Comment