<commit_before>#!/usr/bin/env ruby
#
# Push Redis INFO stats into graphite
# ===
#
# TODO: Only pass integer metrics with options for single metrics.
#
# Copyright 2012 <NAME> <<EMAIL>>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/metric/cli'
require 'redis'

class Redis2Graphite < Sensu::Plugin::Metric::CLI::Graphite

  option :host,
    :short => "-h HOST",
    :long => "--host HOST",
    :description => "Redis Host to connect to",
    :required => true

  option :port,
    :short => "-p PORT",
    :long => "--port PORT",
    :description => "Redis Port to connect to",
    :proc => proc {|p| p.to_i },
    :required => true

  option :scheme,
    :description => "Metric naming scheme, text to prepend to metric",
    :short => "-s SCHEME",
    :long => "--scheme SCHEME",
    :default => "#{Socket.gethostname}.redis"

  def run
    redis = Redis.new(:host => config[:host], :port =>config[:port])

    redis.info.each do |k, v|
      output "#{config[:scheme]}.#{k}", v
    end

    ok
  end

end<commit_msg>skip keys with non-numeric values, add metrics for key counts in each database<commit_after>#!/usr/bin/env ruby
#
# Push Redis INFO stats into graphite
# ===
#
# Copyright 2012 <NAME> <<EMAIL>>
#                <NAME> <<EMAIL>>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/metric/cli'
require 'redis'

class Redis2Graphite < Sensu::Plugin::Metric::CLI::Graphite

  # redis.c - sds genRedisInfoString(char *section)
  SKIP_KEYS_REGEX = ['gcc_version', 'master_host', 'master_link_status',
                     'master_port', 'mem_allocator', 'multiplexing_api', 'process_id',
                     'redis_git_dirty', 'redis_git_sha1', 'redis_version', '^role',
                     'run_id', '^slave', 'used_memory_human', 'used_memory_peak_human']

  option :host,
    :short => "-h HOST",
    :long => "--host HOST",
    :description => "Redis Host to connect to",
    :required => true

  option :port,
    :short => "-p PORT",
    :long => "--port PORT",
    :description => "Redis Port to connect to",
    :proc => proc {|p| p.to_i },
    :required => true

  option :scheme,
    :description => "Metric naming scheme, text to prepend to metric",
    :short => "-s SCHEME",
    :long => "--scheme SCHEME",
    :default => "#{Socket.gethostname}.redis"

  def run
    redis = Redis.new(:host => config[:host], :port => config[:port])

    redis.info.each do |k, v|
      next if SKIP_KEYS_REGEX.map { |re| k.match(/#{re}/)}.compact.empty?

      # "db0"=>"keys=123,expires=12"
      if k =~ /^db/
        keys, expires = v.split(',')
        keys.gsub!('keys=', '')
        expires.gsub!('expires=', '')

        output "#{config[:scheme]}.#{k}.keys", keys
        output "#{config[:scheme]}.#{k}.expires", expires
      else
        output "#{config[:scheme]}.#{k}", v
      end
    end

    ok
  end
end
{'fraction_non_alphanumeric': 0.1701639344262295, 'fraction_numerical': 0.0075409836065573775, 'mean_word_length': 2.988235294117647, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2863232', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1035, 'n_words': 287}