{"id":38350,"date":"2016-07-28T14:51:33","date_gmt":"2016-07-28T09:21:33","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38350"},"modified":"2016-07-30T06:09:28","modified_gmt":"2016-07-30T00:39:28","slug":"monit-with-slack-notifications","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/monit-with-slack-notifications\/","title":{"rendered":"Monit with Slack notifications"},"content":{"rendered":"<p><a href=\"https:\/\/mmonit.com\/monit\/\">Monit<\/a> is a small Open Source utility for managing and monitoring Unix systems, Monit sends out notifications when an alert is triggered usually through emails . Since email is getting a bit old approach nowadays, it\u2019s way more intuitive to receive alerts via push notifications on your smartphone or chat client. <a href=\"https:\/\/slack.com\/\">Slack<\/a> provides supports to the many service integrations and also an Incoming Web-hooks integration. This way you can receive those Monit alerts in a specific channel. Enable the mobile Slack application on your phone, enable push notifications on that specific channel and you\u2019re all set. In this blog, we demonstrate how this can be done by sending the notification to Slack.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38352\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/monit_slack.png\" alt=\"monit_slack\" width=\"615\" height=\"308\" \/><\/p>\n<p><strong>Setup Prerequisites<\/strong><\/p>\n<ul>\n<li>Monit service installed on the server<\/li>\n<li>A valid Slack account with the channel where you can <a title=\"Continuous Integration\" href=\"http:\/\/www.tothenew.com\/devops-automation-consulting\">enable the integration<\/a> to receive the push notifications<\/li>\n<\/ul>\n<p><strong>Create an incoming webhooks<\/strong><\/p>\n<ul>\n<li>Go to https:\/\/&lt;yourteam&gt;.slack.com\/apps\/manage\/custom-integrations<\/li>\n<li>Click Incoming WebHooks<\/li>\n<li>Click Add Configuration<\/li>\n<li>Select an existing channel or create a new one (e.g. #monit) &#8211; you can change as per your preference<\/li>\n<li>Click Add Incoming WebHooks integration<\/li>\n<li>Copy the Webhook URL<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38375\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/monitinter.png\" alt=\"monitinter\" width=\"638\" height=\"272\" \/><\/p>\n<p>Let&#8217;s create a ruby script that uses curl to POST a message to a channel on Slack. This shell script does a simple http POST onto the webhook with our alert:<\/p>\n<p>[js]<br \/>\n#!\/usr\/local\/bin\/ruby<\/p>\n<p>require &#8216;net\/https&#8217;<br \/>\nrequire &#8216;json&#8217;<\/p>\n<p>uri = URI.parse(&quot;https:\/\/hooks.slack.com\/services\/T02GN5HNP\/xxxxxxxx\/xxxxxxxxxxxxxxxxxxx&quot;)<br \/>\nhttp = Net::HTTP.new(uri.host, uri.port)<br \/>\nhttp.use_ssl = true<br \/>\nrequest = Net::HTTP::Post.new(uri.request_uri, {&#8216;Content-Type&#8217; =&amp;gt; &#8216;application\/json&#8217;})<br \/>\nrequest.body = {<br \/>\n&quot;channel&quot; =&amp;gt; &quot;@channel_name&quot;,<br \/>\n&quot;username&quot; =&amp;gt; &quot;@username&quot;,<br \/>\n&quot;icon_emoji&quot; =&gt; &quot;:.trollface:&quot;,<br \/>\n&quot;text&quot; =&amp;gt; &quot;[#{ENV[&#8216;MONIT_HOST&#8217;]}] #{ENV[&#8216;MONIT_SERVICE&#8217;]} &#8211; #{ENV[&#8216;MONIT_DESCRIPTION&#8217;]}&quot;<br \/>\n}.to_json<br \/>\nresponse = http.request(request)<br \/>\nputs response.body<\/p>\n<p>[\/js]<\/p>\n<p>You can pick whatever Slack icon you want to use OR you can upload your own for an emoji and use that(supported by the Slack). Obviously, you&#8217;ll have to change the URI, channel, username and the like to what applies to you.<\/p>\n<p>Save the script file on the instances with the name called slack.rb (remember to replace channel, username with your slack.com integration token). We can now use this script in our Monit control file to get Slack alerts if a check fails like below screenshot:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38409\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/Screenshot-from-2016-07-28-144711.png\" alt=\"Screenshot from 2016-07-28 14:47:11\" width=\"590\" height=\"54\" \/><\/p>\n<p>You will need to invoke this file whenever something happens, so to test it out, we&#8217;re going to create a Jenkins-slave file to monitor Jenkins-slave agent. Let create the sample file to monitor the Jenkins-slave process and PID file if any conditions fail then this will trigger slack.rb which we called through bash script \/home\/devops\/notify.sh in \/etc\/monit.d\/jenkins-slave.<\/p>\n<p>[js]<br \/>\ncheck process jenkins-slave with pidfile \/var\/run\/jenkins-slave.pid<br \/>\n    start program = &quot;\/etc\/init.d\/jenkins-slave start&quot;<br \/>\n    stop  program = &quot;\/etc\/init.d\/jenkins-slave stop&quot;<br \/>\n    if does not exist for 2 cycle<br \/>\n    then exec &quot;\/bin\/bash -c \/home\/devops\/notify.sh&quot;<br \/>\n    else if succeeded for 2 cycle then exec &quot;usr\/local\/bin\/ruby \/home\/devops\/slack.rb&quot;<br \/>\n    group jenkins-slave<\/p>\n<p>check file jenkins_slave_pid with path \/var\/run\/jenkins-slave.pid<br \/>\n    if does not exist for 2 cycle<br \/>\n    then exec &quot;\/bin\/bash -c \/home\/devops\/notify.sh&quot;<br \/>\n    else if succeeded for 2 cycle then exec &quot;usr\/local\/bin\/ruby \/home\/devops\/slack.rb&quot;<br \/>\n    group jenkins-slave<br \/>\n[\/js]<\/p>\n<p>In Monit configuration file jenkins-slave calling \/home\/devops\/notify.sh when any of the defined events is missing like jenkins-slave process or PID file it will trigger the notification on the Slack channel you mentioned in the slack.rb script and then start the jenkins-slave.<\/p>\n<p>[js]<br \/>\n#!\/bin\/bash<br \/>\n\/usr\/local\/bin\/ruby \/home\/devops\/slack.rb<br \/>\n\/etc\/init.d\/jenkins-slave start<br \/>\n[\/js]<\/p>\n<p>After consecutive two successful cycles by Monit then it will trigger the status of the jenkins-slave on Slack channel.There&#8217;s a few second delay but check out Slack! You should see the message with your info all in there.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38407\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/Screenshot-from-2016-07-28-144258.png\" alt=\"Screenshot from 2016-07-28 14:42:58\" width=\"540\" height=\"98\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monit is a small Open Source utility for managing and monitoring Unix systems, Monit sends out notifications when an alert is triggered usually through emails . Since email is getting a bit old approach nowadays, it\u2019s way more intuitive to receive alerts via push notifications on your smartphone or chat client. Slack provides supports to [&hellip;]<\/p>\n","protected":false},"author":215,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[2348,1],"tags":[1895,3813,3811,2078,3361,3812,3814],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38350"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/215"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38350"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38350\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}