Permalink
Browse files

Initial import

  • Loading branch information...
1 parent 3cba695 commit 35f63c22fe03c63061822b3647dac3be4462b8ad @betamatt betamatt committed Aug 26, 2010
Showing with 343 additions and 10 deletions.
  1. +1 −1 LICENSE
  2. +48 −2 README.rdoc
  3. +2 −3 Rakefile
  4. +34 −0 lib/updawg.rb
  5. +30 −0 lib/updawg/check.rb
  6. +21 −0 lib/updawg/monitor.rb
  7. +37 −0 lib/updawg/results.rb
  8. +1 −0 spec/spec_helper.rb
  9. +47 −0 spec/updawg/check_spec.rb
  10. +46 −0 spec/updawg/monitor_spec.rb
  11. +29 −0 spec/updawg/results_spec.rb
  12. +47 −4 spec/updawg_spec.rb
View
@@ -1,4 +1,4 @@
-Copyright (c) 2009 Matt Griffin
+Copyright (c) 2010 Viximo, Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -1,6 +1,52 @@
= updawg
-Description goes here.
+A simple way to see what's up, dawg (and what's not.)
+
+Define a list of checks and create a controller that renders the results. Ping the controller with
+the monitoring tool of your choice, perhaps setting a 503 response when one of the checks fails.
+
+Some options can be set:
+UpDawg.configure do |config|
+ # TODO
+end
+
+Checks are defined with a simple DSL:
+
+monitor = UpDawg.monitor do
+ check 'MySQL' do |c|
+ # Will raise an exception if the connection is unavailable resulting in a failed check
+ ActiveRecord::Base.connection.execute('SELECT 1 FROM DUAL')
+ end
+
+ check 'Some threshold' do
+ return error('Threshold exceeded y') if threshold_value > y
+ return warning('Threshold exceeded x') if threshold_value > x
+ end
+end
+
+results = monitor.perform
+
+results.success? => true if no checks error
+results.warning? => true if no checks error and at least one has a warning
+results.error? => true if at least one check fails
+
+Render with:
+results.to_json
+results.to_xml
+
+monitor.to_html:
+ <table class="updawg monitor">
+ <tr class="check error">
+ <td class="name">MySQL</td>
+ <td class="message">ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax</td>
+ <td class="status">ERROR</td>
+ </tr>
+ <tr class="check pass">
+ <td class="name">Some threshold</td>
+ <td class="message"></td>
+ <td class="status">PASS</td>
+ </tr>
+ </table>
== Note on Patches/Pull Requests
@@ -14,4 +60,4 @@ Description goes here.
== Copyright
-Copyright (c) 2010 Matt Griffin. See LICENSE for details.
+Copyright (c) 2010 Viximo, Inc. See LICENSE for details.
View
@@ -5,10 +5,9 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "updawg"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{A simple way to see what's up, dawg (and what's not.)}
gem.email = "matt@griffinonline.org"
- gem.homepage = "http://github.com/betamatt/updawg"
+ gem.homepage = "http://github.com/Viximo/updawg"
gem.authors = ["Matt Griffin"]
gem.add_development_dependency "rspec", ">= 1.2.9"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
View
@@ -0,0 +1,34 @@
+require 'updawg/monitor'
+require 'updawg/check'
+require 'updawg/results'
+
+module Updawg
+ class Configuration
+ attr_accessor :error_text
+ attr_accessor :warning_text
+
+ def initialize
+ self.error_text = 'ERROR'
+ self.warning_text = 'WARNING'
+ end
+ end
+
+ class << self
+ attr_accessor :configuration
+ end
+ self.configuration = Configuration.new
+
+ class << self
+ def configure
+ self.configuration ||= Configuration.new
+ yield(configuration) if block_given?
+ configuration
+ end
+
+ def monitor(&block)
+ monitor = Updawg::Monitor.new
+ monitor.instance_eval(&block)
+ monitor
+ end
+ end
+end
View
@@ -0,0 +1,30 @@
+module Updawg
+ class Check
+ attr_reader :name
+
+ def initialize(name, &block)
+ @name = name
+ @block = block
+ end
+
+ def perform
+ result = @block.call if @block
+ return result if result.kind_of?(Result)
+ pass
+ rescue RuntimeError => e
+ error e.message
+ end
+
+ def pass
+ Updawg::Result.new(name)
+ end
+
+ def warn(message = nil)
+ Updawg::Result.new(name, :warning, message)
+ end
+
+ def error(message = nil)
+ Updawg::Result.new(name, :error, message)
+ end
+ end
+end
View
@@ -0,0 +1,21 @@
+module Updawg
+ class Monitor
+ attr_accessor :checks
+
+ def initialize
+ self.checks = []
+ end
+
+ def check(name, &block)
+ check = Updawg::Check.new(name, &block)
+ self.checks << check
+ check
+ end
+
+ def perform
+ results = ResultSet.new
+ checks.each{|c| results << c.perform}
+ results
+ end
+ end
+end
View
@@ -0,0 +1,37 @@
+module Updawg
+ class ResultSet < Array
+ def success?
+ map(&:success?).all?
+ end
+
+ def warning?
+ map(&:warning?).any? && !error?
+ end
+
+ def error?
+ map(&:error?).any?
+ end
+ end
+
+ class Result
+ attr_reader :name, :status, :message
+
+ def initialize(name, status = :pass, message = nil)
+ @name = name
+ @status = status
+ @message = message
+ end
+
+ def success?
+ :pass == @status
+ end
+
+ def warning?
+ :warning == @status
+ end
+
+ def error?
+ :error == @status
+ end
+ end
+end
View
@@ -1,5 +1,6 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
require 'updawg'
require 'spec'
require 'spec/autorun'
View
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Updawg::Check do
+ describe 'initialize' do
+ it 'should save the block' do
+ block = lambda { 'eep' }
+ check = Updawg::Check.new('test', &block)
+ check.instance_eval { @block }.should == block
+ end
+ end
+
+ describe 'perform' do
+ describe 'with a successful block' do
+ before(:each) do
+ @check = Updawg::Check.new('test'){}
+ @result = @check.perform
+ end
+
+ it 'should return a result' do
+ @result.should be_instance_of(Updawg::Result)
+ end
+
+ it 'should be successful' do
+ @result.should be_success
+ end
+
+ it 'should not have a message' do
+ @result.message.should == nil
+ end
+ end
+
+ describe 'when the block raises an error' do
+ before(:each) do
+ @check = Updawg::Check.new('test'){ raise 'error: frog' }
+ lambda { @result = @check.perform }.should_not raise_error
+ end
+
+ it 'should be an error' do
+ @result.should be_error
+ end
+
+ it 'should have an error message' do
+ @result.message.should == 'error: frog'
+ end
+ end
+ end
+end
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Updawg::Monitor do
+ describe 'by default' do
+ it 'should have an empty list of checks' do
+ Updawg::Monitor.new.checks.should == []
+ end
+ end
+
+ describe '#check' do
+ before(:each) do
+ @monitor = Updawg::Monitor.new
+ end
+
+ it 'should create a new check' do
+ @monitor.check('test'){ 'eep'}.should be_instance_of(Updawg::Check)
+ end
+
+ it 'should set the name of the check' do
+ @monitor.check('test'){ 'eep'}.name.should == 'test'
+ end
+
+ it 'should create a new check each call' do
+ @monitor.check('test'){}.should_not === @monitor.check('ok'){}
+ end
+
+ it 'should add the check to the list' do
+ check = @monitor.check('test'){}
+ @monitor.checks.should include(check)
+ end
+ end
+
+ describe '#perform' do
+ before(:each) do
+ @monitor = Updawg::Monitor.new
+ @monitor.check 'One'
+ @monitor.check 'Two'
+ end
+
+ it 'should return result set with a result for each check' do
+ results = @monitor.perform
+ results.should be_instance_of(Updawg::ResultSet)
+ results.map(&:name).should == %w(One Two)
+ end
+ end
+end
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Updawg::ResultSet do
+ before(:each) do
+ @results = Updawg::ResultSet.new
+ @results << Updawg::Result.new('One', :pass)
+ end
+
+ it 'should be successful if all checks passed' do
+ @results.should be_success
+ @results.should_not be_warning
+ @results.should_not be_error
+ end
+
+ it 'should be warning if at least one check is a warning' do
+ @results << Updawg::Result.new('Two', :warning, 'oh no!')
+ @results.should be_warning
+ @results.should_not be_success
+ @results.should_not be_error
+ end
+
+ it 'should be error if at least one check is an error' do
+ @results << Updawg::Result.new('Two', :warning, 'oh no!')
+ @results << Updawg::Result.new('Three', :error, 'oh no!')
+ @results.should be_error
+ @results.should_not be_success
+ @results.should_not be_warning
+ end
+end
View
@@ -1,7 +1,50 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe "Updawg" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+describe Updawg do
+ it 'should create a configuration object' do
+ Updawg.configuration.should be_instance_of(Updawg::Configuration)
end
-end
+
+ describe "configuration" do
+ before(:each) do
+ Updawg.configuration = Updawg::Configuration.new
+ end
+
+ describe 'by default' do
+ it 'should set error_text to ERROR' do
+ Updawg.configuration.error_text.should == 'ERROR'
+ end
+
+ it 'should set warning_text to WARNING' do
+ Updawg.configuration.warning_text.should == 'WARNING'
+ end
+ end
+
+ it 'should preserve configuration options between configure calls' do
+ Updawg.configure{|c| c.error_text = 'test'}
+ Updawg.configure
+ Updawg.configuration.error_text.should == 'test'
+ end
+
+ it 'should allow configuration for error_text' do
+ Updawg.configure {|c| c.error_text = 'test'}
+ Updawg.configuration.error_text.should == 'test'
+ end
+
+ it 'should allow configuration for error_text' do
+ Updawg.configure {|c| c.error_text = 'test'}
+ Updawg.configuration.error_text.should == 'test'
+ end
+ end
+
+ describe '#monitor' do
+ it 'should create a monitor and instance_eval the block' do
+ block = lambda { self.class.should == Updawg::Monitor }
+ Updawg.monitor(&block).should be_instance_of(Updawg::Monitor)
+ end
+
+ it 'should return a new monitor with each call' do
+ Updawg.monitor{}.should_not === Updawg.monitor{}
+ end
+ end
+end

0 comments on commit 35f63c2

Please sign in to comment.