{"id":2102,"date":"2020-11-30T21:06:12","date_gmt":"2020-11-30T18:06:12","guid":{"rendered":"https:\/\/artem.services\/?p=2093"},"modified":"2020-12-07T12:14:48","modified_gmt":"2020-12-07T09:14:48","slug":"2102","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=2102&lang=en","title":{"rendered":"PagerDuty &#8212; Python script for creating events"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2094\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo.png\" alt=\"\" width=\"2709\" height=\"557\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo.png 2709w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-300x62.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-1024x211.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-768x158.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-1536x316.png 1536w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-2048x421.png 2048w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-954x196.png 954w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/pagerduty-logo-1354x278.png 1354w\" sizes=\"(max-width: 2709px) 100vw, 2709px\" \/><\/p>\n<p>This <strong>Python<\/strong> script creates events in <strong>PagerDuty<\/strong> using <strong>APIv2<\/strong>.<\/p>\n<p>The following script was taken as a <a href=\"https:\/\/airflow.readthedocs.io\/en\/latest\/_modules\/airflow\/providers\/pagerduty\/hooks\/pagerduty.html\" target=\"_blank\" rel=\"noopener noreferrer\">basis<\/a>.<\/p>\n<p>First you need to create a &quot;<strong>Routing Key<\/strong>&quot;, aka &quot;<strong>Integration Key<\/strong>&quot;, not to be confused with &quot;<strong>API Access Key<\/strong>&quot;, which can be used for any <strong>API<\/strong> calls, we only need a key from a specific service.<\/p>\n<p>Go to the service settings, in my case it is called &quot;<strong>AWS<\/strong>&quot;, and go to the &quot;<strong>Integrations<\/strong>&quot; tab<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2095\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08.png\" alt=\"\" width=\"2826\" height=\"1352\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08.png 2826w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-300x144.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-1024x490.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-768x367.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-1536x735.png 1536w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-2048x980.png 2048w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-954x456.png 954w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.17.08-1354x648.png 1354w\" sizes=\"(max-width: 2826px) 100vw, 2826px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Select &#8212; &quot;<strong>Add a new integration<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2096\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.10.png\" alt=\"\" width=\"1160\" height=\"872\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.10.png 1160w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.10-300x226.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.10-1024x770.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.10-768x577.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.10-954x717.png 954w\" sizes=\"(max-width: 1160px) 100vw, 1160px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Set the name and select &quot;<strong>Integration Type<\/strong>&quot; -&gt; &quot;<strong>Use our API directly<\/strong>&quot; -&gt; &quot;<strong>Events API v2<\/strong>&quot; and get &quot;<strong>Integration Key<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2097\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42.png\" alt=\"\" width=\"2060\" height=\"498\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42.png 2060w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-300x73.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-1024x248.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-768x186.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-1536x371.png 1536w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-2048x495.png 2048w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-954x231.png 954w, https:\/\/artem.services\/wp-content\/uploads\/2020\/11\/Screenshot-2020-11-12-at-16.18.42-1354x327.png 1354w\" sizes=\"(max-width: 2060px) 100vw, 2060px\" \/><\/p>\n<h3>main.py:<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport requests\r\nimport json\r\n\r\nfrom typing import Any, Dict, List, Optional\r\n\r\nrouting_key = 'abc123'\r\napi_url = 'https:\/\/events.pagerduty.com\/v2\/enqueue'\r\n\r\ndef create_event(\r\n    summary: str,\r\n    severity: str,\r\n    source: str = 'aws',\r\n    action: str = 'trigger',\r\n    dedup_key: Optional[str] = None,\r\n    custom_details: Optional[Any] = None,\r\n    group: Optional[str] = None,\r\n    component: Optional[str] = None,\r\n    class_type: Optional[str] = None,\r\n    images: Optional[List[Any]] = None,\r\n    links: Optional[List[Any]] = None\r\n) -&gt; Dict:\r\n    payload = {\r\n        &quot;summary&quot;: summary,\r\n        &quot;severity&quot;: severity,\r\n        &quot;source&quot;: source,\r\n    }\r\n    if custom_details is not None:\r\n        payload[&quot;custom_details&quot;] = custom_details\r\n    if component:\r\n        payload[&quot;component&quot;] = component\r\n    if group:\r\n        payload[&quot;group&quot;] = group\r\n    if class_type:\r\n        payload[&quot;class&quot;] = class_type\r\n\r\n    actions = ('trigger', 'acknowledge', 'resolve')\r\n    if action not in actions:\r\n        raise ValueError(&quot;Event action must be one of: %s&quot; % ', '.join(actions))\r\n    data = {\r\n        &quot;event_action&quot;: action,\r\n        &quot;routing_key&quot;: routing_key,\r\n        &quot;payload&quot;: payload,\r\n    }\r\n    if dedup_key:\r\n        data[&quot;dedup_key&quot;] = dedup_key\r\n    elif action != 'trigger':\r\n        raise ValueError(\r\n            &quot;The dedup_key property is required for event_action=%s events, and it must \\\r\n            be a string.&quot;\r\n            % action\r\n        )\r\n    if images is not None:\r\n        data[&quot;images&quot;] = images\r\n    if links is not None:\r\n        data[&quot;links&quot;] = links\r\n\r\n    response = requests.post(api_url, json = data)\r\n    print(response.content)\r\n\r\ndef main():\r\n    create_event(summary = &quot;Instance is down&quot;, severity = 'critical', custom_details = &quot;Instance ID: i-12345&quot;)\r\n  \r\nif __name__ == '__main__':\r\n    main()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p>Change the value of the variable &quot;<strong>routing_key<\/strong>&quot; to the value &quot;<strong>Integration Key<\/strong>&quot;<\/p><\/blockquote>\n<p>In this example, an event with the &quot;<strong>critical<\/strong>&quot; level will be created with the &quot;<strong>Instance is down<\/strong>&quot; title and &quot;<strong>Instance ID: i-12345<\/strong>&quot; will be specified in detail.<\/p>\n<p>The script can also be used to create events based on <strong>AWS CloudWatch Alarms<\/strong> without using the <strong>AWS PagerDuty<\/strong> integration. To do this, create an <strong>SNS<\/strong> topic, a <strong>Lambda<\/strong> function and specify the <strong>SNS<\/strong> topic as the source for the <strong>Lambda<\/strong> function. After that, you can create a <strong>CloudWatch Alarm<\/strong> and specify the <strong>SNS<\/strong> topic as an action when triggered.<\/p>\n<h3>main.py (Lambda + SNS):<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport requests\r\nimport json\r\n\r\nfrom typing import Any, Dict, List, Optional\r\n\r\nrouting_key = 'abc123'\r\napi_url = 'https:\/\/events.pagerduty.com\/v2\/enqueue'\r\n\r\ndef get_message_info():\r\n    subject = event[&quot;Records&quot;][0][&quot;Sns&quot;][&quot;Subject&quot;]\r\n    message = event[&quot;Records&quot;][0][&quot;Sns&quot;][&quot;Message&quot;]\r\n    return subject, message\r\n\r\ndef create_event(\r\n    summary: str,\r\n    severity: str,\r\n    source: str = 'cloudwatch',\r\n    action: str = 'trigger',\r\n    dedup_key: Optional[str] = None,\r\n    custom_details: Optional[Any] = None,\r\n    group: Optional[str] = None,\r\n    component: Optional[str] = None,\r\n    class_type: Optional[str] = None,\r\n    images: Optional[List[Any]] = None,\r\n    links: Optional[List[Any]] = None\r\n) -&gt; Dict:\r\n    payload = {\r\n        &quot;summary&quot;: summary,\r\n        &quot;severity&quot;: severity,\r\n        &quot;source&quot;: source,\r\n    }\r\n    if custom_details is not None:\r\n        payload[&quot;custom_details&quot;] = custom_details\r\n    if component:\r\n        payload[&quot;component&quot;] = component\r\n    if group:\r\n        payload[&quot;group&quot;] = group\r\n    if class_type:\r\n        payload[&quot;class&quot;] = class_type\r\n\r\n    actions = ('trigger', 'acknowledge', 'resolve')\r\n    if action not in actions:\r\n        raise ValueError(&quot;Event action must be one of: %s&quot; % ', '.join(actions))\r\n    data = {\r\n        &quot;event_action&quot;: action,\r\n        &quot;routing_key&quot;: routing_key,\r\n        &quot;payload&quot;: payload,\r\n    }\r\n    if dedup_key:\r\n        data[&quot;dedup_key&quot;] = dedup_key\r\n    elif action != 'trigger':\r\n        raise ValueError(\r\n            &quot;The dedup_key property is required for event_action=%s events, and it must \\\r\n            be a string.&quot;\r\n            % action\r\n        )\r\n    if images is not None:\r\n        data[&quot;images&quot;] = images\r\n    if links is not None:\r\n        data[&quot;links&quot;] = links\r\n\r\n    response = requests.post(api_url, json = data)\r\n    print(response.content)\r\n\r\ndef main(event, context):\r\n    info = get_message_info()\r\n    create_event(summary = info[0], severity='critical', custom_details = info[1])\r\n  \r\nif __name__ == '__main__':\r\n    main()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The <strong>Python<\/strong> package &quot;<strong>requests<\/strong>&quot; will have to be zipped with the <strong>Lambda<\/strong> function, since <strong>Lambda<\/strong> doesn&#8217;t have it. You can use &quot;<strong>virtualenv<\/strong>&quot; for this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Python script creates events in PagerDuty using APIv2. The following script was taken as a basis. First you need to create a &quot;Routing Key&quot;, aka &quot;Integration Key&quot;, not to be confused with &quot;API Access Key&quot;, which can be used for any API calls, we only need a key from a specific service. Go to &hellip; <a href=\"https:\/\/artem.services\/?p=2102&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;PagerDuty &#8212; Python script for creating events&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[599,405],"tags":[543,887,885,1737,1527,1529,1311],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2102"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2102"}],"version-history":[{"count":3,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2102\/revisions"}],"predecessor-version":[{"id":2117,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2102\/revisions\/2117"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}