Class: Threshold::Thresholds

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/threshold/thresholds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thresholds = []) ⇒ Thresholds

Returns a new instance of Thresholds.



17
18
19
# File 'lib/threshold/thresholds.rb', line 17

def initialize(thresholds = [])
  @thresholds = thresholds
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/threshold/thresholds.rb', line 13

def file
  @file
end

#readonlyObject

Returns the value of attribute readonly.



13
14
15
# File 'lib/threshold/thresholds.rb', line 13

def readonly
  @readonly
end

Instance Method Details

#&(an0ther) ⇒ Object

& (union) Returns a new Threshold Object



167
168
169
# File 'lib/threshold/thresholds.rb', line 167

def &(an0ther)
  Thresholds.new(@thresholds & an0ther.to_a)
end

#+(an0ther) ⇒ Object

+ (concat) Returns a new Threshold Object



155
156
157
# File 'lib/threshold/thresholds.rb', line 155

def +(an0ther)
  Thresholds.new(@thresholds + an0ther.to_a)
end

#-(an0ther) ⇒ Object

  • (Difference)

Returns a new Threshold Object



173
174
175
# File 'lib/threshold/thresholds.rb', line 173

def -(an0ther)
  Thresholds.new(@thresholds - an0ther.to_a)
end

#event_filters(&blk) ⇒ Object

Returns a new Threshold Object with just event_filters



187
188
189
190
191
192
193
# File 'lib/threshold/thresholds.rb', line 187

def event_filters(&blk)
  if block_given? 
    self.event_filters.select(&blk)
  else
    Thresholds.new(@thresholds.select{|t| t.class.to_s == "Threshold::EventFilter"})
  end
end

#flushObject

Write changes to the file



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/threshold/thresholds.rb', line 22

def flush
  
  begin 
    valid_existing_file?(@file)
    raise ReadOnlyThresholdsFile if @readonly
    hash = current_hash
    file = File.open(@file, 'w+')
    raise ThresholdAtomicLockFailure, 'The @file state/hash changed before we could flush the file' unless stored_hash == hash
    file.write self.sort.to_s
    file.close

  rescue NonExistantThresholdFile
    raise ReadOnlyThresholdsFile if @readonly
    file = File.open(@file, 'w')
    file.write self.sort.to_s
    file.close
  end

  stored_hash=current_hash
  return true 
end

#loadfileObject

Append in the thresholds.conf file to current collection



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/threshold/thresholds.rb', line 51

def loadfile
  valid_existing_file?(@file)

  results = Threshold::Parser.new(@file)
  @stored_hash= results.filehash
  #puts stored_hash
  results.caps.each do |result|
     builder = Threshold::Builder.new(result)
     self << builder.build
  end

end

#loadfile!Object

Clears current collection and Read in the thresholds.conf file



45
46
47
48
# File 'lib/threshold/thresholds.rb', line 45

def loadfile!
  @thresholds.clear
  loadfile
end

#rate_filters(&blk) ⇒ Object

Returns a new Threshold Object with just rate_filters



196
197
198
199
200
201
202
# File 'lib/threshold/thresholds.rb', line 196

def rate_filters(&blk)
  if block_given? 
    self.rate_filters.select(&blk)
  else
    Thresholds.new(@thresholds.select{|t| t.class.to_s == "Threshold::RateFilter"})
  end
end

#reject(&blk) ⇒ Object

Returns a new Threshold Object



123
124
125
126
127
128
129
# File 'lib/threshold/thresholds.rb', line 123

def reject(&blk)
  if block_given? 
    Thresholds.new(@thresholds.reject(&blk))
  else
    Thresholds.new(@thresholds.reject)
  end   
end

#reverseObject

Returns a new Threshold Object



113
114
115
# File 'lib/threshold/thresholds.rb', line 113

def reverse
  Thresholds.new(@thresholds.reverse)
end

#select(&blk) ⇒ Object

Returns a new Threshold Object



132
133
134
135
136
137
138
# File 'lib/threshold/thresholds.rb', line 132

def select(&blk)
  if block_given? 
    Thresholds.new(@thresholds.select(&blk))
  else
    Thresholds.new(@thresholds.select)
  end   
end

#shuffleObject

Returns a new Threshold Object



118
119
120
# File 'lib/threshold/thresholds.rb', line 118

def shuffle
  Thresholds.new(@thresholds.shuffle)
end

#sortObject

Returns a new Threshold Object



108
109
110
# File 'lib/threshold/thresholds.rb', line 108

def sort
  Thresholds.new(@thresholds.sort)
end

#stored_hashObject

The calculated hash of the threshold.conf file at load time.



94
95
96
# File 'lib/threshold/thresholds.rb', line 94

def stored_hash
  @stored_hash
end

#suppressions(&blk) ⇒ Object

Returns a new Threshold Object with just suppressions



178
179
180
181
182
183
184
# File 'lib/threshold/thresholds.rb', line 178

def suppressions(&blk)
  if block_given? 
    self.suppressions.select(&blk)
  else
   Thresholds.new(@thresholds.select{|t| t.class.to_s == "Threshold::Suppression"})
  end
end

#to_aObject



98
99
100
# File 'lib/threshold/thresholds.rb', line 98

def to_a
  @thresholds
end

#to_s(skip = false) ⇒ Object

Printer Pass (true) to_s to skip the printing of InternalObjects.comment



82
83
84
85
86
87
88
89
90
91
# File 'lib/threshold/thresholds.rb', line 82

def to_s(skip = false)
  output = ""

  raise InvalidThresholdsObject, "Container object has unknown objects" unless valid?

  self.each do |threshold|
    output << threshold.to_s(skip) + "\n"
  end
  return output
end

#uniq(&blk) ⇒ Object

Uniques by default to printable output Returns a new Threshold Object



142
143
144
145
146
147
148
# File 'lib/threshold/thresholds.rb', line 142

def uniq(&blk)
  if block_given? 
    Thresholds.new(@thresholds.uniq(&blk))
  else
    Thresholds.new(@thresholds.uniq{ |lineitem| lineitem.to_s(true) })
  end   
end

#valid?Boolean

Check if all objects in the Threshold Instance report .valid?

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/threshold/thresholds.rb', line 65

def valid?
  begin 
    self.each do |threshold| 
      if threshold.respond_to?(:valid?)
        return false unless threshold.valid?
      else
        raise InvalidThresholdsObject, "Container object has unknown objects"
      end
    end
    return true
  rescue InvalidThresholdsObject
    return false
  end
end

#|(an0ther) ⇒ Object

| (intersect) Returns a new Threshold Object



161
162
163
# File 'lib/threshold/thresholds.rb', line 161

def |(an0ther)
  Thresholds.new(@thresholds | an0ther.to_a)
end