Skip to content

Instantly share code, notes, and snippets.

@phumpal
Created January 8, 2021 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phumpal/f0a58698111a712e2c21a22d423a8ae2 to your computer and use it in GitHub Desktop.
Save phumpal/f0a58698111a712e2c21a22d423a8ae2 to your computer and use it in GitHub Desktop.
Humanized mutator output for not-so-boring sensu-core alerts
{
"mutators": {
"glyphify": {
"command": "/etc/sensu/mutators/glyphify.rb"
}
}
}
#! /usr/bin/env ruby
# frozen_string_literal: true
#
# Custom 'Humanized' Sensu Mutator for Slack based alerts
#
# USAGE:
#
#
# NOTES:
# Compatible with sensu-core (Ruby) https://docs.sensu.io/sensu-core/latest/
#
# LICENSE:
# Copyright (c) 2015-2020 Airbrake Technologies, Inc <support@airbrake.io>
# Author Patrick Humpal <patrick@netvilla.net>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'sensu-mutator'
class Glyphify < Sensu::Mutator
def human_check_status
case @event['check']['status']
when 0
'OK'
when 1
'WARNING'
when 2
'CRITICAL'
else
'UNKNOWN'
end
end
def mutate
dashboard_url = "https://IP-ADDRESS-OF-YOUR-SENSU-DASH"
dashboard_link = "#{dashboard_url}/#/client/sensu/#{@event['client']['name']}?check=#{@event['check']['name']}"
case @event['check']['status']
when 0
output = <<-BODY
Check Name: #{@event['check']['name']}
Status: #{human_check_status()} (#{@event['check']['status']})
BODY
when 2
output = <<-BODY
Check Name: #{@event['check']['name']}
Status: #{human_check_status()} (#{@event['check']['status']})
Address: `#{@event['client']['address']}`
Dashboard Link: <#{dashboard_link}|Click here> for sensu dashboard
BODY
end
@event['check']['output'] = output
@event.merge!(:mutated => true)
end
end
{
"icon_emoji": ":bell:",
"attachments": [
{
"fallback": "<%= @event["check"]["output"] %>",
"color": "<%= color %>",
"title": "<%= @event["client"]["name"] %>",
"text": "<%= @event["check"]["output"].gsub('"', '\\"') %>"
}
]
}
<%=
@event["check"]["output"]
%>
{
"handlers": {
"slack": {
"type": "pipe",
"mutator": "glyphify",
"command": "/etc/sensu/handlers/slack.rb"
}
},
"slack": {
"webhook_url": "{{ sensu_slack_webhook }}",
"token": "{{ sensu_slack_token }}",
"channel": "#{{ sensu_slack_channel }}",
"message_template": "/etc/sensu/slack-template.eruby",
"payload_template": "/etc/sensu/slack-payload.eruby",
"bot_name": "sensu"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment