{"id":1823,"date":"2016-04-15T13:46:32","date_gmt":"2016-04-15T10:46:32","guid":{"rendered":"http:\/\/joseph.zikusooka.com\/?p=1823"},"modified":"2016-04-15T14:14:27","modified_gmt":"2016-04-15T11:14:27","slug":"receive-linux-server-alerts-phone-via-telegram","status":"publish","type":"post","link":"https:\/\/joseph.zikusooka.com\/?p=1823","title":{"rendered":"How to receive Linux server alerts on your phone via telegram"},"content":{"rendered":"<p>To monitor my Linux servers, I have been using <a href=\"https:\/\/www.icinga.org\">icinga<\/a> since its forking from the popular <a href=\"https:\/\/www.nagios.org\">Nagios<\/a>. Icinga has really matured over the last few years and currently at version 2.4.4, I would recommend anyone serious at network and server monitoring to check this open source project. Installation and setup of icinga2 is somewhat complex, but if you are an experienced command line sort of guy, its not that hard at all. In fact the introduction of <a href=\"https:\/\/www.icinga.org\/products\/screenshots\/icinga-web-2\/\">icingaweb2<\/a> has made it possible to have an easy to use interface to setup your monitoring needs. I will not delve into the nitty gritty of install and setup. For that please take a look at the detailed <a href=\"https:\/\/www.icinga.org\/resources\/docs\/\">documentation<\/a>.<\/p>\n<p>Several alerts are possible when using Icinga including e-mail, sms, XMPP, IRC, twitter etc. I wanted a way to get notified of host and service failures using telegram. Luckily, telegram provides an API to do just that.<\/p>\n<p>The secret source in all of this; is to install a command line interface for telegram called <a href=\"https:\/\/github.com\/vysheng\/tg\">telegram-cli<\/a> . In order for telegram-cli to compile without any issues, ensure that all dependencies are installed<\/p>\n<p>For Fedora:<\/p>\n<pre class=\"western\"><code class=\"western\">sudo dnf -y install\r\nlua-devel openssl-devel libconfig-devel readline-devel libevent-devel\r\njansson-devel python-devel<\/code><\/pre>\n<p>For Ubuntu\/Debian:<\/p>\n<pre class=\"western\"><code class=\"western\">sudo apt-get install\r\nlibreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev\r\nlibevent-dev libjansson-dev libpython-dev make <\/code>\r\n<\/pre>\n<p>Now install telegram-cli<\/p>\n<pre class=\"western\"><code class=\"western\">cd \/usr\/src && git clone \u2013recursive https:\/\/github.com\/vysheng\/tg.git<\/code><\/pre>\n<p><em>Note: Make sure you add the recursive flag to git clone, otherwise make will fail<\/em><\/p>\n<pre class=\"western\"><code class=\"western\">cd tg<\/code><\/pre>\n<pre class=\"western\"><code class=\"western\">.\/configure &amp;&amp; make<\/code><\/pre>\n<p>The binary is under .\/bin\/telegram-cli. Simply create a symbolic link to it.<\/p>\n<pre class=\"western\"><code class=\"western\">cd \/usr\/sbin &amp;&amp; ln -s \/usr\/src\/tg\/bin\/telegram-cli .<\/code><\/pre>\n<p>Thats it for the installation of telegram-cli. Please read the README file in the sources root folder, especially on how to use this application.<\/p>\n<p><strong>Icinga2 telegram-cli setup<\/strong><\/p>\n<p>First ensure that the user icinga (created during icinga install process) can send a message using telegram-cli. Remember all alerts go out under the icinga user, so you have to get this part working first, lest you spend hours troubleshooting.<\/p>\n<p>Temporarily change the shell for icinga from &#8216;\/sbin\/nologin&#8217; to &#8216;\/bin\/bash.&#8217; Remember to change back to nologin for icinga user when testing is completed.<\/p>\n<pre class=\"western\"><code class=\"western\">usermod -s \/bin\/bash icinga<\/code><\/pre>\n<p>Now change to user icinga: Assumming that you are root user, if not you will be prompted for a password, which you would setup as root.<\/p>\n<pre class=\"western\"><code class=\"western\">su -l icinga<\/code><\/pre>\n<pre class=\"western\"><code class=\"western\">telegram-cli<\/code><\/pre>\n<p><em>Enter your phone number and SMS code sent when prompted<\/em><\/p>\n<p>Then test to see if you can send a message:<\/p>\n<pre class=\"western\"><code class=\"western\">telegram-cli -D -W -e \"msg [PEER_NAME] 'Hello World'\"<\/code><\/pre>\n<p>If it works, then you can proceed to configure icinga2.<\/p>\n<p>Create notification script for services<\/p>\n<p>cat &gt; \/etc\/icinga2\/scripts\/notify_services_via_telegram.sh &lt;&lt; EOF<\/p>\n<p>#!\/usr\/bin\/env bash<\/p>\n<p>TELEGRAM_CMD=\/usr\/sbin\/telegram-cli<\/p>\n<p>TELEGRAM_OPTS=&#8221;-D -W&#8221;<\/p>\n<p>TELEGRAM_PEER=[PEER_NAME]<\/p>\n<p>TELEGRAM_MSG=$(cat &lt;&lt;TEMPLATE<\/p>\n<p>[$HOSTALIAS] $NOTIFICATIONTYPE: $SERVICEDESC is $SERVICESTATE<\/p>\n<p>TEMPLATE<\/p>\n<p>)<\/p>\n<p>$TELEGRAM_CMD $TELEGRAM_OPTS -e &#8220;msg $TELEGRAM_PEER &#8216;$TELEGRAM_MSG'&#8221;<\/p>\n<p>EOF<\/p>\n<p>Ensure that above script is executable:<\/p>\n<pre class=\"western\"><code class=\"western\">chmod 755 \/etc\/icinga2\/scripts\/notify_services_via_telegram.sh<\/code><\/pre>\n<p>Proceed to configure commands, notifications, and hosts config files: Note: Am only setting up notifications for services. You can do the same for hosts information.<\/p>\n<p>cat &gt;&gt; \/etc\/icinga2\/conf.d\/commands.conf &lt;&lt;EOF<\/p>\n<p>\/** Telegram *\/<\/p>\n<p>object NotificationCommand &#8220;telegram-service-notification&#8221; {<\/p>\n<p>import &#8220;plugin-notification-command&#8221;<\/p>\n<p>command = [ SysconfDir + &#8220;\/icinga2\/scripts\/notify_services_via_telegram.sh&#8221; ]<\/p>\n<p>env = {<\/p>\n<p>&#8220;NOTIFICATIONTYPE&#8221; = &#8220;$notification.type$&#8221;<\/p>\n<p>&#8220;SERVICEDESC&#8221; = &#8220;$service.name$&#8221;<\/p>\n<p>&#8220;HOSTALIAS&#8221; = &#8220;$host.display_name$&#8221;,<\/p>\n<p>&#8220;HOSTADDRESS&#8221; = &#8220;$address$&#8221;,<\/p>\n<p>&#8220;SERVICESTATE&#8221; = &#8220;$service.state$&#8221;,<\/p>\n<p>&#8220;LONGDATETIME&#8221; = &#8220;$icinga.long_date_time$&#8221;,<\/p>\n<p>&#8220;SERVICEOUTPUT&#8221; = &#8220;$service.output$&#8221;,<\/p>\n<p>&#8220;NOTIFICATIONAUTHORNAME&#8221; = &#8220;$notification.author$&#8221;,<\/p>\n<p>&#8220;NOTIFICATIONCOMMENT&#8221; = &#8220;$notification.comment$&#8221;,<\/p>\n<p>&#8220;HOSTDISPLAYNAME&#8221; = &#8220;$host.display_name$&#8221;,<\/p>\n<p>&#8220;SERVICEDISPLAYNAME&#8221; = &#8220;$service.display_name$&#8221;,<\/p>\n<p>&#8220;USEREMAIL&#8221; = &#8220;$user.email$&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>EOF<\/p>\n<p>cat &gt;&gt; \/etc\/icinga2\/conf.d\/notifications.conf &lt;&lt;EOF<\/p>\n<p>\/** Telegram *\/<\/p>\n<p>apply Notification &#8220;telegram-icingaadmin&#8221; to Service {<\/p>\n<p>import &#8220;telegram-service-notification&#8221;<\/p>\n<p>user_groups = host.vars.notification.telegram.groups<\/p>\n<p>users = host.vars.notification.telegram.users<\/p>\n<p>assign where host.vars.notification.telegram<\/p>\n<p>}<\/p>\n<p>EOF<\/p>\n<p>To get alerts on services for a host, add the following stanza to the host configuration in \/etc\/icinga2\/conf.d\/hosts.conf<\/p>\n<p>\/* Telegram *\/<\/p>\n<p>vars.notification[&#8220;telegram&#8221;] = {<\/p>\n<p>groups = [ &#8220;icingaadmins&#8221; ]<\/p>\n<p>}<\/p>\n<p>Finally, check your config files<\/p>\n<pre class=\"western\"><code class=\"western\">icinga2 daemon -C<\/code><\/pre>\n<p>Restart icinga if all is OK. You will now be notified via telegram when services fail for the above configured host.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To monitor my Linux servers, I have been using icinga since its forking from the popular Nagios. Icinga has really matured over the last few years and currently at version 2.4.4, I would recommend anyone serious at network and server monitoring to check this open source project. Installation and setup of icinga2 is somewhat complex, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1823","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"_links":{"self":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/1823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1823"}],"version-history":[{"count":12,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/1823\/revisions"}],"predecessor-version":[{"id":1835,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/1823\/revisions\/1835"}],"wp:attachment":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}