From 0e616ef8d7f05957692870ff2e1b647e913aa5f9 Mon Sep 17 00:00:00 2001 From: kcrayon Date: Fri, 8 Nov 2013 23:42:49 +0800 Subject: [PATCH 1/3] updates for sensu 0.11+ --- extensions/handlers/redis_output.rb | 61 ++++++++++++++--------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/extensions/handlers/redis_output.rb b/extensions/handlers/redis_output.rb index fa37032f7..f125f5f67 100644 --- a/extensions/handlers/redis_output.rb +++ b/extensions/handlers/redis_output.rb @@ -1,43 +1,40 @@ -# Send event output to Redis -# === -# -# Copyright 2013 kcrayon -# -# Released under the same terms as Sensu (the MIT license); see LICENSE -# for details. +module Sensu::Extension + class RedisOutput < Handler + def definition + { + type: 'extension', + name: 'redis_output', + } + end -module Sensu - module Extension - class RedisOutput < Handler - def name - 'redis_output' - end + def name + definition[:name] + end - def description - 'sends event output to a redis list or channel' - end + def description + 'outputs events output to a redis list or channel' + end - def run(event, settings, &block) - opts = settings["redis_output"] + def run(event) + opts = @settings["redis_output"] - opts["db"] ||= 0 - opts["port"] ||= 6379 - @@redis ||= Redis.connect(:host => opts["host"], :port => opts["port"], :db => opts["db"]) + opts["db"] ||= 0 + opts["port"] ||= 6379 + @redis ||= Sensu::Redis.connect(:host => opts["host"], :port => opts["port"], :db => opts["db"]) - output = JSON.parse(event)["check"]["output"] - output = opts["split"] ? output.split("\n") : Array(output) + output = Oj.load(event)[:check][:output] + output = opts["split"] ? output.split("\n") : Array(output) - output.each do |e| - case opts["data_type"] - when "list" - @@redis.lpush(opts["key"], e) - when "channel" - @@redis.publish(opts["key"], e) - end + output.each do |e| + case opts["data_type"] + when "list" + @redis.lpush(opts["key"], e) + when "channel" + @redis.publish(opts["key"], e) end - - block.call("sent #{output.count} events", 0) end + + yield("sent #{output.count} events", 0) end end end From f23b2d7bd99304e9dda9955f13b84e7487a925dc Mon Sep 17 00:00:00 2001 From: kcrayon Date: Sat, 9 Nov 2013 10:51:09 +0800 Subject: [PATCH 2/3] changes per feedback --- extensions/handlers/redis_output.rb | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/extensions/handlers/redis_output.rb b/extensions/handlers/redis_output.rb index f125f5f67..163e33596 100644 --- a/extensions/handlers/redis_output.rb +++ b/extensions/handlers/redis_output.rb @@ -1,31 +1,28 @@ module Sensu::Extension class RedisOutput < Handler - def definition - { - type: 'extension', - name: 'redis_output', - } - end - def name - definition[:name] + 'redis_output' end def description 'outputs events output to a redis list or channel' end + def post_init + @redis = Sensu::Redis.connect({ + :host => @settings["redis_output"]["host"], + :port => @settings["redis_output"]["port"] || 6379, + :database => @settings["redis_output"]["db"] || 0, + }) + end + def run(event) opts = @settings["redis_output"] - opts["db"] ||= 0 - opts["port"] ||= 6379 - @redis ||= Sensu::Redis.connect(:host => opts["host"], :port => opts["port"], :db => opts["db"]) - output = Oj.load(event)[:check][:output] - output = opts["split"] ? output.split("\n") : Array(output) + output = output.split("\m") if opts["split"] - output.each do |e| + Array(output).each do |e| case opts["data_type"] when "list" @redis.lpush(opts["key"], e) From d7a5f2e79f7fe8f814cbed16e4c28a73ce50f196 Mon Sep 17 00:00:00 2001 From: kcrayon Date: Sat, 9 Nov 2013 10:52:41 +0800 Subject: [PATCH 3/3] typo --- extensions/handlers/redis_output.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/handlers/redis_output.rb b/extensions/handlers/redis_output.rb index 163e33596..38c0d5471 100644 --- a/extensions/handlers/redis_output.rb +++ b/extensions/handlers/redis_output.rb @@ -20,7 +20,7 @@ def run(event) opts = @settings["redis_output"] output = Oj.load(event)[:check][:output] - output = output.split("\m") if opts["split"] + output = output.split("\n") if opts["split"] Array(output).each do |e| case opts["data_type"]