Class: NSCA::PerformanceData::Base

Inherits:
Object
  • Object
show all
Extended by:
Benchmark, Timeout
Defined in:
lib/nsca/check.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Base

Returns a new instance of Base.



46
# File 'lib/nsca/check.rb', line 46

def initialize( value) @value = value end

Class Attribute Details

.critObject (readonly)

Returns the value of attribute crit.



11
12
13
# File 'lib/nsca/check.rb', line 11

def crit
  @crit
end

.labelObject (readonly)

Returns the value of attribute label.



11
12
13
# File 'lib/nsca/check.rb', line 11

def label
  @label
end

.maxObject (readonly)

Returns the value of attribute max.



11
12
13
# File 'lib/nsca/check.rb', line 11

def max
  @max
end

.minObject (readonly)

Returns the value of attribute min.



11
12
13
# File 'lib/nsca/check.rb', line 11

def min
  @min
end

.unitObject (readonly)

Returns the value of attribute unit.



11
12
13
# File 'lib/nsca/check.rb', line 11

def unit
  @unit
end

.warnObject (readonly)

Returns the value of attribute warn.



11
12
13
# File 'lib/nsca/check.rb', line 11

def warn
  @warn
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



45
46
47
# File 'lib/nsca/check.rb', line 45

def value
  @value
end

Class Method Details

.clone(opts = nil) ⇒ Object



42
# File 'lib/nsca/check.rb', line 42

def clone( opts = nil) ::Class.new( self).init opts ? to_h.merge( opts) : to_h end

.init(*args) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
# File 'lib/nsca/check.rb', line 12

def init *args
  a, o = args, args.last.is_a?( Hash) ? args.pop : {}
  @label, @unit = a[0]||o[:label], a[1]||o[:unit]
  @warn, @crit = a[2]||o[:warn], a[3]||o[:crit]
  @min, @max = a[4]||o[:min], a[5]||o[:max]
  raise ArgumentError, "Label expected"  unless @label
  @label = @label.to_s
  self
end

.measure(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nsca/check.rb', line 22

def measure &block
  f = case unit.to_s.to_sym
    when :s then 1
    when :ms then 1000
    else raise TimeUnitExpected, "Unit must be seconds (s) or miliseconds (ms) not (#{unit})"
    end
  exception = ::Class.new Timeout::Error
  timeout = max
  m = realtime do
    begin
      timeout timeout, exception, &block
    rescue exception
    end
  end
  new f * m
end

.to_aObject



41
# File 'lib/nsca/check.rb', line 41

def to_a() [label, unit, warn, crit, min, max] end

.to_hObject



40
# File 'lib/nsca/check.rb', line 40

def to_h() {label: @label, unit: @unit, warn: @warn, crit: @crit, min: @min, max: @max } end

.to_symObject



39
# File 'lib/nsca/check.rb', line 39

def to_sym() label.to_sym end

Instance Method Details

#critObject



50
# File 'lib/nsca/check.rb', line 50

def crit()  self.class.crit  end

#labelObject



47
# File 'lib/nsca/check.rb', line 47

def label()  self.class.label  end

#maxObject



52
# File 'lib/nsca/check.rb', line 52

def max()  self.class.max  end

#minObject



51
# File 'lib/nsca/check.rb', line 51

def min()  self.class.min  end

#return_codeObject



61
62
63
64
65
66
67
# File 'lib/nsca/check.rb', line 61

def return_code
  if @value.nil? then 3
  elsif crit <= @value then 2
  elsif warn <= @value then 1
  else 0
  end
end

#to_aObject



53
# File 'lib/nsca/check.rb', line 53

def to_a() [label, value, unit, warn, crit, min, max] end

#to_hObject



57
58
59
# File 'lib/nsca/check.rb', line 57

def to_h
  {label: @label, value: @value, unit: @unit, warn: @warn, crit: @crit, min: @min, max: @max}
end

#to_sObject



54
# File 'lib/nsca/check.rb', line 54

def to_s() "'#{label.gsub /[\n'\|]/, ''}'=#{value}#{unit},#{warn},#{crit},#{min},#{max}" end

#to_symObject



55
# File 'lib/nsca/check.rb', line 55

def to_sym() self.class.to_sym end

#unitObject



48
# File 'lib/nsca/check.rb', line 48

def unit()  self.class.unit  end

#warnObject



49
# File 'lib/nsca/check.rb', line 49

def warn()  self.class.warn  end