Skip to content

Instantly share code, notes, and snippets.

@felippemr
Created November 18, 2014 18:39
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 felippemr/42cc0413c7ee018b6a0a to your computer and use it in GitHub Desktop.
Save felippemr/42cc0413c7ee018b6a0a to your computer and use it in GitHub Desktop.
Redis info output plugin
module Fluent
class RedisInfo < Input
Plugin.register_input('redisinfo', self)
config_param :uri, :string
config_param :stats_interval, :time, :default => 60 # every minute
config_param :tag_prefix, :string, :default => "redisinfo"
def initialize
super
require 'redis'
end
def configure(conf)
super
unless @uri
raise ConfigError, 'uri must be specified'
end
@conn = Redis.new(:url => @uri)
end
def start
@loop = Coolio::Loop.new
tw = TimerWatcher.new(@stats_interval, true, @log, &method(:collect_info))
tw.attach(@loop)
@thread = Thread.new(&method(:run))
end
def run
@loop.run
rescue
log.error "unexpected error", :error=>$!.to_s
log.error_backtrace
end
def shutdown
@loop.stop
@thread.join
end
def collect_info
begin
stats = @conn.client.call([:info])
if stats.kind_of?(String)
stats = Hash[stats.split("\r\n").map do |line|
line.split(":", 2) unless line =~ /^(#|$)/
end]
end
Engine.emit(@tag_prefix, Engine.now, stats)
rescue => e
log.error "failed to collect Redis info", :error_class => e.class, :error => e
end
end
class TimerWatcher < Coolio::TimerWatcher
def initialize(interval, repeat, log, &callback)
@callback = callback
@log = log
super(interval, repeat)
end
def on_timer
@callback.call
rescue
@log.error $!.to_s
@log.error_backtrace
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment