Home /  MMonit /  Slack Notification

Introduction | M/Monit | Monit

Both M/Monit and Monit can quite easy be setup to send alerts to almost any online service. This is especially straightforward if the service sports a HTTP API. In this example we demonstrate how this can be done by sending notification to slack.com.

On the Slack side you will first need to activate an integration and obtain a token at slack.com integrations by adding an "Incoming WebHooks" integration.

M/Monit

Once you have your slack.com integration setup, go into M/Monit -> Admin -> Alerts, create a new Rule, select the Execute program action and copy the following ruby script into the program box:

ruby -e "
    require 'net/https'
    require 'json'
    uri = URI.parse('https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
    request.body = { \"channel\"  => \"#general\", \"username\" => \"mmonit\", \"text\" => \"[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}\" }.to_json
    response = http.request(request)
    puts response.body
"

Important: Replace "XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ" with your slack.com integration path. Now press the Test button to verify the integration. If you see ok in the output area you have successfully integrated M/Monit with Slack.

Monit environment variables are used to compose the alert message. An overview of available environment variables can be found in the inline help text in the alerts page and also in the Monit manual. In this example we use ruby, but any scripting language can be used, even curl.

Alert events from M/Monit will now show up in Slack:

Monit

You can do the same from Monit if you don't use M/Monit. Below is a program check in Monit which will send an alert using slack.com if the check fails. But first, let's create a simple script which Monit can use to send Slack notification:

#!/usr/bin/ruby

require 'net/https'
require 'json'

uri = URI.parse("https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
request.body = {
    "channel"  => "#general",
    "username" => "mmonit",
    "text"     => "[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}"
}.to_json
response = http.request(request)
puts response.body

Save the script above to a file called, for instance, slack.rb (remember to replace "XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ" with your slack.com integration path). We can now use this script in our Monit control file (.monitrc) to get Slack alerts if a check fails :

  check program check-mysql with path "/opt/monit/check_mysql.sh"
     if status != 0 then exec /path/to/slack.rb

Note also that the content of the environment variable $MONIT_DESCRIPTION is anything your check program script (check_mysql.sh above) outputs to stdout/stderr. Which makes it possible to compose a detailed description of what went wrong.