Class: Sensu::Server::Process

Inherits:
Object
  • Object
show all
Includes:
Daemon, Filter, Handle, Mutate
Defined in:
lib/sensu/server/process.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Handle

#handle_event, #handler_error, #handler_extension, #handler_type_router, #pipe_handler, #tcp_handler, #transport_handler, #udp_handler

Methods included from Mutate

#mutate_event, #mutator_callback, #mutator_extension, #pipe_mutator

Methods included from Filter

#action_subdued?, #check_request_subdued?, #eval_attribute_value, #event_filter, #event_filtered?, #filter_attributes_match?, #filter_event, #handle_action?, #handle_severity?, #handler_subdued?, #handling_disabled?, #subdue_days?, #subdue_exception?, #subdue_time?

Methods included from Daemon

#load_extensions, #load_settings, #log_concerns, #setup_logger, #setup_process, #setup_redis, #setup_signal_traps, #setup_transport

Methods included from Utilities

#deep_merge, #random_uuid, #redact_sensitive, #retry_until_true, #testing?

Constructor Details

#initialize(options = {}) ⇒ Process

Override Daemon initialize() to support Sensu server master election and the handling event count.

Parameters:

  • options (Hash) (defaults to: {})


33
34
35
36
37
38
# File 'lib/sensu/server/process.rb', line 33

def initialize(options={})
  super
  @is_master = false
  @timers[:master] = Array.new
  @handling_event_count = 0
end

Instance Attribute Details

#handling_event_countObject (readonly)

Returns the value of attribute handling_event_count.



14
15
16
# File 'lib/sensu/server/process.rb', line 14

def handling_event_count
  @handling_event_count
end

#is_masterObject (readonly)

Returns the value of attribute is_master.



14
15
16
# File 'lib/sensu/server/process.rb', line 14

def is_master
  @is_master
end

Class Method Details

.run(options = {}) ⇒ Object

Create an instance of the Sensu server process, start the server within the EventMachine event loop, and set up server process signal traps (for stopping).

Parameters:

  • options (Hash) (defaults to: {})


21
22
23
24
25
26
27
# File 'lib/sensu/server/process.rb', line 21

def self.run(options={})
  server = self.new(options)
  EM::run do
    server.start
    server.setup_signal_traps
  end
end

Instance Method Details

#aggregate_check_result(result) ⇒ Object

Add a check result to an aggregate. A check aggregate uses the check ‘:name` and the `:issued` timestamp as its unique identifier. An aggregate uses several counters: the total number of results in the aggregate, and a counter for each check severity (ok, warning, etc). Check output is also stored, to be summarized to aid in identifying outliers for a check execution across a number of Sensu clients. JSON serialization is used for storing check result data.

Parameters:

  • result (Hash)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/sensu/server/process.rb', line 182

def aggregate_check_result(result)
  @logger.debug("adding check result to aggregate", :result => result)
  check = result[:check]
  result_set = "#{check[:name]}:#{check[:issued]}"
  result_data = MultiJson.dump(:output => check[:output], :status => check[:status])
  @redis.hset("aggregation:#{result_set}", result[:client], result_data) do
    SEVERITIES.each do |severity|
      @redis.hsetnx("aggregate:#{result_set}", severity, 0)
    end
    severity = (SEVERITIES[check[:status]] || "unknown")
    @redis.hincrby("aggregate:#{result_set}", severity, 1) do
      @redis.hincrby("aggregate:#{result_set}", "total", 1) do
        @redis.sadd("aggregates:#{check[:name]}", check[:issued]) do
          @redis.sadd("aggregates", check[:name])
        end
      end
    end
  end
end

#bootstrapObject

Bootstrap the Sensu server process, setting up the keepalive and check result consumers, and attemping to become the master to carry out its duties. This method sets the process/daemon ‘@state` to `:running`.



742
743
744
745
746
747
# File 'lib/sensu/server/process.rb', line 742

def bootstrap
  setup_keepalives
  setup_results
  setup_master_monitor
  @state = :running
end

#calculate_check_execution_splay(check) ⇒ Object

Calculate a check execution splay, taking into account the current time and the execution interval to ensure it’s consistent between process restarts.

Parameters:

  • check (Hash)

    definition.



436
437
438
439
440
# File 'lib/sensu/server/process.rb', line 436

def calculate_check_execution_splay(check)
  splay_hash = Digest::MD5.digest(check[:name]).unpack('Q<').first
  current_time = (Time.now.to_f * 1000).to_i
  (splay_hash - current_time) % (check[:interval] * 1000) / 1000.0
end

#check_flapping?(stored_event, check) ⇒ TrueClass, FalseClass

Determine if a check state is flapping, rapidly changing between an OK and non-OK state. Flap detection is only done for checks that have defined low and hight flap detection thresholds, ‘:low_flap_threshold` and `:high_flap_threshold`. The `check_history()` method provides the check history and more importantly the total state change precentage value that is compared with the configured thresholds defined in the check data. If a check hasn’t been flapping, the ‘:total_state_change` must be equal to or higher than the `:high_flap_threshold` to be changed to flapping. If a check has been flapping, the `:total_state_change` must be equal to or lower than the `:low_flap_threshold` to no longer be flapping. This method uses the same algorithm as Nagios: nagios.sourceforge.net/docs/3_0/flapping.html

Parameters:

  • stored_event (Hash)
  • check (Hash)

Returns:

  • (TrueClass, FalseClass)


273
274
275
276
277
278
279
280
281
282
# File 'lib/sensu/server/process.rb', line 273

def check_flapping?(stored_event, check)
  if check.has_key?(:low_flap_threshold) && check.has_key?(:high_flap_threshold)
    was_flapping = stored_event && stored_event[:action] == "flapping"
    check[:total_state_change] >= check[:high_flap_threshold] ||
      (was_flapping && check[:total_state_change] <= check[:low_flap_threshold]) ||
      was_flapping
  else
    false
  end
end

#check_history(client, check, &callback) ⇒ Object

Fetch the execution history for a client/check pair, the 21 most recent check result statuses. This method also calculates the total state change percentage for the history, this value is use for check state flat detection, using a similar algorithm to Nagios: nagios.sourceforge.net/docs/3_0/flapping.html

Parameters:

  • client (Hash)
  • check (Hash)
  • callback (Proc)

    to be called with the check history and total state change value.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/sensu/server/process.rb', line 234

def check_history(client, check, &callback)
  history_key = "history:#{client[:name]}:#{check[:name]}"
  @redis.lrange(history_key, -21, -1) do |history|
    total_state_change = 0
    unless history.size < 21
      state_changes = 0
      change_weight = 0.8
      previous_status = history.first
      history.each do |status|
        unless status == previous_status
          state_changes += change_weight
        end
        change_weight += 0.02
        previous_status = status
      end
      total_state_change = (state_changes.fdiv(20) * 100).to_i
    end
    callback.call(history, total_state_change)
  end
end

#complete_event_handling(&callback) ⇒ Object

Complete event handling currently in progress. The ‘:handling_event_count` is used to determine if event handling is complete, when it is equal to `0`. The provided callback is called when handling is complete.

Parameters:

  • callback (Proc)

    to call when event handling is complete.



726
727
728
729
730
731
732
733
734
735
736
# File 'lib/sensu/server/process.rb', line 726

def complete_event_handling(&callback)
  @logger.info("completing event handling in progress", {
    :handling_event_count => @handling_event_count
  })
  retry_until_true do
    if @handling_event_count == 0
      callback.call
      true
    end
  end
end

#create_keepalive_check(client) ⇒ Object

Create a keepalive check definition for a client. Client definitions may contain ‘:keepalive` configuration, containing specific thresholds and handler information. The keepalive check definition creation begins with default thresholds, and sets the `:handler` to `keepalive`, if the handler has a local definition. If the client provides its own `:keepalive` configuration, it’s deep merged with the defaults. The check ‘:name`, `:issued`, and `:executed` values are always overridden to guard against an invalid definition.



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/sensu/server/process.rb', line 517

def create_keepalive_check(client)
  check = {
    :thresholds => {
      :warning => 120,
      :critical => 180
    }
  }
  if @settings.handler_exists?(:keepalive)
    check[:handler] = "keepalive"
  end
  if client.has_key?(:keepalive)
    check = deep_merge(check, client[:keepalive])
  end
  timestamp = Time.now.to_i
  check.merge(:name => "keepalive", :issued => timestamp, :executed => timestamp)
end

#derive_handlers(handler_list, depth = 0) ⇒ Array

Derive an array of handler definitions from a list of handler names. This method first checks for the existence of standard handlers, followed by handler extensions. If a handler does not exist for a name, it is logged and ignored. Duplicate handler definitions are removed.

Parameters:

  • handler_list (Array)
  • depth (Integer) (defaults to: 0)

    of handler set expansion.

Returns:

  • (Array)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sensu/server/process.rb', line 114

def derive_handlers(handler_list, depth=0)
  handler_list.compact.map { |handler_name|
    case
    when @settings.handler_exists?(handler_name)
      handler = @settings[:handlers][handler_name].merge(:name => handler_name)
      expand_handler_sets(handler, depth)
    when @extensions.handler_exists?(handler_name)
      @extensions[:handlers][handler_name]
    else
      @logger.error("unknown handler", :handler_name => handler_name)
      nil
    end
  }.flatten.compact.uniq
end

#determine_stale_clientsObject

Determine stale clients, those that have not sent a keepalive in a specified amount of time (thresholds). This method iterates through the client registry, creating a keepalive check definition with the ‘create_keepalive_check()` method, containing client specific staleness thresholds. If the time since the latest keepalive is equal to or greater than a threshold, the check `:output` is set to a descriptive message, and `:status` is set to the appropriate non-zero value. If a client has been sending keepalives, `:output` and `:status` are set to indicate an OK state. A check result is published for every client in the registry.



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/sensu/server/process.rb', line 545

def determine_stale_clients
  @logger.info("determining stale clients")
  @redis.smembers("clients") do |clients|
    clients.each do |client_name|
      @redis.get("client:#{client_name}") do |client_json|
        unless client_json.nil?
          client = MultiJson.load(client_json)
          check = create_keepalive_check(client)
          time_since_last_keepalive = Time.now.to_i - client[:timestamp]
          check[:output] = "No keepalive sent from client for "
          check[:output] << "#{time_since_last_keepalive} seconds"
          case
          when time_since_last_keepalive >= check[:thresholds][:critical]
            check[:output] << " (>=#{check[:thresholds][:critical]})"
            check[:status] = 2
          when time_since_last_keepalive >= check[:thresholds][:warning]
            check[:output] << " (>=#{check[:thresholds][:warning]})"
            check[:status] = 1
          else
            check[:output] = "Keepalive sent from client "
            check[:output] << "#{time_since_last_keepalive} seconds ago"
            check[:status] = 0
          end
          publish_check_result(client, check)
        end
      end
    end
  end
end

#event_bridges(event) ⇒ Object

Run event bridge extensions, within the Sensu EventMachine reactor (event loop). The extension API ‘safe_run()` method is used to guard against most errors. Bridges are for relaying Sensu event data to other services.

Parameters:

  • event (Hash)


135
136
137
138
139
140
141
142
143
144
# File 'lib/sensu/server/process.rb', line 135

def event_bridges(event)
  @extensions[:bridges].each do |name, bridge|
    bridge.safe_run(event) do |output, status|
      @logger.debug("bridge extension output", {
        :extension => bridge.definition,
        :output => output
      })
    end
  end
end

#expand_handler_sets(handler, depth = 0) ⇒ Array, ...

Expand event handler sets, creating an array of handler definitions. Handler sets cannot be deeply nested (by choice), this method will return ‘nil` if an attempt is made to deeply nest. If the provided handler definition is not a set, it is returned.

Parameters:

  • handler (Hash)

    definition.

  • depth (Integer) (defaults to: 0)

    of the expansion.

Returns:

  • (Array, Hash, Nil)


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sensu/server/process.rb', line 92

def expand_handler_sets(handler, depth=0)
  if handler[:type] == "set"
    if depth < 2
      derive_handlers(handler[:handlers], depth + 1)
    else
      @logger.error("handler sets cannot be deeply nested", :handler => handler)
      nil
    end
  else
    handler
  end
end

#master_dutiesObject

Set up the master duties, tasks only performed by a single Sensu server at a time. The duties include publishing check requests, monitoring for stale clients, and pruning check result aggregations.



631
632
633
634
635
# File 'lib/sensu/server/process.rb', line 631

def master_duties
  setup_check_request_publisher
  setup_client_monitor
  setup_check_result_aggregation_pruner
end

#pauseObject

Pause the Sensu server process, unless it is being paused or has already been paused. The process/daemon ‘@state` is first set to `:pausing`, to indicate that it’s in progress. All run timers are cancelled, and the references are cleared. The Sensu server will unsubscribe from all transport subscriptions, resign as master (if currently the master), then set the process/daemon ‘@state` to `:paused`.



764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/sensu/server/process.rb', line 764

def pause
  unless @state == :pausing || @state == :paused
    @state = :pausing
    @timers[:run].each do |timer|
      timer.cancel
    end
    @timers[:run].clear
    unsubscribe
    resign_as_master
    @state = :paused
  end
end

#process_check_result(result) ⇒ Object

Process a check result, storing its data, inspecting its contents, and taking the appropriate actions (eg. update the event registry). A check result must have a valid client name, associated with a client in the registry. Results without a valid client are discarded, to keep the system “correct”. If a local check definition exists for the check name, and the check result is not from a standalone check execution, it’s merged with the check result for more context.

Parameters:

  • result (Hash)

    data.



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/sensu/server/process.rb', line 345

def process_check_result(result)
  @logger.debug("processing result", :result => result)
  @redis.get("client:#{result[:client]}") do |client_json|
    unless client_json.nil?
      client = MultiJson.load(client_json)
      check = case
      when @settings.check_exists?(result[:check][:name]) && !result[:check][:standalone]
        @settings[:checks][result[:check][:name]].merge(result[:check])
      else
        result[:check]
      end
      aggregate_check_result(result) if check[:aggregate]
      store_check_result(client, check) do
        check_history(client, check) do |history, total_state_change|
          check[:history] = history
          check[:total_state_change] = total_state_change
          update_event_registry(client, check) do |event|
            process_event(event)
          end
        end
      end
    else
      @logger.warn("client not in registry", :client => result[:client])
    end
  end
end

#process_event(event) ⇒ Object

Process an event: filter -> mutate -> handle.

This method runs event bridges, relaying the event data to other services. This method also determines the appropriate handlers for the event, filtering and mutating the event data for each of them. The ‘@handling_event_count` is incremented by `1`, for each event handler chain (filter -> mutate -> handle).

Parameters:

  • event (Hash)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sensu/server/process.rb', line 156

def process_event(event)
  log_level = event[:check][:type] == "metric" ? :debug : :info
  @logger.send(log_level, "processing event", :event => event)
  event_bridges(event)
  handler_list = Array((event[:check][:handlers] || event[:check][:handler]) || "default")
  handlers = derive_handlers(handler_list)
  handlers.each do |handler|
    @handling_event_count += 1
    filter_event(handler, event) do |event|
      mutate_event(handler, event) do |event_data|
        handle_event(handler, event_data)
      end
    end
  end
end

#prune_check_result_aggregationsObject

Prune check result aggregations (aggregates). Sensu only stores the 20 latest aggregations for a check, to keep the amount of data stored to a minimum.



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/sensu/server/process.rb', line 588

def prune_check_result_aggregations
  @logger.info("pruning check result aggregations")
  @redis.smembers("aggregates") do |checks|
    checks.each do |check_name|
      @redis.smembers("aggregates:#{check_name}") do |aggregates|
        if aggregates.size > 20
          aggregates.sort!
          aggregates.take(aggregates.size - 20).each do |check_issued|
            @redis.srem("aggregates:#{check_name}", check_issued) do
              result_set = "#{check_name}:#{check_issued}"
              @redis.del("aggregate:#{result_set}") do
                @redis.del("aggregation:#{result_set}") do
                  @logger.debug("pruned aggregation", {
                    :check => {
                      :name => check_name,
                      :issued => check_issued
                    }
                  })
                end
              end
            end
          end
        end
      end
    end
  end
end

#publish_check_request(check) ⇒ Object

Publish a check request to the transport. A check request is composted of a check ‘:name`, an `:issued` timestamp, and a check `:command` if available. The check request is published to a transport pipe, for each of the check `:subscribers` in its definition, eg. “webserver”. JSON serialization is used when publishing the check request payload to the transport pipes. Transport errors are logged.

Parameters:

  • check (Hash)

    definition.



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/sensu/server/process.rb', line 408

def publish_check_request(check)
  payload = {
    :name => check[:name],
    :issued => Time.now.to_i
  }
  payload[:command] = check[:command] if check.has_key?(:command)
  @logger.info("publishing check request", {
    :payload => payload,
    :subscribers => check[:subscribers]
  })
  check[:subscribers].each do |subscription|
    @transport.publish(:fanout, subscription, MultiJson.dump(payload)) do |info|
      if info[:error]
        @logger.error("failed to publish check request", {
          :subscription => subscription,
          :payload => payload,
          :error => info[:error].to_s
        })
      end
    end
  end
end

#publish_check_result(client, check) ⇒ Object

Publish a check result to the transport for processing. A check result is composed of a client name and a check definition, containing check ‘:output` and `:status`. JSON serialization is used when publishing the check result payload to the transport pipe. Transport errors are logged.

Parameters:

  • client (Hash)
  • check (Hash)


492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/sensu/server/process.rb', line 492

def publish_check_result(client, check)
  payload = {
    :client => client[:name],
    :check => check
  }
  @logger.debug("publishing check result", :payload => payload)
  @transport.publish(:direct, "results", MultiJson.dump(payload)) do |info|
    if info[:error]
      @logger.error("failed to publish check result", {
        :payload => payload,
        :error => info[:error].to_s
      })
    end
  end
end

#request_master_electionObject

Request a master election, a process to determine if the current process is the master Sensu server, with its own/unique duties. A Redis key/value is used as a central lock, using the “SETNX” Redis command to set the key/value if it does not exist, using a timestamp for the value. If the current process was able to create the key/value, it is the master, and must do the duties of the master. If the current process was not able to create the key/value, but the current timestamp value is equal to or over 30 seconds ago, the “GETSET” Redis command is used to set a new timestamp and fetch the previous value to compare them, to determine if it was set by the current process. If the current process is able to set the timestamp value, it becomes the master. The master has ‘@is_master` set to `true`.



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/sensu/server/process.rb', line 651

def request_master_election
  @redis.setnx("lock:master", Time.now.to_i) do |created|
    if created
      @is_master = true
      @logger.info("i am the master")
      master_duties
    else
      @redis.get("lock:master") do |timestamp|
        if Time.now.to_i - timestamp.to_i >= 30
          @redis.getset("lock:master", Time.now.to_i) do |previous|
            if previous == timestamp
              @is_master = true
              @logger.info("i am now the master")
              master_duties
            end
          end
        end
      end
    end
  end
end

#resign_as_masterObject

Resign as master, if the current process is the Sensu server master. This method cancels and clears the master timers, those with references stored in the timers hash under ‘:master`, and `@is_master`is set to `false`.



698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/sensu/server/process.rb', line 698

def resign_as_master
  if @is_master
    @logger.warn("resigning as master")
    @timers[:master].each do |timer|
      timer.cancel
    end
    @timers[:master].clear
    @is_master = false
  else
    @logger.debug("not currently master")
  end
end

#resumeObject

Resume the Sensu server process if it is currently or will soon be paused. The ‘retry_until_true` helper method is used to determine if the process is paused and if the Redis and transport connections are connected. If the conditions are met, `bootstrap()` will be called and true is returned to stop `retry_until_true`.



783
784
785
786
787
788
789
790
791
792
# File 'lib/sensu/server/process.rb', line 783

def resume
  retry_until_true(1) do
    if @state == :paused
      if @redis.connected? && @transport.connected?
        bootstrap
        true
      end
    end
  end
end

#schedule_check_executions(checks) ⇒ Object

Schedule check executions, using EventMachine periodic timers, using a calculated execution splay. The timers are stored in the timers hash under ‘:master`, as check request publishing is a task for only the Sensu server master, so they can be cancelled etc. Check requests are not published if subdued.

Parameters:

  • checks (Array)

    of definitions.



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/sensu/server/process.rb', line 449

def schedule_check_executions(checks)
  checks.each do |check|
    create_check_request = Proc.new do
      unless check_request_subdued?(check)
        publish_check_request(check)
      else
        @logger.info("check request was subdued", :check => check)
      end
    end
    execution_splay = testing? ? 0 : calculate_check_execution_splay(check)
    interval = testing? ? 0.5 : check[:interval]
    @timers[:master] << EM::Timer.new(execution_splay) do
      create_check_request.call
      @timers[:master] << EM::PeriodicTimer.new(interval, &create_check_request)
    end
  end
end

#setup_check_request_publisherObject

Set up the check request publisher. This method creates an array of check definitions, that are not standalone checks, and do not have ‘:publish` set to `false`. The array of check definitions includes those from standard checks and extensions (with a defined execution `:interval`). The array is provided to the `schedule_check_executions()` method.



473
474
475
476
477
478
479
480
481
482
# File 'lib/sensu/server/process.rb', line 473

def setup_check_request_publisher
  @logger.debug("scheduling check requests")
  standard_checks = @settings.checks.reject do |check|
    check[:standalone] || check[:publish] == false
  end
  extension_checks = @extensions.checks.reject do |check|
    check[:standalone] || check[:publish] == false || !check[:interval].is_a?(Integer)
  end
  schedule_check_executions(standard_checks + extension_checks)
end

#setup_check_result_aggregation_prunerObject

Set up the check result aggregation pruner, using periodic timer to run ‘prune_check_result_aggregations()` every 20 seconds. The timer is stored in the timers hash under `:master`.



620
621
622
623
624
625
# File 'lib/sensu/server/process.rb', line 620

def setup_check_result_aggregation_pruner
  @logger.debug("pruning check result aggregations")
  @timers[:master] << EM::PeriodicTimer.new(20) do
    prune_check_result_aggregations
  end
end

#setup_client_monitorObject

Set up the client monitor, a periodic timer to run ‘determine_stale_clients()` every 30 seconds. The timer is stored in the timers hash under `:master`.



578
579
580
581
582
583
# File 'lib/sensu/server/process.rb', line 578

def setup_client_monitor
  @logger.debug("monitoring client keepalives")
  @timers[:master] << EM::PeriodicTimer.new(30) do
    determine_stale_clients
  end
end

#setup_keepalivesObject

Set up the client keepalive consumer, keeping the Sensu client registry updated. The consumer receives JSON serialized client keepalives from the transport, parses them, and calls ‘update_client_registry()` with the client data to update the registry. Transport message acknowledgements are used to ensure the client registry is updated successfully. Keepalive JSON parsing errors are logged.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sensu/server/process.rb', line 64

def setup_keepalives
  @logger.debug("subscribing to keepalives")
  @transport.subscribe(:direct, "keepalives", "keepalives", :ack => true) do |message_info, message|
    @logger.debug("received keepalive", :message => message)
    begin
      client = MultiJson.load(message)
      update_client_registry(client) do
        @transport.ack(message_info)
      end
    rescue MultiJson::ParseError => error
      @logger.error("failed to parse keepalive payload", {
        :message => message,
        :error => error.to_s
      })
      @transport.ack(message_info)
    end
  end
end

#setup_master_monitorObject

Set up the master monitor. A one-time timer is used to run ‘request_master_exection()` in 2 seconds. A periodic timer is used to update the master lock timestamp if the current process is the master, or to run `request_master_election(), every 10 seconds. The timers are stored in the timers hash under `:run`.



679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/sensu/server/process.rb', line 679

def setup_master_monitor
  @timers[:run] << EM::Timer.new(2) do
    request_master_election
  end
  @timers[:run] << EM::PeriodicTimer.new(10) do
    if @is_master
      @redis.set("lock:master", Time.now.to_i) do
        @logger.debug("updated master lock timestamp")
      end
    else
      request_master_election
    end
  end
end

#setup_resultsObject

Set up the check result consumer. The consumer receives JSON serialized check results from the transport, parses them, and calls ‘process_check_result()` with the result data to be processed. Transport message acknowledgements are used to ensure that results make it to processing. The transport message acknowledgements are currently done in the next tick of the EventMachine reactor (event loop), as a flow control mechanism. Result JSON parsing errors are logged.



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/sensu/server/process.rb', line 380

def setup_results
  @logger.debug("subscribing to results")
  @transport.subscribe(:direct, "results", "results", :ack => true) do |message_info, message|
    begin
      result = MultiJson.load(message)
      @logger.debug("received result", :result => result)
      process_check_result(result)
    rescue MultiJson::ParseError => error
      @logger.error("failed to parse result payload", {
        :message => message,
        :error => error.to_s
      })
    end
    EM::next_tick do
      @transport.ack(message_info)
    end
  end
end

#startObject

Start the Sensu server process, connecting to Redis, the transport, and calling the ‘bootstrap()` method.



751
752
753
754
755
# File 'lib/sensu/server/process.rb', line 751

def start
  setup_redis
  setup_transport
  bootstrap
end

#stopObject

Stop the Sensu server process, pausing it, completing event handling in progress, closing the Redis and transport connections, and exiting the process (exit 0). After pausing the process, the process/daemon ‘@state` is set to `:stopping`.



799
800
801
802
803
804
805
806
807
808
# File 'lib/sensu/server/process.rb', line 799

def stop
  @logger.warn("stopping")
  pause
  @state = :stopping
  complete_event_handling do
    @redis.close
    @transport.close
    super
  end
end

#store_check_result(client, check, &callback) ⇒ Object

Store check result data. This method stores the 21 most recent check result statuses for a client/check pair, this history is used for event context and flap detection. The check execution timestamp is also stored, to provide an indication of how recent the data is.

Parameters:

  • client (Hash)
  • check (Hash)
  • callback (Proc)

    to call when the check result data has been stored (history, etc).



212
213
214
215
216
217
218
219
220
221
# File 'lib/sensu/server/process.rb', line 212

def store_check_result(client, check, &callback)
  @redis.sadd("history:#{client[:name]}", check[:name])
  result_key = "#{client[:name]}:#{check[:name]}"
  history_key = "history:#{result_key}"
  @redis.rpush(history_key, check[:status]) do
    @redis.set("execution:#{result_key}", check[:executed])
    @redis.ltrim(history_key, -21, -1)
    callback.call
  end
end

#unsubscribeObject

Unsubscribe from transport subscriptions (all of them). This method is called when there are issues with connectivity, or the process is stopping.



714
715
716
717
# File 'lib/sensu/server/process.rb', line 714

def unsubscribe
  @logger.warn("unsubscribing from keepalive and result queues")
  @transport.unsubscribe
end

#update_client_registry(client, &callback) ⇒ Object

Update the Sensu client registry, stored in Redis. Sensu client data is used to provide additional event context and enable agent health monitoring. JSON serialization is used for the client data.

Parameters:

  • client (Hash)
  • callback (Proc)

    to call after the the client data has been added to (or updated) the registry.



48
49
50
51
52
53
54
55
# File 'lib/sensu/server/process.rb', line 48

def update_client_registry(client, &callback)
  @logger.debug("updating client registry", :client => client)
  @redis.set("client:#{client[:name]}", MultiJson.dump(client)) do
    @redis.sadd("clients", client[:name]) do
      callback.call
    end
  end
end

#update_event_registry(client, check, &callback) ⇒ Object

Update the event registry, stored in Redis. This method determines if check data results in the creation or update of event data in the registry. Existing event data for a client/check pair is fetched, used in conditionals and the composition of the new event data. If a check ‘:status` is not `0`, or it has been flapping, an event is created/updated in the registry. If there was existing event data, but the check `:status` is now `0`, the event is removed (resolved) from the registry. If the previous conditions are not met, and check `:type` is `metric` and the `:status` is `0`, the event registry is not updated, but the provided callback is called with the event data. JSON serialization is used when storing data in the registry.

Parameters:

  • client (Hash)
  • check (Hash)
  • callback (Proc)

    to be called with the resulting event data if the event registry is updated, or the check is of type ‘:metric`.



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/sensu/server/process.rb', line 303

def update_event_registry(client, check, &callback)
  @redis.hget("events:#{client[:name]}", check[:name]) do |event_json|
    stored_event = event_json ? MultiJson.load(event_json) : nil
    flapping = check_flapping?(stored_event, check)
    event = {
      :id => random_uuid,
      :client => client,
      :check => check,
      :occurrences => 1
    }
    if check[:status] != 0 || flapping
      if stored_event && check[:status] == stored_event[:check][:status]
        event[:occurrences] = stored_event[:occurrences] + 1
      end
      event[:action] = flapping ? :flapping : :create
      @redis.hset("events:#{client[:name]}", check[:name], MultiJson.dump(event)) do
        callback.call(event)
      end
    elsif stored_event
      event[:occurrences] = stored_event[:occurrences]
      event[:action] = :resolve
      unless check[:auto_resolve] == false && !check[:force_resolve]
        @redis.hdel("events:#{client[:name]}", check[:name]) do
          callback.call(event)
        end
      end
    elsif check[:type] == "metric"
      callback.call(event)
    end
  end
end