Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trigger a PagerDuty incident within multiple services
#!/usr/bin/env python
#
# Copyright (c) 2016, PagerDuty, Inc. <info@pagerduty.com>
# 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.
#
# Trigger an incident within multiple services
# CLI Usage: ./trigger_incident_multiple_services incident_key description service_keys...
# incident_key: Unique identifier for this particular incident
# description: Description of the incident
# service_keys: Integration keys for PagerDuty services you want to trigger the incident within
import sys
import json
import requests
def trigger_incident_multiple_services(incident_key, description, services):
url = 'https://events.pagerduty.com/generic/2010-04-15/create_event.json'
headers = {
'Content-type': 'application/json'
}
payload = {
'incident_key': incident_key,
'description': description,
'event_type': 'trigger'
}
for service in services:
payload['service_key'] = service
r = requests.post(url, headers=headers, data=json.dumps(payload))
print r.status_code
print r.json()
if __name__ == '__main__':
if len(sys.argv) == 1:
print "Error: You did not enter any parameters.\nUsage: ./trigger_incident_multiple_services incident_key description service_keys...\n\tincident_key: Unique identifier for this particular incident\n\tdescription: Description of the incident\n\tservice_keys: Integration keys for PagerDuty services you want to trigger the incident within"
elif len(sys.argv) == 2:
print "Error: You did not enter a description.\nUsage: ./trigger_incident_multiple_services incident_key description service_keys...\n\tincident_key: Unique identifier for this particular incident\n\tdescription: Description of the incident\n\tservice_keys: Integration keys for PagerDuty services you want to trigger the incident within"
elif len(sys.argv) == 3:
print "Error: You did not enter any services.\nUsage: ./trigger_incident_multiple_services incident_key description service_keys...\n\tincident_key: Unique identifier for this particular incident\n\tdescription: Description of the incident\n\tservice_keys: Integration keys for PagerDuty services you want to trigger the incident within"
else:
trigger_incident_multiple_services(sys.argv[1], sys.argv[2], sys.argv[3:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.