{"id":3929,"date":"2018-12-11T14:40:27","date_gmt":"2018-12-11T19:40:27","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=3929"},"modified":"2018-12-11T16:30:30","modified_gmt":"2018-12-11T21:30:30","slug":"splunk-posting-to-microsoft-teams-via-webhooks","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=3929","title":{"rendered":"Splunk &#8211; Posting to Microsoft Teams via Webhooks"},"content":{"rendered":"\n<p>Using either the default webhook action or the <a rel=\"noreferrer noopener\" aria-label=\"Using either the default webhook action or the Teams-specific webhook, \n (opens in a new tab)\" href=\"https:\/\/splunkbase.splunk.com\/app\/3375\/\" target=\"_blank\">Teams-specific webhook<\/a>, Splunk searches can post data into Microsoft Teams. First, you need to get a webhook URL for your Teams channel. On the hamburger menu next to the channel, select &#8220;Connectors&#8221;. Select Webhook, provide a name for the webhook, and copy the webhook URL. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1019\" height=\"687\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk01.png\" alt=\"\" class=\"wp-image-3930\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk01.png 1019w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk01-300x202.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk01-768x518.png 768w\" sizes=\"auto, (max-width: 1019px) 100vw, 1019px\" \/><\/figure>\n\n\n\n<p>If you intend to use the generic webhook app, there is no need to install the Teams-specific one. The Teams-specific app gives you prettier output &amp; a &#8220;view in splunk&#8221; button. Download the <a href=\"https:\/\/splunkbase.splunk.com\/app\/3375\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"If you intend to use the generic webhook app, there is no need to install the Teams-specific one. The Teams-specific app gives you prettier output &amp; a &quot;view in splunk&quot; button. Download the app tgz. To install the app, go into &quot;Manage Apps&quot; and select &quot;Install app from file&quot;. \n (opens in a new tab)\">app tgz<\/a>. To install the app, go into &#8220;Manage Apps&#8221; and select &#8220;Install app from file&#8221;. <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"115\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk02-1024x115.png\" alt=\"\" class=\"wp-image-3931\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk02-1024x115.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk02-300x34.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk02-768x86.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk02.png 1899w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Click &#8216;Browse&#8230;&#8217; and find the tgz you downloaded. Click &#8216;Upload&#8217; to install the app to Splunk. <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"427\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk03.png\" alt=\"\" class=\"wp-image-3932\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk03.png 868w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk03-300x148.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk03-768x378.png 768w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><\/figure>\n\n\n\n<p>Now create a search for which you want to post data into your Teams channel. Click &#8220;Save As&#8221; and select &#8220;Alert&#8221;<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"350\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk04-1024x350.png\" alt=\"\" class=\"wp-image-3933\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk04-1024x350.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk04-300x102.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk04-768x262.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk04.png 1913w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Provide a title for the alert &#8212; you can use real-time or scheduled alerts. Once you&#8217;ve got the alert sorted, select &#8220;Add Actions&#8221; and select the Teams webhook action (or the generic webhook action if you intend to use that one). Paste in the URL from your Teams channel webhook and click &#8220;Save&#8221;. <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"799\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk05-1024x799.png\" alt=\"\" class=\"wp-image-3934\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk05-1024x799.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk05-300x234.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk05-768x599.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk05.png 1039w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You&#8221;ll see a confirmation that the alert has been saved. Close this.<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1018\" height=\"411\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk06.png\" alt=\"\" class=\"wp-image-3935\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk06.png 1018w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk06-300x121.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk06-768x310.png 768w\" sizes=\"auto, (max-width: 1018px) 100vw, 1018px\" \/><\/figure>\n\n\n\n<p>Now you would <em>think<\/em> you&#8217;d be ready to use it &#8230; but wait. Neither one works out of the box. In the Splunk log, you see error 400 &#8220;Bad data&#8221; reported. <br><\/p>\n\n\n\n<p>For the default webhook app, edit the Python script (\/opt\/splunk\/etc\/apps\/alert_webhook\/bin\/webhook.py in my case). Find the section where the JSON body is built. Teams requires a summary or title within the POST data. I just added a static summary, but you could do something fancier. <br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body&nbsp;=&nbsp;OrderedDict(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sid=settings.get('sid'),<br><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;summary='LJRWebhook',<br><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search_name=settings.get('search_name'),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app=settings.get('app'),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner=settings.get('owner'),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results_link=settings.get('results_link'),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result=settings.get('result')<\/pre>\n\n\n\n<p>For the Teams-specific webhook, edit the Python script (\/opt\/splunk\/etc\/apps\/alert_msteams\/bin\/teams.py in my case) and find the section where the facts list is populated. There&#8217;s too much data being sent through. There&#8217;s probably a way to filter it out in Splunk, but I don&#8217;t know how \ud83d\ude42 <\/p>\n\n\n\n<p>The <em>right<\/em> way to do it is select the most important items from settings.get(&#8216;result&#8217;).items that you want to be displayed within Teams and populate facts with those elements. I used a new list, strWantedKeys, to determine which keys should be added to the facts list. The quick\/ugly way is to just take the first <em>n<\/em> items from the result items (<strong><em>settings.get(&#8216;results&#8217;).items()[:7]<\/em><\/strong> gets seven &#8230; 8 produced a &#8216;bad payload received by generic incoming webhook&#8217; error from Teams. <br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>try:<\/code><br><code>    settings = json.loads(sys.stdin.read())<\/code><br><code>    print >> sys.stderr, \"DEBUG Settings: %s\" % settings<\/code><br><code>    url = settings['configuration'].get('url')<\/code><br><code>    facts = []<\/code><br><code><strong>    strWantedKeys = ['sourcetype', '_raw', 'host', 'source']<\/strong><\/code><br><code>    for key,value in settings.get('result').items():<\/code><br><code><strong>        if key in strWantedKeys:<\/strong><\/code><br><code>            facts.append({\"name\":key, \"value\":value})<\/code><br><code>    body = OrderedDict(<\/code><\/pre>\n\n\n\n<p>For reference, the original facts list was:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \"facts\": [{\n        \"name\": \"index\",\n        \"value\": \"history\"\n    }, {\n        \"name\": \"_raw\",\n        \"value\": \"Test push to teams 555\"\n    }, {\n        \"name\": \"_eventtype_color\",\n        \"value\": \"\"\n    }, {\n        \"name\": \"host\",\n        \"value\": \"10.10.15.134:8088\"\n    }, {\n        \"name\": \"source\",\n        \"value\": \"http:Sendmail testing\"\n    }, {\n        \"name\": \"_si\",\n        \"value\": [\"49cgc3e5e52e\", \"history\"]\n    }, {\n        \"name\": \"sourcetype\",\n        \"value\": \"mysourcetype\"\n    }, {\n        \"name\": \"_indextime\",\n        \"value\": \"1544554125\"\n    }, {\n        \"name\": \"punct\",\n        \"value\": \"___\"\n    }, {\n        \"name\": \"linecount\",\n        \"value\": \"\"\n    }, {\n        \"name\": \"_time\",\n        \"value\": \"1544554125\"\n    }, {\n        \"name\": \"eventtype\",\n        \"value\": \"\"\n    }, {\n        \"name\": \"_sourcetype\",\n        \"value\": \"mysourcetype\"\n    }, {\n        \"name\": \"_kv\",\n        \"value\": \"1\"\n    }, {\n        \"name\": \"_serial\",\n        \"value\": \"15\"\n    }, {\n        \"name\": \"_confstr\",\n        \"value\": \"source::http:Sendmail testing|host::10.10.15.134:8088|mysourcetype\"\n    }, {\n        \"name\": \"splunk_server\",\n        \"value\": \"\"\n    }]<\/code><\/pre>\n\n\n\n<p><strong>Now<\/strong> generate a message that matches your search &#8212; you&#8217;ll see a post created in your Teams channel.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"490\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2018\/12\/Teams-Splunk99.png\" alt=\"\" class=\"wp-image-3939\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk99.png 654w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2018\/12\/Teams-Splunk99-300x225.png 300w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Using either the default webhook action or the Teams-specific webhook, Splunk searches can post data into Microsoft Teams. First, you need to get a webhook URL for your Teams channel. On the hamburger menu next to the channel, select &#8220;Connectors&#8221;. Select Webhook, provide a name for the webhook, and copy the webhook URL. If you &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[677,30],"tags":[675,666,716,717,665,718],"class_list":["post-3929","post","type-post","status-publish","format-standard","hentry","category-office-365","category-system-administration","tag-did-you-know","tag-microsoft-teams","tag-splunk","tag-splunk-enterprise","tag-teams","tag-webhook"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3929"}],"version-history":[{"count":2,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3929\/revisions"}],"predecessor-version":[{"id":3940,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3929\/revisions\/3940"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}