Sha256: f239bacf0bec6a6fd367bd55b18c8b581c51f4df7bc9a1d024949dbb0849bc02

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

require 'snmp/open/parser'
require 'snmp/open/command_reader'

# Simple Network Management Protocol
module SNMP
  # Converts types introduced by the snmp gem to native ruby types
  module_function def convert_from_snmp_to_ruby(value)
    return nil if [NoSuchInstance, NoSuchObject, Null].include?(value)

    case value
    when Integer
      value.to_i
    when IpAddress, String
      value.to_s
    else
      value
    end
  end

  # Open3-based wrapper for SNMP CLI commands
  class Open
    attr_reader :reader

    # see CommandReader for a description of options
    def initialize(options = {})
      @reader = options[:reader] || CommandReader.new(options)
    end

    # Perform an SNMP get using the "snmpget" command and parse the output
    def get(oids)
      return enum_for(:get, oids) unless block_given?
      texts = oids.map { |oid| reader.capture(:get, oid) }
      Parser.new(oids).parse(texts).fetch(0, []).each { |arg| yield(arg) }
    end

    # Perform an SNMP walk using the "snmpwalk" or "snmpbulkwalk" commands and
    # parse the output
    def walk(oids, **kwargs)
      return enum_for(:walk, oids, **kwargs) unless block_given?
      bulk = kwargs.fetch(:bulk, true)
      options = walk_options(bulk, **kwargs)
      cmd = bulk ? :bulkwalk : :walk
      texts = oids.map { |oid| reader.capture(cmd, oid, options) }
      Parser.new(oids).parse(texts).each { |*args| yield(*args) }
    end

    def walk_options(bulk, **kwargs)
      if bulk
        {
          '-Cn' => kwargs.fetch(:non_repeaters, 0),
          '-Cr' => kwargs.fetch(:max_repetitions, 10)
        }
      else
        {}
      end
    end
  end # class Open
end # module SNMP

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snmp-open-0.6.0 lib/snmp/open.rb
snmp-open-0.5.0 lib/snmp/open.rb
snmp-open-0.4.0 lib/snmp/open.rb
snmp-open-0.3.1 lib/snmp/open.rb
snmp-open-0.3.0 lib/snmp/open.rb
snmp-open-0.2.0 lib/snmp/open.rb