Class: Naginata::Configuration::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/naginata/configuration/filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, values = nil) ⇒ Filter

Returns a new instance of Filter.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/naginata/configuration/filter.rb', line 7

def initialize type, values = nil
  raise "Invalid filter type #{type}" unless [:nagios_server, :host, :service].include? type
  av = Array(values).dup
  @mode = case
          when av.include?(:all) then :all
          else type
          end
  @rex = case @mode
    when :all
      nil # this creates a filter matching any string
    when :nagios_server, :host
      av.map!{|v| (v.is_a?(String) && v =~ /^(?<name>[-A-Za-z0-9.]+)(,\g<name>)*$/) ? v.split(',') : v }
      av.flatten!
      av.map! do |v|
        case v
        when Regexp then v
        else
          vs = v.to_s
          vs =~ /^[-A-Za-z0-9.]+$/ ? vs : Regexp.new(vs)
        end
      end
      Regexp.union av
    when :service
      av.map!{|v| v.is_a?(String) ? v.split(',') : v }
      av.flatten!
      av.map! do |v|
        case v
        when Regexp then v
        else
          vs = v.to_s
          Regexp.new(vs)
        end
      end
      Regexp.union av
    else
      raise "unprocessable type"
    end
end

Instance Method Details

#filter(nagios_servers) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/naginata/configuration/filter.rb', line 46

def filter nagios_servers
  a = Array(nagios_servers)
  case @mode
  when :all
    a
  when :nagios_server
    a.select { |ns| @rex.match ns.to_s }
  else
    a
  end
end

#filter_service(services) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/naginata/configuration/filter.rb', line 58

def filter_service services
  a = Array(services)
  case @mode
  when :all
    a
  when :nagios_server
    a.select { |s| @rex.match s.nagios }
  when :host
    a.select { |s| @rex.match s.hostname }
  when :service
    # Ignore host monitors
    a.reject { |s| s.description == :ping }
     .select { |s| @rex.match s.description }
  else
    a
  end
end