Created
September 8, 2012 09:31
-
-
Save rohit9889/3673040 to your computer and use it in GitHub Desktop.
Rack Application to check status of MySQL and MongoDB
This file contains 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
require 'rack' | |
require 'rack/request' | |
require 'rack/response' | |
require 'rubygems' | |
require 'json' | |
require 'mysql2' | |
require 'mongo' | |
module Rack | |
class ServerStatus | |
def call(env) | |
req = Request.new(env) | |
response_hash = {} | |
begin | |
dbconfig = {:adapter => "mysql2", :encoding => "utf8", :reconnect => true, :database => "simple_cms_local", :pool => 5, :username => "root", :password => "root", :host => "localhost"} | |
connection = Mysql2::Client.new(dbconfig) | |
connection.query('select id from portals') | |
response_hash[:mysql] = "running" | |
rescue Exception => e | |
response_hash[:mysql] = "down" | |
end | |
begin | |
connection = Mongo::Connection.new("localhost", 27017) | |
db = connection.db("mydb") | |
coll = db.collection("content_schemas") | |
coll.find_one | |
response_hash[:mongo] = "running" | |
rescue Exception => e | |
response_hash[:mongo] = "down" | |
end | |
response = Response.new | |
response.write(response_hash.to_json) | |
response.finish | |
end | |
end | |
end | |
if $0 == __FILE__ | |
require 'rack' | |
require 'rack/showexceptions' | |
Rack::Handler::WEBrick.run \ | |
Rack::ShowExceptions.new(Rack::Lint.new(Rack::ServerStatus.new)), | |
:Port => 9292 | |
end | |
run Rack::ServerStatus.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment