diff --git a/integrations/pagerduty/manifest.yaml b/integrations/pagerduty/manifest.yaml new file mode 100644 index 0000000..001d54b --- /dev/null +++ b/integrations/pagerduty/manifest.yaml @@ -0,0 +1,126 @@ +id: pagerduty +name: PagerDuty +version: 1.0.0 +description: "PagerDuty (Events API v2 + REST API v2) — SOC alerting and on-call: trigger/acknowledge/resolve incidents via the routing key, list and read incidents, add notes, and read on-calls, users, and services. Token + routing-key authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: trigger/acknowledge/resolve incident, list/get incident, add note, list on-calls/users/services." +category: notification + +# Per-instance configuration. api_token authenticates the REST API +# ('Authorization: Token token='); routing_key is the Events API v2 +# integration key used to trigger/acknowledge/resolve. from_email is the +# PagerDuty user email required by the REST API for writes such as add-note. +config_schema: + properties: + api_token: + type: string + description: "PagerDuty REST API token" + x-soar-sensitive: true + routing_key: + type: string + description: "Events API v2 integration/routing key (for trigger/ack/resolve)" + x-soar-sensitive: true + from_email: + type: string + description: "A valid PagerDuty user email (required for add-note and other REST writes)" + required: + - api_token + +commands: + - id: trigger_incident + name: pagerduty-trigger-incident + description: "Trigger an incident through the Events API v2. Returns a dedup_key used to acknowledge/resolve it." + inputs_schema: + properties: + summary: { type: string, description: "Short incident summary" } + source: { type: string, description: "Affected source (host, service, IP)" } + severity: { type: string, description: "critical | error | warning | info (default critical)" } + dedup_key: { type: string, description: "Optional dedup key (auto-generated if omitted)" } + component: { type: string, description: "Optional component" } + required: [summary, source] + outputs_schema: { properties: {} } + - id: acknowledge_incident + name: pagerduty-acknowledge-incident + description: "Acknowledge an incident by its dedup_key (Events API v2)." + inputs_schema: + properties: + dedup_key: { type: string, description: "The incident dedup_key" } + required: [dedup_key] + outputs_schema: { properties: {} } + - id: resolve_incident + name: pagerduty-resolve-incident + description: "Resolve an incident by its dedup_key (Events API v2)." + inputs_schema: + properties: + dedup_key: { type: string, description: "The incident dedup_key" } + required: [dedup_key] + outputs_schema: { properties: {} } + - id: list_incidents + name: pagerduty-list-incidents + description: "List incidents (filter by status, urgency, and time window)." + risk: read + inputs_schema: + properties: + statuses: { type: string, description: "Comma-separated: triggered,acknowledged,resolved" } + urgencies: { type: string, description: "Comma-separated: high,low" } + since: { type: string, description: "ISO-8601 start time" } + until: { type: string, description: "ISO-8601 end time" } + limit: { type: number, description: "Max incidents (default 25)" } + required: [] + outputs_schema: { properties: {} } + - id: get_incident + name: pagerduty-get-incident + description: "Get a single incident by ID." + risk: read + inputs_schema: + properties: + incident_id: { type: string, description: "Incident ID" } + required: [incident_id] + outputs_schema: { properties: {} } + - id: add_note + name: pagerduty-add-note + description: "Add a note to an incident (requires from_email in the configuration)." + inputs_schema: + properties: + incident_id: { type: string, description: "Incident ID" } + note: { type: string, description: "Note content" } + required: [incident_id, note] + outputs_schema: { properties: {} } + - id: list_oncalls + name: pagerduty-list-oncalls + description: "List current on-call assignments." + risk: read + inputs_schema: + properties: + schedule_ids: { type: string, description: "Optional comma-separated schedule IDs to filter" } + limit: { type: number, description: "Max entries (default 25)" } + required: [] + outputs_schema: { properties: {} } + - id: list_users + name: pagerduty-list-users + description: "List users (optionally filter by a search query)." + risk: read + inputs_schema: + properties: + query: { type: string, description: "Optional name/email search filter" } + limit: { type: number, description: "Max users (default 25)" } + required: [] + outputs_schema: { properties: {} } + - id: list_services + name: pagerduty-list-services + description: "List services (optionally filter by a search query)." + risk: read + inputs_schema: + properties: + query: { type: string, description: "Optional name search filter" } + limit: { type: number, description: "Max services (default 25)" } + required: [] + outputs_schema: { properties: {} } + + - id: test_connection + name: pagerduty-test-connection + description: "Verify connectivity and the REST API token (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/pagerduty/scripts/acknowledge_incident.py b/integrations/pagerduty/scripts/acknowledge_incident.py new file mode 100644 index 0000000..3601bbb --- /dev/null +++ b/integrations/pagerduty/scripts/acknowledge_incident.py @@ -0,0 +1,70 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + if not cfg.get("routing_key"): + raise Exception("routing_key is not configured") + + dedup_key = str(inputs.get("dedup_key", "") or "").strip() + if not dedup_key: + raise Exception("dedup_key is required") + + return events_enqueue(cfg, { + "routing_key": cfg["routing_key"], + "event_action": "acknowledge", + "dedup_key": dedup_key, + }) + + +_run(main) diff --git a/integrations/pagerduty/scripts/add_note.py b/integrations/pagerduty/scripts/add_note.py new file mode 100644 index 0000000..84e3e18 --- /dev/null +++ b/integrations/pagerduty/scripts/add_note.py @@ -0,0 +1,77 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + incident_id = str(inputs.get("incident_id", "") or "").strip() + if not incident_id: + raise Exception("incident_id is required") + + note = str(inputs.get("note", "") or "").strip() + if not note: + raise Exception("note is required") + + from_email = str(cfg.get("from_email", "") or "").strip() + if not from_email: + raise Exception("from_email must be configured to add notes") + + return rest( + "POST", + "/incidents/" + urllib.parse.quote(incident_id, safe="") + "/notes", + cfg, + body={"note": {"content": note}}, + extra_headers={"From": from_email}, + ) + + +_run(main) diff --git a/integrations/pagerduty/scripts/get_incident.py b/integrations/pagerduty/scripts/get_incident.py new file mode 100644 index 0000000..a8e56e6 --- /dev/null +++ b/integrations/pagerduty/scripts/get_incident.py @@ -0,0 +1,63 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + incident_id = str(inputs.get("incident_id", "") or "").strip() + if not incident_id: + raise Exception("incident_id is required") + + return rest("GET", "/incidents/" + urllib.parse.quote(incident_id, safe=""), cfg) + + +_run(main) diff --git a/integrations/pagerduty/scripts/list_incidents.py b/integrations/pagerduty/scripts/list_incidents.py new file mode 100644 index 0000000..e01e554 --- /dev/null +++ b/integrations/pagerduty/scripts/list_incidents.py @@ -0,0 +1,75 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + statuses = [s.strip() for s in str(inputs.get("statuses", "") or "").split(",") if s.strip()] + urgencies = [s.strip() for s in str(inputs.get("urgencies", "") or "").split(",") if s.strip()] + since = str(inputs.get("since", "") or "").strip() + until = str(inputs.get("until", "") or "").strip() + + try: + limit = int(inputs.get("limit", 25) or 25) + except (TypeError, ValueError): + limit = 25 + + return rest("GET", "/incidents", cfg, params={ + "statuses[]": statuses or None, + "urgencies[]": urgencies or None, + "since": since, + "until": until, + "limit": limit, + }) + + +_run(main) diff --git a/integrations/pagerduty/scripts/list_oncalls.py b/integrations/pagerduty/scripts/list_oncalls.py new file mode 100644 index 0000000..5fe68b1 --- /dev/null +++ b/integrations/pagerduty/scripts/list_oncalls.py @@ -0,0 +1,69 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + schedule_ids = [s.strip() for s in str(inputs.get("schedule_ids", "") or "").split(",") if s.strip()] + + try: + limit = int(inputs.get("limit", 25) or 25) + except (TypeError, ValueError): + limit = 25 + + return rest("GET", "/oncalls", cfg, params={ + "schedule_ids[]": schedule_ids or None, + "limit": limit, + }) + + +_run(main) diff --git a/integrations/pagerduty/scripts/list_services.py b/integrations/pagerduty/scripts/list_services.py new file mode 100644 index 0000000..ffdc76d --- /dev/null +++ b/integrations/pagerduty/scripts/list_services.py @@ -0,0 +1,69 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + query = str(inputs.get("query", "") or "").strip() + + try: + limit = int(inputs.get("limit", 25) or 25) + except (TypeError, ValueError): + limit = 25 + + return rest("GET", "/services", cfg, params={ + "query": query, + "limit": limit, + }) + + +_run(main) diff --git a/integrations/pagerduty/scripts/list_users.py b/integrations/pagerduty/scripts/list_users.py new file mode 100644 index 0000000..4de7761 --- /dev/null +++ b/integrations/pagerduty/scripts/list_users.py @@ -0,0 +1,69 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + query = str(inputs.get("query", "") or "").strip() + + try: + limit = int(inputs.get("limit", 25) or 25) + except (TypeError, ValueError): + limit = 25 + + return rest("GET", "/users", cfg, params={ + "query": query, + "limit": limit, + }) + + +_run(main) diff --git a/integrations/pagerduty/scripts/resolve_incident.py b/integrations/pagerduty/scripts/resolve_incident.py new file mode 100644 index 0000000..c1ea07f --- /dev/null +++ b/integrations/pagerduty/scripts/resolve_incident.py @@ -0,0 +1,70 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + if not cfg.get("routing_key"): + raise Exception("routing_key is not configured") + + dedup_key = str(inputs.get("dedup_key", "") or "").strip() + if not dedup_key: + raise Exception("dedup_key is required") + + return events_enqueue(cfg, { + "routing_key": cfg["routing_key"], + "event_action": "resolve", + "dedup_key": dedup_key, + }) + + +_run(main) diff --git a/integrations/pagerduty/scripts/test_connection.py b/integrations/pagerduty/scripts/test_connection.py new file mode 100644 index 0000000..c0c2079 --- /dev/null +++ b/integrations/pagerduty/scripts/test_connection.py @@ -0,0 +1,60 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + rest("GET", "/users", cfg, params={"limit": 1}) + return {"ok": True} + + +_run(main) diff --git a/integrations/pagerduty/scripts/trigger_incident.py b/integrations/pagerduty/scripts/trigger_incident.py new file mode 100644 index 0000000..ee7b461 --- /dev/null +++ b/integrations/pagerduty/scripts/trigger_incident.py @@ -0,0 +1,93 @@ +import json, os, sys, uuid, urllib.parse, urllib.request, urllib.error + +REST = "https://api.pagerduty.com" +EVENTS = "https://events.pagerduty.com/v2/enqueue" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def rest(method, path, cfg, body=None, params=None, extra_headers=None): + url = REST + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean, doseq=True) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Token token=" + str(cfg.get("api_token", "")), + "Accept": "application/vnd.pagerduty+json;version=2", + } + if data is not None: + headers["Content-Type"] = "application/json" + if extra_headers: + headers.update(extra_headers) + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def events_enqueue(cfg, payload): + data = json.dumps(payload).encode("utf-8") + req = urllib.request.Request(EVENTS, data=data, headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + if not cfg.get("routing_key"): + raise Exception("routing_key is not configured") + + summary = str(inputs.get("summary", "")).strip() + if not summary: + raise Exception("summary is required") + + source = str(inputs.get("source", "")).strip() + if not source: + raise Exception("source is required") + + severity = str(inputs.get("severity", "") or "critical").strip() + if severity not in ("critical", "error", "warning", "info"): + severity = "critical" + + dedup_key = str(inputs.get("dedup_key", "") or "").strip() + component = str(inputs.get("component", "") or "").strip() + + payload = { + "summary": summary, + "source": source, + "severity": severity, + } + if component: + payload["component"] = component + + event = { + "routing_key": cfg["routing_key"], + "event_action": "trigger", + "payload": payload, + } + if dedup_key: + event["dedup_key"] = dedup_key + + return events_enqueue(cfg, event) + + +_run(main)