Class: GRI::PollingUnit

Inherits:
Object show all
Defined in:
lib/gri/polling_unit.rb

Direct Known Subclasses

HRSWRunPerfPollingUnit

Constant Summary collapse

UNITS =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cat) ⇒ PollingUnit

Returns a new instance of PollingUnit.



41
42
43
44
45
46
47
# File 'lib/gri/polling_unit.rb', line 41

def initialize name, cat
  @name = name
  @cat = cat
  @options = {}
  @ophash = nil
  @d_p = false
end

Instance Attribute Details

#catObject (readonly)

Returns the value of attribute cat.



5
6
7
# File 'lib/gri/polling_unit.rb', line 5

def cat
  @cat
end

#dhashObject Also known as: defs

Returns the value of attribute dhash.



6
7
8
# File 'lib/gri/polling_unit.rb', line 6

def dhash
  @dhash
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/gri/polling_unit.rb', line 5

def name
  @name
end

#oidsObject (readonly)

Returns the value of attribute oids.



5
6
7
# File 'lib/gri/polling_unit.rb', line 5

def oids
  @oids
end

#ophashObject

Returns the value of attribute ophash.



6
7
8
# File 'lib/gri/polling_unit.rb', line 6

def ophash
  @ophash
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/gri/polling_unit.rb', line 6

def options
  @options
end

Class Method Details

.all_unitsObject



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
# File 'lib/gri/polling_unit.rb', line 9

def self.all_units
  if UNITS.empty?
    for name, dhash in DEFS
      next unless String === name
      pucat = dhash[:cat] || dhash[:pucat] ||
        (dhash[:tdb] and dhash[:tdb].first.intern) || name.intern
      klass = self
      if (puclass = dhash[:puclass])
        if GRI.const_defined?("#{puclass}PollingUnit") or 
            Object.const_defined?("#{puclass}PollingUnit")
          klass = eval("#{puclass}PollingUnit")
        end
      end
      pu = klass.new name, pucat
      pu.dhash = dhash
      pu.set_oids dhash[:oid]
      if dhash[:tdb]
        dhash[:tdb].each {|item|
          if item =~ /\s+\*\s+/
            pre = Regexp.last_match.pre_match
            post = Regexp.last_match.post_match
            (pu.ophash ||= {})[pre] = proc {|val| val * post.to_f}
          end
        }
      end

      self::UNITS[name] = pu
    end
  end
  self::UNITS
end

Instance Method Details

#feed(wh, enoid, tag, val) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gri/polling_unit.rb', line 56

def feed wh, enoid, tag, val
  if (feed_proc = dhash[:feed])
    feed_proc.call wh, enoid, tag, val
  else
    if enoid.getbyte(-2) < 128
      ind = enoid.getbyte(-1)
      if ind == 0
        oid_ind = enoid
      else
        oid_ind = enoid[0..-2]
      end
    else
      if enoid.getbyte(-3) < 128
        ind = ((enoid.getbyte(-2) & 0x7f) << 7) + enoid.getbyte(-1)
        oid_ind = enoid[0..-3]
      else
        tmpary = BER.dec_oid enoid
        oid_ind = BER.enc_v_oid(tmpary[0..-2].join('.'))
        ind = tmpary[-1]
      end
    end
    if (sym_oid = SNMP::ROIDS[oid_ind])
      (conv_val_proc = dhash[:conv_val]) and
        (val = conv_val_proc.call(sym_oid, val))
      if ophash and (pr = ophash[sym_oid])
        val = pr.call(val)
      end
      (wh[ind] ||= {})[sym_oid] = val
    end
  end
end

#fix_workhash(workhash) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/gri/polling_unit.rb', line 88

def fix_workhash workhash
  if (c = dhash[:fix_workhash])
    if c.arity == 1
      c.call workhash
    elsif c.arity == 2
      c.call workhash, @options
    end
  end
end

#inspectObject



98
99
100
# File 'lib/gri/polling_unit.rb', line 98

def inspect
  "#<PU:#{@name}>"
end

#set_oids(names) ⇒ Object



49
50
51
52
53
54
# File 'lib/gri/polling_unit.rb', line 49

def set_oids names
  @oids = (names || []).map {|name|
    (oid = SNMP::OIDS[name]) ? BER.enc_v_oid(oid) :
      (Log.debug "No such OID: #{name}"; nil)
  }.compact
end