Skip to content

Instantly share code, notes, and snippets.

@jtushman
Created June 28, 2012 20:10
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 jtushman/3013625 to your computer and use it in GitHub Desktop.
Save jtushman/3013625 to your computer and use it in GitHub Desktop.
Watches Mongostat and notifies admins via email if lock % is above threashold
require 'term/ansicolor'
require 'tlsmail'
task :watch_mongo do
@notified = false
file = Tempfile.new('stat_watch')
begin
Thread.new('mongostat') do
# mongostat --rowcount 0 20, will check every 20 seconds
%x[mongostat --rowcount 0 20 --host #{ENV['MONGO_HOST']} --username #{ENV['MONGO_USERNAME']} --password #{ENV['MONGO_PASSWORD']} --noheaders > #{file.path}]
end
sleep(2)
puts
puts
printf "%7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s\n",
'insert' , 'query' ,'update' ,'delete', 'getmore', 'command' ,'flushes', 'mapped' , 'vsize', 'res', 'faults', 'locked %', 'idxmiss%', 'qr|qw' , 'ar|aw' , 'netIn', 'netOut', 'conn', 'time'
f = File.open(file,"r")
f.seek(0,IO::SEEK_END)
while true do
select([f])
line = f.gets
parse_line(line) if line =~ /^/ && !(line =~ /^connected/)
end
ensure
puts "Closing!"
file.close
file.unlink # deletes the temp file
end
end
def parse_line(line)
items = line.split
inserts = items[0]
query = items[1]
update = items[2]
delete = items[3]
getmore = items[4]
command = items[5]
flushes = items[6]
mapped = items[7]
vsize = items[8]
res = items[9]
faults = items[10]
locked = items[11]
idx_miss = items[12]
qrw = items[13]
arw = items[14]
netIn = items[15]
netOut = items[16]
conn = items[17]
set = items[18]
repl = items[19]
time = items[20]
printf "%7s %7s %7s %7s %7s %7s %7s %7s %7s %7s",
inserts , query ,update ,delete, getmore, command ,flushes, mapped , vsize, res
print Term::ANSIColor.bold
printf '%7s', faults
l = Float(locked)
if l > 50
print Term::ANSIColor.yellow
elsif l > 70
print '\a' #beep
notify_admins
print Term::ANSIColor.red
end
printf '%7s', locked
print Term::ANSIColor.clear
printf "%7s %7s %7s %7s %7s %7s %7s\n",
idx_miss, qrw, arw, netIn, netOut, conn, time
end
def notify_admins
if @notified
puts 'already notified'
else
send_email(ENV['GMAIL_USERNAME'])
@notified = true
end
end
def send_email(to)
content = <<EOF
From: #{ENV['GMAIL_USERNAME']}
To: #{to}
Subject: Warning lock levels high
Date: #{Time.now.rfc2822}
Something is up, lock levels high
EOF
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', ENV['GMAIL_USERNAME'], ENV['GMAIL_PASSWORD'], :login) do |smtp|
smtp.send_message(content, ENV['GMAIL_USERNAME'], to)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment