Skip to content

Instantly share code, notes, and snippets.

@matmannion
Last active October 24, 2018 09:33
Show Gist options
  • Save matmannion/57265df46e6b23485a07 to your computer and use it in GitHub Desktop.
Save matmannion/57265df46e6b23485a07 to your computer and use it in GitHub Desktop.
check-mk-notify Slack notifications
#!/usr/bin/env ruby
# Notify by Slack
require 'net/http'
require 'uri'
require 'cgi'
require 'json'
# To create a webhook_url, login to the web page of your slack instance.
# https://my.slack.com/services/new/incoming-webhook/
# Select the slack channel to post to
# Click "Add incoming WebHooks integration"
# Paste the Webhook URL into the variable "webhook_url" below
domain = 'SLACK_DOMAIN'
webhook_url = 'SLACK_WEBHOOK_URL'
monitoring_server = 'YOUR_OMD_HOSTNAME'
monitoring_site = 'YOUR_OMD_SITE_NAME'
COLOURS = {
'CRITICAL' => '#d00000',
'UNKNOWN' => '#e3e4e6',
'WARNING' => '#daa038',
'OK' => '#36a64f',
'DOWN' => '#d00000',
'UNREACHABLE' => '#e3e4e6',
'UP' => '#36a64f',
'PENDING' => '#888888',
'DOWNTIME' => '#00aaff'
}
payload = {
'username' => 'Check_MK',
'icon_emoji' => ':check_mk:',
'channel' => ENV['NOTIFY_PARAMETER_1']
}
host_start_url = "/#{monitoring_site}/check_mk/view.py?view_name=host&host=#{ENV['NOTIFY_HOSTALIAS']}"
host_url = "http://#{monitoring_server}/#{monitoring_site}/check_mk/index.py?start_url=#{CGI::escape(host_start_url)}"
# Check if NOTIFY_SERVICESTATE is set. If it's not, it's a host notification
if ENV.has_key?('NOTIFY_SERVICESTATE') && ENV['NOTIFY_SERVICESTATE'] != '$SERVICESTATE$'
service_start_url = "/#{monitoring_site}/check_mk/view.py?view_name=service&service=#{ENV['NOTIFY_SERVICEDESC']}&host=#{ENV['NOTIFY_HOSTALIAS']}"
service_url = "http://#{monitoring_server}/#{monitoring_site}/check_mk/index.py?start_url=#{CGI::escape(service_start_url)}"
message = "<#{host_url}|#{ENV['NOTIFY_HOSTALIAS']}>/<#{service_url}|#{ENV['NOTIFY_SERVICEDESC']}> is #{ENV['NOTIFY_SERVICESTATE']}:\n#{ENV['NOTIFY_SERVICEOUTPUT']}"
fields = [
{'title' => 'State', 'value' => ENV['NOTIFY_SERVICESTATE'], 'short' => true},
{'title' => 'Host', 'value' => "<#{host_url}|#{ENV['NOTIFY_HOSTALIAS']}>", 'short' => true},
{'title' => 'Service', 'value' => "<#{service_url}|#{ENV['NOTIFY_SERVICEDESC']}>", 'short' => true},
{'title' => 'Detail', 'value' => ENV['NOTIFY_SERVICEOUTPUT']},
]
if ENV.has_key?('NOTIFY_SERVICEACKCOMMENT') and ENV['NOTIFY_SERVICEACKCOMMENT'].length > 0
fields.push({'title' => 'Comment', 'value' => ENV['NOTIFY_SERVICEACKCOMMENT']})
end
payload['attachments'] = [{
'fallback' => message,
'color' => COLOURS[ENV['NOTIFY_SERVICESTATE']],
'fields' => fields,
'mrkdwn_in' => ['text']
}]
else
message = "<#{host_url}|#{ENV['NOTIFY_HOSTALIAS']}>> is #{ENV['NOTIFY_HOSTSTATE']}:\n#{ENV['NOTIFY_HOSTOUTPUT']}"
payload['attachments'] = [{
'fallback' => message,
'color' => COLOURS[ENV['NOTIFY_HOSTSTATE']],
'fields' => [
{'title' => 'State', 'value' => ENV['NOTIFY_HOSTSTATE'], 'short' => true},
{'title' => 'Host', 'value' => "<#{host_url}|#{ENV['NOTIFY_HOSTALIAS']}>", 'short' => true},
{'title' => 'Detail', 'value' => ENV['NOTIFY_HOSTOUTPUT']},
],
'mrkdwn_in' => ['text']
}]
end
response = Net::HTTP.post_form(URI.parse(webhook_url), { 'payload' => payload.to_json })
@dheerajgudipati
Copy link

What is "YOUR_OMD_HOSTNAME" and "YOUR_OMD_SITE_NAME" ?

@grambharos
Copy link

grambharos commented Aug 15, 2017

Hi in which path do you put this file?

@kieulam141
Copy link

Hi,
Can I custome for only notice 1 or many user in the channel?
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment