Skip to content

Instantly share code, notes, and snippets.

@lfepp
Last active May 3, 2023 03:58
Show Gist options
  • Save lfepp/32afebc59aa4b88a733bcc1b4f7236f9 to your computer and use it in GitHub Desktop.
Save lfepp/32afebc59aa4b88a733bcc1b4f7236f9 to your computer and use it in GitHub Desktop.
Script to create a number of recurring maintenance windows in PagerDuty
#!/usr/bin/env python
#
# Copyright (c) 2016, PagerDuty, Inc. <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of PagerDuty Inc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Script to create a number of recurring maintenance windows
# CLI Usage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...
# api_key: Your PagerDuty API access token
# email: The email address associated with your PagerDuty account
# start_date: The date at which you would like the first mainteance window to occur
# duration: The length of time for the maintenance window in minutes
# frequency: The length of time between recurring maintenance windows in hours
# repeat: The number of times you want this maintenance window to recur
# description: A description for the maintenance window
# service_ids: The unique identifiers for the services you want to add these maintenance windows to
import requests
import sys
import json
import dateutil.parser
from datetime import datetime, timedelta
def create_maintenance_windows(api_key, email, start_date, duration, frequency, repeat, description, service_ids):
headers = {
'Authorization': 'Token token=' + api_key,
'Content-type': 'application/json',
'Accept': 'application/vnd.pagerduty+json;version=2',
'From': email
}
start = dateutil.parser.parse(start_date)
end = start + timedelta(minutes=int(duration))
for i in xrange(0, int(repeat), 1):
payload = {
'maintenance_window': {
'type': 'maintenance_window',
'start_time': start.isoformat(),
'end_time': end.isoformat(),
'description': description,
'services': service_ids
}
}
print 'Creating a ' + duration + ' minute maintenance window starting at ' + str(start)
r = requests.post('https://api.pagerduty.com/maintenance_windows', headers=headers, data=json.dumps(payload))
if r.status_code == 201:
print 'Maintenance window with ID ' + r.json()['maintenance_window']['id'] + ' was successfully created'
else:
print 'Error: maintenance window creation failed!\nStatus code: ' + str(r.status_code) + '\nResponse: ' + r.text + '\nExiting...'
start = start + timedelta(hours=int(frequency))
end = end + timedelta(hours=int(frequency))
if __name__ == '__main__':
if len(sys.argv) == 1:
print 'Error: You did not enter any parameters.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 2:
print 'Error: You did not enter an email address.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 3:
print 'Error: You did not enter a start_date.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 4:
print 'Error: You did not enter a duration.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 5:
print 'Error: You did not enter a frequency.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 6:
print 'Error: You did not enter a number of repeats.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 7:
print 'Error: You did not enter a description.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
elif len(sys.argv) == 8:
print 'Error: You did not enter any service_ids.\nUsage: ./recurring_maintenance_windows api_key email start_date duration frequency repeat description service_ids...\n\tapi_key: Your PagerDuty API access token\n\temail: The email address associated with your PagerDuty account\n\tstart_date: The date at which you would like the first mainteance window to occur\n\tduration: The length of time for the maintenance window in minutes\n\tfrequency: The length of time between recurring maintenance windows in hours\n\trepeat: The number of times you want this maintenance window to recur\n\tdescription: A description for the maintenance window\n\tservice_ids: The unique identifiers for the services you want to add these maintenance windows to'
else:
services_list = []
for service in sys.argv[8:]:
services_list.append({
'id': service,
'type': 'service_reference'
})
create_maintenance_windows(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7], services_list)
@CammoKing
Copy link

Not a big deal but there's some typos in here in the error responses:
The date at which you would like the first mainteance...

@benjaminlukeclark
Copy link

Is this gist open to PRs (not a thing in gist I know, but if I made a fork would this get updated or would I need https://support.pagerduty.com/docs/maintenance-windows#section-recurring-maintenance-mode-for-services to point to my gist?)

I'd like to rewrite this to use argparse (https://docs.python.org/3/library/argparse.html) so the argument passing is a bit cleaner.

@razorsedge
Copy link

Is this gist open to PRs?

Fork, update, and leave a comment here.

@Millnert
Copy link

Millnert commented Sep 29, 2022

Forked ( https://gist.github.com/Millnert/34b92e70a5dcc35fc252d7728eb559a9 ) and ported to Python3, added argparser and some logging printer. Worked for me just now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment