net-snmp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/net-snmp.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'forwardable'
2
+ require 'nice-ffi'
3
+ require 'fiber'
4
+ %w(snmp snmp/constants snmp/oid snmp/error snmp/pdu snmp/wrapper snmp/session snmp/varbind snmp/mib snmp/mib/node).each do |f|
5
+ require "#{File.dirname(__FILE__)}/net/#{f}"
6
+ end
7
+ Net::SNMP::MIB.init
8
+ Net::SNMP::MIB.read_all_mibs
9
+ Net::SNMP.init
10
+
11
+
12
+
13
+ # XXX
14
+ # I just monkeypatched this to take a nil first argument. Seems to work
15
+ # Should probably submit this as a patch
16
+
17
+ class NiceFFI::Struct < FFI::Struct
18
+ def initialize( val = nil, options={} )
19
+ # Stores certain kinds of member values so that we don't need
20
+ # to create a new object every time they are read.
21
+ @member_cache = {}
22
+
23
+ options = {:autorelease => true}.merge!( options )
24
+
25
+ case val
26
+
27
+ when Hash
28
+ super(FFI::Buffer.new(size))
29
+ init_from_hash( val ) # Read the values from a Hash.
30
+
31
+ # Note: plain "Array" would mean FFI::Struct::Array in this scope.
32
+ when ::Array
33
+ super(FFI::Buffer.new(size))
34
+ init_from_array( val ) # Read the values from an Array.
35
+
36
+ when String
37
+ super(FFI::Buffer.new(size))
38
+ init_from_bytes( val ) # Read the values from a bytestring.
39
+
40
+ when self.class
41
+ super(FFI::Buffer.new(size))
42
+ init_from_bytes( val.to_bytes ) # Read the values from another instance.
43
+
44
+ when FFI::Pointer, FFI::Buffer
45
+ val = _make_autopointer( val, options[:autorelease] )
46
+
47
+ # Normal FFI::Struct behavior to wrap the pointer.
48
+ super( val )
49
+
50
+ when nil
51
+ super(val)
52
+ else
53
+ raise TypeError, "cannot create new #{self.class} from #{val.inspect}"
54
+
55
+ end
56
+ end
57
+ end
data/lib/net/snmp.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'net/snmp/constants'
2
+ module Net
3
+ module SNMP
4
+ include Net::SNMP::Constants
5
+
6
+
7
+ def self.init(tag="snmp")
8
+ Wrapper.init_snmp(tag)
9
+ end
10
+
11
+ # timeout = nil no block(poll), timeout = false block forever, timeout = int, block int seconds
12
+ def self.dispatcher(timeout = nil)
13
+ fdset = Net::SNMP::Wrapper.get_fd_set
14
+ num_fds = FFI::MemoryPointer.new(:int)
15
+ tv_sec = timeout || 0
16
+ tval = Net::SNMP::Wrapper::TimeVal.new(:tv_sec => tv_sec, :tv_usec => 0)
17
+ block = FFI::MemoryPointer.new(:int)
18
+
19
+ if timeout.nil?
20
+ block.write_int(0)
21
+ else
22
+ block.write_int(1)
23
+ end
24
+
25
+ Net::SNMP::Wrapper.snmp_select_info(num_fds, fdset, tval.pointer, block )
26
+ num_ready = 0
27
+ if num_fds.read_int > 0
28
+ tv = timeout == false ? nil : tval
29
+ num_ready = Net::SNMP::Wrapper.select(num_fds.read_int, fdset, nil, nil, tv)
30
+ Net::SNMP::Wrapper.snmp_read(fdset)
31
+ end
32
+ num_ready
33
+ end
34
+
35
+
36
+ def self._get_oid(name)
37
+ oid_ptr = FFI::MemoryPointer.new(:ulong, Constants::MAX_OID_LEN)
38
+ oid_len_ptr = FFI::MemoryPointer.new(:size_t)
39
+ oid_len_ptr.write_int(Constants::MAX_OID_LEN)
40
+
41
+ if !Wrapper.snmp_parse_oid(name, oid_ptr, oid_len_ptr)
42
+ Wrapper.snmp_perror(name)
43
+ end
44
+ [oid_ptr, oid_len_ptr]
45
+ end
46
+
47
+ def self.get_oid(name)
48
+ oid_ptr, oid_len_ptr = _get_oid(name)
49
+ oid_ptr.read_array_of_long(oid_len_ptr.read_int).join(".")
50
+ end
51
+
52
+
53
+ end
54
+ end
@@ -0,0 +1,239 @@
1
+ module Net
2
+ module SNMP
3
+ module Constants
4
+ MAX_OID_LEN = 128
5
+ SNMP_VERSION_1 = 0
6
+ SNMP_VERSION_2c = 1
7
+ SNMP_VERSION_3 = 3
8
+
9
+ ASN_BOOLEAN = 0x01
10
+ ASN_INTEGER = 0x02
11
+ ASN_BIT_STR = 0x03
12
+ ASN_OCTET_STR = 0x04
13
+ ASN_NULL = 0x05
14
+ ASN_OBJECT_ID = 0x06
15
+ ASN_SEQUENCE = 0x10
16
+ ASN_SET = 0x11
17
+
18
+ ASN_UNIVERSAL = 0x00
19
+ ASN_APPLICATION = 0x40
20
+ ASN_CONTEXT = 0x80
21
+ ASN_PRIVATE = 0xC0
22
+
23
+ ASN_PRIMITIVE = 0x00
24
+ ASN_CONSTRUCTOR = 0x20
25
+
26
+ ASN_LONG_LEN = 0x80
27
+ ASN_EXTENSION_ID = 0x1F
28
+ ASN_BIT8 = 0x80
29
+
30
+
31
+ ASN_IPADDRESS = (ASN_APPLICATION | 0)
32
+ ASN_COUNTER = (ASN_APPLICATION | 1)
33
+ ASN_GAUGE = (ASN_APPLICATION | 2)
34
+ ASN_UNSIGNED = (ASN_APPLICATION | 2) # RFC 1902 - same as GAUGE
35
+ ASN_TIMETICKS = (ASN_APPLICATION | 3)
36
+ ASN_OPAQUE = (ASN_APPLICATION | 4)
37
+
38
+ #define ASN_NSAP (ASN_APPLICATION | 5) /* historic - don't use */
39
+ #define ASN_COUNTER64 (ASN_APPLICATION | 6)
40
+ #define ASN_UINTEGER (ASN_APPLICATION | 7) /* historic - don't use */
41
+
42
+
43
+
44
+ SNMP_MSG_GET = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
45
+ SNMP_MSG_GETNEXT = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
46
+ SNMP_MSG_RESPONSE = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
47
+ SNMP_MSG_SET = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
48
+ SNMP_MSG_TRAP = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
49
+ SNMP_MSG_GETBULK = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
50
+ SNMP_MSG_INFORM = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
51
+ SNMP_MSG_TRAP2 = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
52
+ SNMP_MSG_REPORT = (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
53
+
54
+ NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE = 1
55
+ NETSNMP_CALLBACK_OP_TIMED_OUT = 2
56
+ NETSNMP_CALLBACK_OP_SEND_FAILED = 3
57
+ NETSNMP_CALLBACK_OP_CONNECT = 4
58
+ NETSNMP_CALLBACK_OP_DISCONNECT = 5
59
+
60
+
61
+
62
+ USM_AUTH_KU_LEN = 32
63
+ USM_PRIV_KU_LEN = 32
64
+
65
+ SNMP_SEC_LEVEL_NOAUTH = 1
66
+ SNMP_SEC_LEVEL_AUTHNOPRIV = 2
67
+ SNMP_SEC_LEVEL_AUTHPRIV = 3
68
+
69
+ SNMP_DEFAULT_COMMUNITY_LEN = 0
70
+ SNMP_DEFAULT_RETRIES = -1
71
+ SNMP_DEFAULT_TIMEOUT = -1
72
+ SNMP_DEFAULT_REMPORT = 0
73
+ SNMP_DEFAULT_REQID = -1
74
+ SNMP_DEFAULT_MSGID = -1
75
+ SNMP_DEFAULT_ERRSTAT = -1
76
+ SNMP_DEFAULT_ERRINDEX = -1
77
+ SNMP_DEFAULT_ADDRESS = 0
78
+ SNMP_DEFAULT_ENTERPRISE_LENGTH = 0
79
+ SNMP_DEFAULT_TIME = 0
80
+ SNMP_DEFAULT_VERSION = -1
81
+ SNMP_DEFAULT_SECMODEL = -1
82
+ SNMP_DEFAULT_CONTEXT =
83
+
84
+ SNMP_MAX_MSG_SIZE = 1472
85
+ SNMP_MAX_MSG_V3_HDRS = (4+3+4+7+7+3+7+16)
86
+ SNMP_MAX_ENG_SIZE = 32
87
+ SNMP_MAX_SEC_NAME_SIZE = 256
88
+ SNMP_MAX_CONTEXT_SIZE = 256
89
+ SNMP_SEC_PARAM_BUF_SIZE = 256
90
+ SNMPV3_IGNORE_UNAUTH_REPORTS = 0
91
+ SNMP_SESS_NONAUTHORITATIVE = 0
92
+ SNMP_SESS_AUTHORITATIVE = 1
93
+ SNMP_SESS_UNKNOWNAUTH = 2
94
+ REPORT_STATS_LEN = 9
95
+ REPORT_snmpUnknownSecurityModels_NUM = 1
96
+ REPORT_snmpInvalidMsgs_NUM = 2
97
+ REPORT_usmStatsUnsupportedSecLevels_NUM = 1
98
+ REPORT_usmStatsNotInTimeWindows_NUM = 2
99
+ REPORT_usmStatsUnknownUserNames_NUM = 3
100
+ REPORT_usmStatsUnknownEngineIDs_NUM = 4
101
+ REPORT_usmStatsWrongDigests_NUM = 5
102
+ REPORT_usmStatsDecryptionErrors_NUM = 6
103
+ SNMP_DETAIL_SIZE = 512
104
+ SNMP_FLAGS_RESP_CALLBACK = 0x400
105
+ SNMP_FLAGS_USER_CREATED = 0x200
106
+ SNMP_FLAGS_DONT_PROBE = 0x100
107
+ SNMP_FLAGS_STREAM_SOCKET = 0x80
108
+ SNMP_FLAGS_LISTENING = 0x40
109
+ SNMP_FLAGS_SUBSESSION = 0x20
110
+ SNMP_FLAGS_STRIKE2 = 0x02
111
+ SNMP_FLAGS_STRIKE1 = 0x01
112
+ SNMPERR_SUCCESS = (0)
113
+ SNMPERR_GENERR = (-1)
114
+ SNMPERR_BAD_LOCPORT = (-2)
115
+ SNMPERR_BAD_ADDRESS = (-3)
116
+ SNMPERR_BAD_SESSION = (-4)
117
+ SNMPERR_TOO_LONG = (-5)
118
+ SNMPERR_NO_SOCKET = (-6)
119
+ SNMPERR_V2_IN_V1 = (-7)
120
+ SNMPERR_V1_IN_V2 = (-8)
121
+ SNMPERR_BAD_REPEATERS = (-9)
122
+ SNMPERR_BAD_REPETITIONS = (-10)
123
+ SNMPERR_BAD_ASN1_BUILD = (-11)
124
+ SNMPERR_BAD_SENDTO = (-12)
125
+ SNMPERR_BAD_PARSE = (-13)
126
+ SNMPERR_BAD_VERSION = (-14)
127
+ SNMPERR_BAD_SRC_PARTY = (-15)
128
+ SNMPERR_BAD_DST_PARTY = (-16)
129
+ SNMPERR_BAD_CONTEXT = (-17)
130
+ SNMPERR_BAD_COMMUNITY = (-18)
131
+ SNMPERR_NOAUTH_DESPRIV = (-19)
132
+ SNMPERR_BAD_ACL = (-20)
133
+ SNMPERR_BAD_PARTY = (-21)
134
+ SNMPERR_ABORT = (-22)
135
+ SNMPERR_UNKNOWN_PDU = (-23)
136
+ SNMPERR_TIMEOUT = (-24)
137
+ SNMPERR_BAD_RECVFROM = (-25)
138
+ SNMPERR_BAD_ENG_ID = (-26)
139
+ SNMPERR_BAD_SEC_NAME = (-27)
140
+ SNMPERR_BAD_SEC_LEVEL = (-28)
141
+ SNMPERR_ASN_PARSE_ERR = (-29)
142
+ SNMPERR_UNKNOWN_SEC_MODEL = (-30)
143
+ SNMPERR_INVALID_MSG = (-31)
144
+ SNMPERR_UNKNOWN_ENG_ID = (-32)
145
+ SNMPERR_UNKNOWN_USER_NAME = (-33)
146
+ SNMPERR_UNSUPPORTED_SEC_LEVEL = (-34)
147
+ SNMPERR_AUTHENTICATION_FAILURE = (-35)
148
+ SNMPERR_NOT_IN_TIME_WINDOW = (-36)
149
+ SNMPERR_DECRYPTION_ERR = (-37)
150
+ SNMPERR_SC_GENERAL_FAILURE = (-38)
151
+ SNMPERR_SC_NOT_CONFIGURED = (-39)
152
+ SNMPERR_KT_NOT_AVAILABLE = (-40)
153
+ SNMPERR_UNKNOWN_REPORT = (-41)
154
+ SNMPERR_USM_GENERICERROR = (-42)
155
+ SNMPERR_USM_UNKNOWNSECURITYNAME = (-43)
156
+ SNMPERR_USM_UNSUPPORTEDSECURITYLEVEL = (-44)
157
+ SNMPERR_USM_ENCRYPTIONERROR = (-45)
158
+ SNMPERR_USM_AUTHENTICATIONFAILURE = (-46)
159
+ SNMPERR_USM_PARSEERROR = (-47)
160
+ SNMPERR_USM_UNKNOWNENGINEID = (-48)
161
+ SNMPERR_USM_NOTINTIMEWINDOW = (-49)
162
+ SNMPERR_USM_DECRYPTIONERROR = (-50)
163
+ SNMPERR_NOMIB = (-51)
164
+ SNMPERR_RANGE = (-52)
165
+ SNMPERR_MAX_SUBID = (-53)
166
+ SNMPERR_BAD_SUBID = (-54)
167
+ SNMPERR_LONG_OID = (-55)
168
+ SNMPERR_BAD_NAME = (-56)
169
+ SNMPERR_VALUE = (-57)
170
+ SNMPERR_UNKNOWN_OBJID = (-58)
171
+ SNMPERR_NULL_PDU = (-59)
172
+ SNMPERR_NO_VARS = (-60)
173
+ SNMPERR_VAR_TYPE = (-61)
174
+ SNMPERR_MALLOC = (-62)
175
+ SNMPERR_KRB5 = (-63)
176
+ SNMPERR_PROTOCOL = (-64)
177
+ SNMPERR_OID_NONINCREASING = (-65)
178
+ SNMPERR_MAX = (-65)
179
+
180
+
181
+ STAT_SNMPUNKNOWNSECURITYMODELS = 0
182
+ STAT_SNMPINVALIDMSGS = 1
183
+ STAT_SNMPUNKNOWNPDUHANDLERS = 2
184
+ STAT_MPD_STATS_START = 0
185
+ STAT_MPD_STATS_END = 2
186
+ STAT_USMSTATSUNSUPPORTEDSECLEVELS = 3
187
+ STAT_USMSTATSNOTINTIMEWINDOWS = 4
188
+ STAT_USMSTATSUNKNOWNUSERNAMES = 5
189
+ STAT_USMSTATSUNKNOWNENGINEIDS = 6
190
+ STAT_USMSTATSWRONGDIGESTS = 7
191
+ STAT_USMSTATSDECRYPTIONERRORS = 8
192
+ STAT_USM_STATS_START = 3
193
+ STAT_USM_STATS_END = 8
194
+ STAT_SNMPINPKTS = 9
195
+ STAT_SNMPOUTPKTS = 10
196
+ STAT_SNMPINBADVERSIONS = 11
197
+ STAT_SNMPINBADCOMMUNITYNAMES = 12
198
+ STAT_SNMPINBADCOMMUNITYUSES = 13
199
+ STAT_SNMPINASNPARSEERRS = 14
200
+ STAT_SNMPINTOOBIGS = 16
201
+ STAT_SNMPINNOSUCHNAMES = 17
202
+ STAT_SNMPINBADVALUES = 18
203
+ STAT_SNMPINREADONLYS = 19
204
+ STAT_SNMPINGENERRS = 20
205
+ STAT_SNMPINTOTALREQVARS = 21
206
+ STAT_SNMPINTOTALSETVARS = 22
207
+ STAT_SNMPINGETREQUESTS = 23
208
+ STAT_SNMPINGETNEXTS = 24
209
+ STAT_SNMPINSETREQUESTS = 25
210
+ STAT_SNMPINGETRESPONSES = 26
211
+ STAT_SNMPINTRAPS = 27
212
+ STAT_SNMPOUTTOOBIGS = 28
213
+ STAT_SNMPOUTNOSUCHNAMES = 29
214
+ STAT_SNMPOUTBADVALUES = 30
215
+ STAT_SNMPOUTGENERRS = 32
216
+ STAT_SNMPOUTGETREQUESTS = 33
217
+ STAT_SNMPOUTGETNEXTS = 34
218
+ STAT_SNMPOUTSETREQUESTS = 35
219
+ STAT_SNMPOUTGETRESPONSES = 36
220
+ STAT_SNMPOUTTRAPS = 37
221
+ STAT_SNMPSILENTDROPS = 39
222
+ STAT_SNMPPROXYDROPS = 40
223
+ STAT_SNMP_STATS_START = 9
224
+ STAT_SNMP_STATS_END = 40
225
+ STAT_SNMPUNAVAILABLECONTEXTS = 41
226
+ STAT_SNMPUNKNOWNCONTEXTS = 42
227
+ STAT_TARGET_STATS_START = 41
228
+ STAT_TARGET_STATS_END = 42
229
+ MAX_STATS = 43
230
+ PARSE_PACKET = 0
231
+ DUMP_PACKET = 1
232
+ MAX_SUBID = 0xFFFFFFFF
233
+ MIN_OID_LEN = 2
234
+
235
+ MAX_NAME_LEN = 128
236
+
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,23 @@
1
+ module Net
2
+ module SNMP
3
+ class Error < RuntimeError
4
+ #attr :status, :errno, :snmp_err, :snmp_msg
5
+
6
+ def initialize(opts = {})
7
+ @status = opts[:status]
8
+ if opts[:session]
9
+ errno_ptr = FFI::MemoryPointer.new(:int)
10
+ snmp_err_ptr = FFI::MemoryPointer.new(:int)
11
+ msg_ptr = FFI::MemoryPointer.new(:pointer)
12
+ Wrapper.snmp_error(opts[:session].pointer, errno_ptr, snmp_err_ptr, msg_ptr)
13
+
14
+ @errno = errno_ptr.read_int
15
+ @snmp_err = snmp_err_ptr.read_int
16
+ @snmp_msg = msg_ptr.read_pointer.read_string
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+
@@ -0,0 +1,39 @@
1
+ module Net
2
+ module SNMP
3
+ module Inline
4
+ extend Inliner
5
+
6
+ #module LibC
7
+ # extend FFI::Library
8
+ # ffi_lib 'C'
9
+ # attach_function :malloc, [ :uint ], :pointer
10
+ # attach_function :free, [ :pointer ], :void
11
+ #end
12
+
13
+ inline do |builder|
14
+ builder.include "sys/select.h"
15
+ builder.include "stdio.h"
16
+ builder.library "netsnmp"
17
+ builder.c %q{
18
+ int snmp_process_callbacks() {
19
+ int fds = 0, block = 1;
20
+ fd_set fdset;
21
+ struct timeval timeout;
22
+ FD_ZERO(&fdset);
23
+ snmp_select_info(&fds, &fdset, &timeout, &block);
24
+ fds = select(fds, &fdset, 0,0, block ? 0 : &timeout);
25
+ if(fds) {
26
+ snmp_read(&fdset);
27
+ } else {
28
+ snmp_timeout();
29
+ }
30
+ }
31
+ }
32
+
33
+ end
34
+
35
+
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ module Net
2
+ module SNMP
3
+ module MIB
4
+ def self.init
5
+ Wrapper.init_mib
6
+ end
7
+
8
+ def self.read_all_mibs
9
+ Wrapper.read_all_mibs
10
+ end
11
+
12
+ def self.get_node(oid)
13
+ Node.get_node(oid)
14
+ end
15
+
16
+
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,58 @@
1
+
2
+ module Net::SNMP
3
+ module MIB
4
+ class Node
5
+ extend Forwardable
6
+ attr_accessor :struct
7
+ def_delegators :struct, :label, :type, :access, :status
8
+
9
+ class << self
10
+ def get_node(oid)
11
+ oid_ptr, oid_len_ptr = Net::SNMP._get_oid(oid)
12
+ struct = Wrapper.get_tree(oid_ptr, oid_len_ptr.read_int, Wrapper.get_tree_head().pointer)
13
+ new(struct.pointer)
14
+ end
15
+ end
16
+
17
+ def initialize(arg)
18
+ case arg
19
+ when FFI::Pointer
20
+ @struct = Wrapper::Tree.new(arg)
21
+ else
22
+ raise "invalid type"
23
+ end
24
+ end
25
+
26
+ def oid
27
+ return @oid if @oid
28
+ @oid = Net::SNMP.get_oid(label)
29
+ end
30
+
31
+ # actually seems like list is linked backward, so this will retrieve the previous oid numerically
32
+ def next
33
+ return nil unless @struct.next_peer
34
+ self.class.new(@struct.next_peer)
35
+ end
36
+
37
+ def parent
38
+ return nil unless @struct.parent
39
+ self.class.new(@struct.parent)
40
+ end
41
+
42
+ def children
43
+ return nil unless @struct.child_list
44
+ child = self.class.new(@struct.child_list)
45
+ children = [child]
46
+ while child = child.next
47
+ children << child
48
+ end
49
+ children.reverse # For some reason, net-snmp returns everything backwards
50
+ end
51
+
52
+ def siblings
53
+ parent.children
54
+ end
55
+
56
+ end
57
+ end
58
+ end