Class: Snmp2mkr::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp2mkr/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vhost, name, oid, mib: nil, transformations: []) ⇒ Metric

Returns a new instance of Metric.



5
6
7
8
9
10
# File 'lib/snmp2mkr/metric.rb', line 5

def initialize(vhost, name, oid, mib: nil, transformations: [])
  @vhost_name = vhost.name
  @name = name
  @oid = oid.kind_of?(Oid) ? oid : Oid.new(oid, mib: mib) 
  @transformations = transformations
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/snmp2mkr/metric.rb', line 12

def name
  @name
end

#oidObject (readonly)

Returns the value of attribute oid.



12
13
14
# File 'lib/snmp2mkr/metric.rb', line 12

def oid
  @oid
end

#transformationsObject (readonly)

Returns the value of attribute transformations.



12
13
14
# File 'lib/snmp2mkr/metric.rb', line 12

def transformations
  @transformations
end

#vhost_nameObject (readonly)

Returns the value of attribute vhost_name.



12
13
14
# File 'lib/snmp2mkr/metric.rb', line 12

def vhost_name
  @vhost_name
end

Instance Method Details

#evaluate(varbind, state_holder: nil, time: Time.now) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/snmp2mkr/metric.rb', line 18

def evaluate(varbind, state_holder: nil, time: Time.now)
  if varbind == SNMP::NoSuchObject || varbind == SNMP::NoSuchInstance
    return nil
  end

  if state_holder
    state = state_holder.fetch(self)
  end

  raw = val = varbind.value.to_i

  transformations.each do |xfrm|
    val = transform(xfrm, val, state, time)
  end

  if state_holder
    state_holder.set(self, state.merge(last: val, last_raw: raw, last_at: time))
  end

  return val
end

#safe_nameObject



14
15
16
# File 'lib/snmp2mkr/metric.rb', line 14

def safe_name
  name.gsub(/[^a-zA-Z0-9._-]/, '-')
end