snmp 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. data/README +193 -0
  2. data/Rakefile +52 -0
  3. data/data/ruby/snmp/mibs/ACCOUNTING-CONTROL-MIB.yaml +44 -0
  4. data/data/ruby/snmp/mibs/ADSL-LINE-EXT-MIB.yaml +66 -0
  5. data/data/ruby/snmp/mibs/ADSL-LINE-MIB.yaml +238 -0
  6. data/data/ruby/snmp/mibs/ADSL-TC-MIB.yaml +2 -0
  7. data/data/ruby/snmp/mibs/AGENTX-MIB.yaml +40 -0
  8. data/data/ruby/snmp/mibs/APM-MIB.yaml +98 -0
  9. data/data/ruby/snmp/mibs/APPC-MIB.yaml +295 -0
  10. data/data/ruby/snmp/mibs/APPLETALK-MIB.yaml +284 -0
  11. data/data/ruby/snmp/mibs/APPLICATION-MIB.yaml +161 -0
  12. data/data/ruby/snmp/mibs/APPN-DLUR-MIB.yaml +41 -0
  13. data/data/ruby/snmp/mibs/APPN-MIB.yaml +356 -0
  14. data/data/ruby/snmp/mibs/APPN-TRAP-MIB.yaml +8 -0
  15. data/data/ruby/snmp/mibs/APS-MIB.yaml +62 -0
  16. data/data/ruby/snmp/mibs/ATM-ACCOUNTING-INFORMATION-MIB.yaml +37 -0
  17. data/data/ruby/snmp/mibs/ATM-MIB.yaml +112 -0
  18. data/data/ruby/snmp/mibs/ATM-TC-MIB.yaml +19 -0
  19. data/data/ruby/snmp/mibs/ATM2-MIB.yaml +182 -0
  20. data/data/ruby/snmp/mibs/BGP4-MIB.yaml +56 -0
  21. data/data/ruby/snmp/mibs/BLDG-HVAC-MIB.yaml +35 -0
  22. data/data/ruby/snmp/mibs/BRIDGE-MIB.yaml +63 -0
  23. data/data/ruby/snmp/mibs/CHARACTER-MIB.yaml +48 -0
  24. data/data/ruby/snmp/mibs/CIRCUIT-IF-MIB.yaml +21 -0
  25. data/data/ruby/snmp/mibs/CLNS-MIB.yaml +116 -0
  26. data/data/ruby/snmp/mibs/COFFEE-POT-MIB.yaml +13 -0
  27. data/data/ruby/snmp/mibs/COPS-CLIENT-MIB.yaml +54 -0
  28. data/data/ruby/snmp/mibs/DECNET-PHIV-MIB.yaml +237 -0
  29. data/data/ruby/snmp/mibs/DIAL-CONTROL-MIB.yaml +84 -0
  30. data/data/ruby/snmp/mibs/DIFFSERV-CONFIG-MIB.yaml +15 -0
  31. data/data/ruby/snmp/mibs/DIFFSERV-DSCP-TC.yaml +2 -0
  32. data/data/ruby/snmp/mibs/DIFFSERV-MIB.yaml +171 -0
  33. data/data/ruby/snmp/mibs/DIRECTORY-SERVER-MIB.yaml +52 -0
  34. data/data/ruby/snmp/mibs/DISMAN-EVENT-MIB.yaml +110 -0
  35. data/data/ruby/snmp/mibs/DISMAN-EXPRESSION-MIB.yaml +55 -0
  36. data/data/ruby/snmp/mibs/DISMAN-NSLOOKUP-MIB.yaml +23 -0
  37. data/data/ruby/snmp/mibs/DISMAN-PING-MIB.yaml +57 -0
  38. data/data/ruby/snmp/mibs/DISMAN-SCHEDULE-MIB.yaml +32 -0
  39. data/data/ruby/snmp/mibs/DISMAN-SCRIPT-MIB.yaml +80 -0
  40. data/data/ruby/snmp/mibs/DISMAN-TRACEROUTE-MIB.yaml +72 -0
  41. data/data/ruby/snmp/mibs/DLSW-MIB.yaml +193 -0
  42. data/data/ruby/snmp/mibs/DNS-RESOLVER-MIB.yaml +87 -0
  43. data/data/ruby/snmp/mibs/DNS-SERVER-MIB.yaml +73 -0
  44. data/data/ruby/snmp/mibs/DOCS-BPI-MIB.yaml +101 -0
  45. data/data/ruby/snmp/mibs/DOCS-CABLE-DEVICE-MIB.yaml +107 -0
  46. data/data/ruby/snmp/mibs/DOCS-IF-MIB.yaml +143 -0
  47. data/data/ruby/snmp/mibs/DOT12-IF-MIB.yaml +33 -0
  48. data/data/ruby/snmp/mibs/DS0-MIB.yaml +18 -0
  49. data/data/ruby/snmp/mibs/DS0BUNDLE-MIB.yaml +17 -0
  50. data/data/ruby/snmp/mibs/DS1-MIB.yaml +118 -0
  51. data/data/ruby/snmp/mibs/DS3-MIB.yaml +106 -0
  52. data/data/ruby/snmp/mibs/DSA-MIB.yaml +44 -0
  53. data/data/ruby/snmp/mibs/DSMON-MIB.yaml +219 -0
  54. data/data/ruby/snmp/mibs/EBN-MIB.yaml +47 -0
  55. data/data/ruby/snmp/mibs/ENTITY-MIB.yaml +51 -0
  56. data/data/ruby/snmp/mibs/ENTITY-SENSOR-MIB.yaml +16 -0
  57. data/data/ruby/snmp/mibs/ETHER-CHIPSET-MIB.yaml +79 -0
  58. data/data/ruby/snmp/mibs/ETHER-WIS.yaml +28 -0
  59. data/data/ruby/snmp/mibs/EtherLike-MIB.yaml +58 -0
  60. data/data/ruby/snmp/mibs/FDDI-SMT73-MIB.yaml +128 -0
  61. data/data/ruby/snmp/mibs/FIBRE-CHANNEL-FE-MIB.yaml +122 -0
  62. data/data/ruby/snmp/mibs/FLOW-METER-MIB.yaml +112 -0
  63. data/data/ruby/snmp/mibs/FR-ATM-PVC-SERVICE-IWF-MIB.yaml +45 -0
  64. data/data/ruby/snmp/mibs/FR-MFR-MIB.yaml +52 -0
  65. data/data/ruby/snmp/mibs/FRAME-RELAY-DTE-MIB.yaml +54 -0
  66. data/data/ruby/snmp/mibs/FRNETSERV-MIB.yaml +109 -0
  67. data/data/ruby/snmp/mibs/FRSLD-MIB.yaml +82 -0
  68. data/data/ruby/snmp/mibs/Finisher-MIB.yaml +53 -0
  69. data/data/ruby/snmp/mibs/GSMP-MIB.yaml +82 -0
  70. data/data/ruby/snmp/mibs/HC-ALARM-MIB.yaml +32 -0
  71. data/data/ruby/snmp/mibs/HC-PerfHist-TC-MIB.yaml +2 -0
  72. data/data/ruby/snmp/mibs/HC-RMON-MIB.yaml +193 -0
  73. data/data/ruby/snmp/mibs/HCNUM-TC.yaml +2 -0
  74. data/data/ruby/snmp/mibs/HDSL2-SHDSL-LINE-MIB.yaml +115 -0
  75. data/data/ruby/snmp/mibs/HOST-RESOURCES-MIB.yaml +98 -0
  76. data/data/ruby/snmp/mibs/HOST-RESOURCES-TYPES.yaml +56 -0
  77. data/data/ruby/snmp/mibs/HPR-IP-MIB.yaml +24 -0
  78. data/data/ruby/snmp/mibs/HPR-MIB.yaml +81 -0
  79. data/data/ruby/snmp/mibs/IF-INVERTED-STACK-MIB.yaml +9 -0
  80. data/data/ruby/snmp/mibs/IF-MIB.yaml +73 -0
  81. data/data/ruby/snmp/mibs/IGMP-STD-MIB.yaml +33 -0
  82. data/data/ruby/snmp/mibs/INET-ADDRESS-MIB.yaml +2 -0
  83. data/data/ruby/snmp/mibs/INTEGRATED-SERVICES-GUARANTEED-MIB.yaml +13 -0
  84. data/data/ruby/snmp/mibs/INTEGRATED-SERVICES-MIB.yaml +44 -0
  85. data/data/ruby/snmp/mibs/INTERFACETOPN-MIB.yaml +29 -0
  86. data/data/ruby/snmp/mibs/IP-FORWARD-MIB.yaml +42 -0
  87. data/data/ruby/snmp/mibs/IP-MIB.yaml +66 -0
  88. data/data/ruby/snmp/mibs/IPATM-IPMC-MIB.yaml +203 -0
  89. data/data/ruby/snmp/mibs/IPMROUTE-STD-MIB.yaml +64 -0
  90. data/data/ruby/snmp/mibs/IPOA-MIB.yaml +85 -0
  91. data/data/ruby/snmp/mibs/IPV6-FLOW-LABEL-MIB.yaml +2 -0
  92. data/data/ruby/snmp/mibs/IPV6-ICMP-MIB.yaml +42 -0
  93. data/data/ruby/snmp/mibs/IPV6-MIB.yaml +88 -0
  94. data/data/ruby/snmp/mibs/IPV6-MLD-MIB.yaml +30 -0
  95. data/data/ruby/snmp/mibs/IPV6-TCP-MIB.yaml +14 -0
  96. data/data/ruby/snmp/mibs/IPV6-UDP-MIB.yaml +11 -0
  97. data/data/ruby/snmp/mibs/ISDN-MIB.yaml +71 -0
  98. data/data/ruby/snmp/mibs/Job-Monitoring-MIB.yaml +43 -0
  99. data/data/ruby/snmp/mibs/L2TP-MIB.yaml +156 -0
  100. data/data/ruby/snmp/mibs/MALLOC-MIB.yaml +83 -0
  101. data/data/ruby/snmp/mibs/MAU-MIB.yaml +110 -0
  102. data/data/ruby/snmp/mibs/MIOX25-MIB.yaml +38 -0
  103. data/data/ruby/snmp/mibs/MIP-MIB.yaml +180 -0
  104. data/data/ruby/snmp/mibs/MPLS-FTN-STD-MIB.yaml +44 -0
  105. data/data/ruby/snmp/mibs/MPLS-LDP-ATM-STD-MIB.yaml +35 -0
  106. data/data/ruby/snmp/mibs/MPLS-LDP-FRAME-RELAY-STD-MIB.yaml +28 -0
  107. data/data/ruby/snmp/mibs/MPLS-LDP-GENERIC-STD-MIB.yaml +15 -0
  108. data/data/ruby/snmp/mibs/MPLS-LDP-STD-MIB.yaml +118 -0
  109. data/data/ruby/snmp/mibs/MPLS-LSR-STD-MIB.yaml +98 -0
  110. data/data/ruby/snmp/mibs/MPLS-TC-STD-MIB.yaml +3 -0
  111. data/data/ruby/snmp/mibs/MPLS-TE-STD-MIB.yaml +120 -0
  112. data/data/ruby/snmp/mibs/MTA-MIB.yaml +64 -0
  113. data/data/ruby/snmp/mibs/Modem-MIB.yaml +106 -0
  114. data/data/ruby/snmp/mibs/NETWORK-SERVICES-MIB.yaml +33 -0
  115. data/data/ruby/snmp/mibs/NHRP-MIB.yaml +163 -0
  116. data/data/ruby/snmp/mibs/NOTIFICATION-LOG-MIB.yaml +51 -0
  117. data/data/ruby/snmp/mibs/OPT-IF-MIB.yaml +431 -0
  118. data/data/ruby/snmp/mibs/OSPF-MIB.yaml +150 -0
  119. data/data/ruby/snmp/mibs/OSPF-TRAP-MIB.yaml +11 -0
  120. data/data/ruby/snmp/mibs/P-BRIDGE-MIB.yaml +51 -0
  121. data/data/ruby/snmp/mibs/PARALLEL-MIB.yaml +24 -0
  122. data/data/ruby/snmp/mibs/PIM-MIB.yaml +66 -0
  123. data/data/ruby/snmp/mibs/PINT-MIB.yaml +51 -0
  124. data/data/ruby/snmp/mibs/POWER-ETHERNET-MIB.yaml +36 -0
  125. data/data/ruby/snmp/mibs/PPP-BRIDGE-NCP-MIB.yaml +25 -0
  126. data/data/ruby/snmp/mibs/PPP-IP-NCP-MIB.yaml +13 -0
  127. data/data/ruby/snmp/mibs/PPP-LCP-MIB.yaml +47 -0
  128. data/data/ruby/snmp/mibs/PPP-SEC-MIB.yaml +20 -0
  129. data/data/ruby/snmp/mibs/PTOPO-MIB.yaml +39 -0
  130. data/data/ruby/snmp/mibs/PerfHist-TC-MIB.yaml +2 -0
  131. data/data/ruby/snmp/mibs/Printer-MIB.yaml +209 -0
  132. data/data/ruby/snmp/mibs/Q-BRIDGE-MIB.yaml +99 -0
  133. data/data/ruby/snmp/mibs/RADIUS-ACC-CLIENT-MIB.yaml +26 -0
  134. data/data/ruby/snmp/mibs/RADIUS-ACC-SERVER-MIB.yaml +35 -0
  135. data/data/ruby/snmp/mibs/RADIUS-AUTH-CLIENT-MIB.yaml +28 -0
  136. data/data/ruby/snmp/mibs/RADIUS-AUTH-SERVER-MIB.yaml +37 -0
  137. data/data/ruby/snmp/mibs/RDBMS-MIB.yaml +82 -0
  138. data/data/ruby/snmp/mibs/RFC1065-SMI.yaml +9 -0
  139. data/data/ruby/snmp/mibs/RFC1155-SMI.yaml +9 -0
  140. data/data/ruby/snmp/mibs/RFC1158-MIB.yaml +202 -0
  141. data/data/ruby/snmp/mibs/RFC1213-MIB.yaml +202 -0
  142. data/data/ruby/snmp/mibs/RFC1269-MIB.yaml +29 -0
  143. data/data/ruby/snmp/mibs/RFC1271-MIB.yaml +214 -0
  144. data/data/ruby/snmp/mibs/RFC1285-MIB.yaml +104 -0
  145. data/data/ruby/snmp/mibs/RFC1316-MIB.yaml +42 -0
  146. data/data/ruby/snmp/mibs/RFC1381-MIB.yaml +60 -0
  147. data/data/ruby/snmp/mibs/RFC1382-MIB.yaml +169 -0
  148. data/data/ruby/snmp/mibs/RFC1414-MIB.yaml +10 -0
  149. data/data/ruby/snmp/mibs/RIPv2-MIB.yaml +34 -0
  150. data/data/ruby/snmp/mibs/RMON-MIB.yaml +219 -0
  151. data/data/ruby/snmp/mibs/RMON2-MIB.yaml +282 -0
  152. data/data/ruby/snmp/mibs/ROHC-MIB.yaml +64 -0
  153. data/data/ruby/snmp/mibs/ROHC-RTP-MIB.yaml +29 -0
  154. data/data/ruby/snmp/mibs/ROHC-UNCOMPRESSED-MIB.yaml +11 -0
  155. data/data/ruby/snmp/mibs/RS-232-MIB.yaml +54 -0
  156. data/data/ruby/snmp/mibs/RSVP-MIB.yaml +156 -0
  157. data/data/ruby/snmp/mibs/RTP-MIB.yaml +57 -0
  158. data/data/ruby/snmp/mibs/SFLOW-MIB.yaml +22 -0
  159. data/data/ruby/snmp/mibs/SIP-MIB.yaml +70 -0
  160. data/data/ruby/snmp/mibs/SLAPM-MIB.yaml +144 -0
  161. data/data/ruby/snmp/mibs/SMON-MIB.yaml +63 -0
  162. data/data/ruby/snmp/mibs/SNA-NAU-MIB.yaml +137 -0
  163. data/data/ruby/snmp/mibs/SNA-SDLC-MIB.yaml +147 -0
  164. data/data/ruby/snmp/mibs/SNMP-COMMUNITY-MIB.yaml +22 -0
  165. data/data/ruby/snmp/mibs/SNMP-FRAMEWORK-MIB.yaml +14 -0
  166. data/data/ruby/snmp/mibs/SNMP-MPD-MIB.yaml +11 -0
  167. data/data/ruby/snmp/mibs/SNMP-NOTIFICATION-MIB.yaml +25 -0
  168. data/data/ruby/snmp/mibs/SNMP-PROXY-MIB.yaml +17 -0
  169. data/data/ruby/snmp/mibs/SNMP-REPEATER-MIB.yaml +135 -0
  170. data/data/ruby/snmp/mibs/SNMP-TARGET-MIB.yaml +29 -0
  171. data/data/ruby/snmp/mibs/SNMP-USER-BASED-SM-MIB.yaml +35 -0
  172. data/data/ruby/snmp/mibs/SNMP-USM-DH-OBJECTS-MIB.yaml +21 -0
  173. data/data/ruby/snmp/mibs/SNMP-VIEW-BASED-ACM-MIB.yaml +37 -0
  174. data/data/ruby/snmp/mibs/SNMPv2-MIB.yaml +58 -0
  175. data/data/ruby/snmp/mibs/SNMPv2-SMI.yaml +17 -0
  176. data/data/ruby/snmp/mibs/SNMPv2-TM.yaml +9 -0
  177. data/data/ruby/snmp/mibs/SNMPv2-USEC-MIB.yaml +19 -0
  178. data/data/ruby/snmp/mibs/SONET-MIB.yaml +131 -0
  179. data/data/ruby/snmp/mibs/SOURCE-ROUTING-MIB.yaml +30 -0
  180. data/data/ruby/snmp/mibs/SYSAPPL-MIB.yaml +81 -0
  181. data/data/ruby/snmp/mibs/TCP-MIB.yaml +27 -0
  182. data/data/ruby/snmp/mibs/TCPIPX-MIB.yaml +24 -0
  183. data/data/ruby/snmp/mibs/TN3270E-MIB.yaml +105 -0
  184. data/data/ruby/snmp/mibs/TN3270E-RT-MIB.yaml +43 -0
  185. data/data/ruby/snmp/mibs/TOKEN-RING-RMON-MIB.yaml +183 -0
  186. data/data/ruby/snmp/mibs/TOKENRING-MIB.yaml +58 -0
  187. data/data/ruby/snmp/mibs/TOKENRING-STATION-SR-MIB.yaml +12 -0
  188. data/data/ruby/snmp/mibs/TRANSPORT-ADDRESS-MIB.yaml +19 -0
  189. data/data/ruby/snmp/mibs/TUNNEL-MIB.yaml +23 -0
  190. data/data/ruby/snmp/mibs/UDP-MIB.yaml +14 -0
  191. data/data/ruby/snmp/mibs/UPS-MIB.yaml +117 -0
  192. data/data/ruby/snmp/mibs/VDSL-LINE-MIB.yaml +173 -0
  193. data/data/ruby/snmp/mibs/VRRP-MIB.yaml +50 -0
  194. data/data/ruby/snmp/mibs/WWW-MIB.yaml +91 -0
  195. data/examples/dump.rb +15 -0
  196. data/examples/get.rb +7 -0
  197. data/examples/iftable.rb +14 -0
  198. data/examples/log_traps.rb +23 -0
  199. data/examples/set.rb +8 -0
  200. data/examples/walk.rb +9 -0
  201. data/lib/snmp.rb +10 -0
  202. data/lib/snmp/agent.rb +74 -0
  203. data/lib/snmp/ber.rb +332 -0
  204. data/lib/snmp/manager.rb +502 -0
  205. data/lib/snmp/mib.rb +259 -0
  206. data/lib/snmp/pdu.rb +363 -0
  207. data/lib/snmp/varbind.rb +520 -0
  208. data/setup.rb +1360 -0
  209. data/test/test_ber.rb +236 -0
  210. data/test/test_manager.rb +192 -0
  211. data/test/test_mib.rb +66 -0
  212. data/test/test_pdu.rb +184 -0
  213. data/test/test_varbind.rb +270 -0
  214. metadata +251 -0
@@ -0,0 +1,259 @@
1
+ #
2
+ # Copyright (c) 2004 David R. Halliday
3
+ # All rights reserved.
4
+ #
5
+ # This SNMP library is free software. Redistribution is permitted under the
6
+ # same terms and conditions as the standard Ruby distribution. See the
7
+ # COPYING file in the Ruby distribution for details.
8
+ #
9
+
10
+ require 'snmp/varbind'
11
+ require 'rbconfig'
12
+ require 'fileutils'
13
+ require 'yaml'
14
+
15
+ module SNMP
16
+
17
+ class MIB
18
+
19
+ #:stopdoc:
20
+ share_path = File.join(Config::CONFIG["datadir"], "ruby", "snmp", "mibs")
21
+ data_path = File.expand_path(
22
+ File.join(File.dirname(__FILE__), "..", "..", "data", "ruby", "snmp", "mibs")
23
+ )
24
+ if (File.exist?(share_path) && File.exist?(data_path))
25
+ warn "Found two MIB directories:\n #{share_path}\n #{data_path}\n" +
26
+ "Using MIB::DEFAULT_MIB_PATH=#{data_path}"
27
+ DEFAULT_MIB_PATH = data_path
28
+ elsif (File.exist?(data_path))
29
+ DEFAULT_MIB_PATH = data_path
30
+ elsif (File.exist?(share_path))
31
+ DEFAULT_MIB_PATH = share_path
32
+ else
33
+ warn "Could not find default MIB directory, tried:\n #{share_path}\n #{data_path}"
34
+ DEFAULT_MIB_PATH = nil
35
+ end
36
+ #:startdoc:
37
+
38
+ MODULE_EXT = 'yaml'
39
+
40
+ class ModuleNotLoadedError < RuntimeError; end
41
+
42
+ class << self
43
+ ##
44
+ # Import an SMIv2 MIB file for later loading. A module only needs to
45
+ # be installed once.
46
+ #
47
+ # module_file - the filename of the module to be imported
48
+ # mib_dir - the output directory for the serialized MIB data
49
+ #
50
+ # NOTE: This implementation requires that the 'smidump' tool is available
51
+ # in the PATH. This tool can be obtained from the libsmi website at
52
+ # http://http://www.ibr.cs.tu-bs.de/projects/libsmi/ .
53
+ #
54
+ # ALSO NOTE: The file format in future releases is subject to
55
+ # change. For now, it is a simple YAML hash with the MIB symbol
56
+ # as the key and the OID as the value. These files could be
57
+ # generated manually if 'smidump' is not available.
58
+ #
59
+ # Here is an example of the contents of an output file:
60
+ #
61
+ # ---
62
+ # ipDefaultTTL: 1.3.6.1.2.1.4.2
63
+ # ipForwDatagrams: 1.3.6.1.2.1.4.6
64
+ # ipOutRequests: 1.3.6.1.2.1.4.10
65
+ # ipOutNoRoutes: 1.3.6.1.2.1.4.12
66
+ # ipReasmTimeout: 1.3.6.1.2.1.4.13
67
+ # icmpInDestUnreachs: 1.3.6.1.2.1.5.3
68
+ #
69
+ def import_module(module_file, mib_dir=DEFAULT_MIB_PATH)
70
+ raise "smidump tool must be installed" unless import_supported?
71
+ FileUtils.makedirs mib_dir
72
+ mib_hash = `smidump -f python #{module_file}`
73
+ mib = eval_mib_data(mib_hash)
74
+ if mib
75
+ module_name = mib["moduleName"]
76
+ raise "#{module_file}: invalid file format; no module name" unless module_name
77
+ if mib["nodes"]
78
+ oid_hash = {}
79
+ mib["nodes"].each { |key, value| oid_hash[key] = value["oid"] }
80
+ File.open(module_file_name(module_name, mib_dir), 'w') do |file|
81
+ YAML.dump(oid_hash, file)
82
+ end
83
+ module_name
84
+ else
85
+ warn "*** No nodes defined in: #{module_file} ***"
86
+ nil
87
+ end
88
+ else
89
+ warn "*** Import failed for: #{module_file} ***"
90
+ nil
91
+ end
92
+ end
93
+
94
+ ##
95
+ # Returns the full filename of the imported MIB file for the given
96
+ # module name.
97
+ #
98
+ def module_file_name(module_name, mib_dir=DEFAULT_MIB_PATH)
99
+ File.join(mib_dir, module_name + "." + MODULE_EXT)
100
+ end
101
+
102
+ ##
103
+ # The MIB.import_module method is only supported if the external
104
+ # 'smidump' tool is available. This method returns true if a
105
+ # known version of the tool is available.
106
+ #
107
+ def import_supported?
108
+ `smidump --version` =~ /^smidump 0.4/ && $? == 0
109
+ end
110
+
111
+ ##
112
+ # Returns a list of MIB modules that have been imported. All of
113
+ # the current IETF MIBs should be available from the default
114
+ # MIB directory.
115
+ #
116
+ # If a regex is provided, then the module names are matched
117
+ # against that pattern.
118
+ #
119
+ def list_imported(regex=//, mib_dir=DEFAULT_MIB_PATH)
120
+ list = []
121
+ Dir["#{mib_dir}/*.#{MODULE_EXT}"].each do |name|
122
+ module_name = File.basename(name, ".*")
123
+ list << module_name if module_name =~ regex
124
+ end
125
+ list
126
+ end
127
+
128
+ private
129
+
130
+ def eval_mib_data(mib_hash)
131
+ ruby_hash = mib_hash.
132
+ gsub(':', '=>'). # fix hash syntax
133
+ gsub('(', '[').gsub(')', ']'). # fix tuple syntax
134
+ sub('FILENAME =', 'filename ='). # get rid of constants
135
+ sub('MIB =', 'mib =')
136
+ mib = nil
137
+ eval(ruby_hash)
138
+ mib
139
+ end
140
+ end # class methods
141
+
142
+ def initialize
143
+ @by_name = {}
144
+ @by_module_by_name = {}
145
+ end
146
+
147
+ ##
148
+ # Loads a module into this MIB. The module must be imported before it
149
+ # can be loaded. See MIB.import_module .
150
+ #
151
+ def load_module(module_name, mib_dir=DEFAULT_MIB_PATH)
152
+ oid_hash = nil
153
+ File.open(MIB.module_file_name(module_name, mib_dir)) do |file|
154
+ oid_hash = YAML.load(file.read)
155
+ end
156
+ @by_name.merge!(oid_hash) do |key, old, value|
157
+ warn "warning: overwriting old MIB name '#{key}'"
158
+ end
159
+ @by_module_by_name[module_name] = {}
160
+ @by_module_by_name[module_name].merge!(oid_hash)
161
+ end
162
+
163
+ ##
164
+ # Returns a VarBindList for the provided list of objects. If a
165
+ # string is provided it is interpretted as a symbolic OID.
166
+ #
167
+ # This method accepts many different kinds of objects:
168
+ # - single string object IDs e.g. "1.3.6.1" or "IF-MIB::ifTable.1.1"
169
+ # - single ObjectId
170
+ # - list of string object IDs
171
+ # - list of ObjectIds
172
+ # - list of VarBinds
173
+ #
174
+ def varbind_list(object_list, option=:KeepValue)
175
+ vb_list = VarBindList.new
176
+ if object_list.respond_to? :to_str
177
+ vb_list << oid(object_list).to_varbind
178
+ elsif object_list.respond_to? :to_varbind
179
+ vb_list << apply_option(object_list.to_varbind, option)
180
+ else
181
+ object_list.each do |item|
182
+ if item.respond_to? :to_str
183
+ varbind = oid(item).to_varbind
184
+ else
185
+ varbind = item.to_varbind
186
+ end
187
+ vb_list << apply_option(varbind, option)
188
+ end
189
+ end
190
+ vb_list
191
+ end
192
+
193
+ def apply_option(varbind, option)
194
+ if option == :NullValue
195
+ varbind.value = Null
196
+ elsif option != :KeepValue
197
+ raise ArgumentError, "invalid option: #{option.to_s}", caller
198
+ end
199
+ varbind
200
+ end
201
+ private :apply_option
202
+
203
+ ##
204
+ # Returns a VarBind object for the given name and value. The name
205
+ # can be a String, ObjectId, or anything that responds to :to_varbind.
206
+ #
207
+ # String names are in the format <ModuleName>::<NodeName>.<Index> with
208
+ # ModuleName and Index being optional.
209
+ #
210
+ def varbind(name, value=Null)
211
+ if name.respond_to? :to_str
212
+ vb = VarBind.new(oid(name), value)
213
+ else
214
+ vb = name.to_varbind
215
+ vb.value = value
216
+ end
217
+ vb
218
+ end
219
+
220
+ ##
221
+ # Returns an ObjectId for the given name. Names are in the format
222
+ # <ModuleName>::<NodeName>.<Index> with ModuleName and Index being
223
+ # optional.
224
+ #
225
+ def oid(name)
226
+ module_parts = name.to_str.split("::")
227
+ if module_parts.length == 1
228
+ parse_oid(@by_name, name.to_str)
229
+ elsif module_parts.length == 2
230
+ module_name = module_parts[0]
231
+ oid = module_parts[1]
232
+ module_hash = @by_module_by_name[module_name]
233
+ if module_hash
234
+ parse_oid(module_hash, oid)
235
+ else
236
+ raise ModuleNotLoadedError, "module '#{module_name}' not loaded"
237
+ end
238
+ else
239
+ raise ArgumentError, "invalid format: #{name.to_str}"
240
+ end
241
+ end
242
+
243
+ def parse_oid(node_hash, name)
244
+ oid_parts = name.split(".")
245
+ first_part = oid_parts.shift
246
+ oid_string = node_hash[first_part]
247
+ if oid_string
248
+ oid_array = oid_string.split(".")
249
+ else
250
+ oid_array = [first_part]
251
+ end
252
+ oid_array.concat(oid_parts)
253
+ ObjectId.new(oid_array)
254
+ end
255
+ private :parse_oid
256
+
257
+ end
258
+
259
+ end # module SNMP
@@ -0,0 +1,363 @@
1
+ #
2
+ # Copyright (c) 2004 David R. Halliday
3
+ # All rights reserved.
4
+ #
5
+ # This SNMP library is free software. Redistribution is permitted under the
6
+ # same terms and conditions as the standard Ruby distribution. See the
7
+ # COPYING file in the Ruby distribution for details.
8
+ #
9
+
10
+ require 'snmp/ber'
11
+ require 'snmp/varbind'
12
+
13
+ include SNMP::BER
14
+
15
+ module SNMP
16
+
17
+ # Exceptions thrown during message/pdu decoding
18
+ class UnsupportedVersion < RuntimeError; end
19
+ class UnsupportedPduTag < RuntimeError; end
20
+ class InvalidPduTag < RuntimeError; end
21
+ class ParseError < RuntimeError; end
22
+ class InvalidErrorStatus < RuntimeError; end
23
+ class InvalidTrapVarbind < RuntimeError; end
24
+ class InvalidGenericTrap < RuntimeError; end
25
+
26
+ class Message
27
+ attr_reader :version
28
+ attr_reader :community
29
+ attr_reader :pdu
30
+
31
+ class << self
32
+ def decode(data)
33
+ message_data, remainder = decode_sequence(data)
34
+ assert_no_remainder(remainder)
35
+ version, remainder = decode_version(message_data)
36
+ community, remainder = decode_octet_string(remainder)
37
+ pdu, remainder = decode_pdu(version, remainder)
38
+ assert_no_remainder(remainder)
39
+ Message.new(version, community, pdu)
40
+ end
41
+
42
+ def decode_version(data)
43
+ version_data, remainder = decode_integer(data)
44
+ if version_data == SNMP_V1
45
+ version = :SNMPv1
46
+ elsif version_data == SNMP_V2C
47
+ version = :SNMPv2c
48
+ else
49
+ raise UnsupportedVersion, version_data.to_s
50
+ end
51
+ return version, remainder
52
+ end
53
+
54
+ def decode_pdu(version, data)
55
+ pdu_tag, pdu_data, remainder = decode_tlv(data)
56
+ case pdu_tag
57
+ when GetRequest_PDU_TAG
58
+ pdu = PDU.decode(GetRequest, pdu_data)
59
+ when GetNextRequest_PDU_TAG
60
+ pdu = PDU.decode(GetNextRequest, pdu_data)
61
+ when Response_PDU_TAG
62
+ pdu = PDU.decode(Response, pdu_data)
63
+ when SetRequest_PDU_TAG
64
+ pdu = PDU.decode(SetRequest, pdu_data)
65
+ when SNMPv1_Trap_PDU_TAG
66
+ raise InvalidPduTag, "SNMPv1-trap not valid for #{version.to_s}" if version != :SNMPv1
67
+ pdu = SNMPv1_Trap.decode(pdu_data)
68
+ when GetBulkRequest_PDU_TAG
69
+ raise InvalidPduTag, "get-bulk not valid for #{version.to_s}" if version != :SNMPv2c
70
+ pdu = PDU.decode(GetBulkRequest, pdu_data)
71
+ when SNMPv2_Trap_PDU_TAG
72
+ raise InvalidPduTag, "SNMPv2c-trap not valid for #{version.to_s}" if version != :SNMPv2c
73
+ pdu = PDU.decode(SNMPv2_Trap, pdu_data)
74
+ else
75
+ raise UnsupportedPduTag, pdu_tag.to_s
76
+ end
77
+ return pdu, remainder
78
+ end
79
+ end
80
+
81
+ def initialize(version, community, pdu)
82
+ @version = version
83
+ @community = community
84
+ @pdu = pdu
85
+ end
86
+
87
+ def response
88
+ Message.new(@version, @community, Response.from_pdu(@pdu))
89
+ end
90
+
91
+ def encode_version(version)
92
+ if version == :SNMPv1
93
+ encode_integer(SNMP_V1)
94
+ elsif version == :SNMPv2c
95
+ encode_integer(SNMP_V2C)
96
+ else
97
+ raise UnsupportedVersion, version.to_s
98
+ end
99
+ end
100
+
101
+ def encode
102
+ data = encode_version(@version)
103
+ data << encode_octet_string(@community)
104
+ data << @pdu.encode
105
+ encode_sequence(data)
106
+ end
107
+ end
108
+
109
+ class PDU
110
+ attr_accessor :request_id
111
+ attr_accessor :error_index
112
+ attr_accessor :varbind_list
113
+
114
+ def self.decode(pdu_class, pdu_data)
115
+ request_id, remainder = decode_integer(pdu_data)
116
+ error_status, remainder = decode_integer(remainder)
117
+ error_index, remainder = decode_integer(remainder)
118
+ varbind_list, remainder = VarBindList.decode(remainder)
119
+ assert_no_remainder(remainder)
120
+ pdu_class.new(request_id, varbind_list, error_status, error_index)
121
+ end
122
+
123
+ ERROR_STATUS_NAME = {
124
+ 0 => :noError,
125
+ 1 => :tooBig,
126
+ 2 => :noSuchName,
127
+ 3 => :badValue,
128
+ 4 => :readOnly,
129
+ 5 => :genErr,
130
+ 6 => :noAccess,
131
+ 7 => :wrongType,
132
+ 8 => :wrongLength,
133
+ 9 => :wrongEncoding,
134
+ 10 => :wrongValue,
135
+ 11 => :noCreation,
136
+ 12 => :inconsistentValue,
137
+ 13 => :resourceUnavailable,
138
+ 14 => :commitFailed,
139
+ 15 => :undoFailed,
140
+ 16 => :authorizationError,
141
+ 17 => :notWritable,
142
+ 18 => :inconsistentName
143
+ }
144
+
145
+ ERROR_STATUS_CODE = ERROR_STATUS_NAME.invert
146
+
147
+ def initialize(request_id, varbind_list, error_status=0, error_index=0)
148
+ @request_id = request_id
149
+ self.error_status = error_status
150
+ @error_index = error_index.to_int
151
+ @varbind_list = varbind_list
152
+ end
153
+
154
+ def error_status=(status)
155
+ @error_status = ERROR_STATUS_CODE[status]
156
+ unless @error_status
157
+ if status.respond_to?(:to_int) && ERROR_STATUS_NAME[status.to_int]
158
+ @error_status = status
159
+ else
160
+ raise InvalidErrorStatus, status.to_s
161
+ end
162
+ end
163
+ end
164
+
165
+ def error_status
166
+ ERROR_STATUS_NAME[@error_status]
167
+ end
168
+
169
+ def encode_pdu(pdu_tag)
170
+ pdu_data = encode_integer(@request_id)
171
+ pdu_data << encode_integer(@error_status)
172
+ pdu_data << encode_integer(@error_index)
173
+ pdu_data << @varbind_list.encode
174
+ encode_tlv(pdu_tag, pdu_data)
175
+ end
176
+
177
+ def each_varbind(&block)
178
+ @varbind_list.each(&block)
179
+ end
180
+ end
181
+
182
+ class GetRequest < PDU
183
+ def encode
184
+ encode_pdu(GetRequest_PDU_TAG)
185
+ end
186
+ end
187
+
188
+ class GetNextRequest < PDU
189
+ def encode
190
+ encode_pdu(GetNextRequest_PDU_TAG)
191
+ end
192
+ end
193
+
194
+ class SetRequest < PDU
195
+ def encode
196
+ encode_pdu(SetRequest_PDU_TAG)
197
+ end
198
+ end
199
+
200
+ class GetBulkRequest < PDU
201
+ alias max_repetitions error_index
202
+ alias max_repetitions= error_index=
203
+
204
+ def encode
205
+ encode_pdu(GetBulkRequest_PDU_TAG)
206
+ end
207
+
208
+ def non_repeaters=(number)
209
+ @error_status = number
210
+ end
211
+
212
+ def non_repeaters
213
+ @error_status
214
+ end
215
+ end
216
+
217
+ class Response < PDU
218
+ class << self
219
+ def from_pdu(request)
220
+ Response.new(request.request_id, request.varbind_list,
221
+ request.error_status, request.error_index)
222
+ end
223
+ end
224
+
225
+ def encode
226
+ encode_pdu(Response_PDU_TAG)
227
+ end
228
+ end
229
+
230
+ ##
231
+ # The PDU class for traps in SNMPv2c. Methods are provided for retrieving
232
+ # the values of the mandatory varbinds: the system uptime and the OID of the
233
+ # trap. The complete varbind list is available through the usual varbind_list
234
+ # method. The first two varbinds in this list will always be the uptime
235
+ # and trap OID varbinds.
236
+ #
237
+ class SNMPv2_Trap < PDU
238
+ def encode
239
+ encode_pdu(SNMPv2_Trap_PDU_TAG)
240
+ end
241
+
242
+ ##
243
+ # Returns the source IP address for the trap, usually derived from the
244
+ # source IP address of the packet that delivered the trap.
245
+ #
246
+ attr_accessor :source_ip
247
+
248
+ ##
249
+ # Returns the value of the mandatory sysUpTime varbind for this trap.
250
+ #
251
+ # Throws InvalidTrapVarbind if the sysUpTime varbind is not present.
252
+ #
253
+ def sys_up_time
254
+ sys_up_time_oid = ObjectId.new("1.3.6.1.2.1.1.3.0")
255
+ varbind = @varbind_list[0]
256
+ if varbind && (varbind.name == sys_up_time_oid)
257
+ return varbind.value
258
+ else
259
+ raise InvalidTrapVarbind, "Expected sysUpTime.0, found " + varbind.to_s
260
+ end
261
+ end
262
+
263
+ ##
264
+ # Returns the value of the mandatory snmpTrapOID varbind for this trap.
265
+ #
266
+ # Throws InvalidTrapVarbind if the snmpTrapOID varbind is not present.
267
+ #
268
+ def trap_oid
269
+ snmp_trap_oid_oid = ObjectId.new("1.3.6.1.6.3.1.1.4.1.0")
270
+ varbind = @varbind_list[1]
271
+ if varbind && (varbind.name == snmp_trap_oid_oid)
272
+ return varbind.value
273
+ else
274
+ raise InvalidTrapVarbind, "Expected snmpTrapOID.0, found " + varbind.to_s
275
+ end
276
+ end
277
+ end
278
+
279
+ ##
280
+ # The PDU class for traps in SNMPv1.
281
+ #
282
+ class SNMPv1_Trap
283
+ ##
284
+ # Returns the source IP address for the trap, usually derived from the
285
+ # source IP address of the packet that delivered the trap.
286
+ #
287
+ attr_accessor :source_ip
288
+
289
+ attr_accessor :enterprise
290
+ attr_accessor :agent_addr
291
+ attr_accessor :specific_trap
292
+ attr_accessor :timestamp
293
+ attr_accessor :varbind_list
294
+
295
+ def self.decode(pdu_data)
296
+ oid_data, remainder = decode_object_id(pdu_data)
297
+ enterprise = ObjectId.new(oid_data)
298
+ ip_data, remainder = decode_ip_address(remainder)
299
+ agent_addr = IpAddress.new(ip_data)
300
+ generic_trap, remainder = decode_integer(remainder)
301
+ specific_trap, remainder = decode_integer(remainder)
302
+ time_data, remainder = decode_timeticks(remainder)
303
+ timestamp = TimeTicks.new(time_data)
304
+ varbind_list, remainder = VarBindList.decode(remainder)
305
+ assert_no_remainder(remainder)
306
+ SNMPv1_Trap.new(enterprise, agent_addr, generic_trap, specific_trap,
307
+ timestamp, varbind_list)
308
+ end
309
+
310
+ def initialize(enterprise, agent_addr, generic_trap, specific_trap, timestamp, varbind_list)
311
+ @enterprise = enterprise
312
+ @agent_addr = agent_addr
313
+ self.generic_trap = generic_trap
314
+ @specific_trap = specific_trap
315
+ @timestamp = timestamp
316
+ @varbind_list = varbind_list
317
+ end
318
+
319
+ # Name map for all of the generic traps defined in RFC 1157.
320
+ GENERIC_TRAP_NAME = {
321
+ 0 => :coldStart,
322
+ 1 => :warmStart,
323
+ 2 => :linkDown,
324
+ 3 => :linkUp,
325
+ 4 => :authenticationFailure,
326
+ 5 => :egpNeighborLoss,
327
+ 6 => :enterpriseSpecific
328
+ }
329
+
330
+ # Code map for all of the generic traps defined in RFC 1157.
331
+ GENERIC_TRAP_CODE = GENERIC_TRAP_NAME.invert
332
+
333
+ def generic_trap=(trap)
334
+ @generic_trap = GENERIC_TRAP_CODE[trap]
335
+ unless @generic_trap
336
+ if trap.respond_to?(:to_i) && GENERIC_TRAP_NAME[trap.to_i]
337
+ @generic_trap = trap
338
+ else
339
+ raise InvalidGenericTrap, trap.to_s
340
+ end
341
+ end
342
+ end
343
+
344
+ def generic_trap
345
+ GENERIC_TRAP_NAME[@generic_trap]
346
+ end
347
+
348
+ def encode
349
+ pdu_data = @enterprise.encode <<
350
+ @agent_addr.encode <<
351
+ encode_integer(@generic_trap) <<
352
+ encode_integer(@specific_trap) <<
353
+ @timestamp.encode <<
354
+ @varbind_list.encode
355
+ encode_tlv(SNMPv1_Trap_PDU_TAG, pdu_data)
356
+ end
357
+
358
+ def each_varbind(&block)
359
+ @varbind_list.each(&block)
360
+ end
361
+ end
362
+
363
+ end