SNMP4JR 0.0.20-java

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ * 0.0.1 Initial Release
2
+ # 0.20.0 Set platform to java
data/Manifest ADDED
@@ -0,0 +1,16 @@
1
+ CHANGELOG
2
+ README.rdoc
3
+ Rakefile
4
+ TODO.rdoc
5
+ examples/snmpmulti_async.rb
6
+ examples/snmptarget_get_bulk.rb
7
+ examples/snmptarget_set.rb
8
+ examples/snmptarget_v1get.rb
9
+ examples/snmptarget_v1walk.rb
10
+ examples/snmptarget_walk.rb
11
+ examples/synchronous_get.rb
12
+ init.rb
13
+ lib/SNMP4J.jar
14
+ lib/SNMP4JR.rb
15
+ lib/log4j-1.2.9.jar
16
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,283 @@
1
+ = SNMP4JR - SNMP Library for JRuby
2
+
3
+ == Summary
4
+
5
+ SNMP4JR is both a wrapper for SNMP4J and a library unto itself. I've noticed that many of the JRuby libs seem to be simple wrappers, but interpreted languages usually have easier interfaces to things like SNMP(SNMP for PHP for instance, and the ruby SNMP libs). To understand the differences between this library and the pure ruby SNMP libs please see the SNMP4J website or the feature list below:
6
+
7
+ === Features:
8
+ * Wrappers written in Ruby with ease of use in mind.
9
+ * Very high performance
10
+ * Thread safe(only refers to SNMP4J calls, not wrappers until I finish testing them)
11
+ * Simple interface(SNMPMulti) for polling multiple devices with an OID list _or_ hand crafted PDU
12
+ * Full access to SNMP4J API functionality
13
+
14
+ === SNMP4J API Features:
15
+ * SNMPv3 with MD5 and SHA authentication and DES and AES 128, AES 192, and AES 256 privacy.
16
+ * Pluggable Message Processing Models with implementations for MPv1, MPv2c, and MPv3
17
+ * All PDU types.
18
+ * Pluggable transport mappings. UDP and TCP are supported out-of-the-box.
19
+ * Pluggable timeout model.
20
+ * Synchronous and asynchronous requests.
21
+ * Command generator as well as command responder support.
22
+ * Free open source with the Apache license model
23
+ * Java™ 1.4.1 or later
24
+ * Logging based on Log4J
25
+ * Row-based efficient asynchronous table retrieval with GETBULK.
26
+ * Multi-threading support.
27
+ * JUnit tests (will be available in version 2.x and later)
28
+
29
+ === Usage:
30
+ Wrappers are provided for all API calls for SNMP4J. Use them as you would use the Java libs directly. Please refer to SNMP4J documentation for further direction.
31
+
32
+ SNMPTarget Utility class usage:
33
+
34
+ target = SNMPTarget.new(:host => 'localhost', :community => 'public')
35
+ target.get_bulk ['1.3.6.1.2.1.1.1', '1.3.6.1.2.1.1.3']
36
+ puts target.result_to_s
37
+
38
+ # bulk walk (defaults to version 2c bulk walk, but will do v1 if version is set)
39
+ target.walk('1.3.6.1.2.1.1') # System mib
40
+ puts target.result_to_s
41
+
42
+ # helper functions have been added for common mibs: system, processes, resources,
43
+ # interfaces, ifX, full_interfaces, and volumes
44
+ target.walk_system # System walk helper, same as above
45
+ puts target.result_to_s
46
+
47
+ # you can set too, just make sure you specify the r/w string as the :community
48
+ target = SNMPTarget.new(:host => '127.0.0.1', :community => 'private')
49
+ # you need to pass in the SMI encoded option here
50
+ # Available SMI classes are:
51
+ # AbstractVariable, BitString, Counter32, Counter64, Gauge32, GenericAddress, Integer32,
52
+ # IpAddress, Null, OctetString, OID, Opaque, SMIAddress, TcpAddress, TimeTicks,
53
+ # TransportIpAddress, UdpAddress, UnsignedInteger32, VariantVariable
54
+ email = SNMP4JR::SMI::OctetString.new('mark.cotner@gmail.com')
55
+ target.set('1.3.6.1.2.1.1.4.0', email).to_s
56
+
57
+
58
+ Please see the wiki and examples directory for more examples: http://wiki.github.com/awksedgreep/SNMP4JR/
59
+
60
+
61
+
62
+ === Notes:
63
+
64
+ * Currently only supports the API, but agent support could be added. If you would like to implement an agent in JRuby please let me know and I'll try to add it. Please specify if you would like to implement an agent or AgentX as these are two different things.
65
+ * If you would like commercial support for SNMP4J. Please see this page Support:http://www.snmp4j.org/html/support.html PLEASE NOTE that this is NOT a support mechanism for this gem, but for the library it wraps. For gem support see the github repository at: http://github.com/awksedgreep/SNMP4JR
66
+ * SNMP4J is included with this gem and is released with an Apache 2.0 license which can be found here: http://www.snmp4j.org/LICENSE-2_0.txt
67
+
68
+ Apache License
69
+ Version 2.0, January 2004
70
+ http://www.apache.org/licenses/
71
+
72
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
73
+
74
+ 1. Definitions.
75
+
76
+ "License" shall mean the terms and conditions for use, reproduction,
77
+ and distribution as defined by Sections 1 through 9 of this document.
78
+
79
+ "Licensor" shall mean the copyright owner or entity authorized by
80
+ the copyright owner that is granting the License.
81
+
82
+ "Legal Entity" shall mean the union of the acting entity and all
83
+ other entities that control, are controlled by, or are under common
84
+ control with that entity. For the purposes of this definition,
85
+ "control" means (i) the power, direct or indirect, to cause the
86
+ direction or management of such entity, whether by contract or
87
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
88
+ outstanding shares, or (iii) beneficial ownership of such entity.
89
+
90
+ "You" (or "Your") shall mean an individual or Legal Entity
91
+ exercising permissions granted by this License.
92
+
93
+ "Source" form shall mean the preferred form for making modifications,
94
+ including but not limited to software source code, documentation
95
+ source, and configuration files.
96
+
97
+ "Object" form shall mean any form resulting from mechanical
98
+ transformation or translation of a Source form, including but
99
+ not limited to compiled object code, generated documentation,
100
+ and conversions to other media types.
101
+
102
+ "Work" shall mean the work of authorship, whether in Source or
103
+ Object form, made available under the License, as indicated by a
104
+ copyright notice that is included in or attached to the work
105
+ (an example is provided in the Appendix below).
106
+
107
+ "Derivative Works" shall mean any work, whether in Source or Object
108
+ form, that is based on (or derived from) the Work and for which the
109
+ editorial revisions, annotations, elaborations, or other modifications
110
+ represent, as a whole, an original work of authorship. For the purposes
111
+ of this License, Derivative Works shall not include works that remain
112
+ separable from, or merely link (or bind by name) to the interfaces of,
113
+ the Work and Derivative Works thereof.
114
+
115
+ "Contribution" shall mean any work of authorship, including
116
+ the original version of the Work and any modifications or additions
117
+ to that Work or Derivative Works thereof, that is intentionally
118
+ submitted to Licensor for inclusion in the Work by the copyright owner
119
+ or by an individual or Legal Entity authorized to submit on behalf of
120
+ the copyright owner. For the purposes of this definition, "submitted"
121
+ means any form of electronic, verbal, or written communication sent
122
+ to the Licensor or its representatives, including but not limited to
123
+ communication on electronic mailing lists, source code control systems,
124
+ and issue tracking systems that are managed by, or on behalf of, the
125
+ Licensor for the purpose of discussing and improving the Work, but
126
+ excluding communication that is conspicuously marked or otherwise
127
+ designated in writing by the copyright owner as "Not a Contribution."
128
+
129
+ "Contributor" shall mean Licensor and any individual or Legal Entity
130
+ on behalf of whom a Contribution has been received by Licensor and
131
+ subsequently incorporated within the Work.
132
+
133
+ 2. Grant of Copyright License. Subject to the terms and conditions of
134
+ this License, each Contributor hereby grants to You a perpetual,
135
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
136
+ copyright license to reproduce, prepare Derivative Works of,
137
+ publicly display, publicly perform, sublicense, and distribute the
138
+ Work and such Derivative Works in Source or Object form.
139
+
140
+ 3. Grant of Patent License. Subject to the terms and conditions of
141
+ this License, each Contributor hereby grants to You a perpetual,
142
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
143
+ (except as stated in this section) patent license to make, have made,
144
+ use, offer to sell, sell, import, and otherwise transfer the Work,
145
+ where such license applies only to those patent claims licensable
146
+ by such Contributor that are necessarily infringed by their
147
+ Contribution(s) alone or by combination of their Contribution(s)
148
+ with the Work to which such Contribution(s) was submitted. If You
149
+ institute patent litigation against any entity (including a
150
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
151
+ or a Contribution incorporated within the Work constitutes direct
152
+ or contributory patent infringement, then any patent licenses
153
+ granted to You under this License for that Work shall terminate
154
+ as of the date such litigation is filed.
155
+
156
+ 4. Redistribution. You may reproduce and distribute copies of the
157
+ Work or Derivative Works thereof in any medium, with or without
158
+ modifications, and in Source or Object form, provided that You
159
+ meet the following conditions:
160
+
161
+ (a) You must give any other recipients of the Work or
162
+ Derivative Works a copy of this License; and
163
+
164
+ (b) You must cause any modified files to carry prominent notices
165
+ stating that You changed the files; and
166
+
167
+ (c) You must retain, in the Source form of any Derivative Works
168
+ that You distribute, all copyright, patent, trademark, and
169
+ attribution notices from the Source form of the Work,
170
+ excluding those notices that do not pertain to any part of
171
+ the Derivative Works; and
172
+
173
+ (d) If the Work includes a "NOTICE" text file as part of its
174
+ distribution, then any Derivative Works that You distribute must
175
+ include a readable copy of the attribution notices contained
176
+ within such NOTICE file, excluding those notices that do not
177
+ pertain to any part of the Derivative Works, in at least one
178
+ of the following places: within a NOTICE text file distributed
179
+ as part of the Derivative Works; within the Source form or
180
+ documentation, if provided along with the Derivative Works; or,
181
+ within a display generated by the Derivative Works, if and
182
+ wherever such third-party notices normally appear. The contents
183
+ of the NOTICE file are for informational purposes only and
184
+ do not modify the License. You may add Your own attribution
185
+ notices within Derivative Works that You distribute, alongside
186
+ or as an addendum to the NOTICE text from the Work, provided
187
+ that such additional attribution notices cannot be construed
188
+ as modifying the License.
189
+
190
+ You may add Your own copyright statement to Your modifications and
191
+ may provide additional or different license terms and conditions
192
+ for use, reproduction, or distribution of Your modifications, or
193
+ for any such Derivative Works as a whole, provided Your use,
194
+ reproduction, and distribution of the Work otherwise complies with
195
+ the conditions stated in this License.
196
+
197
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
198
+ any Contribution intentionally submitted for inclusion in the Work
199
+ by You to the Licensor shall be under the terms and conditions of
200
+ this License, without any additional terms or conditions.
201
+ Notwithstanding the above, nothing herein shall supersede or modify
202
+ the terms of any separate license agreement you may have executed
203
+ with Licensor regarding such Contributions.
204
+
205
+ 6. Trademarks. This License does not grant permission to use the trade
206
+ names, trademarks, service marks, or product names of the Licensor,
207
+ except as required for reasonable and customary use in describing the
208
+ origin of the Work and reproducing the content of the NOTICE file.
209
+
210
+ 7. Disclaimer of Warranty. Unless required by applicable law or
211
+ agreed to in writing, Licensor provides the Work (and each
212
+ Contributor provides its Contributions) on an "AS IS" BASIS,
213
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
214
+ implied, including, without limitation, any warranties or conditions
215
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
216
+ PARTICULAR PURPOSE. You are solely responsible for determining the
217
+ appropriateness of using or redistributing the Work and assume any
218
+ risks associated with Your exercise of permissions under this License.
219
+
220
+ 8. Limitation of Liability. In no event and under no legal theory,
221
+ whether in tort (including negligence), contract, or otherwise,
222
+ unless required by applicable law (such as deliberate and grossly
223
+ negligent acts) or agreed to in writing, shall any Contributor be
224
+ liable to You for damages, including any direct, indirect, special,
225
+ incidental, or consequential damages of any character arising as a
226
+ result of this License or out of the use or inability to use the
227
+ Work (including but not limited to damages for loss of goodwill,
228
+ work stoppage, computer failure or malfunction, or any and all
229
+ other commercial damages or losses), even if such Contributor
230
+ has been advised of the possibility of such damages.
231
+
232
+ 9. Accepting Warranty or Additional Liability. While redistributing
233
+ the Work or Derivative Works thereof, You may choose to offer,
234
+ and charge a fee for, acceptance of support, warranty, indemnity,
235
+ or other liability obligations and/or rights consistent with this
236
+ License. However, in accepting such obligations, You may act only
237
+ on Your own behalf and on Your sole responsibility, not on behalf
238
+ of any other Contributor, and only if You agree to indemnify,
239
+ defend, and hold each Contributor harmless for any liability
240
+ incurred by, or claims asserted against, such Contributor by reason
241
+ of your accepting any such warranty or additional liability.
242
+
243
+ END OF TERMS AND CONDITIONS
244
+
245
+ APPENDIX: How to apply the Apache License to your work.
246
+
247
+ To apply the Apache License to your work, attach the following
248
+ boilerplate notice, with the fields enclosed by brackets "[]"
249
+ replaced with your own identifying information. (Don't include
250
+ the brackets!) The text should be enclosed in the appropriate
251
+ comment syntax for the file format. We also recommend that a
252
+ file or class name and description of purpose be included on the
253
+ same "printed page" as the copyright notice for easier
254
+ identification within third-party archives.
255
+
256
+ Copyright [yyyy] [name of copyright owner]
257
+
258
+ Licensed under the Apache License, Version 2.0 (the "License");
259
+ you may not use this file except in compliance with the License.
260
+ You may obtain a copy of the License at
261
+
262
+ http://www.apache.org/licenses/LICENSE-2.0
263
+
264
+ Unless required by applicable law or agreed to in writing, software
265
+ distributed under the License is distributed on an "AS IS" BASIS,
266
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
267
+ See the License for the specific language governing permissions and
268
+ limitations under the License.
269
+
270
+
271
+ * SNMP4JR has a BSD license. You can fork, abuse, use as you wish. The BSD software license is listed below:
272
+
273
+ Copyright (c) 2010, Mark Cotner
274
+ All rights reserved.
275
+
276
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
277
+
278
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
279
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
280
+ * Neither the name of the OVE.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
281
+
282
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('SNMP4JR', '0.0.20') do |p|
6
+ p.description = "High Performance SNMP Library for JRuby which wraps SNMP4J"
7
+ p.url = "http://github.com/awksedgreep/SNMP4JR"
8
+ p.author = "Mark Cotner"
9
+ p.email = "mark.cotner@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.platform = "java"
12
+ p.development_dependencies= []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/SNMP4JR.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{SNMP4JR}
5
+ s.version = "0.0.20"
6
+ s.platform = %q{java}
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["Mark Cotner"]
10
+ s.date = %q{2011-12-13}
11
+ s.description = %q{High Performance SNMP Library for JRuby which wraps SNMP4J}
12
+ s.email = %q{mark.cotner@gmail.com}
13
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "TODO.rdoc", "lib/SNMP4J.jar", "lib/SNMP4JR.rb", "lib/log4j-1.2.9.jar"]
14
+ s.files = ["CHANGELOG", "README.rdoc", "Rakefile", "TODO.rdoc", "examples/snmpmulti_async.rb", "examples/snmptarget_get_bulk.rb", "examples/snmptarget_set.rb", "examples/snmptarget_v1get.rb", "examples/snmptarget_v1walk.rb", "examples/snmptarget_walk.rb", "examples/synchronous_get.rb", "init.rb", "lib/SNMP4J.jar", "lib/SNMP4JR.rb", "lib/log4j-1.2.9.jar", "Manifest", "SNMP4JR.gemspec"]
15
+ s.homepage = %q{http://github.com/awksedgreep/SNMP4JR}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "SNMP4JR", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{snmp4jr}
19
+ s.rubygems_version = %q{1.7.2}
20
+ s.summary = %q{High Performance SNMP Library for JRuby which wraps SNMP4J}
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
data/TODO.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = ToDo List for SNMP4JR
2
+
3
+ 1. Create an SNMPManager class similar to the one in the snmp gem for polling single devices.
4
+ 2. Create an example folder and populate it.
5
+ 3. Profit
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+ #require '../lib/SNMP4JR'
6
+
7
+ # Specify your targets.
8
+ target1 = SNMPTarget.new({:name => 'macbookpro', :host => '127.0.0.1', :community => 'public'})
9
+ target2 = SNMPTarget.new({:name => 'macpro', :host => '192.168.0.254', :community => 'public'})
10
+
11
+ # tell each target what to poll
12
+ # Please note that by default the PDU that is build by SNMPMulti will be a GETBULK request
13
+ # Per SNMP2c specification this means each oid should be expected to be a GETNEXT request
14
+ # therefore you will need to specify either the root of a tree(1.3.6.1.2.1.1.1 vs 1.3.6.1.2.1.1.1.0)
15
+ # or specify the oid just before the request you would like to make
16
+ oids = ['1.3.6.1.2.1.1.1', '1.3.6.1.2.1.1.5']
17
+ target1.oids = oids
18
+ target2.oids = oids
19
+
20
+ # Now use Multi to poll alll devices at the same time. It handles the necessary callbacks
21
+ # and PDU alignment for you
22
+ multi = SNMPMulti.new(target1, target2)
23
+
24
+ # Begin the poll
25
+ multi.poll
26
+ sleep 3 # wait for all pdus to return and get processed
27
+ multi.result.each do |result|
28
+ pp result
29
+ if result[:response].nil?
30
+ puts "Request to host timed out"
31
+ else
32
+ puts result[:event].peer_address.to_s
33
+ result[:response].variable_bindings.each do |vb|
34
+ puts vb.oid.to_s + " => " + vb.variable.to_s
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+
6
+ # This defaults to version 2c bulkwalk(most efficient method) unless you specify version1
7
+ target = SNMPTarget.new(:host => '127.0.0.1', :community => 'public',
8
+ :version => SNMP4JR::MP::Version2c)
9
+ target.max_repetitions = 1
10
+ target.non_repeaters = 2
11
+
12
+ target.get_bulk(['1.3.6.1.2.1.1.1', '1.3.6.1.2.1.1.5']).each do |vb|
13
+ puts vb.to_s
14
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+
6
+ target = SNMPTarget.new(:host => '127.0.0.1', :community => 'private')
7
+
8
+ # Set your sysLocation
9
+ location = SNMP4JR::SMI::OctetString.new('30k Leagues Under the Sea')
10
+ target.set('1.3.6.1.2.1.1.6.0', location)
11
+
12
+ # Set your sysContact
13
+ email = SNMP4JR::SMI::OctetString.new('boogie@basketball.com')
14
+ target.set('1.3.6.1.2.1.1.4.0', email)
15
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+
6
+ # This defaults to version 2c bulkwalk(most efficient method) unless you specify version1
7
+ target = SNMPTarget.new(:host => '127.0.0.1', :community => 'public',
8
+ :version => SNMP4JR::MP::Version1)
9
+
10
+ puts target.get('1.3.6.1.2.1.1.1.0').to_s # system MIB
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+
6
+ # This defaults to version 2c bulkwalk(most efficient method) unless you specify version1
7
+ target = SNMPTarget.new(:host => '127.0.0.1', :community => 'public',
8
+ :version => SNMP4JR::MP::Version1)
9
+
10
+ target.walk('1.3.6.1.2.1.1') # system MIB
11
+ puts target.result_to_s
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+
6
+ # This defaults to version 2c bulkwalk(most efficient method) unless you specify version1
7
+ target = SNMPTarget.new(:host => '127.0.0.1', :community => 'public')
8
+
9
+ target.walk('1.3.6.1.2.1.1') # system MIB
10
+ puts target.result_to_s
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'rubygems'
4
+ require 'SNMP4JR'
5
+
6
+ # This example uses the SNMP4J libraries directly from Ruby. This is advanced use of the library.
7
+ # The wrappers are much simpler. Check out the SNMPTarget and SNMPMulti classes for a much easier
8
+ # example.
9
+
10
+ # Open a new session
11
+ snmp = SNMP4JR::Snmp.new(SNMP4JR::Transport::DefaultUdpTransportMapping.new)
12
+ snmp.listen
13
+
14
+ # Set up your target, including timeout and version
15
+ target = SNMP4JR::CommunityTarget.new
16
+ target.community = SNMP4JR::SMI::OctetString.new('public') # community string to use
17
+ target.address = SNMP4JR::SMI::GenericAddress.parse("udp:127.0.0.1/161") # poll localhost, change this to suit
18
+ target.version = SNMP4JR::MP::Version2c # Set SNMP verion 2c
19
+ target.timeout = 5000 # 5 seconds
20
+
21
+ # Build your request PDU including type and oids to be polled
22
+ pdu = SNMP4JR::PDU.new
23
+ pdu.add(SNMP4JR::SMI::VariableBinding.new(SNMP4JR::SMI::OID.new('1.3.6.1.2.1.1.1')))
24
+ pdu.add(SNMP4JR::SMI::VariableBinding.new(SNMP4JR::SMI::OID.new('1.3.6.1.2.1.1.3')))
25
+ pdu.type = SNMP4JR::Constants::GETBULK
26
+ pdu.max_repetitions = 1
27
+ pdu.non_repeaters = 2
28
+
29
+ result = snmp.send(pdu, target)
30
+ puts result.peer_address.to_s + ': '
31
+ result.response.variable_bindings.each do |vb|
32
+ puts vb.oid.to_s + " => " + vb.variable.to_s
33
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'SNMP4JR'
data/lib/SNMP4J.jar ADDED
Binary file
data/lib/SNMP4JR.rb ADDED
@@ -0,0 +1,385 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ if RUBY_PLATFORM =~ /java/
4
+ require 'java'
5
+ require 'log4j-1.2.9.jar'
6
+ require 'SNMP4J.jar'
7
+ else
8
+ warn "SNMP4JR is only for use with JRuby"
9
+ end
10
+
11
+ module SNMP4JR
12
+ include_package 'org.snmp4j'
13
+
14
+ module Constants
15
+ GET = -96
16
+ GETNEXT = -95
17
+ GETBULK = -91
18
+ INFORM = -90
19
+ NOTIFICATION = -89
20
+ REPORT = -88
21
+ RESPONSE = -94
22
+ SET = -93
23
+ TRAP = -89
24
+ V1TRAP = -92
25
+ end
26
+
27
+ module ASN1
28
+ include_package 'org.snmp4j.asn1'
29
+ end
30
+
31
+ module Event
32
+ include_package 'org.snmp4j.event'
33
+ end
34
+
35
+ module Log
36
+ include_package 'org.snmp4j.log'
37
+ end
38
+
39
+ module MP
40
+ Version1 = 0
41
+ Version2c = 1
42
+ Version3 = 3
43
+ include_package 'org.snmp4j.mp'
44
+ end
45
+
46
+ module Security
47
+ include_package 'org.snmp4j.security'
48
+ end
49
+
50
+ module SMI
51
+ include_package 'org.snmp4j.smi'
52
+ end
53
+
54
+ module Test
55
+ include_package 'org.snmp4j.test'
56
+ end
57
+
58
+ module Tools
59
+ module Console
60
+ include_package 'org.snmp4j.tools.console'
61
+ end
62
+ end
63
+
64
+ module Transport
65
+ include_package 'org.snmp4j.transport'
66
+ end
67
+
68
+ module Util
69
+ include_package 'org.snmp4j.util'
70
+ end
71
+
72
+ module Version
73
+ include_package 'org.snmp4j.version'
74
+ end
75
+ end
76
+
77
+ class SNMPTarget
78
+ attr_accessor :host, :community, :timeout, :version, :max_repetitions, :non_repeaters, :port
79
+ attr_reader :request_type, :snmp, :result, :pdus_sent, :pdu
80
+
81
+ DEFAULTS = {:host => '127.0.0.1', :community => 'public', :timeout => 2000,
82
+ :version => SNMP4JR::MP::Version2c, :transport => 'udp', :port => 161,
83
+ :oids => ['1.3.6.1.2.1.1.1', '1.3.6.1.2.1.1.5'],
84
+ :max_repetitions => 1, :non_repeaters => 2}
85
+
86
+ def initialize(ivar = {})
87
+ ivar = DEFAULTS.merge(ivar)
88
+ self.host = ivar[:host]
89
+ self.community = ivar[:community]
90
+ self.timeout = ivar[:timeout]
91
+ self.version = ivar[:version]
92
+ self.transport = ivar[:transport]
93
+ self.oids = ivar[:oids]
94
+ self.max_repetitions = ivar[:max_repetitions]
95
+ self.non_repeaters = ivar[:non_repeaters]
96
+ self.port = ivar[:port]
97
+ @result = []
98
+ @pdus_sent = 0
99
+ @request_type = SNMP4JR::Constants::GETBULK
100
+ end
101
+
102
+ def snmp_target
103
+ return @snmp_target unless @snmp_target.nil?
104
+ @snmp_target = SNMP4JR::CommunityTarget.new
105
+ @snmp_target.community = SNMP4JR::SMI::OctetString.new(@community)
106
+ @snmp_target.address = SNMP4JR::SMI::GenericAddress.parse("#{@transport}:#{@host}/#{@port}")
107
+ @snmp_target.version = @version
108
+ @snmp_target.timeout = @timeout
109
+ @snmp_target
110
+ end
111
+
112
+ def snmp_target=(target)
113
+ @snmp_target = target
114
+ end
115
+
116
+ def oids
117
+ if @oids.class == String
118
+ @oids = [@oids]
119
+ end
120
+ return @oids
121
+ end
122
+
123
+ def oids=(oids)
124
+ @oids = oids
125
+ end
126
+
127
+ def transport
128
+ if @transport.class == String
129
+ case @transport
130
+ when 'udp'
131
+ return SNMP4JR::Transport::DefaultUdpTransportMapping.new
132
+ when 'tcp'
133
+ return SNMP4JR::Transport::DefaultTcpTransportMapping.new
134
+ else
135
+ return SNMP4JR::Transport::DefaultUdpTransportMapping.new
136
+ end
137
+ else
138
+ return @transport
139
+ end
140
+ end
141
+
142
+ def transport=(ivar)
143
+ @transport = ivar
144
+ end
145
+
146
+ def get(oid = nil)
147
+ self.oids = [oid] unless oid.nil?
148
+ @request_type = SNMP4JR::Constants::GET
149
+ reset_session
150
+ @snmp = SNMP4JR::Snmp.new(self.transport)
151
+ @snmp.listen
152
+ event = @snmp.send(pdu, snmp_target)
153
+ if event.response.nil?
154
+ @result = []
155
+ return nil
156
+ else
157
+ @result = event.response.variable_bindings
158
+ end
159
+ @snmp.close
160
+ event.response.variable_bindings.first.variable
161
+ end
162
+
163
+ def get_bulk(oid_list = nil)
164
+ self.oids = oid_list unless oid_list.nil?
165
+ @request_type = SNMP4JR::Constants::GETBULK
166
+ reset_session
167
+ @snmp = SNMP4JR::Snmp.new(self.transport)
168
+ @snmp.listen
169
+ event = @snmp.send(pdu, snmp_target)
170
+ if event.response.nil?
171
+ @result = []
172
+ return nil
173
+ else
174
+ @result = event.response.variable_bindings
175
+ end
176
+ @snmp.close
177
+ event.response.variable_bindings
178
+ end
179
+
180
+ def set(oid = '1.3.6.1.2.1.1.4.0', variable = SNMP4JR::SMI::OctetString.new('mark.cotner@gmail.com'))
181
+ set_pdu = SNMP4JR::PDU.new
182
+ set_pdu.type = SNMP4JR::Constants::SET
183
+ set_pdu.add(SNMP4JR::SMI::VariableBinding.new(SNMP4JR::SMI::OID.new(oid), variable))
184
+ set_pdu.non_repeaters = 0
185
+ @snmp = SNMP4JR::Snmp.new(self.transport)
186
+ @snmp.listen
187
+ event = @snmp.set(set_pdu, snmp_target)
188
+ if event.response.nil?
189
+ @result = []
190
+ return nil
191
+ end
192
+ return event.response.variable_bindings.get 0
193
+ end
194
+
195
+ def walk(oid = nil)
196
+ self.oids = [oid] unless oid.nil?
197
+ return nil if oid.nil?
198
+ snmp_oid = SNMP4JR::SMI::OID.new(oid)
199
+ case version
200
+ when SNMP4JR::MP::Version1
201
+ @request_type = SNMP4JR::Constants::GETNEXT
202
+ when (SNMP4JR::MP::Version2c or SNMP4JR::MP::Version3)
203
+ @request_type = SNMP4JR::Constants::GETBULK
204
+ @max_repetitions = 40
205
+ @non_repeaters = 0
206
+ end
207
+ # tick a pdu for async to return when complete
208
+ @pdus_sent += 1
209
+ # track when I'm finished polling
210
+ finished = false
211
+ @result = []
212
+ @snmp = SNMP4JR::Snmp.new(self.transport)
213
+ @snmp.listen
214
+ until finished
215
+ response_event = @snmp.send(pdu, snmp_target)
216
+ response_pdu = response_event.response
217
+ response_array = []
218
+ response_array = pdu_to_ruby_array(response_pdu) unless response_pdu.nil?
219
+ # timeout
220
+ if response_pdu.nil?
221
+ finished = true
222
+ # nontimeout error
223
+ elsif response_pdu.error_status != 0
224
+ finished = true
225
+ # returned no oids but was not an error (end of tree?)
226
+ elsif response_array.length == 0
227
+ finished = true
228
+ # lexical compare, are we done with the tree?
229
+ elsif !response_array.last.oid.starts_with(snmp_oid)
230
+ finished = true
231
+ end
232
+ if (response_array.length > 0)
233
+ # only add results that match filter
234
+ @result += response_array.select { |vb| vb.oid.starts_with(snmp_oid) }
235
+ # start next poll with last oid from
236
+ self.oids = [response_array.last.oid.to_s]
237
+ end
238
+ end
239
+ @pdus_sent -= 1
240
+ @snmp.close
241
+ @result
242
+ end
243
+
244
+ def walk_interfaces
245
+ walk('1.3.6.1.2.1.2')
246
+ end
247
+
248
+ def walk_ifX
249
+ walk('1.3.6.1.2.1.31')
250
+ end
251
+
252
+ def walk_full_interfaces
253
+ output = walk_interfaces
254
+ output += walk_ifX
255
+ @result = output
256
+ end
257
+
258
+ def walk_system
259
+ walk('1.3.6.1.2.1.1')
260
+ end
261
+
262
+ def walk_volumes
263
+ walk('1.3.6.1.2.1.25.3.8')
264
+ end
265
+
266
+ def walk_resources
267
+ walk('1.3.6.1.2.1.25.2.3.1')
268
+ end
269
+
270
+ def walk_software
271
+ walk('1.3.6.1.2.1.25.6.3.1')
272
+ end
273
+
274
+ def walk_processes
275
+ walk('1.3.6.1.2.1.25.4.2.1')
276
+ end
277
+
278
+ def nonblocking_walk(oid = nil)
279
+ end
280
+
281
+ def send(callback = nil)
282
+ callback = self if callback.nil?
283
+ @result = []
284
+ @snmp = SNMP4JR::Snmp.new(transport)
285
+ @snmp.listen
286
+ @snmp.send(pdu, snmp_target, self, callback)
287
+ @snmp.close
288
+ @pdus_sent += 1
289
+ end
290
+
291
+ def onResponse(event)
292
+ event.source.cancel(event.request, self)
293
+ @result << {:target => event.user_object, :request => event.request, :response => event.response, :event => event}
294
+ @pdus_sent -= 1
295
+ end
296
+
297
+ def poll_complete?(blocking = true)
298
+ if blocking
299
+ loop do
300
+ return true if @pdus_sent == 0
301
+ sleep 0.1
302
+ end
303
+ else
304
+ if @pdus_sent == 0
305
+ true
306
+ else
307
+ false
308
+ end
309
+ end
310
+ end
311
+
312
+ def result_to_s
313
+ output = ''
314
+ @result.each do |res|
315
+ output += (res.to_s + "\n") if res.class == Java::OrgSnmp4jSmi::VariableBinding
316
+ if res.class == Array
317
+ res.each do |hash_event|
318
+ hash_event.response.variable_bindings.each do |vb|
319
+ output += (vb.to_s + "\n")
320
+ end
321
+ end
322
+ end
323
+ end
324
+ output
325
+ end
326
+
327
+ def pdu
328
+ @pdu = SNMP4JR::PDU.new
329
+ @oids.each do |oid|
330
+ @pdu.add(SNMP4JR::SMI::VariableBinding.new(SNMP4JR::SMI::OID.new(oid)))
331
+ end
332
+ @pdu.max_repetitions = @max_repetitions
333
+ @pdu.non_repeaters = @non_repeaters
334
+ @pdu.type = @request_type
335
+ @pdu
336
+ end
337
+
338
+ private
339
+
340
+ def reset_session
341
+ @pdu = nil unless @oids.nil?
342
+ @snmp_target = nil
343
+ @result = []
344
+ @pdus_sent = 0
345
+ end
346
+
347
+ def pdu_to_ruby_array(ipdu)
348
+ iresult = []
349
+ ipdu.variable_bindings.each do |vb|
350
+ iresult << vb
351
+ end
352
+ iresult
353
+ end
354
+ end
355
+
356
+
357
+ class SNMPMulti
358
+ attr_accessor :targets
359
+ attr_reader :result
360
+
361
+ # Takes a list of targets and polls each
362
+ def initialize(targets = [SNMPTarget.new(:host => '127.0.0.1', :community => 'public', :oids => ['1.3.6.1.2.1.1.1', '1.3.6.1.2.1.1.3']),
363
+ SNMPTarget.new(:host => 'rubydb.ove.local', :community => 'public', :oids => ['1.3.6.1.2.1.1.5'])],
364
+ transport = 'udp')
365
+ @targets = targets
366
+ @result = []
367
+ @transport = transport
368
+ end
369
+
370
+ # Handle PDU responses as they arrive, you don't need to call this
371
+ def onResponse(event)
372
+ event.source.cancel(event.request, self)
373
+ @result << {:target => event.user_object, :request => event.request, :response => event.response, :event => event}
374
+ end
375
+
376
+ # Poll the device
377
+ def poll
378
+ snmp = SNMP4JR::Snmp.new(SNMP4JR::Transport::DefaultUdpTransportMapping.new) if @transport == 'udp'
379
+ snmp = SNMP4JR::Snmp.new(SNMP4JR::Transport::DefaultTcpTransportMapping.new) if @transport == 'tcp'
380
+ snmp.listen
381
+ @targets.each do |target|
382
+ snmp.send(target.pdu, target.snmp_target, target, self)
383
+ end
384
+ end
385
+ end
Binary file
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: SNMP4JR
3
+ version: !ruby/object:Gem::Version
4
+ hash: 55
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 20
10
+ version: 0.0.20
11
+ platform: java
12
+ authors:
13
+ - Mark Cotner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-13 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: High Performance SNMP Library for JRuby which wraps SNMP4J
22
+ email: mark.cotner@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - CHANGELOG
29
+ - README.rdoc
30
+ - TODO.rdoc
31
+ - lib/SNMP4J.jar
32
+ - lib/SNMP4JR.rb
33
+ - lib/log4j-1.2.9.jar
34
+ files:
35
+ - CHANGELOG
36
+ - README.rdoc
37
+ - Rakefile
38
+ - TODO.rdoc
39
+ - examples/snmpmulti_async.rb
40
+ - examples/snmptarget_get_bulk.rb
41
+ - examples/snmptarget_set.rb
42
+ - examples/snmptarget_v1get.rb
43
+ - examples/snmptarget_v1walk.rb
44
+ - examples/snmptarget_walk.rb
45
+ - examples/synchronous_get.rb
46
+ - init.rb
47
+ - lib/SNMP4J.jar
48
+ - lib/SNMP4JR.rb
49
+ - lib/log4j-1.2.9.jar
50
+ - Manifest
51
+ - SNMP4JR.gemspec
52
+ homepage: http://github.com/awksedgreep/SNMP4JR
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --line-numbers
58
+ - --inline-source
59
+ - --title
60
+ - SNMP4JR
61
+ - --main
62
+ - README.rdoc
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 11
80
+ segments:
81
+ - 1
82
+ - 2
83
+ version: "1.2"
84
+ requirements: []
85
+
86
+ rubyforge_project: snmp4jr
87
+ rubygems_version: 1.8.12
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: High Performance SNMP Library for JRuby which wraps SNMP4J
91
+ test_files: []
92
+