Skip to content

Instantly share code, notes, and snippets.

@dankrause
Last active May 30, 2023 00:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dankrause/8546045 to your computer and use it in GitHub Desktop.
Save dankrause/8546045 to your computer and use it in GitHub Desktop.
Simple python client for the Pagerduty integration API
#!/usr/bin/env python
"""pagerduty.py
Usage:
pagerduty.py trigger [options] <description> [<incident_key>]
pagerduty.py acknowledge [options] <description> <incident_key>
pagerduty.py resolve [options] <description> <incident_key>
Options:
-c --conf=FILE A path to a config file
-s --service=GUID The GUID of one of your "Generic API" services. This is the
"service key" listed on a Generic API's service detail page.
This is required unless a config file is provided.
-d --details=TEXT An arbitrary JSON object containing any data
you'd like included in the incident log.
-n --name=CLIENT The name of the monitoring client that is triggering this event.
This is only used when triggering events
-u --url=URL The URL of the monitoring client that is triggering this event.
This is only used when triggering events
API Docs: http://developer.pagerduty.com/documentation/integration/events
"""
import json
import requests
import docopt
_ENTRY = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
class PagerdutyResponse(object):
def __init__(self, response):
r = response.json()
self.response = response
self.success = r["status"] == "success"
self.message = r["message"]
if "incident_key" in r:
self.incident_key = r["incident_key"]
if "errors" in r:
self.errors = r["errors"]
class PagerdutyApiException(Exception):
pass
class PagerdutyApi(object):
def __init__(self, key, name=None, url=None):
self.key = key
self.name = name
self.url = url
def request(self, event_type, description, key=None, details=None):
url = _ENTRY
data = {
"service_key": self.key,
"event_type": event_type,
"description": description
}
if self.name is not None: data["client"] = self.name
if self.url is not None: data["client_url"] = self.url
if key is not None: data["incident_key"] = key
if details is not None: data["details"] = details
r = requests.request("post", url, data=json.dumps(data))
if r.ok:
return PagerdutyResponse(r)
elif r.status_code == 400:
raise PagerdutyApiException(r, PagerdutyResponse(r))
else:
raise PagerdutyApiException(r)
def trigger(self, *args, **kwargs):
return self.request("trigger", *args, **kwargs)
def acknowledge(self, *args, **kwargs):
return self.request("acknowledge", *args, **kwargs)
def resolve(self, *args, **kwargs):
return self.request("resolve", *args, **kwargs)
def main():
args = docopt.docopt(__doc__)
if args["--conf"] is not None:
try:
with open(args["--conf"]) as conf_file:
conf = json.load(conf_file)
except IOError as e:
raise SystemExit("Can't read conf file {}".format(args["--conf"]))
for key in ("service", "name", "url"):
if key in conf and not args["--{}".format(key)]:
args["--{}".format(key)] = conf[key]
if not args["--service"]:
raise SystemExit("You must provide a service ID")
actions = ("trigger", "acknowledge", "resolve")
action = filter(lambda x: args[x], actions)[0]
api = PagerdutyApi(args["--service"], args["--name"], args["--url"])
response = api.request(action, args["<description>"], args["<incident_key>"],
args["--details"])
print response.response.json()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment