net-snmp2 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +24 -0
- data/TODO.md +6 -0
- data/bin/mib2rb +129 -0
- data/bin/net-snmp2 +64 -0
- data/examples/agent.rb +104 -0
- data/examples/manager.rb +11 -0
- data/examples/trap_handler.rb +46 -0
- data/examples/v1_trap_session.rb +24 -0
- data/examples/v2_trap_session.rb +21 -0
- data/lib/net-snmp2.rb +85 -0
- data/lib/net/snmp.rb +27 -0
- data/lib/net/snmp/agent/agent.rb +48 -0
- data/lib/net/snmp/agent/provider.rb +51 -0
- data/lib/net/snmp/agent/provider_dsl.rb +124 -0
- data/lib/net/snmp/agent/request_dispatcher.rb +38 -0
- data/lib/net/snmp/constants.rb +287 -0
- data/lib/net/snmp/debug.rb +54 -0
- data/lib/net/snmp/dispatcher.rb +108 -0
- data/lib/net/snmp/error.rb +29 -0
- data/lib/net/snmp/listener.rb +76 -0
- data/lib/net/snmp/message.rb +142 -0
- data/lib/net/snmp/mib/mib.rb +67 -0
- data/lib/net/snmp/mib/module.rb +39 -0
- data/lib/net/snmp/mib/node.rb +122 -0
- data/lib/net/snmp/mib/templates.rb +48 -0
- data/lib/net/snmp/oid.rb +134 -0
- data/lib/net/snmp/pdu.rb +235 -0
- data/lib/net/snmp/repl/manager_repl.rb +243 -0
- data/lib/net/snmp/session.rb +560 -0
- data/lib/net/snmp/trap_handler/trap_handler.rb +42 -0
- data/lib/net/snmp/trap_handler/v1_trap_dsl.rb +44 -0
- data/lib/net/snmp/trap_handler/v2_trap_dsl.rb +38 -0
- data/lib/net/snmp/trap_session.rb +92 -0
- data/lib/net/snmp/utility.rb +10 -0
- data/lib/net/snmp/varbind.rb +57 -0
- data/lib/net/snmp/version.rb +5 -0
- data/lib/net/snmp/wrapper.rb +450 -0
- data/net-snmp2.gemspec +30 -0
- data/spec/README.md +105 -0
- data/spec/async_spec.rb +123 -0
- data/spec/em_spec.rb +23 -0
- data/spec/error_spec.rb +34 -0
- data/spec/fiber_spec.rb +41 -0
- data/spec/mib_spec.rb +68 -0
- data/spec/net-snmp_spec.rb +18 -0
- data/spec/oid_spec.rb +21 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/sync_spec.rb +132 -0
- data/spec/thread_spec.rb +19 -0
- data/spec/trap_spec.rb +45 -0
- data/spec/utility_spec.rb +10 -0
- data/spec/wrapper_spec.rb +69 -0
- metadata +166 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
module Net::SNMP
|
2
|
+
class TrapHandler
|
3
|
+
include Net::SNMP::Debug
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
attr_accessor :listener, :v1_handler, :v2_handler, :message, :pdu
|
7
|
+
def_delegators :listener, :start, :run, :listen, :stop, :kill
|
8
|
+
|
9
|
+
def self.listen(port = 162, interval = 2, max_packet_size = 65_000, &block)
|
10
|
+
self.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(&block)
|
14
|
+
@listener = Net::SNMP::Listener.new
|
15
|
+
@listener.on_message(&method(:process_trap))
|
16
|
+
self.instance_eval(&block)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def process_trap(message, from_address, from_port)
|
22
|
+
if message.pdu.command == Net::SNMP::Constants::SNMP_MSG_TRAP
|
23
|
+
handler = V1TrapDsl.new(message)
|
24
|
+
handler.instance_eval(&v1_handler)
|
25
|
+
elsif message.pdu.command == Net::SNMP::Constants::SNMP_MSG_TRAP2
|
26
|
+
handler = V2TrapDsl.new(message)
|
27
|
+
handler.instance_eval(&v2_handler)
|
28
|
+
else
|
29
|
+
warn "Trap handler receive invalid command: #{message.pdu.command}"
|
30
|
+
end
|
31
|
+
message.pdu.free
|
32
|
+
end
|
33
|
+
|
34
|
+
def v1(&handler)
|
35
|
+
@v1_handler = handler
|
36
|
+
end
|
37
|
+
|
38
|
+
def v2(&handler)
|
39
|
+
@v2_handler = handler
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Net::SNMP
|
2
|
+
class V1TrapDsl
|
3
|
+
include Debug
|
4
|
+
|
5
|
+
attr_accessor :message
|
6
|
+
|
7
|
+
def initialize(message)
|
8
|
+
@message = message
|
9
|
+
end
|
10
|
+
|
11
|
+
def pdu
|
12
|
+
message.pdu
|
13
|
+
end
|
14
|
+
|
15
|
+
def enterprise
|
16
|
+
pdu.enterprise
|
17
|
+
end
|
18
|
+
alias enterprise_oid enterprise
|
19
|
+
|
20
|
+
def trap_type
|
21
|
+
pdu.trap_type
|
22
|
+
end
|
23
|
+
alias general_trap_type trap_type
|
24
|
+
|
25
|
+
def specific_type
|
26
|
+
pdu.specific_type
|
27
|
+
end
|
28
|
+
alias specific_trap_type specific_type
|
29
|
+
|
30
|
+
def agent_addr
|
31
|
+
pdu.agent_addr
|
32
|
+
end
|
33
|
+
alias agent_address agent_addr
|
34
|
+
|
35
|
+
def uptime
|
36
|
+
pdu.uptime
|
37
|
+
end
|
38
|
+
|
39
|
+
def varbinds
|
40
|
+
pdu.varbinds
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Net::SNMP
|
2
|
+
class V2TrapDsl
|
3
|
+
include Debug
|
4
|
+
|
5
|
+
attr_accessor :message
|
6
|
+
|
7
|
+
def initialize(message)
|
8
|
+
@message = message
|
9
|
+
end
|
10
|
+
|
11
|
+
def pdu
|
12
|
+
message.pdu
|
13
|
+
end
|
14
|
+
|
15
|
+
def oid
|
16
|
+
vb = varbinds.find{|vb| vb.oid.to_s == Constants::OID_SNMP_TRAP_OID}
|
17
|
+
if vb
|
18
|
+
vb.value
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias trap_oid oid
|
24
|
+
|
25
|
+
def uptime
|
26
|
+
vb = varbinds.find{|vb| vb.oid.to_s == Constants::OID_SYS_UP_TIME_INSTANCE}
|
27
|
+
if vb
|
28
|
+
vb.value
|
29
|
+
else
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def varbinds
|
35
|
+
pdu.varbinds
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Net
|
2
|
+
module SNMP
|
3
|
+
class TrapSession < Session
|
4
|
+
# == Represents a session for sending SNMP traps
|
5
|
+
|
6
|
+
# +options+
|
7
|
+
# * :peername The address where the trap will be sent
|
8
|
+
# * :port The port where the trap will be sent (default = 162)
|
9
|
+
def initialize(options = {})
|
10
|
+
# Unless the port was supplied in the peername...
|
11
|
+
unless options[:peername][":"]
|
12
|
+
# ...default to standard trap port
|
13
|
+
options[:port] ||= 162
|
14
|
+
end
|
15
|
+
|
16
|
+
super(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Send an SNMPv1 trap
|
20
|
+
#
|
21
|
+
# Options
|
22
|
+
#
|
23
|
+
# - enterprise: The Oid of the enterprise
|
24
|
+
# - trap_type: The generic trap type.
|
25
|
+
# - specific_type: The specific trap type
|
26
|
+
# - uptime: The uptime for this agent
|
27
|
+
def trap(options = {})
|
28
|
+
pdu = PDU.new(Constants::SNMP_MSG_TRAP)
|
29
|
+
options[:enterprise] ||= '1.3.6.1.4.1.3.1.1'
|
30
|
+
pdu.enterprise = OID.new(options[:enterprise].to_s)
|
31
|
+
pdu.trap_type = options[:trap_type].to_i || 1 # need to check all these defaults
|
32
|
+
pdu.specific_type = options[:specific_type].to_i || 0
|
33
|
+
pdu.time = options[:uptime].to_i || 1
|
34
|
+
pdu.agent_addr = options[:agent_addr] || '127.0.0.1'
|
35
|
+
if options[:varbinds]
|
36
|
+
options[:varbinds].each do |vb|
|
37
|
+
pdu.add_varbind(vb)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
result = send_pdu(pdu)
|
41
|
+
pdu.free
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
# Send an SNMPv2 trap
|
46
|
+
# +options
|
47
|
+
# * :oid The Oid of the trap
|
48
|
+
# * :varbinds A list of Varbind objects to send with the trap
|
49
|
+
#
|
50
|
+
# TODO: You can only send v1 traps on a v1 session, and same for v2.
|
51
|
+
# So, we could always have the client call `trap` and just do the right
|
52
|
+
# thing based on the session.
|
53
|
+
def trap_v2(options = {})
|
54
|
+
if options[:oid].kind_of?(String)
|
55
|
+
options[:oid] = Net::SNMP::OID.new(options[:oid])
|
56
|
+
end
|
57
|
+
pdu = PDU.new(Constants::SNMP_MSG_TRAP2)
|
58
|
+
build_trap_pdu(pdu, options)
|
59
|
+
result = send_pdu(pdu)
|
60
|
+
pdu.free
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
64
|
+
# Send an SNMPv2 inform. Can accept a callback to execute on confirmation of the inform
|
65
|
+
# +options
|
66
|
+
# * :oid The OID of the inform
|
67
|
+
# * :varbinds A list of Varbind objects to send with the inform
|
68
|
+
def inform(options = {}, &callback)
|
69
|
+
if options[:oid].kind_of?(String)
|
70
|
+
options[:oid] = Net::SNMP::OID.new(options[:oid])
|
71
|
+
end
|
72
|
+
pdu = PDU.new(Constants::SNMP_MSG_INFORM)
|
73
|
+
build_trap_pdu(pdu, options)
|
74
|
+
result = send_pdu(pdu, &callback)
|
75
|
+
pdu.free
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
def build_trap_pdu(pdu, options = {})
|
81
|
+
options[:uptime] ||= 1
|
82
|
+
pdu.add_varbind(:oid => OID.new('sysUpTime.0'), :type => Constants::ASN_TIMETICKS, :value => options[:uptime].to_i)
|
83
|
+
pdu.add_varbind(:oid => OID.new('snmpTrapOID.0'), :type => Constants::ASN_OBJECT_ID, :value => options[:oid])
|
84
|
+
if options[:varbinds]
|
85
|
+
options[:varbinds].each do |vb|
|
86
|
+
pdu.add_varbind(vb)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Net::SNMP
|
2
|
+
class Varbind
|
3
|
+
# == Represents an SNMP Variable Binding
|
4
|
+
|
5
|
+
attr_accessor :struct
|
6
|
+
|
7
|
+
def initialize(ptr = nil)
|
8
|
+
@struct = Net::SNMP::Wrapper::VariableList.new(ptr)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.from_pointer(ptr)
|
12
|
+
new(ptr)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns the data type of the varbind
|
16
|
+
def object_type
|
17
|
+
@struct.type
|
18
|
+
end
|
19
|
+
alias type object_type
|
20
|
+
|
21
|
+
# Returns the OID associated with the varbind
|
22
|
+
def oid
|
23
|
+
@oid ||= OID.from_pointer(@struct.name, @struct.name_length)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the value of the varbind
|
27
|
+
def value
|
28
|
+
case object_type
|
29
|
+
when Constants::ASN_OCTET_STR, Constants::ASN_OPAQUE
|
30
|
+
struct.val[:string].read_string(struct.val_len)
|
31
|
+
when Constants::ASN_INTEGER
|
32
|
+
struct.val[:integer].read_long
|
33
|
+
when Constants::ASN_UINTEGER, Constants::ASN_TIMETICKS, Constants::ASN_COUNTER, Constants::ASN_GAUGE
|
34
|
+
struct.val[:integer].read_ulong
|
35
|
+
when Constants::ASN_IPADDRESS
|
36
|
+
struct.val[:objid].read_string(struct.val_len).unpack('CCCC').join(".")
|
37
|
+
when Constants::ASN_NULL
|
38
|
+
nil
|
39
|
+
when Constants::ASN_OBJECT_ID
|
40
|
+
Net::SNMP::OID.from_pointer(struct.val[:objid], struct.val_len / OID.oid_size)
|
41
|
+
when Constants::ASN_COUNTER64
|
42
|
+
counter = Wrapper::Counter64.new(struct.val[:counter64])
|
43
|
+
counter.high * 2^32 + counter.low
|
44
|
+
when Constants::ASN_BIT_STR
|
45
|
+
# XXX not sure what to do here. Is this obsolete?
|
46
|
+
when Constants::SNMP_ENDOFMIBVIEW
|
47
|
+
:endofmibview
|
48
|
+
when Constants::SNMP_NOSUCHOBJECT
|
49
|
+
:nosuchobject
|
50
|
+
when Constants::SNMP_NOSUCHINSTANCE
|
51
|
+
:nosuchinstance
|
52
|
+
else
|
53
|
+
raise Net::SNMP::Error.new, "Unknown value type #{object_type}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,450 @@
|
|
1
|
+
module Net
|
2
|
+
module SNMP
|
3
|
+
module Wrapper
|
4
|
+
extend NiceFFI::Library
|
5
|
+
ffi_lib ["libnetsnmp", "netsnmp"]
|
6
|
+
typedef :u_long, :oid
|
7
|
+
|
8
|
+
class Counter64 < NiceFFI::Struct
|
9
|
+
layout(
|
10
|
+
:high, :u_long,
|
11
|
+
:low, :u_long
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
class TimeVal < NiceFFI::Struct
|
16
|
+
layout(:tv_sec, :long, :tv_usec, :long)
|
17
|
+
end
|
18
|
+
|
19
|
+
class NetsnmpVardata < FFI::Union
|
20
|
+
layout(
|
21
|
+
:integer, :pointer,
|
22
|
+
:string, :pointer,
|
23
|
+
:objid, :pointer,
|
24
|
+
:bitstring, :pointer,
|
25
|
+
:counter64, :pointer,
|
26
|
+
:float, :pointer,
|
27
|
+
:double, :pointer
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.print_varbind(v)
|
32
|
+
puts "---------------------VARBIND------------------------"
|
33
|
+
puts %{
|
34
|
+
name_length #{v.name_length}
|
35
|
+
name #{v.name.read_array_of_long(v.name_length).join(".")}
|
36
|
+
type = #{v.type}
|
37
|
+
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
class VariableList < NiceFFI::Struct
|
42
|
+
layout(
|
43
|
+
:next_variable, VariableList.typed_pointer,
|
44
|
+
:name, :pointer,
|
45
|
+
:name_length, :size_t,
|
46
|
+
:type, :u_char,
|
47
|
+
:val, NetsnmpVardata,
|
48
|
+
:val_len, :size_t,
|
49
|
+
:name_loc, [:oid, Net::SNMP::MAX_OID_LEN],
|
50
|
+
:buf, [:u_char, 40],
|
51
|
+
:data, :pointer,
|
52
|
+
:dataFreeHook, callback([ :pointer ], :void),
|
53
|
+
:index, :int
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.print_pdu(p)
|
58
|
+
puts "--------------PDU---------------"
|
59
|
+
puts %{
|
60
|
+
version = #{p.version}
|
61
|
+
command = #{p.command}
|
62
|
+
errstat = #{p.errstat}
|
63
|
+
errindex = #{p.errindex}
|
64
|
+
}
|
65
|
+
v = p.variables.pointer
|
66
|
+
puts "-----VARIABLES------"
|
67
|
+
while !v.null? do
|
68
|
+
var = VariableList.new v
|
69
|
+
print_varbind(var)
|
70
|
+
v = var.next_variable
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class SnmpPdu < NiceFFI::Struct
|
75
|
+
layout(
|
76
|
+
:version, :long,
|
77
|
+
:command, :int,
|
78
|
+
:reqid, :long,
|
79
|
+
:msgid, :long,
|
80
|
+
:transid, :long,
|
81
|
+
:sessid, :long,
|
82
|
+
:errstat, :long,
|
83
|
+
:errindex, :long,
|
84
|
+
:time, :u_long,
|
85
|
+
:flags, :u_long,
|
86
|
+
:securityModel, :int,
|
87
|
+
:securityLevel, :int,
|
88
|
+
:msgParseModel, :int,
|
89
|
+
:transport_data, :pointer,
|
90
|
+
:transport_data_length, :int,
|
91
|
+
:tDomain, :pointer,
|
92
|
+
:tDomainLen, :size_t,
|
93
|
+
:variables, VariableList.typed_pointer,
|
94
|
+
:community, :pointer,
|
95
|
+
:community_len, :size_t,
|
96
|
+
:enterprise, :pointer,
|
97
|
+
:enterprise_length, :size_t,
|
98
|
+
:trap_type, :long,
|
99
|
+
:specific_type, :long,
|
100
|
+
:agent_addr, [:uchar, 4],
|
101
|
+
:contextEngineID, :pointer,
|
102
|
+
:contextEngineIDLen, :size_t,
|
103
|
+
:contextName, :pointer,
|
104
|
+
:contextNameLen, :size_t,
|
105
|
+
:securityEngineID, :pointer,
|
106
|
+
:securityEngineIDLen, :size_t,
|
107
|
+
:securityName, :pointer,
|
108
|
+
:securityNameLen, :size_t,
|
109
|
+
:priority, :int,
|
110
|
+
:range_subid, :int,
|
111
|
+
:securityStateRef, :pointer
|
112
|
+
)
|
113
|
+
end
|
114
|
+
|
115
|
+
callback(:snmp_callback, [ :int, :pointer, :int, :pointer, :pointer ], :int)
|
116
|
+
callback(:netsnmp_callback, [ :int, :pointer, :int, :pointer, :pointer ], :int)
|
117
|
+
|
118
|
+
def self.print_session(s)
|
119
|
+
puts "-------------------SESSION---------------------"
|
120
|
+
puts %{
|
121
|
+
peername = #{s.peername.read_string}
|
122
|
+
community = #{s.community.read_string(s.community_len)}
|
123
|
+
s_errno = #{s.s_errno}
|
124
|
+
s_snmp_errno = #{s.s_snmp_errno}
|
125
|
+
securityAuthKey = #{s.securityAuthKey.to_ptr.read_string}
|
126
|
+
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
class SnmpSession < NiceFFI::Struct
|
131
|
+
layout(
|
132
|
+
:version, :long,
|
133
|
+
:retries, :int,
|
134
|
+
:timeout, :long,
|
135
|
+
:flags, :u_long,
|
136
|
+
:subsession, :pointer,
|
137
|
+
:next, :pointer,
|
138
|
+
:peername, :pointer,
|
139
|
+
:remote_port, :u_short,
|
140
|
+
:localname, :pointer,
|
141
|
+
:local_port, :u_short,
|
142
|
+
:authenticator, callback([ :pointer, :pointer, :pointer, :uint ], :pointer),
|
143
|
+
:callback, :netsnmp_callback,
|
144
|
+
:callback_magic, :pointer,
|
145
|
+
:s_errno, :int,
|
146
|
+
:s_snmp_errno, :int,
|
147
|
+
:sessid, :long,
|
148
|
+
:community, :pointer,
|
149
|
+
:community_len, :size_t,
|
150
|
+
:rcvMsgMaxSize, :size_t,
|
151
|
+
:sndMsgMaxSize, :size_t,
|
152
|
+
:isAuthoritative, :u_char,
|
153
|
+
:contextEngineID, :pointer,
|
154
|
+
:contextEngineIDLen, :size_t,
|
155
|
+
:engineBoots, :u_int,
|
156
|
+
:engineTime, :u_int,
|
157
|
+
:contextName, :pointer,
|
158
|
+
:contextNameLen, :size_t,
|
159
|
+
:securityEngineID, :pointer,
|
160
|
+
:securityEngineIDLen, :size_t,
|
161
|
+
:securityName, :pointer,
|
162
|
+
:securityNameLen, :size_t,
|
163
|
+
:securityAuthProto, :pointer,
|
164
|
+
:securityAuthProtoLen, :size_t,
|
165
|
+
:securityAuthKey, [:u_char, 32],
|
166
|
+
:securityAuthKeyLen, :size_t,
|
167
|
+
:securityAuthLocalKey, :pointer,
|
168
|
+
:securityAuthLocalKeyLen, :size_t,
|
169
|
+
:securityPrivProto, :pointer,
|
170
|
+
:securityPrivProtoLen, :size_t,
|
171
|
+
:securityPrivKey, [:u_char, 32],
|
172
|
+
:securityPrivKeyLen, :size_t,
|
173
|
+
:securityPrivLocalKey, :pointer,
|
174
|
+
:securityPrivLocalKeyLen, :size_t,
|
175
|
+
:securityModel, :int,
|
176
|
+
:securityLevel, :int,
|
177
|
+
:paramName, :pointer,
|
178
|
+
:securityInfo, :pointer,
|
179
|
+
:myvoid, :pointer
|
180
|
+
)
|
181
|
+
end
|
182
|
+
|
183
|
+
class EnumList < NiceFFI::Struct
|
184
|
+
layout(
|
185
|
+
:next, EnumList.typed_pointer,
|
186
|
+
:value, :int,
|
187
|
+
:label, :pointer
|
188
|
+
)
|
189
|
+
end
|
190
|
+
|
191
|
+
class IndexList < NiceFFI::Struct
|
192
|
+
layout(
|
193
|
+
:next, :pointer,
|
194
|
+
:ilabel, :pointer,
|
195
|
+
:isimplied, :char
|
196
|
+
)
|
197
|
+
end
|
198
|
+
|
199
|
+
class ModuleImport < NiceFFI::Struct
|
200
|
+
layout(
|
201
|
+
:label, :pointer, # The descriptor being imported (pointer to string)
|
202
|
+
:modid, :int # The module id
|
203
|
+
)
|
204
|
+
end
|
205
|
+
|
206
|
+
class Module < NiceFFI::Struct
|
207
|
+
layout(
|
208
|
+
:name, :pointer, # The module's name (pointer to string)
|
209
|
+
:file, :pointer, # The file containing the module (pointer to string)
|
210
|
+
:imports, ModuleImport.typed_pointer, # List of descriptors being imported
|
211
|
+
:no_imports, :int, # The length of the imports array
|
212
|
+
:modid, :int, # The index number of this module
|
213
|
+
:next, Module.typed_pointer # Linked list pointer
|
214
|
+
)
|
215
|
+
end
|
216
|
+
|
217
|
+
class Tree < NiceFFI::Struct
|
218
|
+
layout(
|
219
|
+
:child_list, Tree.typed_pointer,
|
220
|
+
:next_peer, Tree.typed_pointer,
|
221
|
+
:next, Tree.typed_pointer,
|
222
|
+
:parent, :pointer,
|
223
|
+
:label, :string,
|
224
|
+
:subid, :u_long,
|
225
|
+
:modid, :int,
|
226
|
+
:number_modules, :int, # Length of module_list array
|
227
|
+
:module_list, :pointer, # Array of modids (pointer to int)
|
228
|
+
:tc_index, :int,
|
229
|
+
:type, :int,
|
230
|
+
:access, :int,
|
231
|
+
:status, :int,
|
232
|
+
:enums, EnumList.typed_pointer,
|
233
|
+
:ranges, :pointer,
|
234
|
+
:indexes, IndexList.typed_pointer,
|
235
|
+
:augments, :pointer,
|
236
|
+
:varbinds, :pointer,
|
237
|
+
:hint, :pointer,
|
238
|
+
:units, :pointer,
|
239
|
+
:printomat, callback([:pointer, :pointer, :pointer, :int, :pointer, :pointer, :pointer, :pointer], :int),
|
240
|
+
:printer, callback([:pointer, :pointer, :pointer, :pointer, :pointer], :void),
|
241
|
+
:description, :pointer,
|
242
|
+
:reference, :pointer,
|
243
|
+
:reported, :int,
|
244
|
+
:defaultValue, :pointer
|
245
|
+
)
|
246
|
+
end
|
247
|
+
|
248
|
+
# Some of these functions/variables are not available on windows.
|
249
|
+
# (At least with my current setup.) Simple SNMP manager example
|
250
|
+
# seems to work fine without them, so just log and ignore for now.
|
251
|
+
class << self
|
252
|
+
include Net::SNMP::Debug
|
253
|
+
alias af attach_function
|
254
|
+
def attach_function(*args)
|
255
|
+
af(*args)
|
256
|
+
rescue Exception => ex
|
257
|
+
debug ex.message
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
attach_function :snmp_clone_pdu, [ :pointer ], :pointer
|
262
|
+
attach_function :snmp_open, [ :pointer ], SnmpSession.typed_pointer
|
263
|
+
attach_function :snmp_errstring, [:int], :string
|
264
|
+
attach_function :snmp_close, [ :pointer ], :int
|
265
|
+
attach_function :snmp_close_sessions, [ ], :int
|
266
|
+
attach_function :snmp_send, [ :pointer, :pointer ], :int
|
267
|
+
attach_function :snmp_async_send, [ :pointer, :pointer, :netsnmp_callback, :pointer ], :int
|
268
|
+
attach_function :snmp_read, [ :pointer ], :void
|
269
|
+
attach_function :snmp_free_pdu, [ :pointer ], :void
|
270
|
+
attach_function :snmp_free_var, [ :pointer ], :void
|
271
|
+
attach_function :snmp_free_varbind, [ :pointer ], :void
|
272
|
+
attach_function :snmp_select_info, [ :pointer, :pointer, :pointer, :pointer ], :int
|
273
|
+
attach_function :snmp_timeout, [ ], :void
|
274
|
+
|
275
|
+
attach_function :snmp_get_next_msgid, [ ], :long
|
276
|
+
attach_function :snmp_get_next_reqid, [ ], :long
|
277
|
+
attach_function :snmp_get_next_sessid, [ ], :long
|
278
|
+
attach_function :snmp_get_next_transid, [ ], :long
|
279
|
+
attach_function :snmp_oid_compare, [ :pointer, :uint, :pointer, :uint ], :int
|
280
|
+
attach_function :snmp_oid_ncompare, [ :pointer, :uint, :pointer, :uint, :uint ], :int
|
281
|
+
attach_function :snmp_oidtree_compare, [ :pointer, :uint, :pointer, :uint ], :int
|
282
|
+
attach_function :snmp_oidsubtree_compare, [ :pointer, :uint, :pointer, :uint ], :int
|
283
|
+
attach_function :netsnmp_oid_compare_ll, [ :pointer, :uint, :pointer, :uint, :pointer ], :int
|
284
|
+
attach_function :netsnmp_oid_equals, [ :pointer, :uint, :pointer, :uint ], :int
|
285
|
+
attach_function :netsnmp_oid_tree_equals, [ :pointer, :uint, :pointer, :uint ], :int
|
286
|
+
attach_function :netsnmp_oid_is_subtree, [ :pointer, :uint, :pointer, :uint ], :int
|
287
|
+
attach_function :netsnmp_oid_find_prefix, [ :pointer, :uint, :pointer, :uint ], :int
|
288
|
+
attach_function :netsnmp_transport_open_client, [:string, :pointer], :pointer
|
289
|
+
attach_function :init_snmp, [ :string ], :void
|
290
|
+
attach_function :snmp_pdu_build, [ :pointer, :pointer, :pointer ], :pointer
|
291
|
+
attach_function :snmpv3_parse, [ :pointer, :pointer, :pointer, :pointer, :pointer ], :int
|
292
|
+
attach_function :snmpv3_packet_build, [ :pointer, :pointer, :pointer, :pointer, :pointer, :uint ], :int
|
293
|
+
attach_function :snmpv3_packet_rbuild, [ :pointer, :pointer, :pointer, :pointer, :pointer, :uint ], :int
|
294
|
+
attach_function :snmpv3_make_report, [ :pointer, :int ], :int
|
295
|
+
attach_function :snmpv3_get_report_type, [ :pointer ], :int
|
296
|
+
attach_function :snmp_pdu_parse, [ :pointer, :pointer, :pointer ], :int
|
297
|
+
attach_function :snmpv3_scopedPDU_parse, [ :pointer, :pointer, :pointer ], :pointer
|
298
|
+
attach_function :snmp_store, [ :string ], :void
|
299
|
+
attach_function :snmp_shutdown, [ :string ], :void
|
300
|
+
attach_function :snmp_pdu_add_variable, [ :pointer, :pointer, :uint, :u_char, :pointer, :size_t ], :pointer
|
301
|
+
attach_function :snmp_varlist_add_variable, [ :pointer, :pointer, :uint, :u_char, :pointer, :uint ], :pointer
|
302
|
+
attach_function :snmp_add_var, [ :pointer, :pointer, :uint, :char, :string ], :int
|
303
|
+
attach_function :snmp_duplicate_objid, [ :pointer, :uint ], :pointer
|
304
|
+
attach_function :snmp_increment_statistic, [ :int ], :u_int
|
305
|
+
attach_function :snmp_increment_statistic_by, [ :int, :int ], :u_int
|
306
|
+
attach_function :snmp_get_statistic, [ :int ], :u_int
|
307
|
+
attach_function :snmp_init_statistics, [ ], :void
|
308
|
+
attach_function :create_user_from_session, [ :pointer ], :int
|
309
|
+
attach_function :snmp_get_fd_for_session, [ :pointer ], :int
|
310
|
+
attach_function :snmp_open_ex, [ :pointer, callback([ :pointer, :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :uint ], :int), callback([ :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :uint ], :int) ], :pointer
|
311
|
+
attach_function :snmp_set_do_debugging, [ :int ], :void
|
312
|
+
attach_function :snmp_get_do_debugging, [ ], :int
|
313
|
+
attach_function :snmp_error, [ :pointer, :pointer, :pointer, :pointer ], :void
|
314
|
+
attach_function :snmp_sess_init, [ :pointer ], :void
|
315
|
+
attach_function :snmp_sess_open, [ :pointer ], SnmpSession.typed_pointer
|
316
|
+
attach_function :snmp_sess_pointer, [ :pointer ], :pointer
|
317
|
+
attach_function :snmp_sess_session, [ :pointer ], SnmpSession.typed_pointer
|
318
|
+
attach_function :snmp_sess_transport, [ :pointer ], :pointer
|
319
|
+
attach_function :snmp_sess_transport_set, [ :pointer, :pointer ], :void
|
320
|
+
attach_function :snmp_sess_add_ex, [ :pointer, :pointer, callback([ :pointer, :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :uint ], :int), callback([ :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :uint ], :int), callback([ :pointer, :pointer, :uint ], :pointer) ], :pointer
|
321
|
+
attach_function :snmp_sess_add, [ :pointer, :pointer, callback([ :pointer, :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :int ], :int) ], :pointer
|
322
|
+
attach_function :snmp_add, [ :pointer, :pointer, callback([ :pointer, :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :int ], :int) ], :pointer
|
323
|
+
attach_function :snmp_add_full, [ :pointer, :pointer, callback([ :pointer, :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :uint ], :int), callback([ :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :uint ], :int), callback([ :pointer, :pointer, :uint ], :pointer) ], :pointer
|
324
|
+
attach_function :snmp_sess_send, [ :pointer, :pointer ], :int
|
325
|
+
attach_function :snmp_sess_async_send, [ :pointer, :pointer, :snmp_callback, :pointer ], :int
|
326
|
+
attach_function :snmp_sess_select_info, [ :pointer, :pointer, :pointer, :pointer, :pointer ], :int
|
327
|
+
attach_function :snmp_sess_read, [ :pointer, :pointer ], :int
|
328
|
+
attach_function :snmp_sess_timeout, [ :pointer ], :void
|
329
|
+
attach_function :snmp_sess_close, [ :pointer ], :int
|
330
|
+
attach_function :snmp_sess_error, [ :pointer, :pointer, :pointer, :pointer ], :void
|
331
|
+
attach_function :netsnmp_sess_log_error, [ :int, :string, :pointer ], :void
|
332
|
+
attach_function :snmp_sess_perror, [ :string, :pointer ], :void
|
333
|
+
attach_function :snmp_pdu_type, [ :int ], :string
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
attach_function :asn_check_packet, [ :pointer, :uint ], :int
|
338
|
+
attach_function :asn_parse_int, [ :pointer, :pointer, :pointer, :pointer, :uint ], :pointer
|
339
|
+
attach_function :asn_build_int, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
340
|
+
attach_function :asn_parse_unsigned_int, [ :pointer, :pointer, :pointer, :pointer, :uint ], :pointer
|
341
|
+
attach_function :asn_build_unsigned_int, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
342
|
+
attach_function :asn_parse_string, [ :pointer, :pointer, :pointer, :pointer, :pointer ], :pointer
|
343
|
+
attach_function :asn_build_string, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
344
|
+
attach_function :asn_parse_header, [ :pointer, :pointer, :pointer ], :pointer
|
345
|
+
attach_function :asn_parse_sequence, [ :pointer, :pointer, :pointer, :u_char, :string ], :pointer
|
346
|
+
attach_function :asn_build_header, [ :pointer, :pointer, :u_char, :uint ], :pointer
|
347
|
+
attach_function :asn_build_sequence, [ :pointer, :pointer, :u_char, :uint ], :pointer
|
348
|
+
attach_function :asn_parse_length, [ :pointer, :pointer ], :pointer
|
349
|
+
attach_function :asn_build_length, [ :pointer, :pointer, :uint ], :pointer
|
350
|
+
attach_function :asn_parse_objid, [ :pointer, :pointer, :pointer, :pointer, :pointer ], :pointer
|
351
|
+
attach_function :asn_build_objid, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
352
|
+
attach_function :asn_parse_null, [ :pointer, :pointer, :pointer ], :pointer
|
353
|
+
attach_function :asn_build_null, [ :pointer, :pointer, :u_char ], :pointer
|
354
|
+
attach_function :asn_parse_bitstring, [ :pointer, :pointer, :pointer, :pointer, :pointer ], :pointer
|
355
|
+
attach_function :asn_build_bitstring, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
356
|
+
attach_function :asn_parse_unsigned_int64, [ :pointer, :pointer, :pointer, :pointer, :uint ], :pointer
|
357
|
+
attach_function :asn_build_unsigned_int64, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
358
|
+
attach_function :asn_parse_signed_int64, [ :pointer, :pointer, :pointer, :pointer, :uint ], :pointer
|
359
|
+
attach_function :asn_build_signed_int64, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
360
|
+
attach_function :asn_build_float, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
361
|
+
attach_function :asn_parse_float, [ :pointer, :pointer, :pointer, :pointer, :uint ], :pointer
|
362
|
+
attach_function :asn_build_double, [ :pointer, :pointer, :u_char, :pointer, :uint ], :pointer
|
363
|
+
attach_function :asn_parse_double, [ :pointer, :pointer, :pointer, :pointer, :uint ], :pointer
|
364
|
+
|
365
|
+
attach_function :snmp_pdu_create, [:int], SnmpPdu.typed_pointer
|
366
|
+
attach_function :get_node,[:string, :pointer, :pointer], :int
|
367
|
+
attach_function :read_objid, [:string, :pointer, :pointer], :int
|
368
|
+
attach_function :snmp_add_null_var, [:pointer, :pointer, :size_t], :pointer
|
369
|
+
attach_function :snmp_sess_synch_response, [:pointer, :pointer, :pointer], :int
|
370
|
+
attach_function :snmp_synch_response, [:pointer, :pointer, :pointer], :int
|
371
|
+
attach_function :snmp_parse_oid, [:string, :pointer, :pointer], :pointer
|
372
|
+
attach_function :snmp_api_errstring, [ :int ], :string
|
373
|
+
attach_function :snmp_perror, [ :string ], :void
|
374
|
+
attach_function :snmp_set_detail, [ :string ], :void
|
375
|
+
|
376
|
+
attach_function :generate_Ku, [:pointer, :int, :string, :int, :pointer, :pointer], :int
|
377
|
+
|
378
|
+
# MIB functions
|
379
|
+
attach_function :netsnmp_init_mib, [], :void
|
380
|
+
attach_function :read_all_mibs, [], :void
|
381
|
+
attach_function :add_mibdir, [:string], :int
|
382
|
+
attach_function :read_mib, [:string], Tree.typed_pointer
|
383
|
+
attach_function :netsnmp_read_module, [:string], Tree.typed_pointer
|
384
|
+
attach_function :snmp_set_save_descriptions, [:int], :void
|
385
|
+
|
386
|
+
attach_function :get_tree_head, [], Tree.typed_pointer
|
387
|
+
attach_function :get_tree, [:pointer, :int, :pointer], Tree.typed_pointer
|
388
|
+
|
389
|
+
# struct module *find_module(int modid);
|
390
|
+
attach_function :find_module, [:int], Module.typed_pointer
|
391
|
+
|
392
|
+
# Trap functions
|
393
|
+
attach_function :send_easy_trap, [:int, :int], :void
|
394
|
+
attach_function :send_trap_vars, [:int, :int, :pointer], :void
|
395
|
+
attach_function :send_v2trap, [:pointer], :void
|
396
|
+
|
397
|
+
def self.get_fd_set
|
398
|
+
FFI::MemoryPointer.new(:pointer, 128)
|
399
|
+
end
|
400
|
+
|
401
|
+
end
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
module FFI
|
406
|
+
module LibC
|
407
|
+
extend FFI::Library
|
408
|
+
ffi_lib 'c'
|
409
|
+
|
410
|
+
typedef :pointer, :FILE
|
411
|
+
typedef :uint32, :in_addr_t
|
412
|
+
typedef :uint16, :in_port_t
|
413
|
+
|
414
|
+
class Timeval < FFI::Struct
|
415
|
+
layout :tv_sec, :time_t,
|
416
|
+
:tv_usec, :suseconds_t
|
417
|
+
end
|
418
|
+
|
419
|
+
# Some of these functions/variables are not available on windows.
|
420
|
+
# (At least with my current setup.) Simple SNMP manager example
|
421
|
+
# seems to work fine without them, so just log and ignore for now.
|
422
|
+
class << self
|
423
|
+
include Net::SNMP::Debug
|
424
|
+
alias af attach_function
|
425
|
+
def attach_function(*args)
|
426
|
+
af(*args)
|
427
|
+
rescue Exception => ex
|
428
|
+
debug ex.message
|
429
|
+
end
|
430
|
+
|
431
|
+
alias av attach_variable
|
432
|
+
def attach_variable(*args)
|
433
|
+
av(*args)
|
434
|
+
rescue Exception => ex
|
435
|
+
debug ex.message
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
# Standard IO functions
|
440
|
+
#@blocking = true # some undocumented voodoo that tells the next attach_function to release the GIL
|
441
|
+
attach_function :malloc, [:size_t], :pointer
|
442
|
+
attach_function :calloc, [:size_t, :size_t], :pointer
|
443
|
+
attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer
|
444
|
+
attach_function :free, [:pointer], :void
|
445
|
+
#attach_variable :errno, :int
|
446
|
+
|
447
|
+
ffi_lib ['Ws2_32.dll'] if (ENV['OS'] =~ /windows/i)
|
448
|
+
attach_function :select, [:int, :pointer, :pointer, :pointer, :pointer], :int
|
449
|
+
end
|
450
|
+
end
|