Skip to content

Commit

Permalink
Merge pull request #2 from vjanelle/templated_output
Browse files Browse the repository at this point in the history
Cleanups, format output
  • Loading branch information
mattyjones committed Jun 27, 2015
2 parents da8794c + ef8207b commit 51e5d17
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
"channel": "#notifications-room, optional defaults to slack defined",
"message_prefix": "optional prefix - can be used for mentions",
"surround": "optional - can be used for bold(*), italics(_), code(`) and preformatted(```)",
"bot_name": "optional bot name, defaults to slack defined"
"bot_name": "optional bot name, defaults to slack defined",
"template": "/some/path/to/template.erb",
"fields": [
"list",
"of",
"optional",
"clientkeys",
"to_render"
]
}
}
```
Expand Down
31 changes: 31 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'chef/centos-6.6'
config.vm.box_download_checksum = true
config.vm.box_download_checksum_type = 'md5'
config.vm.hostname = 'sensu-plugins-dev'

script = <<EOF
sudo yum update -y
sudo yum groupinstall -y development
sudo yum install -y vim nano
#sudo yum install -y ImagicMagic ImageMagick-devel mysql-devel # needed for bundle install
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -L get.rvm.io | bash -s stable
source /home/vagrant/.rvm/scripts/rvm
rvm reload
#rvm install 1.9.3
rvm install 2.1.4
#rvm install 2.0.0
#rvm use 1.9.3@sensu_plugins --create
#rvm use 2.0.0@sensu_plugins --create
rvm use 2.1.4@sensu_plugins --create
rvm use 2.1.4@sensu_plugins --default
EOF

config.vm.provision 'shell', inline: script, privileged: false
end
63 changes: 56 additions & 7 deletions bin/handler-slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

require 'sensu-handler'
require 'json'
require 'erubis'

class Slack < Sensu::Handler
option :json_config,
Expand All @@ -26,7 +27,11 @@ def slack_webhook_url
end

def slack_channel
get_setting('channel')
if @event['check']['slack_channel']
@event['check']['slack_channel']
else
get_setting('channel')
end
end

def slack_message_prefix
Expand All @@ -41,6 +46,14 @@ def slack_surround
get_setting('surround')
end

def message_template
get_setting('template')
end

def fields
get_setting('fields')
end

def incident_key
@event['client']['name'] + '/' + @event['check']['name']
end
Expand All @@ -55,11 +68,20 @@ def handle
end

def build_description
[
@event['check']['output'],
@event['client']['address'],
@event['client']['subscriptions'].join(',')
].join(' : ')
if message_template && File.readable?(message_template)
template = File.read(message_template)
else
template = '''<%=
[
@event["check"]["output"],
@event["client"]["address"],
@event["client"]["subscriptions"].join(",")
].join(" : ")
%>
'''
end
eruby = Erubis::Eruby.new(template)
eruby.result(binding)
end

def post_data(notice)
Expand All @@ -85,11 +107,28 @@ def verify_response(response)
end

def payload(notice)
client_fields = []

unless fields.nil?
fields.each do |field|
# arbritary based on what I feel like
# -vjanelle
is_short = true unless @event['client'][field].length > 50
client_fields << {
title: field,
value: @event['client'][field],
short: is_short
}
end
end

{
icon_url: 'http://sensuapp.org/img/sensu_logo_large-c92d73db.png',
attachments: [{
title: "#{@event['client']['address']} - #{translate_status}",
text: [slack_message_prefix, notice].compact.join(' '),
color: color
color: color,
fields: client_fields
}]
}.tap do |payload|
payload[:channel] = slack_channel if slack_channel
Expand All @@ -110,4 +149,14 @@ def color
def check_status
@event['check']['status']
end

def translate_status
status = {
0 => :OK,
1 => :WARNING,
2 => :CRITICAL,
3 => :UNKNOWN
}
status[check_status.to_i]
end
end
1 change: 1 addition & 0 deletions sensu-plugins-slack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'sensu-plugin', '1.1.0'
s.add_runtime_dependency 'json', '1.8.2'
s.add_runtime_dependency 'erubis'

s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
s.add_development_dependency 'rubocop', '0.30'
Expand Down

0 comments on commit 51e5d17

Please sign in to comment.