Skip to content

Instantly share code, notes, and snippets.

@felippemr
Created November 18, 2014 18:39
Show Gist options
  • Save felippemr/42cc0413c7ee018b6a0a to your computer and use it in GitHub Desktop.
Save felippemr/42cc0413c7ee018b6a0a to your computer and use it in GitHub Desktop.

Revisions

  1. felippemr created this gist Nov 18, 2014.
    75 changes: 75 additions & 0 deletions redis_info.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    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