Class: Snmp2mkr::Snmp

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

Overview

Wrapper of SNMP::Manager for future implementation change

Defined Under Namespace

Classes: Closed, Varbind

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port: 161, community: 'public', mib: nil, &block) ⇒ Snmp

:nodoc:



25
26
27
28
# File 'lib/snmp2mkr/snmp.rb', line 25

def initialize(host, port: 161, community: 'public', mib: nil, &block) # :nodoc:
  @manager = SNMP::Manager.new(host: host, port: port, community: community, &block)
  @mib = mib || self.class.default_mib
end

Instance Attribute Details

#mibObject (readonly)

Returns the value of attribute mib.



30
31
32
# File 'lib/snmp2mkr/snmp.rb', line 30

def mib
  @mib
end

Class Method Details

.default_mibObject



11
# File 'lib/snmp2mkr/snmp.rb', line 11

def self.default_mib; @default_mib ||= Mib.new; end

.default_mib=(o) ⇒ Object



12
# File 'lib/snmp2mkr/snmp.rb', line 12

def self.default_mib=(o); @default_mib = o; end

.open(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/snmp2mkr/snmp.rb', line 14

def self.open(*args)
  conn = new(*args)
  return conn unless block_given?

  begin
    yield conn
  ensure
    conn.close
  end
end

Instance Method Details

#closeObject



32
33
34
35
# File 'lib/snmp2mkr/snmp.rb', line 32

def close
  @manager.close
  @manager = nil
end

#closed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/snmp2mkr/snmp.rb', line 37

def closed?
  @manager.nil?
end

#get(*oids_) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/snmp2mkr/snmp.rb', line 41

def get(*oids_)
  raise Closed if closed?
  return to_enum(__method__, *oids_) unless block_given?

  oids = oids_.flatten.map(&:to_s)
  oids.each_slice(8) do |slice|
    @manager.get(slice).varbind_list.each do |varbind|
      yield Varbind.new(Snmp2mkr::Oid.new(varbind.name.to_a, mib: mib), varbind.value)
    end
  end
end

#subtree(oid_) ⇒ Object

Raises:



53
54
55
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
# File 'lib/snmp2mkr/snmp.rb', line 53

def subtree(oid_)
  raise Closed if closed?
  return to_enum(__method__, oid_) unless block_given?

  oid = oid_.kind_of?(Snmp2mkr::Oid) ? oid_ : Snmp2mkr::Oid.new(mib.name_to_oid(oid_), name: oid_.to_s)
  pointer = oid

  while pointer
    @manager.get_bulk(0, 20, pointer.to_s).varbind_list.each do |varbind|
      if varbind == SNMP::EndOfMibView
        pointer = nil
        break
      end

      vb = Varbind.new(Snmp2mkr::Oid.new(varbind.name.to_a, mib: mib), varbind.value)
      unless oid.subtree?(vb.oid)
        pointer = nil
        break
      end

      yield vb

      pointer = vb.oid
    end
  end

  nil
end