#!/usr/bin/python3# Python script to create reoccurring PagerDuty Maintenance Window and scheduled via AWS Cloudwatch/Lambda Function# Tested with Python 3.5.1## You will need to intall python requests module# %python_home%\Scripts\pip install requests# https://pypi.python.org/pypi/requests/2.9.1## February 27, 2016## 3/20/16 - Added abiltiy to schedule maintenance on a single specific day of the upcoming weekimportrequestsimportjsonimportdatetimeimportpytzclassPdMaintenanceWindow:"""PagerDuty Maintenance Windows This class allows you to setup reoccurring maintenance windows using the PagerDuty API """__url='https://{ENTER SUBDOMAIN HERE}.pagerduty.com/api/v1/maintenance_windows'#Your PagerDuty URL__description='Reoccurring Routine Scheduled Maintenance'#Description of the maintanence__requester=''#The PagerDuty User that created the maintenace, will default to 'API' if not specified__timezone=pytz.timezone('ENTER TIMEZONE HERE')#Your timezone, required to set current date/time to match your timezonedef__init__(self,apiToken,services,startDay,startTime,minutes):""" :param apiToken: The PagerDuty API tocken generated from your account :param services: The service IDs :param startDay: The day maintenance begins, 0=Mon,1=Tue,2=Wed,3=Thu,4=Fri,5=Sat,6=Sun,*="daily" :param startTime: The time maintenance begins :param minutes: The duration of maintenance :return: """self.apiToken=apiTokenself.services=servicesself.startDay=startDayself.startTime=startTimeself.minutes=minutesdefCreateWeeklySchedule(self):""" Creates the daily maintenance schedule The function will create the next 7 days of maintenance beginning tomorrow from the date it is run. :raises: A runtime error if a http 201 "created" is not returned. """nextDay=1if(self.startDay=='*'):loop=7else:loop=1d=datetime.datetime.now(self.__timezone)d+=datetime.timedelta(days=nextDay)whiled.weekday()!=int(self.startDay):d+=datetime.timedelta(days=1)nextDay+=1startTime=datetime.datetime.now(self.__timezone).replace(hour=self.startTime.hour,minute=self.startTime.minute,second=0,microsecond=0)foriinrange(loop):startTime+=datetime.timedelta(days=nextDay)endTime=startTimeendTime+=datetime.timedelta(minutes=self.minutes)headers={'Content-type':'application/json','Authorization':'Token token='+self.apiToken}payload=({"maintenance_window":{"start_time":startTime.isoformat(),"end_time":endTime.isoformat(),"description":self.__description,"service_ids":self.services},"requester_id":self.__requester})r=requests.post(self.__url,data=json.dumps(payload),headers=headers)ifr.status_code!=201:raiseRuntimeError("API call failed, status code: "+str(r.status_code))#Handler Name must match AWS Lambda Handler Configurationdeflambda_handler(event,context):apiToken="ENTER PAGERDUTY API TOKEN HERE"## Create weekly service schedule 1PdMaintenanceWindow(apiToken,["ENTER SERVICES HERE"],"ENTER DAY HERE",datetime.time("ENTER HOUR HERE","ENTER MINUTE HERE"),"ENTER DURATION HERE").CreateWeeklySchedule()## Create weekly service schedule 2PdMaintenanceWindow(apiToken,["ENTER SERVICES HERE"],"ENTER DAY HERE",datetime.time("ENTER HOUR HERE","ENTER MINUTE HERE"),"ENTER DURATION HERE").CreateWeeklySchedule()return"success"
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.