Class: SNMPPass::VariableStore

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp_pass/variable_store.rb

Overview

Stores a collection of variables for use by PassPersist

Instance Method Summary collapse

Constructor Details

#initializeVariableStore

Create a new variable_store.


22
23
24
# File 'lib/snmp_pass/variable_store.rb', line 22

def initialize()
  @data = Array.new
end

Instance Method Details

#[](oid) ⇒ Variable?

Fetch a variable by it's OID.

Parameters:

  • oid (String, #to_s)

Returns:


58
59
60
61
62
# File 'lib/snmp_pass/variable_store.rb', line 58

def [](oid)
  index = index_of_oid(oid.to_s)
  return nil if index.nil?
  @data[index][1]
end

#[]=(oid, value) ⇒ Variable

Assign a variable to a given OID.

Parameters:

Returns:


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/snmp_pass/variable_store.rb', line 30

def []=(oid, value)
  to_add = [oid.to_s, value]

  if @data.empty?
    # First item so we can just add it
    return @data.push to_add
  end

  # Find where to insert it
  @data.each_with_index do |item, index|
    case COMPARE_OID_PROC.call(item[0], oid)
      when 0
        # Identical OID so update instead of insert
        return @data[index] = to_add
      when -1
        # New OID is larger than this OID
        next
      when 1
        # New OID is less than this OID
        return @data.insert(index, to_add)
    end # case
  end # @data.each
  @data.push to_add
end

#dumpArray<Array>

Get a dump of all the OIDs and variables

Returns:

  • (Array<Array>)

80
81
82
# File 'lib/snmp_pass/variable_store.rb', line 80

def dump
  @data.clone
end

#first_oidnil, String

Get the first OID in the collection.

Returns:

  • (nil, String)

86
87
88
89
# File 'lib/snmp_pass/variable_store.rb', line 86

def first_oid
  item = @data.first || []
  item[0]
end

#get_next(oid) ⇒ Variable?

Fetch the variable and it's OID which comes after a given OID.

Parameters:

  • oid (String, #to_s)

Returns:


67
68
69
70
71
72
73
74
75
76
# File 'lib/snmp_pass/variable_store.rb', line 67

def get_next(oid)
  oid = oid.to_s
  @data.each do |item|
    if COMPARE_OID_PROC.call(item[0], oid).eql?(1)
      # Found the first one ater the passed OID
      return item
    end
  end
  return nil # No next one
end

#last_oidnil, String

Get the last OID in the collection.

Returns:

  • (nil, String)

93
94
95
96
# File 'lib/snmp_pass/variable_store.rb', line 93

def last_oid
  item = @data.last || []
  item[0]
end