Created
November 18, 2014 18:39
-
-
Save felippemr/42cc0413c7ee018b6a0a to your computer and use it in GitHub Desktop.
Redis info output plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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