Skip to content
This repository has been archived by the owner on Jan 1, 2020. It is now read-only.

Commit

Permalink
[daemonize] fix up config and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
portertech committed Dec 22, 2011
1 parent 8167c52 commit 234bd4b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
1 change: 0 additions & 1 deletion bin/sensu-api
Expand Up @@ -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)
1 change: 0 additions & 1 deletion bin/sensu-client
Expand Up @@ -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)
1 change: 0 additions & 1 deletion bin/sensu-server
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion lib/sensu/api.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/sensu/client.rb
Expand Up @@ -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)
Expand Down
44 changes: 28 additions & 16 deletions lib/sensu/config.rb
Expand Up @@ -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',
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/sensu/server.rb
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion 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\"",
Expand Down

0 comments on commit 234bd4b

Please sign in to comment.