eventmachine_snmpagent 0.0.1

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/COPYING ADDED
@@ -0,0 +1,281 @@
1
+ .
2
+
3
+ GNU GENERAL PUBLIC LICENSE
4
+ Version 2, June 1991
5
+
6
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
7
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8
+ Everyone is permitted to copy and distribute verbatim copies
9
+ of this license document, but changing it is not allowed.
10
+
11
+ Preamble
12
+
13
+ The licenses for most software are designed to take away your
14
+ freedom to share and change it. By contrast, the GNU General Public
15
+ License is intended to guarantee your freedom to share and change free
16
+ software--to make sure the software is free for all its users. This
17
+ General Public License applies to most of the Free Software
18
+ Foundation's software and to any other program whose authors commit to
19
+ using it. (Some other Free Software Foundation software is covered by
20
+ the GNU Lesser General Public License instead.) You can apply it to
21
+ your programs, too.
22
+
23
+ When we speak of free software, we are referring to freedom, not
24
+ price. Our General Public Licenses are designed to make sure that you
25
+ have the freedom to distribute copies of free software (and charge for
26
+ this service if you wish), that you receive source code or can get it
27
+ if you want it, that you can change the software or use pieces of it
28
+ in new free programs; and that you know you can do these things.
29
+
30
+ To protect your rights, we need to make restrictions that forbid
31
+ anyone to deny you these rights or to ask you to surrender the rights.
32
+ These restrictions translate to certain responsibilities for you if you
33
+ distribute copies of the software, or if you modify it.
34
+
35
+ For example, if you distribute copies of such a program, whether
36
+ gratis or for a fee, you must give the recipients all the rights that
37
+ you have. You must make sure that they, too, receive or can get the
38
+ source code. And you must show them these terms so they know their
39
+ rights.
40
+
41
+ We protect your rights with two steps: (1) copyright the software, and
42
+ (2) offer you this license which gives you legal permission to copy,
43
+ distribute and/or modify the software.
44
+
45
+ Also, for each author's protection and ours, we want to make certain
46
+ that everyone understands that there is no warranty for this free
47
+ software. If the software is modified by someone else and passed on, we
48
+ want its recipients to know that what they have is not the original, so
49
+ that any problems introduced by others will not reflect on the original
50
+ authors' reputations.
51
+
52
+ Finally, any free program is threatened constantly by software
53
+ patents. We wish to avoid the danger that redistributors of a free
54
+ program will individually obtain patent licenses, in effect making the
55
+ program proprietary. To prevent this, we have made it clear that any
56
+ patent must be licensed for everyone's free use or not licensed at all.
57
+
58
+ The precise terms and conditions for copying, distribution and
59
+ modification follow.
60
+
61
+ GNU GENERAL PUBLIC LICENSE
62
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
63
+
64
+ 0. This License applies to any program or other work which contains
65
+ a notice placed by the copyright holder saying it may be distributed
66
+ under the terms of this General Public License. The "Program", below,
67
+ refers to any such program or work, and a "work based on the Program"
68
+ means either the Program or any derivative work under copyright law:
69
+ that is to say, a work containing the Program or a portion of it,
70
+ either verbatim or with modifications and/or translated into another
71
+ language. (Hereinafter, translation is included without limitation in
72
+ the term "modification".) Each licensee is addressed as "you".
73
+
74
+ Activities other than copying, distribution and modification are not
75
+ covered by this License; they are outside its scope. The act of
76
+ running the Program is not restricted, and the output from the Program
77
+ is covered only if its contents constitute a work based on the
78
+ Program (independent of having been made by running the Program).
79
+ Whether that is true depends on what the Program does.
80
+
81
+ 1. You may copy and distribute verbatim copies of the Program's
82
+ source code as you receive it, in any medium, provided that you
83
+ conspicuously and appropriately publish on each copy an appropriate
84
+ copyright notice and disclaimer of warranty; keep intact all the
85
+ notices that refer to this License and to the absence of any warranty;
86
+ and give any other recipients of the Program a copy of this License
87
+ along with the Program.
88
+
89
+ You may charge a fee for the physical act of transferring a copy, and
90
+ you may at your option offer warranty protection in exchange for a fee.
91
+
92
+ 2. You may modify your copy or copies of the Program or any portion
93
+ of it, thus forming a work based on the Program, and copy and
94
+ distribute such modifications or work under the terms of Section 1
95
+ above, provided that you also meet all of these conditions:
96
+
97
+ a) You must cause the modified files to carry prominent notices
98
+ stating that you changed the files and the date of any change.
99
+
100
+ b) You must cause any work that you distribute or publish, that in
101
+ whole or in part contains or is derived from the Program or any
102
+ part thereof, to be licensed as a whole at no charge to all third
103
+ parties under the terms of this License.
104
+
105
+ c) If the modified program normally reads commands interactively
106
+ when run, you must cause it, when started running for such
107
+ interactive use in the most ordinary way, to print or display an
108
+ announcement including an appropriate copyright notice and a
109
+ notice that there is no warranty (or else, saying that you provide
110
+ a warranty) and that users may redistribute the program under
111
+ these conditions, and telling the user how to view a copy of this
112
+ License. (Exception: if the Program itself is interactive but
113
+ does not normally print such an announcement, your work based on
114
+ the Program is not required to print an announcement.)
115
+
116
+ These requirements apply to the modified work as a whole. If
117
+ identifiable sections of that work are not derived from the Program,
118
+ and can be reasonably considered independent and separate works in
119
+ themselves, then this License, and its terms, do not apply to those
120
+ sections when you distribute them as separate works. But when you
121
+ distribute the same sections as part of a whole which is a work based
122
+ on the Program, the distribution of the whole must be on the terms of
123
+ this License, whose permissions for other licensees extend to the
124
+ entire whole, and thus to each and every part regardless of who wrote it.
125
+
126
+ Thus, it is not the intent of this section to claim rights or contest
127
+ your rights to work written entirely by you; rather, the intent is to
128
+ exercise the right to control the distribution of derivative or
129
+ collective works based on the Program.
130
+
131
+ In addition, mere aggregation of another work not based on the Program
132
+ with the Program (or with a work based on the Program) on a volume of
133
+ a storage or distribution medium does not bring the other work under
134
+ the scope of this License.
135
+
136
+ 3. You may copy and distribute the Program (or a work based on it,
137
+ under Section 2) in object code or executable form under the terms of
138
+ Sections 1 and 2 above provided that you also do one of the following:
139
+
140
+ a) Accompany it with the complete corresponding machine-readable
141
+ source code, which must be distributed under the terms of Sections
142
+ 1 and 2 above on a medium customarily used for software interchange; or,
143
+
144
+ b) Accompany it with a written offer, valid for at least three
145
+ years, to give any third party, for a charge no more than your
146
+ cost of physically performing source distribution, a complete
147
+ machine-readable copy of the corresponding source code, to be
148
+ distributed under the terms of Sections 1 and 2 above on a medium
149
+ customarily used for software interchange; or,
150
+
151
+ c) Accompany it with the information you received as to the offer
152
+ to distribute corresponding source code. (This alternative is
153
+ allowed only for noncommercial distribution and only if you
154
+ received the program in object code or executable form with such
155
+ an offer, in accord with Subsection b above.)
156
+
157
+ The source code for a work means the preferred form of the work for
158
+ making modifications to it. For an executable work, complete source
159
+ code means all the source code for all modules it contains, plus any
160
+ associated interface definition files, plus the scripts used to
161
+ control compilation and installation of the executable. However, as a
162
+ special exception, the source code distributed need not include
163
+ anything that is normally distributed (in either source or binary
164
+ form) with the major components (compiler, kernel, and so on) of the
165
+ operating system on which the executable runs, unless that component
166
+ itself accompanies the executable.
167
+
168
+ If distribution of executable or object code is made by offering
169
+ access to copy from a designated place, then offering equivalent
170
+ access to copy the source code from the same place counts as
171
+ distribution of the source code, even though third parties are not
172
+ compelled to copy the source along with the object code.
173
+
174
+ 4. You may not copy, modify, sublicense, or distribute the Program
175
+ except as expressly provided under this License. Any attempt
176
+ otherwise to copy, modify, sublicense or distribute the Program is
177
+ void, and will automatically terminate your rights under this License.
178
+ However, parties who have received copies, or rights, from you under
179
+ this License will not have their licenses terminated so long as such
180
+ parties remain in full compliance.
181
+
182
+ 5. You are not required to accept this License, since you have not
183
+ signed it. However, nothing else grants you permission to modify or
184
+ distribute the Program or its derivative works. These actions are
185
+ prohibited by law if you do not accept this License. Therefore, by
186
+ modifying or distributing the Program (or any work based on the
187
+ Program), you indicate your acceptance of this License to do so, and
188
+ all its terms and conditions for copying, distributing or modifying
189
+ the Program or works based on it.
190
+
191
+ 6. Each time you redistribute the Program (or any work based on the
192
+ Program), the recipient automatically receives a license from the
193
+ original licensor to copy, distribute or modify the Program subject to
194
+ these terms and conditions. You may not impose any further
195
+ restrictions on the recipients' exercise of the rights granted herein.
196
+ You are not responsible for enforcing compliance by third parties to
197
+ this License.
198
+
199
+ 7. If, as a consequence of a court judgment or allegation of patent
200
+ infringement or for any other reason (not limited to patent issues),
201
+ conditions are imposed on you (whether by court order, agreement or
202
+ otherwise) that contradict the conditions of this License, they do not
203
+ excuse you from the conditions of this License. If you cannot
204
+ distribute so as to satisfy simultaneously your obligations under this
205
+ License and any other pertinent obligations, then as a consequence you
206
+ may not distribute the Program at all. For example, if a patent
207
+ license would not permit royalty-free redistribution of the Program by
208
+ all those who receive copies directly or indirectly through you, then
209
+ the only way you could satisfy both it and this License would be to
210
+ refrain entirely from distribution of the Program.
211
+
212
+ If any portion of this section is held invalid or unenforceable under
213
+ any particular circumstance, the balance of the section is intended to
214
+ apply and the section as a whole is intended to apply in other
215
+ circumstances.
216
+
217
+ It is not the purpose of this section to induce you to infringe any
218
+ patents or other property right claims or to contest validity of any
219
+ such claims; this section has the sole purpose of protecting the
220
+ integrity of the free software distribution system, which is
221
+ implemented by public license practices. Many people have made
222
+ generous contributions to the wide range of software distributed
223
+ through that system in reliance on consistent application of that
224
+ system; it is up to the author/donor to decide if he or she is willing
225
+ to distribute software through any other system and a licensee cannot
226
+ impose that choice.
227
+
228
+ This section is intended to make thoroughly clear what is believed to
229
+ be a consequence of the rest of this License.
230
+
231
+ 8. If the distribution and/or use of the Program is restricted in
232
+ certain countries either by patents or by copyrighted interfaces, the
233
+ original copyright holder who places the Program under this License
234
+ may add an explicit geographical distribution limitation excluding
235
+ those countries, so that distribution is permitted only in or among
236
+ countries not thus excluded. In such case, this License incorporates
237
+ the limitation as if written in the body of this License.
238
+
239
+ 9. The Free Software Foundation may publish revised and/or new versions
240
+ of the General Public License from time to time. Such new versions will
241
+ be similar in spirit to the present version, but may differ in detail to
242
+ address new problems or concerns.
243
+
244
+ Each version is given a distinguishing version number. If the Program
245
+ specifies a version number of this License which applies to it and "any
246
+ later version", you have the option of following the terms and conditions
247
+ either of that version or of any later version published by the Free
248
+ Software Foundation. If the Program does not specify a version number of
249
+ this License, you may choose any version ever published by the Free Software
250
+ Foundation.
251
+
252
+ 10. If you wish to incorporate parts of the Program into other free
253
+ programs whose distribution conditions are different, write to the author
254
+ to ask for permission. For software which is copyrighted by the Free
255
+ Software Foundation, write to the Free Software Foundation; we sometimes
256
+ make exceptions for this. Our decision will be guided by the two goals
257
+ of preserving the free status of all derivatives of our free software and
258
+ of promoting the sharing and reuse of software generally.
259
+
260
+ NO WARRANTY
261
+
262
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
263
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
264
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
265
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
266
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
267
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
268
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
269
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
270
+ REPAIR OR CORRECTION.
271
+
272
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
273
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
274
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
275
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
276
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
277
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
278
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
279
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
280
+ POSSIBILITY OF SUCH DAMAGES.
281
+
data/README ADDED
@@ -0,0 +1,4 @@
1
+ $Id$
2
+
3
+ EventMachine::SnmpAgent preliminary release.
4
+
@@ -0,0 +1,4 @@
1
+ EventMachine::SnmpAgent
2
+
3
+ 0.0.1: 14Apr07
4
+
@@ -0,0 +1,32 @@
1
+ # $Id: snmpagent.rb 4087 2007-04-14 19:20:01Z francis $
2
+ #
3
+ #
4
+ #----------------------------------------------------------------------------
5
+ #
6
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # Gmail: garbagecat10 --> gmail
9
+ #
10
+ # This program is free software; you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation; either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this program; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
+ #
24
+ #---------------------------------------------------------------------------
25
+ #
26
+
27
+ require 'snmpagent/agent'
28
+ require 'snmpagent/miblist'
29
+ require 'snmpagent/mibclass'
30
+
31
+
32
+
@@ -0,0 +1,190 @@
1
+ # $Id: agent.rb 4089 2007-04-15 05:37:27Z francis $
2
+ #
3
+ #
4
+ #----------------------------------------------------------------------------
5
+ #
6
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # Gmail: garbagecat10 --> gmail
9
+ #
10
+ # This program is free software; you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation; either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this program; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
+ #
24
+ #---------------------------------------------------------------------------
25
+ #
26
+
27
+
28
+
29
+ require 'ostruct'
30
+
31
+
32
+ module EventMachine
33
+ module SnmpAgent
34
+
35
+ class ProtocolHandler < EventMachine::Connection
36
+ def initialize *args
37
+ super
38
+ end
39
+ def set_handler handler
40
+ @handler = handler
41
+ end
42
+ def receive_data data
43
+ pdu = Net::SnmpPdu.parse(data.read_ber(Net::SNMP::AsnSyntax))
44
+ result = @handler.process_request pdu.pdu_type, pdu.community, pdu.variables
45
+ if result.error_status
46
+ resp = Net::SnmpPdu.new
47
+ resp.pdu_type = :get_response
48
+ resp.request_id = pdu.request_id
49
+ resp.error_status = result.error_status
50
+ resp.error_index = result.error_index if result.error_index
51
+ if result.variables
52
+ result.variables.each {|name, value| resp.add_variable_binding name, value}
53
+ end
54
+ send_data resp.to_ber_string
55
+ end
56
+ end
57
+ end
58
+
59
+ class Agent
60
+ extend Forwardable
61
+
62
+ def_delegator :@miblist, :add_object
63
+ def_delegator :@miblist, :get, :get_object
64
+ def_delegator :@miblist, :get_next, :get_next_object
65
+
66
+ def initialize args={}
67
+ @host = args[:host] || "0.0.0.0"
68
+ @port = args[:port] || 161
69
+ @community = args[:community] || "public"
70
+ @effective_user = args[:effective_user] # default nil
71
+ @verbose = args[:verbose] || false
72
+
73
+ r = (args[:recalculate_interval] || 15).to_i
74
+ if (r > 0)
75
+ add_hook proc {EventMachine.add_periodic_timer(r) {self.recalculate_values } }
76
+ end
77
+
78
+ @miblist = MibList.new
79
+ @mibclasses = []
80
+
81
+ mibs = args[:mib] || []
82
+ mibs = [mibs] unless mibs.respond_to?(:each)
83
+ mibs.each {|mib|
84
+ # classes are modules, but modules aren't classes.
85
+ # If we get a class, we EXPECT it to be a subclass of
86
+ # EventMachine::SnmpAgent::Mib. We *instantiate* the
87
+ # class, and call its methods for each individual OID
88
+ # processor that is defined as taking a symbol.
89
+ # If we get a Module, extend ourself with it.
90
+ # The module will be expected to make add_object calls.
91
+ if mib.respond_to?(:new)
92
+ m = mib.new
93
+ @mibclasses << m
94
+ this = self
95
+ mib.instance_eval {
96
+ (@my_user_defined_oids ||= []).each {|oid,processor|
97
+ pr = if processor.is_a?(Symbol)
98
+ proc {m.send processor}
99
+ else
100
+ processor
101
+ end
102
+ this.add_object oid, pr
103
+ }
104
+ }
105
+ else
106
+ self.extend mib
107
+ end
108
+ }
109
+ end
110
+
111
+ # This is called on an EventMachine periodic timer, installed as a hook
112
+ # if the caller specifies it. It will call the #recalculate_values method
113
+ # of each user-defined MIB class. Default value is every fifteen seconds.
114
+ # User code can disable the hook altogether by specifying a :recalculate_interval
115
+ # of 0.
116
+ def recalculate_values
117
+ @mibclasses.each {|m| m.recalculate_values}
118
+ end
119
+
120
+ def add_hook hookproc
121
+ @hooks ||= []
122
+ @hooks << hookproc
123
+ end
124
+
125
+ def run
126
+ EventMachine.run {
127
+ EventMachine.open_datagram_socket(@host, @port, ProtocolHandler) {|c|
128
+ c.set_handler self # probably could have a used a delegate
129
+ }
130
+ if @effective_user
131
+ EventMachine.set_effective_user @effective_user
132
+ end
133
+ @hooks and @hooks.each {|h| h.call}
134
+ if @verbose
135
+ EventMachine.add_periodic_timer(60) {$>.puts "bayshore-snmp-agent #{Time.now}"}
136
+ end
137
+ }
138
+ end
139
+
140
+ #--
141
+ # return an openstruct to indicate the results of the operation.
142
+ # DO NOT set that error_status member of the output struct unless we can
143
+ # actually interpret the request. A protocol-handler should deal with
144
+ # such a case by ignoring the request.
145
+ def process_request req_type, community, variables = []
146
+
147
+ @verbose and $>.puts "Received #{req_type} for #{variables.map {|v| v.first}.join(",")}"
148
+
149
+ os = OpenStruct.new
150
+ return os unless community == @community
151
+
152
+ case req_type
153
+ when :get_request
154
+ os.error_status = 0 # SNMP noError
155
+ variables.each_with_index {|v, ix|
156
+ if hndlr = get_object(v.first)
157
+ os.variables ||= []
158
+ #value = hndlr.respond_to?(:call) ? hndlr.call : hndlr
159
+ os.variables << [v.first, hndlr]
160
+ @verbose and $>.puts "Fulfilled #{v.first.join(".")}::=#{hndlr.inspect}"
161
+ else
162
+ os.variables = nil
163
+ os.error_status = 2 # SNMP noSuchName
164
+ os.error_index = ix
165
+ break
166
+ end
167
+ }
168
+ when :get_next_request
169
+ os.error_status = 0 # SNMP noError
170
+ variables.each_with_index {|v, ix|
171
+ oid,handler = get_next_object(v.first)
172
+ if oid
173
+ os.variables ||= []
174
+ #value = handler.respond_to?(:call) ? handler.call : handler
175
+ @verbose and $>.puts "Fulfilled #{oid.join(".")}::=#{handler.inspect}"
176
+ os.variables << [oid, handler]
177
+ else
178
+ os.variables = nil
179
+ os.error_status = 2 # SNMP noSuchName
180
+ os.error_index = ix
181
+ break
182
+ end
183
+ }
184
+ end
185
+ os
186
+ end
187
+ end
188
+ end
189
+ end
190
+
@@ -0,0 +1,49 @@
1
+ # $Id: mibclass.rb 4089 2007-04-15 05:37:27Z francis $
2
+ #
3
+ #
4
+ #
5
+ #----------------------------------------------------------------------------
6
+ #
7
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
8
+ #
9
+ # Gmail: garbagecat10 --> gmail
10
+ #
11
+ # This program is free software; you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # This program is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program; if not, write to the Free Software
23
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+ #
25
+ #---------------------------------------------------------------------------
26
+ #
27
+
28
+
29
+
30
+ module EventMachine
31
+ module SnmpAgent
32
+
33
+ class Mib
34
+ class << self
35
+ def oid oidname, processor
36
+ @my_user_defined_oids ||= []
37
+ @my_user_defined_oids << [oidname, processor]
38
+ end
39
+ end
40
+
41
+ # This is intended to be overridden by user code, in case they
42
+ # want to recalculate values periodically rather than on demand.
43
+ def recalculate_values
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+
@@ -0,0 +1,81 @@
1
+ # $Id: miblist.rb 4087 2007-04-14 19:20:01Z francis $
2
+ #
3
+ #
4
+ #
5
+ #----------------------------------------------------------------------------
6
+ #
7
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
8
+ #
9
+ # Gmail: garbagecat10 --> gmail
10
+ #
11
+ # This program is free software; you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # This program is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program; if not, write to the Free Software
23
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+ #
25
+ #---------------------------------------------------------------------------
26
+ #
27
+
28
+
29
+ require 'set'
30
+
31
+ module EventMachine
32
+ module SnmpAgent
33
+
34
+ class MibList
35
+ def initialize
36
+ @hash = {}
37
+ @set = SortedSet.new
38
+ @successors = {}
39
+ end
40
+
41
+ def add_object oid, value
42
+ if oid.is_a?(String)
43
+ oid = oid.split(".").map {|a| a.to_i}
44
+ end
45
+
46
+ @set.add oid
47
+ @hash [oid] = value
48
+
49
+ @successors.clear
50
+ prev = nil
51
+ @set.each {|s|
52
+ if prev
53
+ @successors[prev] = s
54
+ end
55
+ prev = s
56
+ }
57
+ end
58
+
59
+
60
+ def get oid
61
+ h = @hash [oid]
62
+ h.respond_to?(:call) ? h.call : h
63
+ end
64
+
65
+ def get_next oid
66
+ nextoid = if @successors.member?(oid)
67
+ @successors[oid]
68
+ else
69
+ # Linear search.
70
+ @set.find {|s| (oid <=> s) < 0 }
71
+ end
72
+
73
+ if nextoid
74
+ [nextoid, get(nextoid)]
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+
@@ -0,0 +1,137 @@
1
+ # $Id: agent.rb 4515 2007-06-23 21:51:44Z francis $
2
+ #
3
+ #
4
+ #
5
+ #----------------------------------------------------------------------------
6
+ #
7
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
8
+ #
9
+ # Gmail: garbagecat10 --> gmail
10
+ #
11
+ # This program is free software; you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # This program is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program; if not, write to the Free Software
23
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+ #
25
+ #---------------------------------------------------------------------------
26
+ #
27
+
28
+
29
+ $VERBOSE = true
30
+
31
+ $:.unshift( File.dirname(__FILE__) + '/../lib' )
32
+
33
+ require 'rubygems'
34
+ require 'eventmachine'
35
+ require 'net/snmp'
36
+ require 'snmpagent'
37
+ require 'socket'
38
+
39
+
40
+ require 'test/unit/testcase'
41
+
42
+ include EventMachine::SnmpAgent
43
+
44
+ #--------------------------------------------
45
+
46
+
47
+ class TestAgent < Test::Unit::TestCase
48
+
49
+ def test_get_request
50
+ retrieved_value = nil
51
+ Thread.abort_on_exception = true
52
+ pdu = Net::SnmpPdu.new
53
+ pdu.pdu_type = :get_request
54
+ pdu.request_id = 1000
55
+ pdu.community = "public"
56
+ pdu.add_variable_binding [1,3,6,1,2,1,1,1,0]
57
+
58
+ a = Agent.new :host=>"127.0.0.1",
59
+ :port=>16100,
60
+ :community=>"public"
61
+ a.add_object( [1,3,6,1,2,1,1,1,0], "ABCDEFG" )
62
+ a.add_hook proc {
63
+ EventMachine.defer proc {sleep 1}, proc {EventMachine.stop} #auto timeout
64
+ EventMachine.defer proc {
65
+ u = UDPSocket.new
66
+ u.send pdu.to_ber_string, 0, "127.0.0.1", 16100
67
+ resp = Net::SnmpPdu.parse( u.recv(4096).read_ber( Net::SNMP::AsnSyntax ) )
68
+ retrieved_value = resp.variables
69
+ }, proc {
70
+ EventMachine.stop
71
+ }
72
+ }
73
+ a.run
74
+ assert_equal( [[[1,3,6,1,2,1,1,1,0],"ABCDEFG"]], retrieved_value )
75
+ end
76
+
77
+ def test_get_next_request
78
+ retrieved_value = nil
79
+ Thread.abort_on_exception = true
80
+ pdu = Net::SnmpPdu.new
81
+ pdu.pdu_type = :get_next_request
82
+ pdu.request_id = 1000
83
+ pdu.community = "public"
84
+ pdu.add_variable_binding [1,3,6,1,2,1,1] # SNMP MIB2::system
85
+
86
+ a = Agent.new :host=>"127.0.0.1",
87
+ :port=>16100,
88
+ :community=>"public"
89
+ a.add_object( [1,3,6,1,2,1,1,1,0], "ABCDEFG" )
90
+ a.add_hook proc {
91
+ EventMachine.defer proc {sleep 1}, proc {EventMachine.stop} #auto timeout
92
+ EventMachine.defer proc {
93
+ u = UDPSocket.new
94
+ u.send pdu.to_ber_string, 0, "127.0.0.1", 16100
95
+ resp = Net::SnmpPdu.parse( u.recv(4096).read_ber( Net::SNMP::AsnSyntax ) )
96
+ retrieved_value = resp.variables
97
+ }, proc {
98
+ EventMachine.stop
99
+ }
100
+ }
101
+ a.run
102
+ assert_equal( [[[1,3,6,1,2,1,1,1,0],"ABCDEFG"]], retrieved_value )
103
+ end
104
+
105
+
106
+ def test_bad_community_string
107
+ retrieved_value = nil
108
+ Thread.abort_on_exception = true
109
+ pdu = Net::SnmpPdu.new
110
+ pdu.pdu_type = :get_next_request
111
+ pdu.request_id = 1000
112
+ pdu.community = "xxxxx"
113
+ pdu.add_variable_binding [1,3,6,1,2,1,1] # SNMP MIB2::system
114
+
115
+ a = Agent.new :host=>"127.0.0.1",
116
+ :port=>16100,
117
+ :community=>"public"
118
+ a.add_object( [1,3,6,1,2,1,1,1,0], "ABCDEFG" )
119
+ a.add_hook proc {
120
+ EventMachine.defer proc {sleep 1}, proc {retrieved_value = :timed_out; EventMachine.stop} #auto timeout
121
+ EventMachine.defer proc {
122
+ u = UDPSocket.new
123
+ u.send pdu.to_ber_string, 0, "127.0.0.1", 16100
124
+ resp = Net::SnmpPdu.parse( u.recv(4096).read_ber( Net::SNMP::AsnSyntax ) )
125
+ retrieved_value = resp.variables
126
+ }, proc {
127
+ EventMachine.stop
128
+ }
129
+ }
130
+ a.run
131
+ assert_equal( :timed_out, retrieved_value )
132
+ end
133
+
134
+
135
+
136
+ end
137
+
@@ -0,0 +1,87 @@
1
+ # $Id: mibclass.rb 4515 2007-06-23 21:51:44Z francis $
2
+ #
3
+ #
4
+ #
5
+ #----------------------------------------------------------------------------
6
+ #
7
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
8
+ #
9
+ # Gmail: garbagecat10 --> gmail
10
+ #
11
+ # This program is free software; you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # This program is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program; if not, write to the Free Software
23
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+ #
25
+ #---------------------------------------------------------------------------
26
+ #
27
+
28
+
29
+ $VERBOSE = true
30
+
31
+ $:.unshift( File.dirname(__FILE__) + '/../lib' )
32
+
33
+ require 'rubygems'
34
+ require 'eventmachine'
35
+ require 'net/snmp'
36
+ require 'snmpagent'
37
+ require 'socket'
38
+
39
+
40
+ require 'test/unit/testcase'
41
+
42
+ include EventMachine::SnmpAgent
43
+
44
+
45
+ #--------------------------------------
46
+
47
+
48
+ class TestMibClass < Test::Unit::TestCase
49
+
50
+ class Small < Mib
51
+
52
+ oid "1.3.6.1.2.1.1.1.0", "macaroni and cheese"
53
+ oid "1.3.6.1.2.1.1.3.0", :uptime
54
+
55
+ def initialize
56
+ super
57
+ @starttime = Time.now
58
+ end
59
+ def uptime
60
+ (Time.now - @starttime).to_i * 100
61
+ end
62
+ end
63
+ def test_a
64
+ a = Agent.new :mib => Small
65
+ end
66
+
67
+
68
+ class NullMib
69
+ end
70
+ def test_null_mib
71
+ a = Agent.new :mib => NullMib
72
+ end
73
+
74
+
75
+ class CycleMib
76
+ def recalculate_values
77
+ # This will automatically be called at intervals
78
+ # specified by the :recalculate_interval parameter
79
+ # given to Agent#new.
80
+ end
81
+ end
82
+ def test_cycle_mib
83
+ a = Agent.new :mib => CycleMib, :recalculate_interval => 1
84
+ end
85
+
86
+ end
87
+
@@ -0,0 +1,96 @@
1
+ # $Id: miblist.rb 4515 2007-06-23 21:51:44Z francis $
2
+ #
3
+ #
4
+ #
5
+ #----------------------------------------------------------------------------
6
+ #
7
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
8
+ #
9
+ # Gmail: garbagecat10 --> gmail
10
+ #
11
+ # This program is free software; you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # This program is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program; if not, write to the Free Software
23
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+ #
25
+ #---------------------------------------------------------------------------
26
+ #
27
+
28
+
29
+ $VERBOSE = true
30
+
31
+ $:.unshift( File.dirname(__FILE__) + '/../lib' )
32
+
33
+ require 'rubygems'
34
+ require 'eventmachine'
35
+ require 'net/snmp'
36
+ require 'snmpagent'
37
+ require 'socket'
38
+
39
+
40
+ require 'test/unit/testcase'
41
+
42
+ include EventMachine::SnmpAgent
43
+
44
+
45
+ #--------------------------------------
46
+
47
+
48
+ class TestMibList < Test::Unit::TestCase
49
+
50
+ def test_a
51
+ ml = MibList.new
52
+ oid = [1,3,6,1,2,1,1,1,0]
53
+ ml.add_object oid, "ABCDEFG"
54
+ assert_equal( "ABCDEFG", ml.get(oid))
55
+ assert_equal( nil, ml.get_next(oid))
56
+ end
57
+
58
+ def test_b
59
+ ml = MibList.new
60
+ ml.add_object [1,3,8], 138
61
+ ml.add_object [1,3,7], 137
62
+ ml.add_object [1,3,6], 136
63
+ assert_equal( 138, ml.get([1,3,8]))
64
+ assert_equal( nil, ml.get([1,3]))
65
+ assert_equal( [[1,3,6],136], ml.get_next([1,3]))
66
+ assert_equal( [[1,3,7],137], ml.get_next([1,3,6]))
67
+ assert_equal( nil, ml.get_next([1,3,8]))
68
+ end
69
+
70
+ def test_c
71
+ ml = MibList.new
72
+ ml.add_object [1,2,3,4,5,0], proc {"Abcde" + "123"}
73
+ assert_equal( "Abcde123", ml.get([1,2,3,4,5,0]))
74
+ end
75
+
76
+
77
+ # MibList#add_object is a public interface that is delegated to
78
+ # objects of type Agent.
79
+ # MibList#get and #get_next are not intended for user code to access
80
+ # directly. Oids can be specified as strings or arrays but are always
81
+ # converted to and stored internally as arrays.
82
+ #
83
+ def test_strings
84
+ ml = MibList.new
85
+ ml.add_object "1.3.8", 138
86
+ ml.add_object "1.3.7", 137
87
+ ml.add_object "1.3.6", 136
88
+ assert_equal( 138, ml.get([1,3,8]))
89
+ assert_equal( nil, ml.get([1,3]))
90
+ assert_equal( [[1,3,6],136], ml.get_next([1,3]))
91
+ assert_equal( [[1,3,7],137], ml.get_next([1,3,6]))
92
+ assert_equal( nil, ml.get_next([1,3,8]))
93
+ end
94
+
95
+ end
96
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: eventmachine_snmpagent
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-06-23 00:00:00 -04:00
8
+ summary: Evented SNMP Agent
9
+ require_paths:
10
+ - lib
11
+ email: garbagecat10@gmail.com
12
+ homepage: http://www.rubyeventmachine.com
13
+ rubyforge_project:
14
+ description: ""
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Francis Cianfrocca
31
+ files:
32
+ - lib/snmpagent
33
+ - lib/snmpagent.rb
34
+ - lib/snmpagent/mibclass.rb
35
+ - lib/snmpagent/agent.rb
36
+ - lib/snmpagent/miblist.rb
37
+ - test/mibclass.rb
38
+ - test/agent.rb
39
+ - test/miblist.rb
40
+ - README
41
+ - COPYING
42
+ - RELEASE_NOTES
43
+ test_files: []
44
+
45
+ rdoc_options:
46
+ - --title
47
+ - eventmachine_snmpagent
48
+ - --main
49
+ - README
50
+ - --line-numbers
51
+ extra_rdoc_files:
52
+ - README
53
+ - COPYING
54
+ - RELEASE_NOTES
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ requirements: []
60
+
61
+ dependencies: []
62
+