Last active
July 18, 2016 00:37
-
-
Save somebox/7039510 to your computer and use it in GitHub Desktop.
graphite-redis : monitor redis statistics with graphite across several hosts
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
#!/usr/bin/env ruby | |
require 'socket' | |
# This script runs every minute, captures stats about redis | |
# and forwards them to graphite as counter values. | |
# Graphite/carbon settings | |
GRAPHITE_HOST="graphite.intra.local.ch" | |
GRAPHITE_PORT=8125 | |
def instrument_redis(redis_host) | |
namespace = "rails.redis.#{redis_host}" | |
redis = {} | |
`/usr/bin/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" | |
puts msg | |
@socket.send(msg, 0, GRAPHITE_HOST, GRAPHITE_PORT) | |
msg | |
end | |
# do stuff | |
@socket = UDPSocket.new | |
instrument_redis('rails-hw-snr01') | |
instrument_redis('rails-hw-snr02') | |
instrument_redis('rails-hw-inx01') | |
instrument_redis('rails-hw-inx02') | |
@socket.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment