Last active
October 24, 2018 09:33
-
-
Save matmannion/57265df46e6b23485a07 to your computer and use it in GitHub Desktop.
check-mk-notify Slack notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 }) |
Hi in which path do you put this file?
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
What is "YOUR_OMD_HOSTNAME" and "YOUR_OMD_SITE_NAME" ?