public
Last active

  • Download Gist
core_helper.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#!/custom/ree/bin/ruby
 
require 'etc'
require 'net/smtp'
 
CORE_DIR = "/cores"
TO_EMAIL = "dev@blah.com"
FROM_EMAIL = "core_watcher@blah.com"
MAIL_SERVER = "blah"
 
class CoreHelper
def self.start!
 
time = Time.now.strftime("%Y-%m-%d-%H:%M")
corefile = "#{CORE_DIR}/core-#{time}.core"
cmdline = File.read("/proc/#{ARGV[0]}/cmdline")
 
f = File.new("#{CORE_DIR}/core-#{time}-metadata", "w")
f.puts "PID: #{ARGV[0]}"
f.puts "Signal: #{ARGV[1]}"
f.puts "UID: #{Etc.getpwuid(ARGV[2].to_i)[:name]}/#{ARGV[2]}, GID: #{Etc.getgrgid(ARGV[3].to_i)[:name]}/#{ARGV[3]}"
f.puts "Command line: #{cmdline}"
f.puts
f.puts
f.flush
 
core = File.new(corefile, "w")
begin
while (chunk = STDIN.read)
core.write chunk
break if chunk == nil || chunk.length == 0
end
rescue EOFError
core.close
end
core.close
 
r,w = IO.pipe
child = fork{ $stdin.reopen(r); $stdout.reopen(f); $stderr.reopen(f); exec "/usr/bin/gdb -q -c #{corefile}" }
w.puts "set height 0"
w.puts "backtrace"
w.puts "info registers"
w.puts "x/5i $rip"
w.puts "quit"
Process.wait
 
msg = <<MESSAGE
From: #{FROM_EMAIL}
To: #{TO_EMAIL}
Subject: Core dump on #{`hostname`}
 
#{File.read("#{CORE_DIR}/core-#{time}-metadata")}
MESSAGE
 
Net::SMTP.start(MAIL_SERVER) do |smtp|
smtp.send_message msg, FROM_EMAIL, TO_EMAIL
end
 
f.close
 
exit 0
end
end
 
CoreHelper.start!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.