Class: Corona::SNMP
- Inherits:
-
Object
- Object
- Corona::SNMP
- Defined in:
- lib/corona/snmp.rb
Constant Summary collapse
- DB_OID =
{ :sysDescr => '1.3.6.1.2.1.1.1.0', :sysLocation => '1.3.6.1.2.1.1.6.0', :sysName => '1.3.6.1.2.1.1.5.0', }
- OID =
{ :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 :ifDescr => '1.3.6.1.2.1.2.2.1.2', }
- UNICAST =
1
- IPV4 =
4
- BULK_MAX =
30
Instance Method Summary collapse
- #bulkwalk(root) ⇒ Object
- #close ⇒ Object
- #get(*oid) ⇒ Object
- #ifdescr(index) ⇒ Object
-
#initialize(host, community = CFG.community) ⇒ SNMP
constructor
A new instance of SNMP.
- #ip2index(ip) ⇒ Object
- #mget(oids = DB_OID) ⇒ Object (also: #dbget)
- #sysdescr ⇒ Object
Constructor Details
#initialize(host, community = CFG.community) ⇒ SNMP
Returns a new instance of SNMP.
19 20 21 22 |
# File 'lib/corona/snmp.rb', line 19 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
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/corona/snmp.rb', line 51 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 |
#close ⇒ Object
23 24 25 |
# File 'lib/corona/snmp.rb', line 23 def close @snmp.close end |
#get(*oid) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/corona/snmp.rb', line 26 def get *oid oid = [oid].flatten.join('.') begin @snmp.get(oid).each_varbind { |vb| return vb } rescue ::SNMP::RequestTimeout return false end end |
#ifdescr(index) ⇒ Object
79 80 81 82 83 |
# File 'lib/corona/snmp.rb', line 79 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
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/corona/snmp.rb', line 68 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/corona/snmp.rb', line 34 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 return false end result end |
#sysdescr ⇒ Object
64 65 66 |
# File 'lib/corona/snmp.rb', line 64 def sysdescr get DB_OID[:sysDescr] end |