Module: Sensu::Daemon
- Includes:
- Utilities
- Included in:
- API::Process, Client::Process, Server::Process
- Defined in:
- lib/sensu/daemon.rb
Constant Summary
Constants included from Utilities
Instance Attribute Summary collapse
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Object
Initialize the Sensu process.
-
#load_extensions(options = {}) ⇒ Object
Load Sensu extensions and log any notices.
-
#load_settings(options = {}) ⇒ Object
Load Sensu settings.
-
#log_notices(notices = [], level = :warn) ⇒ Object
Log setting or extension loading notices, sensitive information is redacted.
-
#pause ⇒ Object
Pause the Sensu service and set the service state to ‘:paused`.
-
#print_settings!(settings) ⇒ Object
Print the Sensu settings (JSON) to STDOUT and immediately exit the process with the appropriate exit status code.
-
#resume ⇒ Object
Resume the paused Sensu service and set the service state to ‘:running`.
-
#setup_logger(options = {}) ⇒ Object
Set up the Sensu logger and its process signal traps for log rotation and debug log level toggling.
-
#setup_process(options) ⇒ Object
Manage the current process, optionally daemonize and/or write the current process ID to a PID file.
-
#setup_redis {|Object| ... } ⇒ Object
Set up the Redis connection.
-
#setup_signal_traps ⇒ Object
Set up process signal traps.
-
#setup_spawn ⇒ Object
Set up Sensu spawn, creating a worker to create, control, and limit spawned child processes.
-
#setup_transport {|Object| ... } ⇒ Object
Set up the Sensu transport connection.
-
#start ⇒ Object
Start the Sensu service and set the service state to ‘:running`.
-
#stop ⇒ Object
Stop the Sensu service and set the service state to ‘:stopped`.
-
#validate_settings!(settings) ⇒ Object
Determine if the Sensu settings are valid, if there are load or validation errors, and immediately exit the process with the appropriate exit status code.
Methods included from Utilities
#attributes_match?, #check_subdued?, #deep_merge, #determine_check_cron_time, #eval_attribute_value, #find_attribute_value, #in_time_window?, #in_time_windows?, #object_substitute_tokens, #process_cpu_times, #process_eval_string, #random_uuid, #redact_sensitive, #retry_until_true, #substitute_tokens, #system_address, #system_hostname, #testing?
Instance Attribute Details
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
37 38 39 |
# File 'lib/sensu/daemon.rb', line 37 def settings @settings end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
37 38 39 |
# File 'lib/sensu/daemon.rb', line 37 def start_time @start_time end |
Instance Method Details
#initialize(options = {}) ⇒ Object
Initialize the Sensu process. Set the start time, initial service state, double the maximum number of EventMachine timers, set up the logger, and load settings. This method will load extensions and setup Sensu Spawn if the Sensu process is not the Sensu API. This method can and optionally daemonize the process and/or create a PID file.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sensu/daemon.rb', line 47 def initialize(={}) @start_time = Time.now.to_i @state = :initializing @timers = {:run => []} unless EM::reactor_running? EM::epoll EM::set_max_timers(200000) end setup_logger() load_settings() unless sensu_service_name == "api" load_extensions() setup_spawn end setup_process() end |
#load_extensions(options = {}) ⇒ Object
Load Sensu extensions and log any notices. Set the logger and settings for each extension instance. This method creates the extensions instance variable: ‘@extensions`.
github.com/sensu/sensu-extensions github.com/sensu/sensu-extension
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/sensu/daemon.rb', line 156 def load_extensions(={}) = .merge(:extensions => @settings[:extensions]) @extensions = Extensions.get() log_notices(@extensions.warnings) extension_settings = @settings.to_hash.dup @extensions.all.each do |extension| extension.logger = @logger extension.settings = extension_settings end end |
#load_settings(options = {}) ⇒ Object
Load Sensu settings. This method creates the settings instance variable: ‘@settings`. If the `validate_config` option is true, this method calls `validate_settings!()` to validate the latest compiled configuration settings and will then exit the process. If the `print_config` option is true, this method calls `print_settings!()` to output the compiled configuration settings and will then exit the process. If there are loading or validation errors, they will be logged (notices), and this method will exit(2) the process.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sensu/daemon.rb', line 135 def load_settings(={}) @settings = Settings.get() validate_settings!(@settings) if [:validate_config] log_notices(@settings.warnings) log_notices(@settings.errors, :fatal) print_settings!(@settings) if [:print_config] unless @settings.errors.empty? @logger.fatal("SENSU NOT RUNNING!") exit 2 end @settings.set_env! end |
#log_notices(notices = [], level = :warn) ⇒ Object
Log setting or extension loading notices, sensitive information is redacted.
81 82 83 84 85 86 |
# File 'lib/sensu/daemon.rb', line 81 def log_notices(notices=[], level=:warn) notices.each do |concern| = concern.delete(:message) @logger.send(level, , redact_sensitive(concern)) end end |
#pause ⇒ Object
Pause the Sensu service and set the service state to ‘:paused`. This method will likely be overridden by a subclass.
200 201 202 |
# File 'lib/sensu/daemon.rb', line 200 def pause @state = :paused end |
#print_settings!(settings) ⇒ Object
Print the Sensu settings (JSON) to STDOUT and immediately exit the process with the appropriate exit status code. This method is used while troubleshooting configuration issues, triggered by a CLI argument, e.g. ‘–print_config`. Sensu settings with sensitive values (e.g. passwords) are first redacted.
114 115 116 117 118 119 |
# File 'lib/sensu/daemon.rb', line 114 def print_settings!(settings) redacted_settings = redact_sensitive(settings.to_hash) @logger.warn("outputting compiled configuration and exiting") puts Sensu::JSON.dump(redacted_settings, :pretty => true) exit(settings.errors.empty? ? 0 : 2) end |
#resume ⇒ Object
Resume the paused Sensu service and set the service state to ‘:running`. This method will likely be overridden by a subclass.
206 207 208 |
# File 'lib/sensu/daemon.rb', line 206 def resume @state = :running end |
#setup_logger(options = {}) ⇒ Object
Set up the Sensu logger and its process signal traps for log rotation and debug log level toggling. This method creates the logger instance variable: ‘@logger`.
71 72 73 74 |
# File 'lib/sensu/daemon.rb', line 71 def setup_logger(={}) @logger = Logger.get() @logger.setup_signal_traps end |
#setup_process(options) ⇒ Object
Manage the current process, optionally daemonize and/or write the current process ID to a PID file.
185 186 187 188 |
# File 'lib/sensu/daemon.rb', line 185 def setup_process() daemonize if [:daemonize] write_pid([:pid_file]) if [:pid_file] end |
#setup_redis {|Object| ... } ⇒ Object
Set up the Redis connection. Sensu uses Redis as a data store, to store the client registry, current events, etc. The Sensu service will stop gracefully in the event of a Redis error, and pause/resume in the event of connectivity issues. This method creates the Redis instance variable: ‘@redis`.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/sensu/daemon.rb', line 293 def setup_redis @logger.debug("connecting to redis", :settings => @settings[:redis]) Redis.logger = @logger Redis.connect(@settings[:redis]) do |connection| @redis = connection @redis.on_error do |error| @logger.error("redis connection error", :error => error.to_s) end @redis.before_reconnect do unless testing? @logger.warn("reconnecting to redis") pause end end @redis.after_reconnect do @logger.info("reconnected to redis") resume end yield(@redis) if block_given? end end |
#setup_signal_traps ⇒ Object
Set up process signal traps. This method uses the ‘STOP_SIGNALS` constant to determine which process signals will result in a graceful service stop. A periodic timer must be used to poll for received signals, as Mutex#lock cannot be used within the context of `trap()`.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/sensu/daemon.rb', line 224 def setup_signal_traps @signals = [] STOP_SIGNALS.each do |signal| Signal.trap(signal) do @signals << signal end end EM::PeriodicTimer.new(1) do signal = @signals.shift if STOP_SIGNALS.include?(signal) @logger.warn("received signal", :signal => signal) stop end end end |
#setup_spawn ⇒ Object
Set up Sensu spawn, creating a worker to create, control, and limit spawned child processes. This method adjusts the EventMachine thread pool size to accommodate the concurrent process spawn limit and other Sensu process operations.
173 174 175 176 177 178 179 |
# File 'lib/sensu/daemon.rb', line 173 def setup_spawn @logger.info("configuring sensu spawn", :settings => @settings[:sensu][:spawn]) threadpool_size = @settings[:sensu][:spawn][:limit] + 10 @logger.debug("setting eventmachine threadpool size", :size => threadpool_size) EM::threadpool_size = threadpool_size Spawn.setup(@settings[:sensu][:spawn]) end |
#setup_transport {|Object| ... } ⇒ Object
Set up the Sensu transport connection. Sensu uses a transport API, allowing it to use various message brokers. By default, Sensu will use the built-in “rabbitmq” transport. The Sensu service will stop gracefully in the event of a transport error, and pause/resume in the event of connectivity issues. This method creates the transport instance variable: ‘@transport`.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/sensu/daemon.rb', line 251 def setup_transport transport_name = @settings[:transport][:name] transport_settings = @settings[transport_name] @logger.debug("connecting to transport", { :name => transport_name, :settings => transport_settings }) Transport.logger = @logger Transport.connect(transport_name, transport_settings) do |connection| @transport = connection @transport.on_error do |error| @logger.error("transport connection error", :error => error.to_s) if @settings[:transport][:reconnect_on_error] @transport.reconnect else stop end end @transport.before_reconnect do unless testing? @logger.warn("reconnecting to transport") pause end end @transport.after_reconnect do @logger.info("reconnected to transport") resume end yield(@transport) if block_given? end end |
#start ⇒ Object
Start the Sensu service and set the service state to ‘:running`. This method will likely be overridden by a subclass. Yield if a block is provided.
193 194 195 196 |
# File 'lib/sensu/daemon.rb', line 193 def start @state = :running yield if block_given? end |
#stop ⇒ Object
Stop the Sensu service and set the service state to ‘:stopped`. This method will likely be overridden by a subclass. This method should stop the EventMachine event loop.
213 214 215 216 217 |
# File 'lib/sensu/daemon.rb', line 213 def stop @state = :stopped @logger.warn("stopping reactor") EM::stop_event_loop end |
#validate_settings!(settings) ⇒ Object
Determine if the Sensu settings are valid, if there are load or validation errors, and immediately exit the process with the appropriate exit status code. This method is used to determine if the latest configuration changes are valid prior to restarting the Sensu service, triggered by a CLI argument, e.g. ‘–validate_config`.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sensu/daemon.rb', line 96 def validate_settings!(settings) if settings.errors.empty? puts "configuration is valid" exit else puts "configuration is invalid" puts Sensu::JSON.dump({:errors => @settings.errors}, :pretty => true) exit 2 end end |