module SNMPUtils

@author Ted Cok

Public Class Methods

get(opts, oids) click to toggle source

SNMP get. Return a Hash representation of the values at the input oids

@api public @param opts [Hash] the snmp connection options. Check {snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values @param oids [Array] a list of oids containing the interesting values to retrieve @return [Hash] a Hash representation of the values at the requested oids as !{ name(oids) => value(oids), oid(oids) => value(oids) } for each value in the input array oids @example snmpvalue = SNMPUtils.get({:host => 'localhost'}, 'SNMPv2-MIB::sysName.0') # => {“SNMPv2-MIB::sysName.0”=>“medusa”, “1.3.6.1.2.1.1.5.0”=>“medusa”}

# File lib/snmputils.rb, line 102
def self.get(opts, oids)
  return snmpget(opts,oids).inject({}) do |row, value| 
    row.merge(oid_keyval(value)).merge(name_keyval(value))
  end
end
walk(opts, oids) click to toggle source

SNMP walk. Return a List representation of the values at the input oids

@api public @param opts [Hash] the snmp connection options. Check {snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values @param oids [Array] a list of oids containing the interesting snmp columns to retrieve @return [List] a List of Hashes describing each row in the table as @example snmpvalues = SNMPUtils.walk({:host => 'localhost'}, ['SNMPv2-MIB::sysORDescr', 'SNMPv2-MIB::sysORID']) # =>

# File lib/snmputils.rb, line 115
def self.walk(opts, oids) 
  return snmpwalk(opts, oids).inject([]) do |table, row| 
    rawoids = row.inject({}) do |row_n, value| 
      row_n.merge(oid_keyval(value)).merge(name_keyval(value))
    end
    suffix = rawoids.keys.suffix
    table << rawoids.inject({}) do |row_n, kvpair|
      key, value = kvpair
      row_n.merge(key.sub(/#{suffix}$/, '') => value)
    end
  end
end

Private Class Methods

name(x) click to toggle source

Return the symbolic string representation of a SNMP::VarBind's OID

@api private @param x [SNMP::VarBind] the input VarBind value @return [String] the symbolic string representation of the VarBind's OID

# File lib/snmputils.rb, line 23
def self.name(x) x.oid.to_str end
name_keyval(x) click to toggle source

Return a Hash representation of a SNMP::VarBind's value indexed by symbolic OID

@api private @param x [SNMP::VarBind] the input VarBind value @return [String] the Hash representation of the VarBind as !{ name(x) => value(x) } @see name @see value

# File lib/snmputils.rb, line 50
def self.name_keyval(x) 
  value(x) && ( name(x) != oid(x) ) && { name(x) => value(x) } or {}
end
oid(x) click to toggle source

Return the dot notation string representation of a SNMP::VarBind's OID

@api private @param x [SNMP::VarBind] the input VarBind value @return [String] the dot notation string representation of the VarBind's OID

# File lib/snmputils.rb, line 16
def self.oid(x) x.oid.to_s end
oid_keyval(x) click to toggle source

Return a Hash representation of a SNMP::VarBind's value indexed by dotted OID

@api private @param x [SNMP::VarBind] the input VarBind value @return [String] the Hash representation of the VarBind as !{ oid(x) => value(x) } @see oid @see value

# File lib/snmputils.rb, line 39
def self.oid_keyval(x) 
  value(x) && { oid(x) => value(x) } or {}
end
snmpget(opts, oids) click to toggle source

SNMP get. Return an Enumerator of the SNMP::VarBind at each input oids

@api private @param opts [Hash] the snmp connection options. Check {snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values @param oids [Array] a list of oids containing the interesting values to retrieve @return [Enumerator] an {www.ruby-doc.org/core-2.0/Enumerator.html Enumerator} of {snmplib.rubyforge.org/doc/SNMP/VarBind.html SNMP::VarBind} values

# File lib/snmputils.rb, line 60
def self.snmpget(opts, oids)
    return Enumerator.new do |yielder|
      SNMP::Manager.open(opts) do |manager|
        begin
          response = manager.get([oids].flatten)       
          response.each_varbind do |v|
            yielder.yield v
          end
        rescue ArgumentError
        end
      end
    end
end
snmpwalk(opts, oids) click to toggle source

SNMP walk. Return an Enumerator of each SNMP::VarBindList at the input oids

@api private @param opts [Hash] the snmp connection options. Check {snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values @param oids [Array] a list of oids containing the interesting columns to scan/retrieve from a snmp table @return [Enumerator] an {www.ruby-doc.org/core-2.0/Enumerator.html Enumerator} of {snmplib.rubyforge.org/doc/SNMP/VarBindList.html SNMP::VarBindList} values

# File lib/snmputils.rb, line 80
def self.snmpwalk(opts, oids)
  return Enumerator.new do |yielder|
    SNMP::Manager.open(opts) do |manager|
      begin
        manager.walk([oids].flatten) do |row|
          yielder.yield row
        end
      rescue ArgumentError
      end
    end
  end
end
value(x) click to toggle source

Return the string representation of a SNMP::VarBind's value

@api private @param x [SNMP::VarBind] the input VarBind value @return [String] the string representation of the VarBind's value

# File lib/snmputils.rb, line 30
def self.value(x) x.value.to_s unless ['noSuchInstance', 'noSuchObject'].include? x.value.to_s end