Home /  MMonit /  Hip Chat 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 hipchat.com.

On the HipChat side you will first need to activate an integration and obtain a token at hipchat.com API by enabling an "API access".

M/Monit

Once you have your hipchat.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://api.hipchat.com/v2/room/YYYYY/notification?auth_token=XXXXX\")
    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 = {
        \"notify\"          => true,
        \"message_format\"  => \"text\",
        \"message\"         => \"[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}\"
    }.to_json
    response = http.request(request)
    puts response.body
"

Important: Replace "YYYYY" with your room name (URL encoded) and "XXXXX" with your hipchat.com API access token. Now press the Test button to verify the integration.

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 HipChat room:

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 hipchat.com if the check fails. But first, let's create a simple script which Monit can use to send HipChat notification:

#!/usr/bin/ruby

require 'net/https'
require 'json'

uri = URI.parse("https://api.hipchat.com/v2/room/YYYYY/notification?auth_token=XXXXX")
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 = {
    "notify"          => true,
    "message_format"  => "text",
    "message"         => "[#{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, hipchat.rb (remember to replace "YYYYY" with your hipchat.com room name (URL encoded) and "XXXXX" with your hipchat.com API access token). We can now use this script in our Monit control file (.monitrc) to get HipChat alerts if a check fails :

  check process apache with pidfile "/var/run/apache.pid"
     if failed port 80 protocol http then exec /path/to/hipchat.rb