{"id":945,"date":"2020-06-27T23:26:48","date_gmt":"2020-06-27T21:26:48","guid":{"rendered":"https:\/\/benedikt-merz.de\/Blog\/?p=945"},"modified":"2020-06-27T23:26:48","modified_gmt":"2020-06-27T21:26:48","slug":"grafana-alerts-with-nextcloud-talk","status":"publish","type":"post","link":"https:\/\/benedikt-merz.de\/Blog\/?p=945","title":{"rendered":"Grafana-Alerts with Nextcloud Talk"},"content":{"rendered":"\n<p>Grafana supports several notification channels for alerts. There are some messenger services available like Telegram or Threema Gateway. But <a rel=\"noreferrer noopener\" href=\"https:\/\/grafana.com\/\" target=\"_blank\">Grafana<\/a> does not support notifications by <a rel=\"noreferrer noopener\" href=\"https:\/\/nextcloud.com\/talk\/\" target=\"_blank\">Nextcloud Talk<\/a> out of the box.<\/p>\n\n\n\n<p>In this post you can read how to set up such notifications.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>What you should already have configured:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Grafana Server<br>I have version 7.0.3 but others should work, too.<\/li><li>Nextcloud Server<br>Talk App needs to be active<\/li><li>Nextcloud Talk App<br>installed on your Smartphone<\/li><li>Webserver with php activated<br>could be the same physical server as your Nextcloud server<\/li><\/ul>\n\n\n\n<p>First we configure Nextcloud to receive the notifications:<\/p>\n\n\n\n<p>Create a new user (e. g. &#8222;Chatbot&#8220;) with a strong password. This user will send us the alert messages.<\/p>\n\n\n\n<p>Then you log in to the Nextcloud server with the user who should receive the notifications. Go to the Talk app and create a new chat. Name it &#8222;Alerts&#8220; or any other name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"243\" height=\"148\" src=\"https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/create_chat.png\" alt=\"\" class=\"wp-image-947\"\/><figcaption>Create a new chat<\/figcaption><\/figure>\n\n\n\n<p>In the second step you are asked to invite users to the chat. Select the newly created user. You can also add further users.<\/p>\n\n\n\n<p>When your chat is open in the browser have a look at the URL. It should look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:\/\/&lt;your-server>\/nextcloud\/call\/&lt;chat-id><\/code><\/pre>\n\n\n\n<p>Remember the chat-id. You need it later to configure the php-script.<\/p>\n\n\n\n<p>To get always a notification when a new message is posted go to the chat options on the left side and set the notification option to &#8222;all messages&#8220;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"214\" height=\"388\" src=\"https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/chat_options.png\" alt=\"\" class=\"wp-image-950\" srcset=\"https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/chat_options.png 214w, https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/chat_options-165x300.png 165w\" sizes=\"auto, (max-width: 214px) 100vw, 214px\" \/><figcaption>Chat options<\/figcaption><\/figure>\n\n\n\n<p>Now install the Nextcloud Talk app on your smartphone and configure server connection. You should see the chat.<\/p>\n\n\n\n<p>In the next step we will create a php-script on our webserver. It receives the alert from grafana by <a rel=\"noreferrer noopener\" href=\"https:\/\/grafana.com\/docs\/grafana\/latest\/alerting\/notifications\/#webhook\" target=\"_blank\">webhook notification<\/a> and uses the <a rel=\"noreferrer noopener\" href=\"https:\/\/nextcloud-talk.readthedocs.io\/en\/latest\/\" target=\"_blank\">API of nextcloud<\/a> talk to write a message. Adapt the following php code and put it on your webserver.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$chat_user = \"&lt;new_nextcloud_user>\";\n$chat_pwd = \"&lt;passwort>\";\n$chat_token = \"&lt;chat-id>\";\n\n$json = file_get_contents(\"php:\/\/input\");\n\n\/\/ This can be used for debugging.\n\/\/ All Grafana alert information is stored\nfile_put_contents(\"alert.log\", $json);\n\n$daten = json_decode($json);\n\n\/\/ We select title, message and URL of the dashboard that triggers the alert\n$alert_title = strval($daten->title);\n$alert_message = strval($daten->message);\n$alert_url = strval($daten->ruleUrl);\n\n\/\/ This is the URL of the nextcloud chat\n$url = \"https:\/\/&lt;your_nextcloud_server_url>\/ocs\/v2.php\/apps\/spreed\/api\/v1\/chat\/$chat_token\";\n\n\/\/ Initialize curl session\n$ch = curl_init($url);\n\n\/\/ fill array with data for the nextcloud chat\n$data = array(\n    'token' => $chat_token,\n    'message' => \"$alert_title:\\r\\n$alert_message\\r\\n$alert_url\"\n);\n$payload = json_encode($data);\n\/\/ set curl options\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);\n\/\/ Following option needs to be set to false if you access\n\/\/ your nextcloud server by IP instead of domain name.\ncurl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLINFO_HEADER_OUT, true);\ncurl_setopt($ch, CURLOPT_POST, true);\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $payload);\ncurl_setopt($ch, CURLOPT_USERPWD, \"$chat_user:$chat_pwd\");\ncurl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n\n\/\/ Set HTTP Header\ncurl_setopt($ch, CURLOPT_HTTPHEADER, array(\n    'Content-Type: application\/json',\n    'Content-Length: ' . strlen($payload),\n    'Accept: application\/json',\n    'OCS-APIRequest: true')\n);\n\/\/ Execute the POST request\n$result = curl_exec($ch);\n\n\/\/ Close curl session\ncurl_close($ch);\n\n?><\/code><\/pre>\n\n\n\n<p>Don&#8217;t forget to set the permissions of the script to be accessible by your webserver user.<\/p>\n\n\n\n<p>Now the configuration of your webserver needs to be adapted to execute the php code. I have nginx running and added location tag according to nginx documentation. Don&#8217;t forget to reload the configuration afterwards.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>location \/html\/sensor\/ {\n   fastcgi_split_path_info ^(.+?\\.php)(\/.*)$;\n   if (!-f $document_root$fastcgi_script_name) {\n       return 404;\n   }\n\n   # Mitigate https:\/\/httpoxy.org\/ vulnerabilities\n   fastcgi_param HTTP_PROXY \"\";\n\n   fastcgi_pass php-handler;\n   fastcgi_index index.php;\n\n   # include the fastcgi_param setting\n   include fastcgi_params;\n\n   # SCRIPT_FILENAME parameter is used for PHP FPM determining\n   #  the script name. If it is not set in fastcgi_params file,\n   # i.e. \/etc\/nginx\/fastcgi_params or in the parent contexts,\n   # please comment off following line:\n   fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;\n}<\/code><\/pre>\n\n\n\n<p>Final step is to set up the webhook notification in Grafana. Open Grafana and go to Alerting \/ Notification channels. Add a new channel and select &#8222;webhook&#8220;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"596\" src=\"https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/notification_channel.png\" alt=\"\" class=\"wp-image-952\" srcset=\"https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/notification_channel.png 624w, https:\/\/benedikt-merz.de\/Blog\/wp-content\/uploads\/2020\/06\/notification_channel-300x287.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><figcaption>Grafana notification channel<\/figcaption><\/figure>\n\n\n\n<p>As everything is now ready and set up you can press the &#8222;Send Test&#8220;-Button. You should receive a message on your smartphone. The new created notification channel can now be used in Grafana to trigger alerts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grafana supports several notification channels for alerts. There are some messenger services available like Telegram or Threema Gateway. But Grafana does not support notifications by Nextcloud Talk out of the box. In this post you can read how to set up such notifications.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[107,105,106,108,109],"class_list":["post-945","post","type-post","status-publish","format-standard","hentry","category-technik","tag-alert","tag-grafana","tag-nextcloud","tag-nextcloud-talk","tag-notification"],"_links":{"self":[{"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=945"}],"version-history":[{"count":5,"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/945\/revisions"}],"predecessor-version":[{"id":953,"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/945\/revisions\/953"}],"wp:attachment":[{"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benedikt-merz.de\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}