Created
October 7, 2011 19:29
-
-
Save heffergm/1271159 to your computer and use it in GitHub Desktop.
Send Mongo data to graphite
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 'rubygems' | |
require 'json' | |
require 'mongo' | |
graphite_host = File.read("/opt/scripts/etc/graphite_host") | |
loop do | |
time = Time.new | |
epoch = time.to_i | |
## Graphite host | |
begin | |
s = TCPSocket.open("#{graphite_host}", 2003) | |
rescue | |
puts "error: #{$!}" | |
puts "Sleeping before retrying..." | |
sleep 10 | |
else | |
## loop | |
#for host in mongo_hosts | |
begin | |
db = Mongo::Connection.new("localhost", 27017, :op_timeout => 3).db("mydb_production") | |
status = db.command('serverStatus' => 1) | |
stats = db.stats | |
repldb = Mongo::Connection.new("localhost", 27017, :op_timeout => 3).db("admin") | |
replstatus = repldb.command('replSetGetStatus' => 1) | |
rescue | |
puts "error: #{$!}" | |
puts "Sleeping before retrying..." | |
sleep 10 | |
else | |
resultshash = { | |
"connections_current" => status['connections']['current'], | |
"connections_available" => status['connections']['available'], | |
"globallock_ratio" => status['globalLock']['ratio'], | |
"globallock_total" => status['globalLock']['currentQueue']['total'], | |
"globallock_readers" => status['globalLock']['currentQueue']['readers'], | |
"globallock_writers" => status['globalLock']['currentQueue']['writers'], | |
"memory_resident" => status['mem']['resident'], | |
"memory_virtual" => status['mem']['virtual'], | |
"indexsize" => stats['indexSize'], | |
"collections" => stats['collections'], | |
"objects" => stats['objects'], | |
"avgobjectsize" => stats['avgObjSize'], | |
"datasize" => stats['dataSize'], | |
"storagesize" => stats['storageSize'], | |
"numextents" => stats['numExtents'], | |
"indexes" => stats['indexes'], | |
"filesize" => stats['fileSize'], | |
"mystate" => replstatus['myState'] | |
} | |
hostname = status['host'].split(/\./)[0] | |
ismaster = status['repl']['ismaster'].to_s | |
if ismaster == "true" | |
is_master = 1 | |
elsif ismaster == "false" | |
is_master = 0 | |
else | |
is_master = 7 | |
end | |
## Send data | |
s.write("stats.mydb.production.mongo.#{hostname}.is_master #{is_master} #{epoch}\n") | |
resultshash.each do |metric, result| | |
s.write("stats.mydb.production.mongo.#{hostname}.#{metric} #{result} #{epoch}\n") | |
end | |
end | |
s.close | |
sleep 5 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment