sensu 0.23.3 → 0.24.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sensu/daemon.rb CHANGED
@@ -2,14 +2,14 @@ require "rubygems"
2
2
 
3
3
  gem "eventmachine", "1.2.0.1"
4
4
 
5
- gem "sensu-json", "1.1.1"
5
+ gem "sensu-json", "2.0.0"
6
6
  gem "sensu-logger", "1.2.0"
7
- gem "sensu-settings", "3.4.0"
7
+ gem "sensu-settings", "5.1.0"
8
8
  gem "sensu-extension", "1.5.0"
9
9
  gem "sensu-extensions", "1.5.0"
10
- gem "sensu-transport", "5.0.0"
10
+ gem "sensu-transport", "6.0.0"
11
11
  gem "sensu-spawn", "2.2.0"
12
- gem "sensu-redis", "1.3.0"
12
+ gem "sensu-redis", "1.4.0"
13
13
 
14
14
  require "time"
15
15
  require "uri"
@@ -49,6 +49,7 @@ module Sensu
49
49
  setup_logger(options)
50
50
  load_settings(options)
51
51
  load_extensions(options)
52
+ setup_spawn
52
53
  setup_process(options)
53
54
  end
54
55
 
@@ -64,56 +65,78 @@ module Sensu
64
65
  @logger.setup_signal_traps
65
66
  end
66
67
 
67
- # Log setting or extension loading concerns, sensitive information
68
+ # Log setting or extension loading notices, sensitive information
68
69
  # is redacted.
69
70
  #
70
- # @param concerns [Array] to be logged.
71
- # @param level [Symbol] to log the concerns at.
72
- def log_concerns(concerns=[], level=:warn)
73
- concerns.each do |concern|
71
+ # @param notices [Array] to be logged.
72
+ # @param level [Symbol] to log the notices at.
73
+ def log_notices(notices=[], level=:warn)
74
+ notices.each do |concern|
74
75
  message = concern.delete(:message)
75
76
  @logger.send(level, message, redact_sensitive(concern))
76
77
  end
77
78
  end
78
79
 
79
- # Print the Sensu settings and immediately exit the process. This
80
- # method is used while troubleshooting configuration issues,
81
- # triggered by a CLI argument, e.g. `--print_config`. Sensu
82
- # settings with sensitive values (e.g. passwords) are first
83
- # redacted.
80
+ # Determine if the Sensu settings are valid, if there are load or
81
+ # validation errors, and immediately exit the process with the
82
+ # appropriate exit status code. This method is used to determine
83
+ # if the latest configuration changes are valid prior to
84
+ # restarting the Sensu service, triggered by a CLI argument, e.g.
85
+ # `--validate_config`.
84
86
  #
85
87
  # @param settings [Object]
86
- def print_settings(settings)
88
+ def validate_settings!(settings)
89
+ if settings.errors.empty?
90
+ puts "configuration is valid"
91
+ exit
92
+ else
93
+ puts "configuration is invalid"
94
+ puts Sensu::JSON.dump({:errors => @settings.errors}, :pretty => true)
95
+ exit 2
96
+ end
97
+ end
98
+
99
+ # Print the Sensu settings (JSON) to STDOUT and immediately exit
100
+ # the process with the appropriate exit status code. This method
101
+ # is used while troubleshooting configuration issues, triggered by
102
+ # a CLI argument, e.g. `--print_config`. Sensu settings with
103
+ # sensitive values (e.g. passwords) are first redacted.
104
+ #
105
+ # @param settings [Object]
106
+ def print_settings!(settings)
87
107
  redacted_settings = redact_sensitive(settings.to_hash)
88
108
  @logger.warn("outputting compiled configuration and exiting")
89
109
  puts Sensu::JSON.dump(redacted_settings, :pretty => true)
90
- exit
110
+ exit(settings.errors.empty? ? 0 : 2)
91
111
  end
92
112
 
93
- # Load Sensu settings and validate them. If there are validation
94
- # failures, log them (concerns), then cause the Sensu process to
95
- # exit (2). This method creates the settings instance variable:
96
- # `@settings`. If the `print_config` option is true, this method
97
- # calls `print_settings()` to output the compiled configuration
98
- # settings and then exit the process.
113
+ # Load Sensu settings. This method creates the settings instance
114
+ # variable: `@settings`. If the `validate_config` option is true,
115
+ # this method calls `validate_settings!()` to validate the latest
116
+ # compiled configuration settings and will then exit the process.
117
+ # If the `print_config` option is true, this method calls
118
+ # `print_settings!()` to output the compiled configuration
119
+ # settings and will then exit the process. If there are loading or
120
+ # validation errors, they will be logged (notices), and this
121
+ # method will exit(2) the process.
122
+ #
99
123
  #
100
124
  # https://github.com/sensu/sensu-settings
101
125
  #
102
126
  # @param options [Hash]
103
127
  def load_settings(options={})
104
128
  @settings = Settings.get(options)
105
- log_concerns(@settings.warnings)
106
- failures = @settings.validate
107
- unless failures.empty?
108
- @logger.fatal("invalid settings")
109
- log_concerns(failures, :fatal)
129
+ validate_settings!(@settings) if options[:validate_config]
130
+ log_notices(@settings.warnings)
131
+ log_notices(@settings.errors, :fatal)
132
+ print_settings!(@settings) if options[:print_config]
133
+ unless @settings.errors.empty?
110
134
  @logger.fatal("SENSU NOT RUNNING!")
111
135
  exit 2
112
136
  end
113
- print_settings(@settings) if options[:print_config]
114
137
  end
115
138
 
116
- # Load Sensu extensions and log any concerns. Set the logger and
139
+ # Load Sensu extensions and log any notices. Set the logger and
117
140
  # settings for each extension instance. This method creates the
118
141
  # extensions instance variable: `@extensions`.
119
142
  #
@@ -123,7 +146,7 @@ module Sensu
123
146
  # @param options [Hash]
124
147
  def load_extensions(options={})
125
148
  @extensions = Extensions.get(options)
126
- log_concerns(@extensions.warnings)
149
+ log_notices(@extensions.warnings)
127
150
  extension_settings = @settings.to_hash.dup
128
151
  @extensions.all.each do |extension|
129
152
  extension.logger = @logger
@@ -131,6 +154,20 @@ module Sensu
131
154
  end
132
155
  end
133
156
 
157
+ # Set up Sensu spawn, creating a worker to create, control, and
158
+ # limit spawned child processes. This method adjusts the
159
+ # EventMachine thread pool size to accommodate the concurrent
160
+ # process spawn limit and other Sensu process operations.
161
+ #
162
+ # https://github.com/sensu/sensu-spawn
163
+ def setup_spawn
164
+ @logger.info("configuring sensu spawn", :settings => @settings[:sensu][:spawn])
165
+ threadpool_size = @settings[:sensu][:spawn][:limit] + 10
166
+ @logger.debug("setting eventmachine threadpool size", :size => threadpool_size)
167
+ EM.threadpool_size = threadpool_size
168
+ Spawn.setup(@settings[:sensu][:spawn])
169
+ end
170
+
134
171
  # Manage the current process, optionally daemonize and/or write
135
172
  # the current process ID to a PID file.
136
173
  #
@@ -345,14 +345,14 @@ module Sensu
345
345
  end
346
346
  if filter_message
347
347
  @logger.info(filter_message, details)
348
- @handling_event_count -= 1 if @handling_event_count
348
+ @in_progress[:events] -= 1 if @in_progress
349
349
  else
350
350
  event_filtered?(handler, event) do |filtered|
351
351
  unless filtered
352
352
  yield(event)
353
353
  else
354
354
  @logger.info("event was filtered", details)
355
- @handling_event_count -= 1 if @handling_event_count
355
+ @in_progress[:events] -= 1 if @in_progress
356
356
  end
357
357
  end
358
358
  end
@@ -4,7 +4,7 @@ module Sensu
4
4
  module Server
5
5
  module Handle
6
6
  # Create a handler error callback, for logging the error and
7
- # decrementing the `@handling_event_count` by `1`.
7
+ # decrementing the `@in_progress[:events]` by `1`.
8
8
  #
9
9
  # @param handler [Object]
10
10
  # @param event_data [Object]
@@ -16,14 +16,14 @@ module Sensu
16
16
  :event_data => event_data,
17
17
  :error => error.to_s
18
18
  })
19
- @handling_event_count -= 1 if @handling_event_count
19
+ @in_progress[:events] -= 1 if @in_progress
20
20
  end
21
21
  end
22
22
 
23
23
  # Execute a pipe event handler, using the defined handler
24
24
  # command to spawn a process, passing it event data via STDIN.
25
25
  # Log the handler output lines and decrement the
26
- # `@handling_event_count` by `1` when the handler executes
26
+ # `@in_progress[:events]` by `1` when the handler executes
27
27
  # successfully.
28
28
  #
29
29
  # @param handler [Hash] definition.
@@ -36,7 +36,7 @@ module Sensu
36
36
  :handler => handler,
37
37
  :output => output.split("\n+")
38
38
  })
39
- @handling_event_count -= 1 if @handling_event_count
39
+ @in_progress[:events] -= 1 if @in_progress
40
40
  end
41
41
  end
42
42
 
@@ -47,7 +47,7 @@ module Sensu
47
47
  # `handler_error()` method is used to create the `on_error`
48
48
  # callback for the connection handler. The `on_error` callback
49
49
  # is call in the event of any error(s). The
50
- # `@handling_event_count` is decremented by `1` when the data is
50
+ # `@in_progress[:events]` is decremented by `1` when the data is
51
51
  # transmitted successfully, `on_success`.
52
52
  #
53
53
  # @param handler [Hash] definition.
@@ -57,7 +57,7 @@ module Sensu
57
57
  begin
58
58
  EM::connect(handler[:socket][:host], handler[:socket][:port], Socket) do |socket|
59
59
  socket.on_success = Proc.new do
60
- @handling_event_count -= 1 if @handling_event_count
60
+ @in_progress[:events] -= 1 if @in_progress
61
61
  end
62
62
  socket.on_error = on_error
63
63
  timeout = handler[:timeout] || 10
@@ -71,7 +71,7 @@ module Sensu
71
71
  end
72
72
 
73
73
  # Transmit event data to a UDP socket, then close the
74
- # connection. The `@handling_event_count` is decremented by `1`
74
+ # connection. The `@in_progress[:events]` is decremented by `1`
75
75
  # when the data is assumed to have been transmitted.
76
76
  #
77
77
  # @param handler [Hash] definition.
@@ -81,7 +81,7 @@ module Sensu
81
81
  EM::open_datagram_socket("0.0.0.0", 0, nil) do |socket|
82
82
  socket.send_datagram(event_data.to_s, handler[:socket][:host], handler[:socket][:port])
83
83
  socket.close_connection_after_writing
84
- @handling_event_count -= 1 if @handling_event_count
84
+ @in_progress[:events] -= 1 if @in_progress
85
85
  end
86
86
  rescue => error
87
87
  handler_error(handler, event_data).call(error)
@@ -90,7 +90,7 @@ module Sensu
90
90
 
91
91
  # Publish event data to a Sensu transport pipe. Event data that
92
92
  # is `nil` or empty will not be published, to prevent transport
93
- # errors. The `@handling_event_count` is decremented by `1`,
93
+ # errors. The `@in_progress[:events]` is decremented by `1`,
94
94
  # even if the event data is not published.
95
95
  #
96
96
  # @param handler [Hash] definition.
@@ -105,14 +105,14 @@ module Sensu
105
105
  end
106
106
  end
107
107
  end
108
- @handling_event_count -= 1 if @handling_event_count
108
+ @in_progress[:events] -= 1 if @in_progress
109
109
  end
110
110
 
111
111
  # Run a handler extension, within the Sensu EventMachine reactor
112
112
  # (event loop). The extension API `safe_run()` method is used to
113
113
  # guard against most errors. The `safe_run()` callback is always
114
114
  # called, logging the extension run output and status, and
115
- # decrementing the `@handling_event_count` by `1`.
115
+ # decrementing the `@in_progress[:events]` by `1`.
116
116
  #
117
117
  # @param handler [Hash] definition.
118
118
  # @param event_data [Object] to pass to the handler extension.
@@ -123,7 +123,7 @@ module Sensu
123
123
  :output => output,
124
124
  :status => status
125
125
  })
126
- @handling_event_count -= 1 if @handling_event_count
126
+ @in_progress[:events] -= 1 if @in_progress
127
127
  end
128
128
  end
129
129
 
@@ -6,7 +6,7 @@ module Sensu
6
6
  # created callback can be used for standard mutators and mutator
7
7
  # extensions. The provided callback will only be called when the
8
8
  # mutator status is `0` (OK). If the status is not `0`, an error
9
- # is logged, and the `@handling_event_count` is decremented by
9
+ # is logged, and the `@in_progress[:events]` is decremented by
10
10
  # `1`.
11
11
  #
12
12
  # @param mutator [Object] definition or extension.
@@ -25,7 +25,7 @@ module Sensu
25
25
  :output => output,
26
26
  :status => status
27
27
  })
28
- @handling_event_count -= 1 if @handling_event_count
28
+ @in_progress[:events] -= 1 if @in_progress
29
29
  end
30
30
  end
31
31
  end
@@ -63,7 +63,7 @@ module Sensu
63
63
  # mutator is used, unless the handler specifies another mutator.
64
64
  # If a mutator does not exist, not defined or a missing
65
65
  # extension, an error will be logged and the
66
- # `@handling_event_count` is decremented by `1`. This method
66
+ # `@in_progress[:events]` is decremented by `1`. This method
67
67
  # first checks for the existence of a standard mutator, then
68
68
  # checks for an extension if a standard mutator is not defined.
69
69
  #
@@ -84,7 +84,7 @@ module Sensu
84
84
  @logger.error("unknown mutator", {
85
85
  :mutator_name => mutator_name
86
86
  })
87
- @handling_event_count -= 1 if @handling_event_count
87
+ @in_progress[:events] -= 1 if @in_progress
88
88
  end
89
89
  end
90
90
  end