Class: NETSNMP::PDU

Inherits:
Object
  • Object
show all
Defined in:
lib/netsnmp/pdu.rb

Overview

Abstracts the PDU base structure into a ruby object. It gives access to its varbinds.

Direct Known Subclasses

ScopedPDU

Constant Summary collapse

MAXREQUESTID =
2147483647

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, headers:, request_id: nil, error_status: 0, error_index: 0, varbinds: []) ⇒ PDU

Returns a new instance of PDU.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/netsnmp/pdu.rb', line 66

def initialize(type:, headers:,
               request_id: nil,
               error_status: 0,
               error_index: 0,
               varbinds: [])
  @version, @community = headers
  @version = @version.to_i
  @error_status = error_status
  @error_index  = error_index
  @type = type
  @varbinds = []
  varbinds.each do |varbind|
    add_varbind(varbind)
  end
  @request_id = request_id || SecureRandom.random_number(MAXREQUESTID)
  check_error_status(@error_status)
end

Instance Attribute Details

#communityObject (readonly)

Returns the value of attribute community.



64
65
66
# File 'lib/netsnmp/pdu.rb', line 64

def community
  @community
end

#request_idObject (readonly)

Returns the value of attribute request_id.



64
65
66
# File 'lib/netsnmp/pdu.rb', line 64

def request_id
  @request_id
end

#typeObject (readonly)

Returns the value of attribute type.



62
63
64
# File 'lib/netsnmp/pdu.rb', line 62

def type
  @type
end

#varbindsObject (readonly)

Returns the value of attribute varbinds.



62
63
64
# File 'lib/netsnmp/pdu.rb', line 62

def varbinds
  @varbinds
end

#versionObject (readonly)

Returns the value of attribute version.



64
65
66
# File 'lib/netsnmp/pdu.rb', line 64

def version
  @version
end

Class Method Details

.build(type, **args) ⇒ Object

factory method that abstracts initialization of the pdu types that the library supports.

Parameters:

  • type (Symbol)

    the type of pdu structure to build



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/netsnmp/pdu.rb', line 47

def build(type, **args)
  typ = case type
        when :get       then 0
        when :getnext   then 1
        #          when :getbulk   then 5
        when :set       then 3
        when :inform    then 6
        when :trap      then 7
        when :response  then 2
        else raise Error, "#{type} is not supported as type"
        end
  new(args.merge(type: typ))
end

.decode(der) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/netsnmp/pdu.rb', line 10

def decode(der)
  asn_tree = case der
             when String
               OpenSSL::ASN1.decode(der)
             when OpenSSL::ASN1::ASN1Data
               der
             else
               raise "#{der}: unexpected data"
             end

  *headers, request = asn_tree.value

  version, community = headers.map(&:value)

  type = request.tag

  *request_headers, varbinds = request.value

  request_id, error_status, error_index = request_headers.map(&:value).map(&:to_i)

  varbs = varbinds.value.map do |varbind|
    oid_asn, val_asn = varbind.value
    oid = oid_asn.value
    { oid: oid, value: val_asn }
  end

  new(type: type, headers: [version, community],
      error_status: error_status,
      error_index: error_index,
      request_id: request_id,
      varbinds: varbs)
end

Instance Method Details

#add_varbind(oid:, **options) ⇒ Object Also known as: <<

Adds a request varbind to the pdu

Parameters:

  • oid (OID)

    a valid oid

  • options (Hash)

    additional request varbind options

Options Hash (**options):

  • :value (Object)

    the value for the oid



93
94
95
# File 'lib/netsnmp/pdu.rb', line 93

def add_varbind(oid:, **options)
  @varbinds << Varbind.new(oid, **options)
end

#to_asnObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/netsnmp/pdu.rb', line 98

def to_asn
  request_id_asn = OpenSSL::ASN1::Integer.new(@request_id)
  error_asn = OpenSSL::ASN1::Integer.new(@error_status)
  error_index_asn = OpenSSL::ASN1::Integer.new(@error_index)

  varbind_asns = OpenSSL::ASN1::Sequence.new(@varbinds.map(&:to_asn))

  request_asn = OpenSSL::ASN1::ASN1Data.new([request_id_asn,
                                             error_asn, error_index_asn,
                                             varbind_asns], @type,
                                            :CONTEXT_SPECIFIC)

  OpenSSL::ASN1::Sequence.new([*encode_headers_asn, request_asn])
end

#to_derObject



84
85
86
# File 'lib/netsnmp/pdu.rb', line 84

def to_der
  to_asn.to_der
end