Skip to content

Instantly share code, notes, and snippets.

@heffergm
Created October 7, 2011 19:36
Show Gist options
  • Save heffergm/1271176 to your computer and use it in GitHub Desktop.
Save heffergm/1271176 to your computer and use it in GitHub Desktop.
Monitor mongo w/monit
#!/usr/bin/env ruby
## used by monit to check for unhealthy cluster state
require 'rubygems'
require 'mongo'
begin
db = Mongo::Connection.new("localhost", 27017, :op_timeout => 3).db("admin")
rescue
abort("MongoDB Connection Error: #{$!}")
else
health = db.command('serverStatus' => 1)
state = db.command('replSetGetStatus' => 1)
# comb the entire output for any bad error messages && make sure we got a response
if (health.to_s =~ /not reachable\/unhealthy/ or health.nil?)
then
abort("Found 'not reachable/unhealthy' node, or health.nil? => true")
# check my state, >2 is generally not good, && make sure we got a response
elsif (state['myState'].to_i > 2 or state['myState'].nil?)
then
abort("Found 'myState > 2', or state['myState'].nil? => true")
# everything looks jolly, exit cleanly
else
exit 0
end
end
## Mongo cluster state
check program prod_mongodb_cluster_health with path "/some/path/to/bin/monit/mongo_replSetGetStatus.rb"
if status != 0 timeout 3 seconds for 2 cycles then alert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment