what 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/what/config.rb CHANGED
@@ -1,57 +1,59 @@
1
- class What::Config
2
- DEFAULTS = {
3
- 'interval' => 10,
4
- 'formatter' => 'json',
5
- 'configs' => [],
6
- 'module_paths' => [],
7
- 'modules' => [],
8
- 'module_config' => {}
9
- }
10
-
11
- @config = {}
12
-
13
- def self.load(fn)
14
- load_primary(fn)
15
- load_secondary(@config['configs'])
16
- end
1
+ module What
2
+ class Config
3
+ DEFAULTS = {
4
+ 'interval' => 10,
5
+ 'formatter' => 'json',
6
+ 'configs' => [],
7
+ 'module_paths' => [],
8
+ 'modules' => [],
9
+ 'module_config' => {}
10
+ }
11
+
12
+ @config = {}
13
+
14
+ def self.load(fn)
15
+ load_primary(fn)
16
+ load_secondary(@config['configs'])
17
+ end
17
18
 
18
- def self.load_primary(fn)
19
- @config = DEFAULTS.merge(YAML.load_file(fn))
20
- @config['base'] ||= File.expand_path(File.dirname(fn))
21
- @loaded = true
22
- end
19
+ def self.load_primary(fn)
20
+ @config = DEFAULTS.merge(YAML.load_file(fn))
21
+ @config['base'] ||= File.expand_path(File.dirname(fn))
22
+ @loaded = true
23
+ end
23
24
 
24
- def self.load_secondary(fns)
25
- return if !fns
26
-
27
- fns.each do |fn|
28
- path = if fn.match(%r(^/))
29
- fn
30
- else
31
- File.join(@config['base'], fn)
32
- end
33
- begin
34
- opts = YAML.load_file(path)
35
- @config.merge!(opts)
36
- rescue Exception => e
37
- puts "Error loading config file #{path}: #{e}"
25
+ def self.load_secondary(fns)
26
+ return if !fns
27
+
28
+ fns.each do |fn|
29
+ path = if fn.match(%r(^/))
30
+ fn
31
+ else
32
+ File.join(@config['base'], fn)
33
+ end
34
+ begin
35
+ opts = YAML.load_file(path)
36
+ @config.merge!(opts)
37
+ rescue Exception => e
38
+ puts "Error loading config file #{path}: #{e}"
39
+ end
38
40
  end
39
41
  end
40
- end
41
42
 
42
- def self.loaded?
43
- @loaded
44
- end
43
+ def self.loaded?
44
+ @loaded
45
+ end
45
46
 
46
- def self.[](attr)
47
- @config[attr]
48
- end
47
+ def self.[](attr)
48
+ @config[attr]
49
+ end
49
50
 
50
- def self.[]=(attr, val)
51
- @config[attr] = val
52
- end
51
+ def self.[]=(attr, val)
52
+ @config[attr] = val
53
+ end
53
54
 
54
- def self.all
55
- @config
55
+ def self.all
56
+ @config
57
+ end
56
58
  end
57
59
  end
@@ -1,20 +1,22 @@
1
- class What::Formatter
2
- def self.use(name)
3
- @formatter = case name
4
- when 'json'
5
- Formatters::JSON.new
6
- when 'yaml'
7
- Formatters::YAML.new
8
- else
9
- raise "Unknown formatter #{name}"
10
- end
11
- end
1
+ module What
2
+ class Formatter
3
+ def self.use(name)
4
+ @formatter = case name
5
+ when 'json'
6
+ Formatters::JSON.new
7
+ when 'yaml'
8
+ Formatters::YAML.new
9
+ else
10
+ raise "Unknown formatter #{name}"
11
+ end
12
+ end
12
13
 
13
- def self.mime
14
- @formatter.mime
15
- end
14
+ def self.mime
15
+ @formatter.mime
16
+ end
16
17
 
17
- def self.format(hash)
18
- @formatter.format(hash)
18
+ def self.format(hash)
19
+ @formatter.format(hash)
20
+ end
19
21
  end
20
22
  end
@@ -1,9 +1,11 @@
1
- class What::Formatters::Base
2
- def mime
3
- raise "Formatter #{self.class.name} doesn't override 'mime'"
4
- end
1
+ module What
2
+ class Formatters::Base
3
+ def mime
4
+ raise "Formatter #{self.class.name} doesn't override 'mime'"
5
+ end
5
6
 
6
- def format(_)
7
- raise "Formatter #{self.class.name} doesn't override 'format'"
7
+ def format(_)
8
+ raise "Formatter #{self.class.name} doesn't override 'format'"
9
+ end
8
10
  end
9
11
  end
@@ -1,9 +1,11 @@
1
- class What::Formatters::JSON < What::Formatters::Base
2
- def mime
3
- 'application/json'
4
- end
1
+ module What
2
+ class Formatters::JSON < Formatters::Base
3
+ def mime
4
+ 'application/json'
5
+ end
5
6
 
6
- def format(hash)
7
- JSON.unparse(hash) + "\n"
7
+ def format(hash)
8
+ JSON.unparse(hash) + "\n"
9
+ end
8
10
  end
9
11
  end
@@ -1,9 +1,11 @@
1
- class What::Formatters::YAML < What::Formatters::Base
2
- def mime
3
- 'application/x-yaml'
4
- end
1
+ module What
2
+ class Formatters::YAML < Formatters::Base
3
+ def mime
4
+ 'application/x-yaml'
5
+ end
5
6
 
6
- def format(hash)
7
- hash.to_yaml
7
+ def format(hash)
8
+ hash.to_yaml
9
+ end
8
10
  end
9
11
  end
@@ -1,23 +1,25 @@
1
- module What::Formatters
2
- def self.load_all
3
- # load all formatters defined in what/formatters, in addition to any paths
4
- # specified in the config file.
5
- require 'what/formatters/base'
1
+ module What
2
+ module Formatters
3
+ def self.load_all
4
+ # load all formatters defined in what/formatters, in addition to any paths
5
+ # specified in the config file.
6
+ require 'what/formatters/base'
6
7
 
7
- globs = [File.join(File.dirname(__FILE__), 'formatters', '*.rb')]
8
+ globs = [File.join(File.dirname(__FILE__), 'formatters', '*.rb')]
8
9
 
9
- if Config['formatter_paths']
10
- Config['formatter_paths'].each do |formatter_path|
11
- globs << File.join(Config['base'], formatter_path, '*.rb')
10
+ if Config['formatter_paths']
11
+ Config['formatter_paths'].each do |formatter_path|
12
+ globs << File.join(Config['base'], formatter_path, '*.rb')
13
+ end
12
14
  end
13
- end
14
15
 
15
- globs.each do |glob|
16
- Dir[glob].each do |fn|
17
- require fn
16
+ globs.each do |glob|
17
+ Dir[glob].each do |fn|
18
+ require fn
19
+ end
18
20
  end
19
- end
20
21
 
21
- Formatter.use(Config['formatter'])
22
+ Formatter.use(Config['formatter'])
23
+ end
22
24
  end
23
25
  end
data/lib/what/helpers.rb CHANGED
@@ -1,35 +1,37 @@
1
- module What::Helpers
2
- # Take an array of healths and determine overall health, on
3
- # the principle that overall health == the worst sub-health.
4
- def self.overall_health(healths)
5
- healths.reduce(:ok) do |overall, current|
6
- case current
7
- when :ok
8
- overall
9
- when :warning
10
- :warning if overall != :alert
11
- else
12
- :alert
1
+ module What
2
+ module Helpers
3
+ # Take an array of healths and determine overall health, on
4
+ # the principle that overall health == the worst sub-health.
5
+ def self.overall_health(healths)
6
+ healths.reduce(:ok) do |overall, current|
7
+ case current
8
+ when :ok
9
+ overall
10
+ when :warning
11
+ :warning if overall != :alert
12
+ else
13
+ :alert
14
+ end
13
15
  end
14
16
  end
15
- end
16
17
 
17
- # Stolen from Rails (http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html)
18
- def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
19
- if first_letter_in_uppercase
20
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
21
- else
22
- lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
18
+ # Stolen from Rails (http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html)
19
+ def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
20
+ if first_letter_in_uppercase
21
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
22
+ else
23
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
24
+ end
23
25
  end
24
- end
25
26
 
26
- def self.underscore(camel_cased_word)
27
- word = camel_cased_word.to_s.dup
28
- word.gsub!(/::/, '/')
29
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
30
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
31
- word.tr!("-", "_")
32
- word.downcase!
33
- word
27
+ def self.underscore(camel_cased_word)
28
+ word = camel_cased_word.to_s.dup
29
+ word.gsub!(/::/, '/')
30
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
31
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
32
+ word.tr!("-", "_")
33
+ word.downcase!
34
+ word
35
+ end
34
36
  end
35
37
  end
@@ -1,26 +1,30 @@
1
- class What::Modules::Base
2
- DEFAULTS = {}
1
+ module What
2
+ class Modules::Base
3
+ def initialize
4
+ @config = if defined?(DEFAULTS)
5
+ DEFAULTS.merge(Config['module_config'][self.name] || {})
6
+ else
7
+ Config['module_config'][self.name]
8
+ end
9
+ end
3
10
 
4
- def initialize
5
- @config = DEFAULTS.merge(Config['module_config'][self.name] || {})
6
- end
7
-
8
- def name
9
- Helpers.underscore(self.class.name.split('::').last)
10
- end
11
+ def name
12
+ Helpers.underscore(self.class.name.split('::').last)
13
+ end
11
14
 
12
- def check!
13
- end
15
+ def check!
16
+ end
14
17
 
15
- def status
16
- { :health => health }.merge(details)
17
- end
18
+ def status
19
+ { :health => health }.merge(details)
20
+ end
18
21
 
19
- def health
20
- raise "Module #{self.class.name} doesn't override 'health'"
21
- end
22
+ def health
23
+ raise "Module #{self.class.name} doesn't override 'health'"
24
+ end
22
25
 
23
- def details
24
- {}
26
+ def details
27
+ {}
28
+ end
25
29
  end
26
30
  end
@@ -1,33 +1,34 @@
1
- class What::Modules::Processes < What::Modules::Base
2
- def initialize
3
- super
4
- @config.each do |name, regexp_str|
5
- @config[name] = Regexp.new(regexp_str)
1
+ module What
2
+ class Modules::Processes < Modules::Base
3
+ def initialize
4
+ super
5
+ @config.each do |name, regexp_str|
6
+ @config[name] = Regexp.new(regexp_str)
7
+ end
8
+ @processes = {}
6
9
  end
7
- @processes = {}
8
- end
9
10
 
10
- def check!
11
- @config.each do |name, regexp|
12
- @processes[name] = `ps aux`.split("\n").grep(regexp).map do |ln|
13
- ln =~ /^\w+\s+(\d+).*(\d+:\d\d(?:\.\d\d)?) (.*)$/
14
- {:pid => $1, :cpu_time => $2, :proctitle => $3.strip}
15
- end
11
+ def check!
12
+ @config.each do |name, regexp|
13
+ @processes[name] = `ps aux`.split("\n").grep(regexp).map do |ln|
14
+ ln =~ /^\w+\s+(\d+).*(\d+:\d\d(?:\.\d\d)?) (.*)$/
15
+ {:pid => $1, :cpu_time => $2, :proctitle => $3.strip}
16
+ end
17
+ end
16
18
  end
17
- end
18
19
 
19
- def health
20
- all_ok = true
21
- @processes.each do |name, results|
22
- if results.count == 0
23
- all_ok = false
20
+ def health
21
+ all_ok = true
22
+ @processes.each do |name, results|
23
+ if results.count == 0
24
+ all_ok = false
25
+ end
24
26
  end
27
+ all_ok ? :ok : :alert
25
28
  end
26
- all_ok ? :ok : :alert
27
- end
28
29
 
29
- def details
30
- @processes
30
+ def details
31
+ @processes
32
+ end
31
33
  end
32
34
  end
33
-
@@ -1,32 +1,34 @@
1
- class What::Modules::Unicorn < What::Modules::Base
2
- DEFAULTS = {
3
- 'warning' => 1,
4
- 'alert' => 0
5
- }
1
+ module What
2
+ class Modules::Unicorn < Modules::Base
3
+ DEFAULTS = {
4
+ 'warning' => 1,
5
+ 'alert' => 0
6
+ }
6
7
 
7
- def initialize
8
- super
9
- @unicorns = []
10
- end
8
+ def initialize
9
+ super
10
+ @unicorns = []
11
+ end
11
12
 
12
- def check!
13
- @unicorns = `ps aux`.split("\n").grep(/unicorn_rails worker/).map do |ln|
14
- ln =~ /^\w+\s+(\d+).*(\d+:\d\d(?:\.\d\d)?) unicorn/
15
- {:pid => $1, :cpu_time => $2}
16
- end
17
- end
13
+ def check!
14
+ @unicorns = `ps aux`.split("\n").grep(/unicorn_rails worker/).map do |ln|
15
+ ln =~ /^\w+\s+(\d+).*(\d+:\d\d(?:\.\d\d)?) unicorn/
16
+ {:pid => $1, :cpu_time => $2}
17
+ end
18
+ end
18
19
 
19
- def health
20
- if @unicorns.count > @config['warning']
21
- :ok
22
- elsif @unicorns.count > @config['alert']
23
- :warning
24
- else
25
- :alert
20
+ def health
21
+ if @unicorns.count > @config['warning']
22
+ :ok
23
+ elsif @unicorns.count > @config['alert']
24
+ :warning
25
+ else
26
+ :alert
27
+ end
26
28
  end
27
- end
28
29
 
29
- def details
30
- {:workers => @unicorns.count, :details => @unicorns}
30
+ def details
31
+ {:workers => @unicorns.count, :details => @unicorns}
32
+ end
31
33
  end
32
34
  end
data/lib/what/modules.rb CHANGED
@@ -1,26 +1,28 @@
1
- module What::Modules
2
- # load all modules defined in what/modules, in addition to any paths
3
- # specified in the config file.
4
- def self.load_all
5
- require 'what/modules/base'
1
+ module What
2
+ module Modules
3
+ # load all modules defined in what/modules, in addition to any paths
4
+ # specified in the config file.
5
+ def self.load_all
6
+ require 'what/modules/base'
6
7
 
7
- default_modules_path = File.join(File.dirname(__FILE__), 'modules')
8
- require_dir(included_modules_path)
8
+ default_modules_path = File.join(File.dirname(__FILE__), 'modules')
9
+ require_dir(default_modules_path)
9
10
 
10
- Config['module_paths'].each do |module_path|
11
- path = if module_path.match(%r(^/))
12
- module_path
13
- else
14
- File.join(Config['base'], module_path)
15
- end
16
- require_dir(path)
11
+ Config['module_paths'].each do |module_path|
12
+ path = if module_path.match(%r(^/))
13
+ module_path
14
+ else
15
+ File.join(Config['base'], module_path)
16
+ end
17
+ require_dir(path)
18
+ end
17
19
  end
18
- end
19
20
 
20
- private
21
- def require_dir(path)
22
- Dir[File.join(path, '*.rb')].each do |fn|
23
- require fn
21
+ private
22
+ def self.require_dir(path)
23
+ Dir[File.join(path, '*.rb')].each do |fn|
24
+ require fn
25
+ end
24
26
  end
25
- end
27
+ end
26
28
  end
data/lib/what/monitor.rb CHANGED
@@ -1,25 +1,27 @@
1
- class What::Monitor
2
- # don't worry, these method names are ironic
3
- def self.go!
4
- @modules = Config['modules'].map do |m|
5
- name = Helpers.camelize(m)
6
- Modules.const_get(name).new
7
- end
1
+ module What
2
+ class Monitor
3
+ # don't worry, these method names are ironic
4
+ def self.go!
5
+ @modules = Config['modules'].map do |m|
6
+ name = Helpers.camelize(m)
7
+ Modules.const_get(name).new
8
+ end
8
9
 
9
- Thread.abort_on_exception = true
10
- @thread = Thread.new(@modules) { |modules| self.do_it(modules) }
11
- end
10
+ Thread.abort_on_exception = true
11
+ @thread = Thread.new(@modules) { |modules| self.do_it(modules) }
12
+ end
12
13
 
13
- def self.do_it(modules)
14
- loop do
15
- healths = []
16
- modules.each do |mod|
17
- mod.check!
18
- healths << mod.health
19
- Status[mod.name] = mod.status
14
+ def self.do_it(modules)
15
+ loop do
16
+ healths = []
17
+ modules.each do |mod|
18
+ mod.check!
19
+ healths << mod.health
20
+ Status[mod.name] = mod.status
21
+ end
22
+ Status[:health] = Helpers.overall_health(healths)
23
+ sleep Config['interval']
20
24
  end
21
- Status[:health] = Helpers.overall_health(healths)
22
- sleep Config['interval']
23
25
  end
24
26
  end
25
27
  end
data/lib/what/server.rb CHANGED
@@ -1,15 +1,17 @@
1
- class What::Server
2
- def initialize
3
- Modules.load_all
4
- Formatters.load_all
5
- Monitor.go!
6
- end
1
+ module What
2
+ class Server
3
+ def initialize
4
+ Modules.load_all
5
+ Formatters.load_all
6
+ Monitor.go!
7
+ end
7
8
 
8
- def call(_)
9
- [
10
- Status[:health] != :alert ? 200 : 503,
11
- {'Content-Type' => Formatter.mime},
12
- Formatter.format(Status.all)
13
- ]
9
+ def call(_)
10
+ [
11
+ Status[:health] != :alert ? 200 : 503,
12
+ {'Content-Type' => Formatter.mime},
13
+ Formatter.format(Status.all)
14
+ ]
15
+ end
14
16
  end
15
17
  end
data/lib/what/status.rb CHANGED
@@ -1,15 +1,17 @@
1
- class What::Status
2
- @status = {}
1
+ module What
2
+ class Status
3
+ @status = {}
3
4
 
4
- def self.[](attr)
5
- @status[attr]
6
- end
5
+ def self.[](attr)
6
+ @status[attr]
7
+ end
7
8
 
8
- def self.[]=(attr, val)
9
- @status[attr] = val
10
- end
9
+ def self.[]=(attr, val)
10
+ @status[attr] = val
11
+ end
11
12
 
12
- def self.all
13
- @status
13
+ def self.all
14
+ @status
15
+ end
14
16
  end
15
17
  end
data/lib/what/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module What
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: what
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Lower