Skip to content

Instantly share code, notes, and snippets.

@lfender6445
Forked from somebox/redis-graphite.sh
Last active May 4, 2016 17:15
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 lfender6445/44f7967cc59a47ba37e8 to your computer and use it in GitHub Desktop.
Save lfender6445/44f7967cc59a47ba37e8 to your computer and use it in GitHub Desktop.
graphite-redis : monitor redis statistics with graphite across several hosts
#!/usr/bin/env ruby
require 'socket'
# graphite/carbon settings
GRAPHITE_HOST="graphite.intra.local.ch"
GRAPHITE_PORT=8125
def instrument_redis(redis_host)
namespace = "#{redis_host}"
redis = {}
`/usr/bin/redis-cli -h #{redis_host} info`.each_line do |line|
# `redis-cli -h #{redis_host} info`.each_line do |line|
key,value = line.chomp.split(/:/)
redis[key]=value
end
%w{used_memory total_commands_processed
total_connections_received keyspace_hits
changes_since_last_save
connected_clients keyspace_misses expired_keys}.each do |item|
send_data("#{namespace}.#{item}", redis[item].to_i)
end
frag1000 = redis['mem_fragmentation_ratio'].to_f*1000;
send_data("#{namespace}.mem_fragmentation_ratio", frag1000)
end
def send_data(path, value, time=nil)
time ||= Time.new
msg = "#{path}:#{value}|c\n"
$stdout.puts msg
@socket.send(msg, 0, GRAPHITE_HOST, GRAPHITE_PORT)
msg
end
@socket = UDPSocket.new
instrument_redis('localhost')
@socket.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment