Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[daemonize] fix up config and logging #152

Merged
merged 3 commits into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  bin/sensu-api
@@ -4,5 +4,4 @@ $: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE_
require 'sensu/api'
options = Sensu::Config.read_arguments(ARGV)
-options['type'] = 'api'
Sensu::API.run(options)
View
1  bin/sensu-client
@@ -4,5 +4,4 @@ $: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE_
require 'sensu/client'
options = Sensu::Config.read_arguments(ARGV)
-options['type'] = 'client'
Sensu::Client.run(options)
View
1  bin/sensu-server
@@ -4,5 +4,4 @@ $: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE_
require 'sensu/server'
options = Sensu::Config.read_arguments(ARGV)
-options['type'] = 'server'
Sensu::Server.run(options)
View
2  lib/sensu/api.rb
@@ -25,7 +25,7 @@ def self.run(options={})
def self.setup(options={})
config = Sensu::Config.new(options)
$settings = config.settings
- $logger = config.open_log
+ $logger = config.logger || config.open_log
if options[:daemonize]
Process.daemonize
end
View
2  lib/sensu/client.rb
@@ -26,7 +26,7 @@ def self.run(options={})
def initialize(options={})
config = Sensu::Config.new(options)
@settings = config.settings
- @logger = config.open_log
+ @logger = config.logger || config.open_log
end
def stop(signal)
View
44 lib/sensu/config.rb
@@ -17,6 +17,8 @@ module Sensu
class Config
attr_accessor :settings, :logger
+ SERVICE = File.basename($0).split('-').last
+
DEFAULT_OPTIONS = {
:log_file => '/tmp/sensu.log',
:config_file => '/etc/sensu/config.json',
@@ -28,20 +30,26 @@ class Config
def initialize(options={})
@options = DEFAULT_OPTIONS.merge(options)
+ if options[:log_file]
+ open_log
+ end
read_config
- validate_config if @options[:validate]
+ if @options[:validate]
+ validate_config
+ end
end
def open_log
@logger = Cabin::Channel.new
if File.writable?(@options[:log_file]) || !File.exist?(@options[:log_file]) && File.writable?(File.dirname(@options[:log_file]))
- ruby_logger = case
- when @options[:daemonize]
+ ruby_logger = case SERVICE
+ when 'rake'
+ Logger.new(@options[:log_file])
+ else
STDOUT.reopen(@options[:log_file], 'a')
STDERR.reopen(STDOUT)
+ STDOUT.sync = true
Logger.new(STDOUT)
- else
- Logger.new(@options[:log_file])
end
else
invalid_config('log file is not writable: ' + @options[:log_file])
@@ -72,22 +80,29 @@ def read_config
invalid_config('configuration snippet file (' + snippet_file + ') must be valid JSON: ' + error.to_s)
end
merged_settings = @settings.to_hash.deep_merge(snippet_hash)
- @logger.warn('[settings] configuration snippet (' + snippet_file + ') applied changes: ' + @settings.deep_diff(merged_settings).to_json) if @logger
+ if @logger
+ @logger.warn('[settings] configuration snippet (' + snippet_file + ') applied changes: ' + @settings.deep_diff(merged_settings).to_json)
+ end
@settings = Hashie::Mash.new(merged_settings)
end
end
end
def validate_config
- @logger.debug('[config] -- validating configuration') if @logger
+ if @logger
+ @logger.debug('[config] -- validating configuration')
+ end
has_keys(%w[rabbitmq])
- case @options['type']
- when 'server'
+ case SERVICE
+ when 'server', 'rake'
has_keys(%w[redis handlers checks])
unless @settings.handlers.include?('default')
invalid_config('missing default handler')
end
@settings.handlers.each do |name, details|
+ unless details.is_a?(Hash)
+ invalid_config('hander details must be a hash ' + name)
+ end
unless details.key?('type')
invalid_config('missing type for handler ' + name)
end
@@ -107,9 +122,9 @@ def validate_config
invalid_config('unknown type for handler ' + name)
end
end
- when 'api'
+ when 'api', 'rake'
has_keys(%w[redis api])
- when 'client'
+ when 'client', 'rake'
has_keys(%w[client checks])
unless @settings.client.name.is_a?(String)
invalid_config('client must have a name')
@@ -142,9 +157,8 @@ def validate_config
end
end
end
- if @options['type']
- @logger.debug('[config] -- configuration valid -- running ' + @options['type']) if @logger
- puts 'configuration valid -- running ' + @options['type']
+ if @logger
+ @logger.debug('[config] -- configuration valid -- running ' + SERVICE)
end
end
@@ -157,7 +171,6 @@ def has_keys(keys)
end
def invalid_config(message)
- @logger.error('[config] -- configuration invalid -- ' + message) if @logger
raise 'configuration invalid, ' + message
end
@@ -168,7 +181,6 @@ def self.read_arguments(arguments)
puts opts
exit
end
- current_process = File.basename($0)
opts.on('-c', '--config FILE', 'Sensu JSON config FILE (default: /etc/sensu/config.json)') do |file|
options[:config_file] = file
end
View
2  lib/sensu/server.rb
@@ -34,7 +34,7 @@ def self.run(options={})
def initialize(options={})
config = Sensu::Config.new(options)
@settings = config.settings
- @logger = config.open_log
+ @logger = config.logger || config.open_log
end
def stop(signal)
View
7 test/conf.d/snippet.json
@@ -1,5 +1,10 @@
{
- "handlers": { "new_handler": "this won't override the other handlers" },
+ "handlers": {
+ "new_handler": {
+ "type": "pipe",
+ "command": "this won't override the other handlers"
+ }
+ },
"checks": {
"b": {
"command": "ruby -e \"puts ':::name:::'; exit 2\"",
View
2  test/config.json
@@ -9,7 +9,7 @@
},
"api": {
"host": "localhost",
- "port": 7070
+ "port": 4567
},
"dashboard": {
"host": "localhost",
View
64 test/sensu_api_test.rb
@@ -12,6 +12,29 @@ def setup
Sensu::API.test(@options)
end
+ def test_get_events
+ EM.add_timer(1) do
+ http = EM::HttpRequest.new(@api + '/events').get
+ http.callback do
+ assert_equal(200, http.response_header.status)
+ events = JSON.parse(http.response)
+ assert(events.is_a?(Hash))
+ assert_block "Response didn't contain the test event" do
+ contains_test_event = false
+ events.each do |client, events|
+ if client == @settings.client.name
+ events.each do |check, event|
+ contains_test_event = true if check == 'test'
+ end
+ end
+ end
+ contains_test_event
+ end
+ done
+ end
+ end
+ end
+
def test_get_clients
EM.add_timer(1) do
http = EM::HttpRequest.new(@api + '/clients').get
@@ -29,24 +52,14 @@ def test_get_clients
end
end
- def test_get_events
+ def test_get_checks
EM.add_timer(1) do
- http = EM::HttpRequest.new(@api + '/events').get
+ http = EM::HttpRequest.new(@api + '/checks').get
http.callback do
assert_equal(200, http.response_header.status)
- events = JSON.parse(http.response)
- assert(events.is_a?(Hash))
- assert_block "Response didn't contain the test event" do
- contains_test_event = false
- events.each do |client, events|
- if client == @settings.client.name
- events.each do |check, event|
- contains_test_event = true if check == 'test'
- end
- end
- end
- contains_test_event
- end
+ checks = JSON.parse(http.response)
+ assert(checks.is_a?(Hash))
+ assert_equal(checks, @settings.checks.to_hash)
done
end
end
@@ -149,6 +162,27 @@ def test_delete_nonexistent_client
end
end
+ def test_get_check
+ EM.add_timer(1) do
+ http = EM::HttpRequest.new(@api + '/check/a').get
+ http.callback do
+ assert_equal(200, http.response_header.status)
+ assert_equal(@settings.checks.a.to_hash, JSON.parse(http.response))
+ done
+ end
+ end
+ end
+
+ def test_get_nonexistent_check
+ EM.add_timer(1) do
+ http = EM::HttpRequest.new(@api + '/check/nonexistent').get
+ http.callback do
+ assert_equal(404, http.response_header.status)
+ done
+ end
+ end
+ end
+
def test_create_stash
EM.add_timer(1) do
options = {
Something went wrong with that request. Please try again.