| 1 |
#!/usr/bin/python |
| 2 |
# Copyright Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: pagerduty_alert |
| 10 |
short_description: Trigger, acknowledge or resolve PagerDuty incidents |
| 11 |
description: |
| 12 |
- This module lets you trigger, acknowledge or resolve a PagerDuty incident by sending events. |
| 13 |
author: |
| 14 |
- "Amanpreet Singh (@ApsOps)" |
| 15 |
- "Xiao Shen (@xshen1)" |
| 16 |
requirements: |
| 17 |
- PagerDuty API access |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
name: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- PagerDuty unique subdomain. Obsolete. It is not used with PagerDuty REST v2 API. |
| 30 |
api_key: |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- The pagerduty API key (readonly access), generated on the pagerduty site. |
| 34 |
- Required if O(api_version=v1). |
| 35 |
integration_key: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- The GUID of one of your 'Generic API' services. |
| 39 |
- This is the 'integration key' listed on a 'Integrations' tab of PagerDuty service. |
| 40 |
service_id: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- ID of PagerDuty service when incidents are triggered, acknowledged or resolved. |
| 44 |
- Required if O(api_version=v1). |
| 45 |
service_key: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- The GUID of one of your 'Generic API' services. Obsolete. Please use O(integration_key). |
| 49 |
state: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- Type of event to be sent. |
| 53 |
required: true |
| 54 |
choices: |
| 55 |
- 'triggered' |
| 56 |
- 'acknowledged' |
| 57 |
- 'resolved' |
| 58 |
api_version: |
| 59 |
type: str |
| 60 |
description: |
| 61 |
- The API version we want to use to run the module. |
| 62 |
- V1 is more limited with option we can provide to trigger incident. |
| 63 |
- V2 has more variables for example, O(severity), O(source), O(custom_details) and so on. |
| 64 |
default: 'v1' |
| 65 |
choices: |
| 66 |
- 'v1' |
| 67 |
- 'v2' |
| 68 |
version_added: 7.4.0 |
| 69 |
client: |
| 70 |
type: str |
| 71 |
description: |
| 72 |
- The name of the monitoring client that is triggering this event. |
| 73 |
client_url: |
| 74 |
type: str |
| 75 |
description: |
| 76 |
- The URL of the monitoring client that is triggering this event. |
| 77 |
component: |
| 78 |
type: str |
| 79 |
description: |
| 80 |
- Component of the source machine that is responsible for the event, for example C(mysql) or C(eth0). |
| 81 |
version_added: 7.4.0 |
| 82 |
custom_details: |
| 83 |
type: dict |
| 84 |
description: |
| 85 |
- Additional details about the event and affected system. |
| 86 |
- A dictionary with custom keys and values. |
| 87 |
version_added: 7.4.0 |
| 88 |
desc: |
| 89 |
type: str |
| 90 |
description: |
| 91 |
- For O(state=triggered) - Required. Short description of the problem that led to this trigger. This field (or a truncated |
| 92 |
version) is used when generating phone calls, SMS messages and alert emails. It also appears on the incidents tables |
| 93 |
in the PagerDuty UI. The maximum length is 1024 characters. |
| 94 |
- For O(state=acknowledged) or O(state=resolved) - Text that appears in the incident's log associated with this event. |
| 95 |
default: Created via Ansible |
| 96 |
incident_class: |
| 97 |
type: str |
| 98 |
description: |
| 99 |
- The class/type of the event, for example C(ping failure) or C(cpu load). |
| 100 |
version_added: 7.4.0 |
| 101 |
incident_key: |
| 102 |
type: str |
| 103 |
description: |
| 104 |
- Identifies the incident to which this O(state) should be applied. |
| 105 |
- For O(state=triggered) - If there is no open (in other words unresolved) incident with this key, a new one is created. |
| 106 |
If there is already an open incident with a matching key, this event is appended to that incident's log. The event |
| 107 |
key provides an easy way to 'de-dup' problem reports. If no O(incident_key) is provided, then it is generated by PagerDuty. |
| 108 |
- For O(state=acknowledged) or O(state=resolved) - This should be the incident_key you received back when the incident |
| 109 |
was first opened by a trigger event. Acknowledge events referencing resolved or nonexistent incidents is discarded. |
| 110 |
link_url: |
| 111 |
type: str |
| 112 |
description: |
| 113 |
- Relevant link URL to the alert. For example, the website or the job link. |
| 114 |
version_added: 7.4.0 |
| 115 |
link_text: |
| 116 |
type: str |
| 117 |
description: |
| 118 |
- A short description of the O(link_url). |
| 119 |
version_added: 7.4.0 |
| 120 |
source: |
| 121 |
type: str |
| 122 |
description: |
| 123 |
- The unique location of the affected system, preferably a hostname or FQDN. |
| 124 |
- Required in case of O(state=trigger) and O(api_version=v2). |
| 125 |
version_added: 7.4.0 |
| 126 |
severity: |
| 127 |
type: str |
| 128 |
description: |
| 129 |
- The perceived severity of the status the event is describing with respect to the affected system. |
| 130 |
- Required in case of O(state=trigger) and O(api_version=v2). |
| 131 |
default: 'critical' |
| 132 |
choices: |
| 133 |
- 'critical' |
| 134 |
- 'warning' |
| 135 |
- 'error' |
| 136 |
- 'info' |
| 137 |
version_added: 7.4.0 |
| 138 |
""" |
| 139 |
|
| 140 |
EXAMPLES = r""" |
| 141 |
- name: Trigger an incident with just the basic options |
| 142 |
community.general.pagerduty_alert: |
| 143 |
name: companyabc |
| 144 |
integration_key: xxx |
| 145 |
api_key: yourapikey |
| 146 |
service_id: PDservice |
| 147 |
state: triggered |
| 148 |
desc: problem that led to this trigger |
| 149 |
|
| 150 |
- name: Trigger an incident with more options |
| 151 |
community.general.pagerduty_alert: |
| 152 |
integration_key: xxx |
| 153 |
api_key: yourapikey |
| 154 |
service_id: PDservice |
| 155 |
state: triggered |
| 156 |
desc: problem that led to this trigger |
| 157 |
incident_key: somekey |
| 158 |
client: Sample Monitoring Service |
| 159 |
client_url: http://service.example.com |
| 160 |
|
| 161 |
- name: Acknowledge an incident based on incident_key |
| 162 |
community.general.pagerduty_alert: |
| 163 |
integration_key: xxx |
| 164 |
api_key: yourapikey |
| 165 |
service_id: PDservice |
| 166 |
state: acknowledged |
| 167 |
incident_key: somekey |
| 168 |
desc: "some text for incident's log" |
| 169 |
|
| 170 |
- name: Resolve an incident based on incident_key |
| 171 |
community.general.pagerduty_alert: |
| 172 |
integration_key: xxx |
| 173 |
api_key: yourapikey |
| 174 |
service_id: PDservice |
| 175 |
state: resolved |
| 176 |
incident_key: somekey |
| 177 |
desc: "some text for incident's log" |
| 178 |
|
| 179 |
- name: Trigger an v2 incident with just the basic options |
| 180 |
community.general.pagerduty_alert: |
| 181 |
integration_key: xxx |
| 182 |
api_version: v2 |
| 183 |
source: My Ansible Script |
| 184 |
state: triggered |
| 185 |
desc: problem that led to this trigger |
| 186 |
|
| 187 |
- name: Trigger an v2 incident with more options |
| 188 |
community.general.pagerduty_alert: |
| 189 |
integration_key: xxx |
| 190 |
api_version: v2 |
| 191 |
source: My Ansible Script |
| 192 |
state: triggered |
| 193 |
desc: problem that led to this trigger |
| 194 |
incident_key: somekey |
| 195 |
client: Sample Monitoring Service |
| 196 |
client_url: http://service.example.com |
| 197 |
component: mysql |
| 198 |
incident_class: ping failure |
| 199 |
link_url: https://pagerduty.com |
| 200 |
link_text: PagerDuty |
| 201 |
|
| 202 |
- name: Acknowledge an incident based on incident_key using v2 |
| 203 |
community.general.pagerduty_alert: |
| 204 |
api_version: v2 |
| 205 |
integration_key: xxx |
| 206 |
incident_key: somekey |
| 207 |
state: acknowledged |
| 208 |
|
| 209 |
- name: Resolve an incident based on incident_key |
| 210 |
community.general.pagerduty_alert: |
| 211 |
api_version: v2 |
| 212 |
integration_key: xxx |
| 213 |
incident_key: somekey |
| 214 |
state: resolved |
| 215 |
""" |
| 216 |
import json |
| 217 |
from datetime import datetime |
| 218 |
from urllib.parse import urlencode, urlparse, urlunparse |
| 219 |
|
| 220 |
from ansible.module_utils.basic import AnsibleModule |
| 221 |
from ansible.module_utils.urls import fetch_url |
| 222 |
|
| 223 |
|
| 224 |
def check(module, name, state, service_id, integration_key, api_key, incident_key=None, http_call=fetch_url): |
| 225 |
url = "https://api.pagerduty.com/incidents" |
| 226 |
headers = { |
| 227 |
"Content-type": "application/json", |
| 228 |
"Authorization": f"Token token={api_key}", |
| 229 |
"Accept": "application/vnd.pagerduty+json;version=2", |
| 230 |
} |
| 231 |
|
| 232 |
params = {"service_ids[]": service_id, "sort_by": "incident_number:desc", "time_zone": "UTC"} |
| 233 |
if incident_key: |
| 234 |
params["incident_key"] = incident_key |
| 235 |
|
| 236 |
url_parts = list(urlparse(url)) |
| 237 |
url_parts[4] = urlencode(params, True) |
| 238 |
|
| 239 |
url = urlunparse(url_parts) |
| 240 |
|
| 241 |
response, info = http_call(module, url, method="get", headers=headers) |
| 242 |
|
| 243 |
if info["status"] != 200: |
| 244 |
module.fail_json(msg=f"failed to check current incident status.Reason: {info['msg']}") |
| 245 |
|
| 246 |
incidents = json.loads(response.read())["incidents"] |
| 247 |
msg = "No corresponding incident" |
| 248 |
|
| 249 |
if len(incidents) == 0: |
| 250 |
if state in ("acknowledged", "resolved"): |
| 251 |
return msg, False |
| 252 |
return msg, True |
| 253 |
elif state != incidents[0]["status"]: |
| 254 |
return incidents[0], True |
| 255 |
|
| 256 |
return incidents[0], False |
| 257 |
|
| 258 |
|
| 259 |
def send_event_v1(module, service_key, event_type, desc, incident_key=None, client=None, client_url=None): |
| 260 |
url = "https://events.pagerduty.com/generic/2010-04-15/create_event.json" |
| 261 |
headers = {"Content-type": "application/json"} |
| 262 |
|
| 263 |
data = { |
| 264 |
"service_key": service_key, |
| 265 |
"event_type": event_type, |
| 266 |
"incident_key": incident_key, |
| 267 |
"description": desc, |
| 268 |
"client": client, |
| 269 |
"client_url": client_url, |
| 270 |
} |
| 271 |
|
| 272 |
response, info = fetch_url(module, url, method="post", headers=headers, data=json.dumps(data)) |
| 273 |
if info["status"] != 200: |
| 274 |
module.fail_json(msg=f"failed to {event_type}. Reason: {info['msg']}") |
| 275 |
json_out = json.loads(response.read()) |
| 276 |
return json_out |
| 277 |
|
| 278 |
|
| 279 |
def send_event_v2(module, service_key, event_type, payload, link, incident_key=None, client=None, client_url=None): |
| 280 |
url = "https://events.pagerduty.com/v2/enqueue" |
| 281 |
headers = {"Content-type": "application/json"} |
| 282 |
data = { |
| 283 |
"routing_key": service_key, |
| 284 |
"event_action": event_type, |
| 285 |
"payload": payload, |
| 286 |
"client": client, |
| 287 |
"client_url": client_url, |
| 288 |
} |
| 289 |
if link: |
| 290 |
data["links"] = [link] |
| 291 |
if incident_key: |
| 292 |
data["dedup_key"] = incident_key |
| 293 |
if event_type != "trigger": |
| 294 |
data.pop("payload") |
| 295 |
response, info = fetch_url(module, url, method="post", headers=headers, data=json.dumps(data)) |
| 296 |
if info["status"] != 202: |
| 297 |
module.fail_json(msg=f"failed to {event_type}. Reason: {info['msg']}") |
| 298 |
json_out = json.loads(response.read()) |
| 299 |
return json_out, True |
| 300 |
|
| 301 |
|
| 302 |
def main(): |
| 303 |
module = AnsibleModule( |
| 304 |
argument_spec=dict( |
| 305 |
name=dict(), |
| 306 |
api_key=dict(no_log=True), |
| 307 |
integration_key=dict(no_log=True), |
| 308 |
service_id=dict(), |
| 309 |
service_key=dict(no_log=True), |
| 310 |
state=dict(required=True, choices=["triggered", "acknowledged", "resolved"]), |
| 311 |
api_version=dict(type="str", default="v1", choices=["v1", "v2"]), |
| 312 |
client=dict(), |
| 313 |
client_url=dict(), |
| 314 |
component=dict(), |
| 315 |
custom_details=dict(type="dict"), |
| 316 |
desc=dict(default="Created via Ansible"), |
| 317 |
incident_class=dict(), |
| 318 |
incident_key=dict(no_log=False), |
| 319 |
link_url=dict(), |
| 320 |
link_text=dict(), |
| 321 |
source=dict(), |
| 322 |
severity=dict(default="critical", choices=["critical", "warning", "error", "info"]), |
| 323 |
), |
| 324 |
required_if=[ |
| 325 |
("api_version", "v1", ["service_id", "api_key"]), |
| 326 |
("state", "acknowledged", ["incident_key"]), |
| 327 |
("state", "resolved", ["incident_key"]), |
| 328 |
], |
| 329 |
required_one_of=[("service_key", "integration_key")], |
| 330 |
supports_check_mode=True, |
| 331 |
) |
| 332 |
|
| 333 |
name = module.params["name"] |
| 334 |
service_id = module.params.get("service_id") |
| 335 |
integration_key = module.params.get("integration_key") |
| 336 |
service_key = module.params.get("service_key") |
| 337 |
api_key = module.params.get("api_key") |
| 338 |
state = module.params.get("state") |
| 339 |
client = module.params.get("client") |
| 340 |
client_url = module.params.get("client_url") |
| 341 |
desc = module.params.get("desc") |
| 342 |
incident_key = module.params.get("incident_key") |
| 343 |
payload = { |
| 344 |
"summary": desc, |
| 345 |
"source": module.params.get("source"), |
| 346 |
"timestamp": datetime.now().isoformat(), |
| 347 |
"severity": module.params.get("severity"), |
| 348 |
"component": module.params.get("component"), |
| 349 |
"class": module.params.get("incident_class"), |
| 350 |
"custom_details": module.params.get("custom_details"), |
| 351 |
} |
| 352 |
link = {} |
| 353 |
if module.params.get("link_url"): |
| 354 |
link["href"] = module.params.get("link_url") |
| 355 |
if module.params.get("link_text"): |
| 356 |
link["text"] = module.params.get("link_text") |
| 357 |
if integration_key is None: |
| 358 |
integration_key = service_key |
| 359 |
module.warn('"service_key" is obsolete parameter and will be removed. Please, use "integration_key" instead') |
| 360 |
|
| 361 |
state_event_dict = { |
| 362 |
"triggered": "trigger", |
| 363 |
"acknowledged": "acknowledge", |
| 364 |
"resolved": "resolve", |
| 365 |
} |
| 366 |
|
| 367 |
event_type = state_event_dict[state] |
| 368 |
if module.params.get("api_version") == "v1": |
| 369 |
out, changed = check(module, name, state, service_id, integration_key, api_key, incident_key) |
| 370 |
if not module.check_mode and changed is True: |
| 371 |
out = send_event_v1(module, integration_key, event_type, desc, incident_key, client, client_url) |
| 372 |
else: |
| 373 |
changed = True |
| 374 |
if event_type == "trigger" and not payload["source"]: |
| 375 |
module.fail_json(msg='"service" is a required variable for v2 api endpoint.') |
| 376 |
out, changed = send_event_v2( |
| 377 |
module, |
| 378 |
integration_key, |
| 379 |
event_type, |
| 380 |
payload, |
| 381 |
link, |
| 382 |
incident_key, |
| 383 |
client, |
| 384 |
client_url, |
| 385 |
) |
| 386 |
|
| 387 |
module.exit_json(result=out, changed=changed) |
| 388 |
|
| 389 |
|
| 390 |
if __name__ == "__main__": |
| 391 |
main() |