lib/flapjack/data/notification_rule.rb in flapjack-0.8.8 vs lib/flapjack/data/notification_rule.rb in flapjack-0.8.9
- old
+ new
@@ -10,12 +10,12 @@
module Data
class NotificationRule
extend Flapjack::Utility
- attr_accessor :id, :contact_id, :entities, :tags, :time_restrictions,
- :unknown_media, :warning_media, :critical_media,
+ attr_accessor :id, :contact_id, :entities, :tags, :regex_tags,
+ :time_restrictions, :unknown_media, :warning_media, :critical_media,
:unknown_blackhole, :warning_blackhole, :critical_blackhole
def self.exists_with_id?(rule_id, options = {})
raise "Redis connection not set" unless redis = options[:redis]
raise "No id value passed" unless not (rule_id.nil? || rule_id == '')
@@ -72,11 +72,11 @@
IceCube::Schedule.from_hash(tr)
end
def to_json(*args)
- self.class.hashify(:id, :contact_id, :tags, :entities,
+ self.class.hashify(:id, :contact_id, :tags, :regex_tags, :entities,
:time_restrictions, :unknown_media, :warning_media, :critical_media,
:unknown_blackhole, :warning_blackhole, :critical_blackhole) {|k|
[k, self.send(k)]
}.to_json
end
@@ -91,10 +91,22 @@
def match_tags?(event_tags)
return false unless @tags && @tags.length > 0
@tags.subset?(event_tags)
end
+ # regex_tags match?
+ def match_regex_tags?(event_tags)
+ return false unless @regex_tags && @regex_tags.length > 0
+ matches = 0
+ event_tags.each do |event_tag|
+ @regex_tags.each do |regex_tag|
+ matches += 1 if /#{regex_tag}/ === event_tag
+ end
+ end
+ matches >= @regex_tags.length
+ end
+
def blackhole?(severity)
('unknown'.eql?(severity.downcase) && @unknown_blackhole) ||
('warning'.eql?(severity.downcase) && @warning_blackhole) ||
('critical'.eql?(severity.downcase) && @critical_blackhole)
end
@@ -110,11 +122,12 @@
end
end
def is_specific?
(!@entities.nil? && !@entities.empty?) ||
- (!@tags.nil? && !@tags.empty?)
+ (!@tags.nil? && !@tags.empty?) ||
+ (!@regex_tags.nil? && !@regex_tags.empty?)
end
private
def initialize(rule_data, opts = {})
@@ -135,10 +148,13 @@
rule_data[:warning_blackhole] = rule_data[:warning_blackhole] || false
rule_data[:critical_blackhole] = rule_data[:critical_blackhole] || false
if rule_data[:tags].is_a?(Array)
rule_data[:tags] = Flapjack::Data::TagSet.new(rule_data[:tags])
end
+ if rule_data[:regex_tags].is_a?(Array)
+ rule_data[:regex_tags] = Flapjack::Data::TagSet.new(rule_data[:regex_tags])
+ end
rule_data
end
def self.add_or_update(rule_data, options = {})
redis = options[:redis]
@@ -148,16 +164,18 @@
rule_data = preen(rule_data)
errors = self.validate_data(rule_data, options)
return errors unless errors.nil? || errors.empty?
# whitelisting fields, rather than passing through submitted data directly
- tag_data = rule_data[:tags].is_a?(Set) ? rule_data[:tags].to_a : nil
+ tag_data = rule_data[:tags].is_a?(Set) ? rule_data[:tags].to_a : nil
+ regex_tag_data = rule_data[:regex_tags].is_a?(Set) ? rule_data[:regex_tags].to_a : nil
json_rule_data = {
:id => rule_data[:id].to_s,
:contact_id => rule_data[:contact_id].to_s,
:entities => Oj.dump(rule_data[:entities]),
:tags => Oj.dump(tag_data),
+ :regex_tags => Oj.dump(regex_tag_data),
:time_restrictions => Oj.dump(rule_data[:time_restrictions], :mode => :compat),
:unknown_media => Oj.dump(rule_data[:unknown_media]),
:warning_media => Oj.dump(rule_data[:warning_media]),
:critical_media => Oj.dump(rule_data[:critical_media]),
:unknown_blackhole => rule_data[:unknown_blackhole],
@@ -248,10 +266,16 @@
( d[:tags].nil? ||
d[:tags].is_a?(Flapjack::Data::TagSet) &&
d[:tags].all? {|et| et.is_a?(String)} ) } =>
"tags must be a tag_set of strings",
+ proc {|d| !d.has_key?(:regex_tags) ||
+ ( d[:regex_tags].nil? ||
+ d[:regex_tags].is_a?(Flapjack::Data::TagSet) &&
+ d[:regex_tags].all? {|et| et.is_a?(String)} ) } =>
+ "regex_tags must be a tag_set of strings",
+
proc {|d| !d.has_key?(:time_restrictions) ||
( d[:time_restrictions].nil? ||
d[:time_restrictions].all? {|tr|
!!prepare_time_restriction(symbolize(tr))
} )
@@ -316,9 +340,11 @@
rule_data = @redis.hgetall("notification_rule:#{@id}")
@contact_id = rule_data['contact_id']
tags = Oj.load(rule_data['tags'] || '')
@tags = tags ? Flapjack::Data::TagSet.new(tags) : nil
+ regex_tags = Oj.load(rule_data['regex_tags'] || '')
+ @regex_tags = regex_tags ? Flapjack::Data::TagSet.new(regex_tags) : nil
@entities = Oj.load(rule_data['entities'] || '')
@time_restrictions = Oj.load(rule_data['time_restrictions'] || '')
@unknown_media = Oj.load(rule_data['unknown_media'] || '')
@warning_media = Oj.load(rule_data['warning_media'] || '')
@critical_media = Oj.load(rule_data['critical_media'] || '')