Last active
April 11, 2016 18:55
-
-
Save mdcollins05/d9213561a058f92cbd0542c18248799d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Python script to create recurring maintenance windows in PagerDuty | |
# | |
# Copyright (c) 2015, 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. | |
# | |
# Tested with Python 2.7.10 | |
# Requires requests | |
# | |
#Your PagerDuty API key. A read-only key will work for this. | |
auth_token = 'API_KEY_HERE' | |
auth_token = '9xvdcSXjAGT2ifJrmGMs' | |
#The PagerDuty subdomain | |
pd_subdomain = 'YOUR_PD_SUBDOMAIN' | |
pd_subdomain = 'pdt-mdc' | |
#Your user ID | |
requester_id = 'P4G5FIY' | |
#The service ID or service IDs you want to create the maintenance window for. | |
#service_ids = ['P4REQ0E', 'P90NKT3'] # Multiple services example | |
service_ids = ['P4REQ0E', 'P90NKT3'] | |
#The first maintenance window to create | |
first_maint_window_date = '2015-10-29T20:00:00-07:00' | |
#Description of the maintenance | |
description = 'Testing the script out!' | |
#Maintenance window duration | |
duration_minutes = 120 | |
#How often to schedule a maintenance window | |
#168 = once a week | |
frequency_hours = 24 | |
#Repeat 20 times | |
repeat = 20 | |
## END OF CONFIGURABLE ITEMS ## | |
import requests | |
import sys | |
import json | |
import dateutil.parser | |
from datetime import datetime, timedelta | |
HEADERS = { | |
'Authorization': 'Token token={0}'.format(auth_token), | |
'Content-type': 'application/json', | |
} | |
start_date = dateutil.parser.parse(first_maint_window_date) | |
end_date = dateutil.parser.parse(first_maint_window_date) + timedelta(minutes=duration_minutes) | |
for iter in range(1, repeat, 1): | |
data = {'requester_id': requester_id, | |
'maintenance_window': { | |
'start_time': start_date.isoformat(), | |
'end_time': end_date.isoformat(), | |
'description': description, | |
'service_ids': service_ids | |
}} | |
print("Creating a {0} minute maintenance window starting {1}.".format(duration_minutes, start_date)) | |
maintenance_window = requests.post( | |
'https://{0}.pagerduty.com/api/v1/maintenance_windows'.format(pd_subdomain), | |
headers=HEADERS, | |
data=json.dumps(data) | |
) | |
if maintenance_window.status_code != 201: | |
print("Creation failed. Status code: {0}".format(maintenance_window.status_code)) | |
print("Response: {0}".format(maintenance_window.text)) | |
print("Exiting..") | |
sys.exit(1) | |
else: | |
print("Creation successful. Window ID: {0}".format(maintenance_window.json()['maintenance_window']['id'])) | |
start_date = start_date + timedelta(hours=frequency_hours) | |
end_date = end_date + timedelta(hours=frequency_hours) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Python script to create recurring maintenance windows in PagerDuty | |
# | |
# Copyright (c) 2015, 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. | |
# | |
# Tested with Python 2.7.10 | |
# Requires requests | |
# | |
import requests | |
import sys | |
import json | |
#Your PagerDuty API key. A read-only key will work for this. | |
auth_token = 'API_KEY_HERE' | |
#The PagerDuty subdomain | |
pd_subdomain = 'YOUR_PD_SUBDOMAIN' | |
#Deletes any maintenance windows that have this service ID | |
service_id = '' | |
HEADERS = { | |
'Authorization': 'Token token={0}'.format(auth_token), | |
'Content-type': 'application/json', | |
} | |
def get_future_maint_window_count(): | |
maint_window_count = requests.get( | |
'https://{0}.pagerduty.com/api/v1/maintenance_windows?filter=future'.format(pd_subdomain), | |
headers=HEADERS | |
) | |
return [maint_window_count.json()['total'], maint_window_count.json()['limit']] | |
def remove_all_future_maint_windows(): | |
total_maint_windows = get_future_maint_window_count() | |
print("Total number of future maintenance windows: {0}".format(total_maint_windows[0])) | |
for offset in xrange(0, total_maint_windows[0]): | |
if offset % total_maint_windows[1] == 0: | |
maint_windows = get_future_maint_window(offset, total_maint_windows[1]) | |
for maint_window in maint_windows: | |
if service_id == '': | |
delete_maint_window(maint_window['id']) | |
else: | |
delete_it = False | |
for service in maint_window['services']: | |
if service['id'] == service_id: | |
delete_it = True | |
if delete_it: | |
delete_maint_window(maint_window['id']) | |
def get_future_maint_window(offset, limit): | |
params = { | |
'offset': offset, | |
'limit': limit | |
} | |
maint_windows = requests.get( | |
'https://{0}.pagerduty.com/api/v1/maintenance_windows?filter=future'.format(pd_subdomain), | |
headers=HEADERS, | |
data=json.dumps(params), | |
) | |
return maint_windows.json()['maintenance_windows'] | |
def delete_maint_window(window_id): | |
requests.delete( | |
'https://{0}.pagerduty.com/api/v1/maintenance_windows/{1}'.format(pd_subdomain, window_id), | |
headers=HEADERS | |
) | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
remove_all_future_maint_windows() | |
if __name__=='__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment