Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
sensu-plugin committed Feb 11, 2015
1 parent eacd694 commit 8b203a8
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
65 changes: 65 additions & 0 deletions bin/graphite_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env ruby
#
# This is a drop-in replacement for the graphite mutator written
# as a Sensu Extention for better performance.
#
# It transforms parameter name if it's a hostname.
#
# There are two transforms you can apply separately:
# * Replace dots in FQDN to user specified strings.
# e.g. foo.example.com -> foo_example_com
# * Output the hostname in reverse order.
# e.g. foo.example.com -> com.example.foo
#
# The default configuration is:
#
# {
# "graphite": {
# "reverse": false,
# "replace": "_"
# }
# }
#
# Copyright 2013 Mitsutoshi Aoe <[email protected]>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

# #YELLOW
module Sensu::Extension # rubocop:disable Style/ClassAndModuleChildren
class Graphite < Mutator
def name
'graphite'
end

def description
'OnlyCheckOutput mutator for Graphite'
end

def post_init
@reverse = false
@replace = '_'

# #YELLOW
if settings['graphite'] # rubocop:disable GuardClause
# #YELLOW
@reverse = true if settings['graphite']['reverse'] == true
if settings['graphite']['replace']
@replace = settings['graphite']['replace']
end
end
end

def run(event, &block)
client_name = event[:client][:name]
if @reverse
renamed_client_name = client_name.split('.').reverse.join('.')
else
renamed_client_name = client_name
end
renamed_client_name = renamed_client_name.gsub('.', @replace)
mutated = event[:check][:output].gsub(client_name, renamed_client_name)
block.call(mutated, 0)
end
end
end
38 changes: 38 additions & 0 deletions bin/graphite_mut.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env ruby
#
# Graphite
# ===
#
# DESCRIPTION:
# I extend OnlyCheckOutput mutator specialy for Graphite.
# This mutator only send event output (so you don't need to use
# OnlyCheckOutput) and change parameter name if it is hostname
# for better view in Graphite.
#
# OUTPUT:
# event output with all dot changed to underline in host name
# If -r or --reverse parameter given script put hostname in
# reverse order for better graphite tree view
#
# PLATFORM:
# all
#
# DEPENDENCIES:
#
# json Ruby gem
#
# Copyright 2013 Peter Kepes <https://github.com/kepes>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'json'

# parse event
event = JSON.parse(STDIN.read, symbolize_names: true)

if ARGV[0] == '-r' || ARGV[0] == '--reverse'
puts event[:check][:output].gsub(event[:client][:name], event[:client][:name].split('.').reverse.join('.'))
else
puts event[:check][:output].gsub(event[:client][:name], event[:client][:name].gsub('.', '_'))
end

0 comments on commit 8b203a8

Please sign in to comment.