Class: Corona::SNMP

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

Defined Under Namespace

Classes: InvalidResponse

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.



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

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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/corona/snmp.rb', line 57

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



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

def close
  @snmp.close
end

#get(*oid) ⇒ Object



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

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



85
86
87
88
89
# File 'lib/corona/snmp.rb', line 85

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



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/corona/snmp.rb', line 74

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



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

def mget oids=DB_OID
  result = {}
  begin
    res = @snmp.get(oids.map{|_,oid|oid})
    raise InvalidResponse, "#{res.error_status} from #{@snmp.config[:host]}" unless res.error_status == :noError
    res.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
  rescue InvalidResponse => e
    return false
  end
  result
end

#sysdescrObject



70
71
72
# File 'lib/corona/snmp.rb', line 70

def sysdescr
  get DB_OID[:sysDescr]
end