Skip to content

Commit

Permalink
Merge pull request #19 from romanbsd/master
Browse files Browse the repository at this point in the history
Make Redmon a mountable Rack app
  • Loading branch information
steelThread committed Jul 26, 2012
2 parents 177dc3d + 9ac2469 commit 03b1c58
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 27 deletions.
26 changes: 25 additions & 1 deletion README.md
Expand Up @@ -67,6 +67,30 @@ $ ruby load_sim.rb

Open your browser to 0.0.0.0:4567


## Using in a Rails application

Add to Gemfile:

gem 'redmon', require: false

Add to config/routes.rb:

require 'redmon/app'
mount Redmon::App => '/redmon'

You can configure the Redmon using an initializer config/initializers/redmon.rb:

```ruby
Redmon.configure do |config|
config.redis_url = 'redis://127.0.0.1:6379'
config.namespace = 'redmon'
config.poll_interval = 10
end
```

This will mount the Redmon application to the /redmon/ path. The trailing slash is important.

## License

Copyright (c) 2012 Sean McDaniel
Expand All @@ -75,4 +99,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 changes: 4 additions & 10 deletions lib/redmon.rb
@@ -1,3 +1,4 @@
require 'redmon/config'
require 'active_support/core_ext'
require 'eventmachine'
require 'haml'
Expand All @@ -10,16 +11,8 @@ module Redmon

attr_reader :opts

@opts = {
:web_interface => ['0.0.0.0', 4567],
:redis_url => 'redis://127.0.0.1:6379',
:namespace => 'redmon',
:worker => true,
:poll_interval => 10
}

def run(opts={})
@opts.merge! opts
@opts = Redmon::Config::DEFAULTS.merge(opts)
start_em
rescue Exception => e
log "!!! Redmon has shit the bed, restarting... #{e.message}"
Expand Down Expand Up @@ -55,6 +48,7 @@ def log(msg)
puts "[#{Time.now.strftime('%y-%m-%d %H:%M:%S')}] #{msg}"
end

# @deprecated
def [](option)
opts[option]
end
Expand All @@ -64,4 +58,4 @@ def [](option)
require 'redmon/redis'
require 'redmon/helpers'
require 'redmon/app'
require 'redmon/worker'
require 'redmon/worker'
9 changes: 8 additions & 1 deletion lib/redmon/app.rb
@@ -1,5 +1,12 @@
require 'redmon/helpers'

module Redmon
class App < Sinatra::Base
use Rack::Static, {
urls: [/\.css$/, /\.js$/],
root: "#{root}/public",
cache_control: 'public, max-age=3600'
}

helpers Redmon::Helpers

Expand Down Expand Up @@ -36,4 +43,4 @@ class App < Sinatra::Base
end

end
end
end
26 changes: 26 additions & 0 deletions lib/redmon/config.rb
@@ -0,0 +1,26 @@
module Redmon
def self.configure
yield config
end

def self.config
@config ||= Config.new
end

class Config
DEFAULTS = {
:web_interface => ['0.0.0.0', 4567],
:redis_url => 'redis://127.0.0.1:6379',
:namespace => 'redmon',
:worker => true,
:poll_interval => 10
}

attr_accessor(*DEFAULTS.keys)

def initialize
DEFAULTS.each { |k,v| send("#{k}=", v) }
end

end
end
6 changes: 4 additions & 2 deletions lib/redmon/helpers.rb
@@ -1,3 +1,5 @@
require 'redmon/redis'

module Redmon
module Helpers
include Redmon::Redis
Expand All @@ -7,12 +9,12 @@ def prompt
end

def poll_interval
Redmon[:poll_interval] * 1000
Redmon.config.poll_interval * 1000
end

def count
-(params[:count] ? params[:count].to_i : 1)
end

end
end
end
10 changes: 6 additions & 4 deletions lib/redmon/redis.rb
@@ -1,3 +1,5 @@
require 'redmon/config'

module Redmon
module Redis
extend self
Expand All @@ -13,15 +15,15 @@ module Redis
]

def redis
@redis ||= ::Redis.connect(:url => Redmon[:redis_url])
@redis ||= ::Redis.connect(:url => Redmon.config.redis_url)
end

def ns
Redmon[:namespace]
Redmon.config.namespace
end

def redis_url
@redis_url ||= Redmon[:redis_url].gsub(/\w*:\w*@/, '')
@redis_url ||= Redmon.config.redis_url.gsub(/\w*:\w*@/, '')
end

def redis_host
Expand Down Expand Up @@ -60,4 +62,4 @@ def stats_key
"#{ns}:redis:#{redis_host}:stats"
end
end
end
end
4 changes: 2 additions & 2 deletions lib/redmon/worker.rb
Expand Up @@ -34,8 +34,8 @@ def sort(slowlog)
end

def interval
Redmon[:poll_interval]
Redmon.config.poll_interval
end

end
end
end
10 changes: 5 additions & 5 deletions spec/helpers_spec.rb
Expand Up @@ -4,7 +4,7 @@

describe "redis" do
it "should call Redis.connect" do
Redis.should_receive(:connect).with({:url => Redmon[:redis_url]})
Redis.should_receive(:connect).with({:url => Redmon.config.redis_url})
Redmon::Redis.redis
end
end
Expand All @@ -19,19 +19,19 @@ def em_redis

describe "#ns" do
it "should return the configured namespace" do
Redmon[:namespace].should == Redmon::Redis.ns
Redmon.config.namespace.should == Redmon::Redis.ns
end
end

describe "#redis_url" do
it "should return the configured redis url" do
Redmon[:redis_url].should == Redmon::Redis.redis_url
Redmon.config.redis_url.should == Redmon::Redis.redis_url
end
end

describe "#redis_host" do
it "should return the configured redis host" do
Redmon[:redis_url].gsub('redis://', '').should == Redmon::Redis.redis_host
Redmon.config.redis_url.gsub('redis://', '').should == Redmon::Redis.redis_host
end
end

Expand Down Expand Up @@ -83,4 +83,4 @@ def em_redis
end
end

end
end
4 changes: 2 additions & 2 deletions spec/worker_spec.rb
Expand Up @@ -55,8 +55,8 @@ def mock_timer

describe "#interval" do
it "should return the configured poll interval" do
@worker.interval.should == Redmon[:poll_interval]
@worker.interval.should == Redmon.config.poll_interval
end
end

end
end

0 comments on commit 03b1c58

Please sign in to comment.