Skip to content

Commit

Permalink
[default-settings] setting defaults, no backtrace w/ invalid config
Browse files Browse the repository at this point in the history
  - rabbitmq & redis settings default to empty hashes
  - separate file for sensu version
  • Loading branch information
portertech committed Mar 20, 2012
1 parent b08d5cb commit 8850947
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/sensu.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Sensu
VERSION = "0.9.5.beta.2"
# A monitoring framework that aims to be simple, malleable, & scalable.
end
29 changes: 20 additions & 9 deletions lib/sensu/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require 'cabin'
require 'cabin/outputs/em/stdlib-logger'

require File.join(File.dirname(__FILE__), '..', 'sensu')
require File.join(File.dirname(__FILE__), 'version')
require File.join(File.dirname(__FILE__), 'patches', 'ruby')
require File.join(File.dirname(__FILE__), 'patches', 'amqp')

Expand All @@ -36,7 +36,9 @@ def initialize(options={})
end

def invalid_config(message)
raise 'configuration invalid, ' + message
puts 'CONFIGURATION INVALID: ' + message
puts 'SENSU NOT RUNNING'
exit 2
end

def setup_logging
Expand Down Expand Up @@ -75,6 +77,11 @@ def validate_common_settings
unless details.subscribers.is_a?(Array) && details.subscribers.count > 0
invalid_config('missing subscribers for check ' + name)
end
details.subscribers.each do |subscriber|
unless subscriber.is_a?(String) && !subscriber.empty?
invalid_config('a check subscriber must be a string (not empty) for check ' + name)
end
end
end
if details.key?('handler')
unless details.handler.is_a?(String)
Expand All @@ -95,7 +102,7 @@ def validate_server_settings
end
@settings.handlers.each do |name, details|
unless details.is_a?(Hash)
invalid_config('hander details must be a hash ' + name)
invalid_config('handler details must be a hash for handler ' + name)
end
unless details['type'].is_a?(String)
invalid_config('missing type for handler ' + name)
Expand Down Expand Up @@ -128,12 +135,13 @@ def validate_server_settings
end

def validate_api_settings
if @settings.api.key?('user')
unless @settings.api.port.is_a?(Integer)
invalid_config('api port must be an integer')
end
if @settings.api.key?('user') || @settings.api.key?('password')
unless @settings.api.user.is_a?(String)
invalid_config('api user must be a string')
end
end
if @settings.api.key?('password')
unless @settings.api.password.is_a?(String)
invalid_config('api password must be a string')
end
Expand All @@ -152,7 +160,7 @@ def validate_client_settings
end
@settings.client.subscriptions.each do |subscription|
unless subscription.is_a?(String) && !subscription.empty?
invalid_config('subscription must not be an empty string')
invalid_config('a client subscription must be a string (not empty)')
end
end
end
Expand All @@ -167,11 +175,11 @@ def has_keys(keys)

def validate_settings
@logger.debug('[validate] -- validating configuration')
has_keys(%w[rabbitmq checks])
has_keys(%w[checks])
validate_common_settings
case File.basename($0)
when 'rake'
has_keys(%w[redis api handlers client])
has_keys(%w[api handlers client])
validate_server_settings
validate_api_settings
validate_client_settings
Expand All @@ -192,6 +200,9 @@ def setup_settings
if File.readable?(@options[:config_file])
begin
config_hash = JSON.parse(File.open(@options[:config_file], 'r').read)
%w[rabbitmq redis].each do |key|
config_hash[key] ||= Hash.new
end
rescue JSON::ParserError => error
invalid_config('configuration file (' + @options[:config_file] + ') must be valid JSON: ' + error.to_s)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/sensu/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Sensu
VERSION = "0.9.5.beta.2"
end
2 changes: 1 addition & 1 deletion sensu.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path("../lib/sensu", __FILE__)
require File.expand_path("../lib/sensu/version", __FILE__)

Gem::Specification.new do |s|
s.name = "sensu"
Expand Down

0 comments on commit 8850947

Please sign in to comment.