Class: Corona::SNMP

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

Constant Summary collapse

DB_OID =
{
  :sysDescr     => '1.3.6.1.2.1.1.1.0',
  :sysObjectID  => '1.3.6.1.2.1.1.2.0',
  :sysName      => '1.3.6.1.2.1.1.5.0',
  :sysLocation  => '1.3.6.1.2.1.1.6.0',
}
OID =
{
   :ifDescr            => '1.3.6.1.2.1.2.2.1.2',
   :ipCidrRouteIfIndex => '1.3.6.1.2.1.4.24.4.1.5',  # addr.255.255.255.255.0.0.0.0.0
   :ipAdEntIfIndex     => '1.3.6.1.2.1.4.20.1.2',    # addr
   :ipAddressIfIndex   => '1.3.6.1.2.1.4.34.1.3',    # 1,2 (uni,any) . 4,16 (size) . addr
}
UNICAST =

1,2 (uni,any) . 4,16 (size) . addr

1
IPV4 =
4
BULK_MAX =
30

Instance Method Summary collapse

Constructor Details

#initialize(host, community = CFG.community) ⇒ SNMP

Returns a new instance of SNMP.



20
21
22
23
# File 'lib/corona/snmp.rb', line 20

def initialize host, community=CFG.community
  @snmp = ::SNMP::Manager.new :Host => host, :Community => community,
                              :Timeout => CFG.timeout, :Retries => CFG.retries, :MibModules => []
end

Instance Method Details

#bulkwalk(root) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/corona/snmp.rb', line 52

def bulkwalk root
  last, oid, vbs = false, root, []
  while not last
    r = @snmp.get_bulk 0, BULK_MAX, oid
    r.varbind_list.each do |vb|
      oid = vb.name.to_str
      (last = true; break) if not oid.match(/^#{Regexp.quote root}/)
      vbs.push vb
    end
  end
  vbs
end

#closeObject



24
25
26
# File 'lib/corona/snmp.rb', line 24

def close
  @snmp.close
end

#get(*oid) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/corona/snmp.rb', line 27

def get *oid
  oid = [oid].flatten.join('.')
  begin
    @snmp.get(oid).each_varbind { |vb| return vb }
  rescue ::SNMP::RequestTimeout, Errno::EACCES
    return false
  end
end

#ifdescr(index) ⇒ Object



80
81
82
83
84
# File 'lib/corona/snmp.rb', line 80

def ifdescr index
  descr = get OID[:ifDescr], index
  return false unless descr and descr.value.class == ::SNMP::OctetString
  descr.value.to_s
end

#ip2index(ip) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/corona/snmp.rb', line 69

def ip2index ip
  oids = mget :route => [OID[:ipCidrRouteIfIndex], ip, '255.255.255.255.0.0.0.0.0'].join('.'),
              :new   => [OID[:ipAddressIfIndex], UNICAST, IPV4, ip].join('.'),
              :old   => [OID[:ipAdEntIfIndex], ip].join('.')
  return false unless oids
  index = oids[:route]
  index = oids[:new] if not index.class == ::SNMP::Integer or index.to_s == '0'
  index = oids[:old] if not index.class == ::SNMP::Integer or index.to_s == '0'
  return false unless index.class == ::SNMP::Integer
  index.to_s
end

#mget(oids = DB_OID) ⇒ Object Also known as: dbget



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/corona/snmp.rb', line 35

def mget oids=DB_OID
  result = {}
  begin
    @snmp.get(oids.map{|_,oid|oid}).each_varbind do |vb|
      oids.each do |name,oid|
        if vb.name.to_str == oid
          result[name] = vb.value
          next
        end
      end
    end
  rescue ::SNMP::RequestTimeout, Errno::EACCES
    return false
  end
  result
end

#sysdescrObject



65
66
67
# File 'lib/corona/snmp.rb', line 65

def sysdescr
  get DB_OID[:sysDescr]
end