class Riemann::Tools::RedisKeys

Public Class Methods

new() click to toggle source
# File bin/riemann-redis-keys, line 20
def initialize
  options = if opts[:redis_url] != ''
              { :url => opts[:redis_url] }
            elsif opts[:redis_socket] != ''
              { :path => opts[:redis_socket] }
            else
              { :host => opts[:redis_host], :port => opts[:redis_port] }
            end
  @redis = ::Redis.new(options)
  @redis.auth(opts[:redis_password]) unless opts[:redis_password] == ''
  @key_types = Hash[opts[:redis_key].collect { |v| [v.first, nil] }]

  @key_operations = {"list" => :llen, "set" => :scard, "zset" => :zcard,
                     "hash" => :hlen, "string" => :strlen}

end

Public Instance Methods

tick() click to toggle source
# File bin/riemann-redis-keys, line 38
def tick
  begin
    @key_types.each do |key, key_type|
      type = key_type
      # If nil, grab type from redis
      if type.nil?
        t = @redis.type key
        @key_types[key] = t unless t == "none"
      end

      if !type.nil?
        value = @redis.send(@key_operations[type], key)

        state = "ok"
        [:warning, :critical].each do |threshold|
          if !opts[threshold].nil? && value > opts[threshold]
            state = threshold.to_s
          end
        end

        data = {
          :host    => opts[:redis_host].dup,
          :service => "redis #{key}",
          :metric  => value.to_s,
          :state   => state,
          :tags    => ['redis']
        }

        report(data)
      end
    end
  rescue ::Redis::CommandError => e
    if e.message == "ERR operation not permitted"
      @redis.auth(opts[:redis_password]) unless opts[:redis_password] == ''
    end
  end
end