|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (c) 2018, PagerDuty, Inc. <[email protected]> |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# * Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# * Neither the name of PagerDuty Inc nor the |
| 14 | +# names of its contributors may be used to endorse or promote products |
| 15 | +# derived from this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY |
| 21 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +# -------------------------------------------------------------------------------- |
| 29 | +# INSTRUCTIONS |
| 30 | +# |
| 31 | +# This script will search for all currently active maintenance windows on all |
| 32 | +# services and end them. It does not support pagination, so it is limited to 100 |
| 33 | +# maintenance windows (by default it will sort by name) |
| 34 | +# |
| 35 | +# REQUIRED: API_KEY, EMAIL |
| 36 | +# |
| 37 | +# OPTIONAL: You can set the END_TIME to a future date if required. |
| 38 | +# |
| 39 | +# # Date format: '2018-09-30T14:00:00' |
| 40 | +# |
| 41 | +# There are also other parameters for filtering maintenance windows. |
| 42 | +# |
| 43 | +# -------------------------------------------------------------------------------- |
| 44 | + |
| 45 | +import requests |
| 46 | +import datetime |
| 47 | + |
| 48 | +current_time = datetime.datetime.now() |
| 49 | + |
| 50 | +# Update to match your v2 REST API key |
| 51 | +API_KEY = '' |
| 52 | + |
| 53 | +# Update to match your login email |
| 54 | +EMAIL = '' |
| 55 | + |
| 56 | +# Update to match your chosen parameters |
| 57 | +# You can also specify a different end date here instead (Date format: '2018-09-30T14:00:00') |
| 58 | +END_TIME = current_time |
| 59 | +TEAM_IDS = [] |
| 60 | +SERVICE_IDS = [] |
| 61 | +INCLUDE = [] |
| 62 | +FILTER = 'ongoing' |
| 63 | +QUERY = '' |
| 64 | + |
| 65 | + |
| 66 | +def get_maintenance_windows(): |
| 67 | + url = 'https://api.pagerduty.com/maintenance_windows?total=true' |
| 68 | + headers = { |
| 69 | + 'Accept': 'application/vnd.pagerduty+json;version=2', |
| 70 | + 'Authorization': 'Token token={token}'.format(token=API_KEY) |
| 71 | + } |
| 72 | + payload = { |
| 73 | + 'team_ids[]': TEAM_IDS, |
| 74 | + 'service_ids[]': SERVICE_IDS, |
| 75 | + 'include[]': INCLUDE, |
| 76 | + 'filter': FILTER, |
| 77 | + 'query': QUERY |
| 78 | + } |
| 79 | + maintenance_windows_ids = [] |
| 80 | + r = requests.get(url, headers=headers, params=payload) |
| 81 | + print('Getting ongoing maintenance windows...') |
| 82 | + print('\tStatus Code: {code}'.format(code=r.status_code)) |
| 83 | + if r.status_code < 200 or r.status_code >= 204: |
| 84 | + print("\tThere was an error getting your maintenance windows.") |
| 85 | + print("\tPlease ensure that the login email and v2 REST API key in this script are correct.") |
| 86 | + print(r.text) |
| 87 | + return maintenance_windows_ids |
| 88 | + maintenance_list = r.json() |
| 89 | + ids = maintenance_list['maintenance_windows'] |
| 90 | + total = int(maintenance_list['total']) |
| 91 | + |
| 92 | + if total > 0: |
| 93 | + if total == 1: |
| 94 | + print("\t1 ongoing maintenance window found:") |
| 95 | + if total == 2: |
| 96 | + print("\t", total, 'ongoing maintenance windows found') |
| 97 | + for maintenance_window in range(total): |
| 98 | + maintenance_windows_ids.append(ids[maintenance_window]['id']) |
| 99 | + print("\t", maintenance_windows_ids) |
| 100 | + else: |
| 101 | + print("No ongoing maintenance windows found") |
| 102 | + return maintenance_windows_ids |
| 103 | + |
| 104 | + |
| 105 | +def end_maintenance_window(MAINTENANCE_WINDOWS): |
| 106 | + if len(MAINTENANCE_WINDOWS) == 1: |
| 107 | + print("Ending maintenance window...") |
| 108 | + else: |
| 109 | + print("Ending maintenance windows...") |
| 110 | + headers = { |
| 111 | + 'Accept': 'application/vnd.pagerduty+json;version=2', |
| 112 | + 'Authorization': 'Token token={token}'.format(token=API_KEY) |
| 113 | + } |
| 114 | + for maintenance_window in range(len(MAINTENANCE_WINDOWS)): |
| 115 | + url = 'https://api.pagerduty.com/maintenance_windows/{id}'.format(id=MAINTENANCE_WINDOWS[maintenance_window]) |
| 116 | + print("\t", str(MAINTENANCE_WINDOWS[maintenance_window])) |
| 117 | + r = requests.delete(url, headers=headers) |
| 118 | + print('\tStatus Code: {code}'.format(code=r.status_code)) |
| 119 | + if r.status_code == 204: |
| 120 | + print('\tMaintenance window ended successfully!') |
| 121 | + else: |
| 122 | + print("\tThere was an error ending this maintenance window") |
| 123 | + print("\tPlease ensure that the login email and v2 REST API key in this script have proper permissions") |
| 124 | + return maintenance_windows_ids |
| 125 | + print(r.text) |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + |
| 129 | + if EMAIL == '': |
| 130 | + print("Please add your login email to this script and run it again.") |
| 131 | + elif API_KEY == '': |
| 132 | + print("Please add your v2 REST API key to this script and run it again.") |
| 133 | + else: |
| 134 | + MAINTENANCE_WINDOWS = get_maintenance_windows() |
| 135 | + if MAINTENANCE_WINDOWS != []: |
| 136 | + end_maintenance_window(MAINTENANCE_WINDOWS) |
| 137 | + |
| 138 | + |
| 139 | +# ----------------------------------------------------------------------- |
| 140 | + |
0 commit comments