Class: Flapjack::Data::Check

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, Extensions::Associations, Extensions::ShortName, Swagger::Blocks, Zermelo::Records::RedisSet
Defined in:
lib/flapjack/data/check.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject

Returns the value of attribute count.



216
217
218
# File 'lib/flapjack/data/check.rb', line 216

def count
  @count
end

Class Method Details

.destroy_states(check_id, *st_ids) ⇒ Object



72
73
74
75
76
# File 'lib/flapjack/data/check.rb', line 72

def self.destroy_states(check_id, *st_ids)
  # states won't be deleted if still referenced elsewhere -- see the State
  # before_destroy callback
  Flapjack::Data::State.intersect(:id => st_ids).destroy_all
end

.jsonapi_associationsObject



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/flapjack/data/check.rb', line 420

def self.jsonapi_associations
  unless instance_variable_defined?('@jsonapi_associations')
    @jsonapi_associations = {
      :alerting_media => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :type => 'medium',
        :klass => Flapjack::Data::Medium,
        :callback_classes => [
          Flapjack::Data::Contact,
          Flapjack::Data::Rule,
          Flapjack::Data::Tag,
          Flapjack::Data::ScheduledMaintenance
        ],
        :descriptions => {
          :get => "While this check is failing, returns media records " \
                  "which are receiving notifications during this failure."
        }
      ),
      :contacts => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :type => 'contact',
        :klass => Flapjack::Data::Contact,
        :callback_classes => [
          Flapjack::Data::Rule,
          Flapjack::Data::Tag
        ],
        :descriptions => {
          :get => "Returns contacts whose notification rules will " \
                  "allow them to receive notifications for events on " \
                  "this check."
        }
      ),
      :current_scheduled_maintenances => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :type => 'scheduled_maintenance',
        :klass => Flapjack::Data::ScheduledMaintenance,
        :descriptions => {
          :get => "Returns scheduled maintenance periods currently in " \
                  " effect for this check."
        }
      ),
      :current_state => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :singular, :link => true, :includable => true,
        :descriptions => {
          :get => "Returns the current State record for this check."
        }
      ),
      :current_unscheduled_maintenance => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :singular, :link => true, :includable => true,
        :type => 'unscheduled_maintenance',
        :klass => Flapjack::Data::UnscheduledMaintenance,
        :descriptions => {
          :get => "If the check is currently acknowledged, returns the " \
                  "unscheduled maintenance period created for that."
        }
      ),
      :latest_notifications => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :get => "Returns the most recent State records for each " \
                  "problem condition that produced notifications."
        }
      ),
      :scheduled_maintenances => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => false,
        :descriptions => {
          :get => "Returns all scheduled maintenance periods for the " \
                  "check; default sort order is newest first."
        }
      ),
      :states => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => false,
        :descriptions => {
          :get => "Returns all state records for the check; default " \
                  "sort order is newest first."
        }
      ),
      :tags => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :post => true, :get => true, :patch => true, :delete => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :post => "Associate tags with this check.",
          :get => "Returns all tags linked to this check.",
          :patch => "Update the tags associated with this check.",
          :delete => "Delete associations between tags and this check."
        }
      ),
      :unscheduled_maintenances => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => false,
        :descriptions => {
          :get => "Returns all unscheduled maintenance periods for the " \
                  "check; default sort order is newest first."
        }
      )
    }
    populate_association_data(@jsonapi_associations)
  end
  @jsonapi_associations
end

.jsonapi_methodsObject



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/flapjack/data/check.rb', line 384

def self.jsonapi_methods
  @jsonapi_methods ||= {
    :post => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :enabled, :initial_failure_delay,
                      :repeat_failure_delay, :initial_recovery_delay],
      :descriptions => {
        :singular => "Create a check.",
        :multiple => "Create checks."
      }
    ),
    :get => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :enabled, :initial_failure_delay,
                      :repeat_failure_delay, :initial_recovery_delay,
                      :failing, :condition, :ack_hash],
      :descriptions => {
        :singular => "Returns data for a check.",
        :multiple => "Returns data for multiple check records."
      }
    ),
    :patch => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :enabled, :initial_failure_delay,
                      :repeat_failure_delay, :initial_recovery_delay],
      :descriptions => {
        :singular => "Update a check.",
        :multiple => "Update checks."
      }
    ),
    :delete => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :descriptions => {
        :singular => "Delete a check.",
        :multiple => "Delete checks."
      }
    )
  }
end

.swagger_included_classesObject



370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/flapjack/data/check.rb', line 370

def self.swagger_included_classes
  # hack -- hardcoding for now
  [
    Flapjack::Data::Check,
    Flapjack::Data::Contact,
    Flapjack::Data::Medium,
    Flapjack::Data::Rule,
    Flapjack::Data::ScheduledMaintenance,
    Flapjack::Data::State,
    Flapjack::Data::Tag,
    Flapjack::Data::UnscheduledMaintenance
  ]
end

Instance Method Details

#alerting_media(opts = {}) ⇒ Object

this can be called from the API (with no args) or from notifier.rb (which will pass a severity to use, and an effective time)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/flapjack/data/check.rb', line 91

def alerting_media(opts = {})
  time = opts[:time] || Time.now
  severity = opts[:severity] || self.condition

  # return empty set if disabled, or in a maintenance period (for API only,
  # these will have been checked already in processor if called by notifier)
  if opts.empty?
    unless self.enabled
      return Flapjack::Data::Medium.empty
    end

    unless self.current_unscheduled_maintenance.nil?
      return Flapjack::Data::Medium.empty
    end

    start_range = Zermelo::Filters::IndexRange.new(nil, time, :by_score => true)
    end_range   = Zermelo::Filters::IndexRange.new(time, nil, :by_score => true)

    unless self.scheduled_maintenances.
      intersect(:start_time => start_range, :end_time => end_range).empty?

      return Flapjack::Data::Medium.empty
    end
  end

  # determine matching acceptors
  tag_ids = self.tags.ids

  acceptor_ids = matching_rule_ids(tag_ids, :blackhole => false, :severity => severity)
  acceptor_media_ids = Flapjack::Data::Rule.matching_media_ids(acceptor_ids,
    :time => time)

  return Flapjack::Data::Medium.empty if acceptor_media_ids.empty?

  # and matching rejectors
  rejector_ids = matching_rule_ids(tag_ids, :blackhole => true, :severity => severity)
  rejector_media_ids = Flapjack::Data::Rule.matching_media_ids(rejector_ids,
    :time => time)

  unless rejector_media_ids.empty?
    acceptor_media_ids -= rejector_media_ids
    return Flapjack::Data::Medium.empty if acceptor_media_ids.empty?
  end

  Flapjack::Data::Medium.intersect(:id => acceptor_media_ids)
end

#clear_unscheduled_maintenance(end_time) ⇒ Object



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/flapjack/data/check.rb', line 582

def clear_unscheduled_maintenance(end_time)
  Flapjack::Data::UnscheduledMaintenance.lock do
    t = Time.now
    start_range = Zermelo::Filters::IndexRange.new(nil, t, :by_score => true)
    end_range   = Zermelo::Filters::IndexRange.new(t, nil, :by_score => true)
    unsched_maints = self.unscheduled_maintenances.intersect(:start_time => start_range,
      :end_time => end_range)
    unsched_maints_count = unsched_maints.empty?
    unless unsched_maints_count == 0
      # FIXME log warning if count > 1
      unsched_maints.each do |usm|
        usm.end_time = end_time
        usm.save
      end
    end
  end
end

#contactsObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/flapjack/data/check.rb', line 138

def contacts
  # return empty set if disabled
  return Flapjack::Data::Contact.empty unless self.enabled

  # determine matching acceptors
  tag_ids = self.tags.ids
  time = Time.now

  acceptor_ids = matching_rule_ids(tag_ids, :blackhole => false,)
  acceptor_contact_ids = Flapjack::Data::Rule.matching_contact_ids(acceptor_ids,
    :time => time)
  return Flapjack::Data::Contact.empty if acceptor_contact_ids.empty?


  # and matching rejectors
  rejector_ids = matching_rule_ids(tag_ids, :blackhole => true)
  rejector_contact_ids = Flapjack::Data::Rule.matching_contact_ids(rejector_ids,
    :time => time)
  unless rejector_contact_ids.empty?
    acceptor_contact_ids -= rejector_contact_ids
    return Flapjack::Data::Contact.empty if acceptor_contact_ids.empty?
  end

  Flapjack::Data::Contact.intersect(:id => acceptor_contact_ids)
end

#current_scheduled_maintenancesObject



533
534
535
# File 'lib/flapjack/data/check.rb', line 533

def current_scheduled_maintenances
  scheduled_maintenances_at(Time.now)
end

#current_unscheduled_maintenanceObject



541
542
543
# File 'lib/flapjack/data/check.rb', line 541

def current_unscheduled_maintenance
  unscheduled_maintenances_at(Time.now).all.first
end

#end_scheduled_maintenance(sched_maint, at_time) ⇒ Object

TODO allow summary to be changed as part of the termination



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/flapjack/data/check.rb', line 546

def end_scheduled_maintenance(sched_maint, at_time)
  at_time = Time.at(at_time) unless at_time.is_a?(Time)

  if sched_maint.start_time >= at_time
    # the scheduled maintenance period is in the future
    self.scheduled_maintenances.remove(sched_maint)
    sched_maint.destroy
    return true
  elsif sched_maint.end_time >= at_time
    # it spans the current time, so we'll stop it at that point
    sched_maint.end_time = at_time
    sched_maint.save
    return true
  end

  false
end

#in_scheduled_maintenance?(t = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


529
530
531
# File 'lib/flapjack/data/check.rb', line 529

def in_scheduled_maintenance?(t = Time.now)
  !scheduled_maintenances_at(t).empty?
end

#in_unscheduled_maintenance?(t = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


537
538
539
# File 'lib/flapjack/data/check.rb', line 537

def in_unscheduled_maintenance?(t = Time.now)
  !unscheduled_maintenances_at(t).empty?
end

#matching_rule_ids(tag_ids, opts = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/flapjack/data/check.rb', line 164

def matching_rule_ids(tag_ids, opts = {})
  severity = opts[:severity]
  blackhole = opts[:blackhole]

  matcher_by_strategy = {
    'global'   => nil,
    'all_tags' => proc {|rule_tag_ids| (rule_tag_ids - tag_ids).empty? },
    'any_tag'  => proc {|rule_tag_ids| !((rule_tag_ids & tag_ids).empty?) },
    'no_tag'   => proc {|rule_tag_ids| (rule_tag_ids & tag_ids).empty? }
  }

  matcher_by_strategy.each_with_object(Set.new) do |(strategy, matcher), memo|
    rules = Flapjack::Data::Rule.intersect(:enabled => true,
      :blackhole => blackhole, :strategy => strategy)
    unless severity.nil?
      rules = rules.intersect(:conditions_list => [nil, /(?:^|,)#{severity}(?:,|$)/])
    end

    if matcher.nil?
      memo.merge(rules.ids)
      next
    end

    rules.associated_ids_for(:tags).each_pair do |rule_id, rule_tag_ids|
      memo << rule_id if matcher.call(rule_tag_ids)
    end
  end
end

#set_unscheduled_maintenance(unsched_maint, options = {}) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/flapjack/data/check.rb', line 564

def set_unscheduled_maintenance(unsched_maint, options = {})
  current_time = Time.now

  self.class.lock(Flapjack::Data::UnscheduledMaintenance,
    Flapjack::Data::State) do

    self.alertable = false
    self.save!

    # time_remaining
    if (unsched_maint.end_time - current_time) > 0
      self.clear_unscheduled_maintenance(unsched_maint.start_time)
    end

    self.unscheduled_maintenances << unsched_maint
  end
end