Advertisement
rockdrilla

snmp wrapper

Aug 7th, 2015
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.43 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'date'
  3. require 'resolv'
  4. require 'uri'
  5. require 'net/telnet'
  6.  
  7. ### external gems
  8. require 'snmp'
  9.  
  10. class KUtil
  11.     def self.vblist_to_hash (varbind_list)
  12.         r = {}
  13.         varbind_list.each { |x| r[x.name.to_s] = x.value }
  14.         return r
  15.     end
  16.  
  17.     def self.snmpget (callee, oid)
  18.         x = self.snmpgetbulk(callee, [ oid ] )
  19.         return x[oid] if ! x.nil?
  20.         return nil
  21.     end
  22.     def self.snmpgetbulk (callee, oids)
  23.         if ( callee.kind_of? SNMP::Manager ) then
  24.             begin
  25.                 return self.vblist_to_hash(callee.get(oids).varbind_list)
  26.             rescue SNMP::RequestTimeout => e
  27.             end
  28.             return nil
  29.         end
  30.  
  31.         if ( callee.kind_of? String ) then
  32.             result = nil
  33.             SNMP::Manager.open(:host => callee, :mib_modules => [], :timeout => 3, :retries => 0, :read_community => 'public') { |m|
  34.                 result = self.snmpgetbulk(m, oids)
  35.             }
  36.             return result
  37.         end
  38.  
  39.         puts "#{self.name}.#{__method__} TypeError #{callee.inspect}"
  40.         raise TypeError
  41.     end
  42.  
  43.     def self.snmpset (callee, oid, value)
  44.         x = self.snmpsetbulk(callee, { oid => value } )
  45.         return x[oid] if ! x.nil?
  46.         return nil
  47.     end
  48.     def self.snmpsetbulk (callee, hashed_oid_value_pairs)
  49.         if ( callee.kind_of? SNMP::Manager ) then
  50.             vblist = hashed_oid_value_pairs.keys.map { |k|
  51.                 value = hashed_oid_value_pairs[k]
  52.                 if ( value.kind_of? String ) then
  53.                     value = SNMP::OctetString.new(value)
  54.                 end
  55.                 if ( value.kind_of? Fixnum ) then
  56.                     value = SNMP::Integer.new(value)
  57.                 end
  58.                 SNMP::VarBind.new(k, value)
  59.             }
  60.             begin
  61.                 return self.vblist_to_hash(callee.set(vblist).varbind_list)
  62.             rescue SNMP::RequestTimeout => e
  63.             end
  64.             return nil
  65.         end
  66.  
  67.         if ( callee.kind_of? String ) then
  68.             result = nil
  69.             SNMP::Manager.open(:host => callee, :mib_modules => [], :timeout => 3, :retries => 0, :write_community => 'private') { |m|
  70.                 result = self.snmpsetbulk(m, hashed_oid_value_pairs)
  71.             }
  72.             return result
  73.         end
  74.  
  75.         puts "#{self.name}.#{__method__} TypeError #{callee.inspect}"
  76.         raise TypeError
  77.     end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement