ohai 6.24.2 → 7.0.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/lib/ohai.rb +1 -1
  2. data/lib/ohai/application.rb +5 -12
  3. data/lib/ohai/common/dmi.rb +126 -0
  4. data/lib/ohai/config.rb +9 -9
  5. data/lib/ohai/{plugins/darwin/hostname.rb → dsl.rb} +7 -7
  6. data/lib/ohai/dsl/plugin.rb +186 -0
  7. data/lib/ohai/dsl/plugin/versionvi.rb +62 -0
  8. data/lib/ohai/dsl/plugin/versionvii.rb +106 -0
  9. data/lib/ohai/exception.rb +11 -2
  10. data/lib/ohai/hints.rb +48 -0
  11. data/lib/ohai/loader.rb +205 -0
  12. data/lib/ohai/mixin/command.rb +14 -18
  13. data/lib/ohai/mixin/constant_helper.rb +53 -0
  14. data/lib/ohai/mixin/gce_metadata.rb +0 -2
  15. data/lib/ohai/{plugins/freebsd/uptime.rb → mixin/network_constants.rb} +15 -13
  16. data/lib/ohai/mixin/os.rb +59 -0
  17. data/lib/ohai/mixin/seconds_to_human.rb +48 -0
  18. data/lib/ohai/plugins/aix/cpu.rb +26 -22
  19. data/lib/ohai/plugins/aix/filesystem.rb +47 -48
  20. data/lib/ohai/plugins/aix/kernel.rb +11 -7
  21. data/lib/ohai/plugins/aix/memory.rb +8 -4
  22. data/lib/ohai/plugins/aix/network.rb +73 -82
  23. data/lib/ohai/plugins/aix/platform.rb +9 -6
  24. data/lib/ohai/plugins/aix/uptime.rb +14 -12
  25. data/lib/ohai/plugins/azure.rb +33 -11
  26. data/lib/ohai/plugins/c.rb +103 -78
  27. data/lib/ohai/plugins/chef.rb +20 -8
  28. data/lib/ohai/plugins/cloud.rb +234 -230
  29. data/lib/ohai/plugins/command.rb +6 -2
  30. data/lib/ohai/plugins/darwin/cpu.rb +10 -4
  31. data/lib/ohai/plugins/darwin/filesystem.rb +32 -32
  32. data/lib/ohai/plugins/darwin/network.rb +157 -154
  33. data/lib/ohai/plugins/darwin/platform.rb +18 -16
  34. data/lib/ohai/plugins/darwin/system_profiler.rb +49 -46
  35. data/lib/ohai/plugins/dmi.rb +102 -102
  36. data/lib/ohai/plugins/ec2.rb +30 -28
  37. data/lib/ohai/plugins/erlang.rb +19 -16
  38. data/lib/ohai/plugins/eucalyptus.rb +37 -36
  39. data/lib/ohai/plugins/freebsd/cpu.rb +34 -29
  40. data/lib/ohai/plugins/freebsd/filesystem.rb +32 -32
  41. data/lib/ohai/plugins/freebsd/memory.rb +35 -25
  42. data/lib/ohai/plugins/freebsd/network.rb +94 -89
  43. data/lib/ohai/plugins/freebsd/platform.rb +10 -4
  44. data/lib/ohai/plugins/freebsd/virtualization.rb +62 -61
  45. data/lib/ohai/plugins/gce.rb +30 -36
  46. data/lib/ohai/plugins/groovy.rb +14 -11
  47. data/lib/ohai/plugins/hostname.rb +132 -10
  48. data/lib/ohai/plugins/ip_scopes.rb +31 -16
  49. data/lib/ohai/plugins/java.rb +45 -21
  50. data/lib/ohai/plugins/kernel.rb +186 -19
  51. data/lib/ohai/plugins/keys.rb +6 -2
  52. data/lib/ohai/plugins/languages.rb +6 -2
  53. data/lib/ohai/plugins/linode.rb +38 -30
  54. data/lib/ohai/plugins/linux/block_device.rb +19 -15
  55. data/lib/ohai/plugins/linux/cpu.rb +42 -38
  56. data/lib/ohai/plugins/linux/filesystem.rb +70 -130
  57. data/lib/ohai/plugins/linux/lsb.rb +35 -35
  58. data/lib/ohai/plugins/linux/memory.rb +65 -61
  59. data/lib/ohai/plugins/linux/network.rb +343 -362
  60. data/lib/ohai/plugins/linux/platform.rb +90 -88
  61. data/lib/ohai/plugins/linux/virtualization.rb +95 -90
  62. data/lib/ohai/plugins/lua.rb +14 -10
  63. data/lib/ohai/plugins/mono.rb +17 -13
  64. data/lib/ohai/plugins/netbsd/cpu.rb +25 -23
  65. data/lib/ohai/plugins/netbsd/filesystem.rb +32 -32
  66. data/lib/ohai/plugins/netbsd/memory.rb +59 -59
  67. data/lib/ohai/plugins/netbsd/network.rb +92 -88
  68. data/lib/ohai/plugins/netbsd/platform.rb +10 -3
  69. data/lib/ohai/plugins/netbsd/virtualization.rb +41 -38
  70. data/lib/ohai/plugins/network.rb +131 -130
  71. data/lib/ohai/plugins/network_listeners.rb +26 -22
  72. data/lib/ohai/plugins/nodejs.rb +14 -10
  73. data/lib/ohai/plugins/ohai.rb +11 -6
  74. data/lib/ohai/plugins/ohai_time.rb +6 -2
  75. data/lib/ohai/plugins/openbsd/cpu.rb +20 -15
  76. data/lib/ohai/plugins/openbsd/filesystem.rb +32 -32
  77. data/lib/ohai/plugins/openbsd/memory.rb +59 -59
  78. data/lib/ohai/plugins/openbsd/network.rb +92 -88
  79. data/lib/ohai/plugins/openbsd/platform.rb +10 -4
  80. data/lib/ohai/plugins/openbsd/virtualization.rb +41 -39
  81. data/lib/ohai/plugins/openstack.rb +23 -21
  82. data/lib/ohai/plugins/os.rb +8 -32
  83. data/lib/ohai/plugins/passwd.rb +27 -22
  84. data/lib/ohai/plugins/perl.rb +21 -16
  85. data/lib/ohai/plugins/php.rb +15 -12
  86. data/lib/ohai/plugins/platform.rb +9 -9
  87. data/lib/ohai/plugins/{linux/kernel.rb → ps.rb} +13 -12
  88. data/lib/ohai/plugins/python.rb +16 -12
  89. data/lib/ohai/plugins/rackspace.rb +82 -77
  90. data/lib/ohai/plugins/root_group.rb +13 -9
  91. data/lib/ohai/plugins/ruby.rb +50 -48
  92. data/lib/ohai/plugins/sigar/cpu.rb +21 -17
  93. data/lib/ohai/plugins/sigar/filesystem.rb +26 -23
  94. data/lib/ohai/plugins/sigar/memory.rb +17 -14
  95. data/lib/ohai/plugins/sigar/network.rb +133 -67
  96. data/lib/ohai/plugins/sigar/network_route.rb +33 -28
  97. data/lib/ohai/plugins/sigar/platform.rb +10 -6
  98. data/lib/ohai/plugins/solaris2/cpu.rb +41 -33
  99. data/lib/ohai/plugins/solaris2/dmi.rb +146 -146
  100. data/lib/ohai/plugins/solaris2/filesystem.rb +74 -77
  101. data/lib/ohai/plugins/{aix/hostname.rb → solaris2/memory.rb} +7 -4
  102. data/lib/ohai/plugins/solaris2/network.rb +91 -86
  103. data/lib/ohai/plugins/solaris2/platform.rb +40 -36
  104. data/lib/ohai/plugins/solaris2/virtualization.rb +54 -59
  105. data/lib/ohai/plugins/solaris2/zpools.rb +38 -38
  106. data/lib/ohai/plugins/ssh_host_key.rb +49 -45
  107. data/lib/ohai/plugins/uptime.rb +87 -25
  108. data/lib/ohai/plugins/virtualization.rb +59 -55
  109. data/lib/ohai/plugins/windows/cpu.rb +44 -40
  110. data/lib/ohai/plugins/windows/filesystem.rb +28 -22
  111. data/lib/ohai/plugins/windows/network.rb +88 -79
  112. data/lib/ohai/plugins/windows/platform.rb +10 -3
  113. data/lib/ohai/provides_map.rb +184 -0
  114. data/lib/ohai/runner.rb +109 -0
  115. data/lib/ohai/system.rb +103 -184
  116. data/lib/ohai/version.rb +4 -3
  117. data/spec/data/plugins/___lib64___libc.so.6.output +135 -0
  118. data/spec/data/plugins/___lib___libc.so.6.output +135 -0
  119. data/spec/data/plugins/cc.output +135 -0
  120. data/spec/data/plugins/cl.output +135 -0
  121. data/spec/data/plugins/devenv.com.output +135 -0
  122. data/spec/data/plugins/env.output +71 -0
  123. data/spec/data/plugins/erl.output +144 -0
  124. data/spec/data/plugins/gcc.output +135 -0
  125. data/spec/data/plugins/groovy.output +159 -0
  126. data/spec/data/plugins/java.output +143 -0
  127. data/spec/data/plugins/lua.output +143 -0
  128. data/spec/data/plugins/messages.rb +8 -0
  129. data/spec/data/plugins/node.output +143 -0
  130. data/spec/data/plugins/perl.output +143 -0
  131. data/spec/data/plugins/php.output +143 -0
  132. data/spec/data/plugins/python.output +143 -0
  133. data/spec/data/plugins/uname.output +71 -0
  134. data/spec/data/plugins/v6message.rb +2 -0
  135. data/spec/data/plugins/v7message.rb +7 -0
  136. data/spec/data/plugins/what.output +135 -0
  137. data/spec/data/plugins/xlc.output +135 -0
  138. data/spec/spec_helper.rb +52 -18
  139. data/spec/support/integration_helper.rb +44 -0
  140. data/spec/unit/dsl/plugin_spec.rb +298 -0
  141. data/spec/unit/hints_spec.rb +65 -0
  142. data/spec/unit/loader_spec.rb +226 -0
  143. data/spec/unit/mixin/ec2_metadata_spec.rb +9 -9
  144. data/spec/unit/plugins/aix/cpu_spec.rb +17 -18
  145. data/spec/unit/plugins/aix/filesystem_spec.rb +20 -18
  146. data/spec/unit/plugins/aix/hostname_spec.rb +8 -4
  147. data/spec/unit/plugins/aix/kernel_spec.rb +13 -12
  148. data/spec/unit/plugins/aix/network_spec.rb +66 -45
  149. data/spec/unit/plugins/aix/platform_spec.rb +11 -10
  150. data/spec/unit/plugins/aix/uptime_spec.rb +7 -8
  151. data/spec/unit/plugins/azure_spec.rb +20 -21
  152. data/spec/unit/plugins/c_spec.rb +88 -87
  153. data/spec/unit/plugins/chef_spec.rb +5 -6
  154. data/spec/unit/plugins/cloud_spec.rb +81 -82
  155. data/spec/unit/plugins/darwin/cpu_spec.rb +8 -9
  156. data/spec/unit/plugins/darwin/hostname_spec.rb +23 -11
  157. data/spec/unit/plugins/darwin/kernel_spec.rb +39 -16
  158. data/spec/unit/plugins/darwin/network_spec.rb +375 -378
  159. data/spec/unit/plugins/darwin/platform_spec.rb +23 -50
  160. data/spec/unit/plugins/darwin/system_profiler_spec.rb +7 -8
  161. data/spec/unit/plugins/dmi_spec.rb +7 -12
  162. data/spec/unit/plugins/ec2_spec.rb +70 -59
  163. data/spec/unit/plugins/erlang_spec.rb +18 -20
  164. data/spec/unit/plugins/eucalyptus_spec.rb +37 -33
  165. data/spec/unit/plugins/fail_spec.rb +276 -23
  166. data/spec/unit/plugins/freebsd/hostname_spec.rb +11 -9
  167. data/spec/unit/plugins/freebsd/kernel_spec.rb +8 -8
  168. data/spec/unit/plugins/freebsd/platform_spec.rb +9 -10
  169. data/spec/unit/plugins/freebsd/virtualization_spec.rb +32 -32
  170. data/spec/unit/plugins/gce_spec.rb +19 -25
  171. data/spec/unit/plugins/groovy_spec.rb +10 -16
  172. data/spec/unit/plugins/hostname_spec.rb +45 -20
  173. data/spec/unit/plugins/ip_scopes_spec.rb +97 -0
  174. data/spec/unit/plugins/java_spec.rb +145 -99
  175. data/spec/unit/plugins/kernel_spec.rb +51 -15
  176. data/spec/unit/plugins/linode_spec.rb +31 -29
  177. data/spec/unit/plugins/linux/cpu_spec.rb +42 -43
  178. data/spec/unit/plugins/linux/filesystem_spec.rb +143 -324
  179. data/spec/unit/plugins/linux/hostname_spec.rb +16 -23
  180. data/spec/unit/plugins/linux/kernel_spec.rb +22 -8
  181. data/spec/unit/plugins/linux/lsb_spec.rb +54 -56
  182. data/spec/unit/plugins/linux/network_spec.rb +196 -238
  183. data/spec/unit/plugins/linux/platform_spec.rb +186 -205
  184. data/spec/unit/plugins/linux/uptime_spec.rb +13 -25
  185. data/spec/unit/plugins/linux/virtualization_spec.rb +149 -129
  186. data/spec/unit/plugins/lua_spec.rb +12 -15
  187. data/spec/unit/plugins/mono_spec.rb +10 -15
  188. data/spec/unit/plugins/netbsd/hostname_spec.rb +17 -7
  189. data/spec/unit/plugins/netbsd/kernel_spec.rb +8 -8
  190. data/spec/unit/plugins/netbsd/platform_spec.rb +8 -10
  191. data/spec/unit/plugins/network_spec.rb +358 -235
  192. data/spec/unit/plugins/nodejs_spec.rb +13 -15
  193. data/spec/unit/plugins/ohai_spec.rb +3 -4
  194. data/spec/unit/plugins/ohai_time_spec.rb +8 -9
  195. data/spec/unit/plugins/openbsd/hostname_spec.rb +21 -11
  196. data/spec/unit/plugins/openbsd/kernel_spec.rb +8 -8
  197. data/spec/unit/plugins/openbsd/platform_spec.rb +6 -12
  198. data/spec/unit/plugins/openstack_spec.rb +214 -0
  199. data/spec/unit/plugins/os_spec.rb +13 -16
  200. data/spec/unit/plugins/passwd_spec.rb +12 -13
  201. data/spec/unit/plugins/perl_spec.rb +20 -44
  202. data/spec/unit/plugins/php_spec.rb +20 -15
  203. data/spec/unit/plugins/platform_spec.rb +24 -29
  204. data/spec/unit/plugins/python_spec.rb +13 -17
  205. data/spec/unit/plugins/rackspace_spec.rb +90 -57
  206. data/spec/unit/plugins/root_group_spec.rb +17 -19
  207. data/spec/unit/plugins/ruby_spec.rb +15 -16
  208. data/spec/unit/plugins/sigar/network_route_spec.rb +12 -13
  209. data/spec/unit/plugins/solaris2/cpu_spec.rb +82 -0
  210. data/spec/unit/plugins/solaris2/hostname_spec.rb +12 -13
  211. data/spec/unit/plugins/solaris2/kernel_spec.rb +11 -15
  212. data/{lib/ohai/plugins/openbsd/uptime.rb → spec/unit/plugins/solaris2/memory_spec.rb} +10 -12
  213. data/spec/unit/plugins/solaris2/network_spec.rb +44 -20
  214. data/spec/unit/plugins/solaris2/platform_spec.rb +13 -22
  215. data/spec/unit/plugins/solaris2/virtualization_spec.rb +37 -45
  216. data/spec/unit/plugins/ssh_host_keys_spec.rb +11 -15
  217. data/spec/unit/provides_map_spec.rb +208 -0
  218. data/spec/unit/runner_spec.rb +488 -0
  219. data/spec/unit/system_spec.rb +639 -104
  220. metadata +151 -87
  221. checksums.yaml +0 -7
  222. data/lib/ohai/mixin/from_file.rb +0 -36
  223. data/lib/ohai/plugins/aix/ps.rb +0 -23
  224. data/lib/ohai/plugins/darwin/kernel.rb +0 -37
  225. data/lib/ohai/plugins/darwin/ps.rb +0 -23
  226. data/lib/ohai/plugins/darwin/uptime.rb +0 -32
  227. data/lib/ohai/plugins/dmi_common.rb +0 -121
  228. data/lib/ohai/plugins/freebsd/hostname.rb +0 -22
  229. data/lib/ohai/plugins/freebsd/kernel.rb +0 -37
  230. data/lib/ohai/plugins/freebsd/ps.rb +0 -24
  231. data/lib/ohai/plugins/hpux/cpu.rb +0 -19
  232. data/lib/ohai/plugins/hpux/filesystem.rb +0 -19
  233. data/lib/ohai/plugins/hpux/hostname.rb +0 -19
  234. data/lib/ohai/plugins/hpux/memory.rb +0 -19
  235. data/lib/ohai/plugins/hpux/network.rb +0 -19
  236. data/lib/ohai/plugins/hpux/platform.rb +0 -19
  237. data/lib/ohai/plugins/hpux/ps.rb +0 -23
  238. data/lib/ohai/plugins/hpux/uptime.rb +0 -19
  239. data/lib/ohai/plugins/linux/hostname.rb +0 -40
  240. data/lib/ohai/plugins/linux/ps.rb +0 -23
  241. data/lib/ohai/plugins/linux/uptime.rb +0 -28
  242. data/lib/ohai/plugins/netbsd/hostname.rb +0 -22
  243. data/lib/ohai/plugins/netbsd/kernel.rb +0 -35
  244. data/lib/ohai/plugins/netbsd/ps.rb +0 -24
  245. data/lib/ohai/plugins/netbsd/uptime.rb +0 -31
  246. data/lib/ohai/plugins/openbsd/hostname.rb +0 -22
  247. data/lib/ohai/plugins/openbsd/kernel.rb +0 -35
  248. data/lib/ohai/plugins/openbsd/ps.rb +0 -24
  249. data/lib/ohai/plugins/sigar/hostname.rb +0 -28
  250. data/lib/ohai/plugins/sigar/uptime.rb +0 -27
  251. data/lib/ohai/plugins/solaris2/hostname.rb +0 -34
  252. data/lib/ohai/plugins/solaris2/kernel.rb +0 -40
  253. data/lib/ohai/plugins/solaris2/ps.rb +0 -23
  254. data/lib/ohai/plugins/solaris2/uptime.rb +0 -36
  255. data/lib/ohai/plugins/windows/hostname.rb +0 -33
  256. data/lib/ohai/plugins/windows/kernel.rb +0 -58
  257. data/lib/ohai/plugins/windows/kernel_devices.rb +0 -39
  258. data/lib/ohai/plugins/windows/uptime.rb +0 -23
  259. data/spec/unit/mixin/from_file_spec.rb +0 -53
@@ -16,44 +16,44 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
- provides "lsb"
19
+ Ohai.plugin(:LSB) do
20
+ provides "lsb"
20
21
 
21
- lsb Mash.new
22
+ collect_data(:linux) do
23
+ lsb Mash.new
22
24
 
23
- if File.exists?("/etc/lsb-release")
24
- File.open("/etc/lsb-release").each do |line|
25
- case line
26
- when /^DISTRIB_ID=["']?(.+?)["']?$/
27
- lsb[:id] = $1
28
- when /^DISTRIB_RELEASE=["']?(.+?)["']?$/
29
- lsb[:release] = $1
30
- when /^DISTRIB_CODENAME=["']?(.+?)["']?$/
31
- lsb[:codename] = $1
32
- when /^DISTRIB_DESCRIPTION=["']?(.+?)["']?$/
33
- lsb[:description] = $1
34
- end
35
- end
36
- elsif File.exists?("/usr/bin/lsb_release")
37
- # Fedora/Redhat, requires redhat-lsb package
38
- popen4("lsb_release -a") do |pid, stdin, stdout, stderr|
39
-
40
- stdin.close
41
- stdout.each do |line|
42
- case line
43
- when /^Distributor ID:\s+(.+)$/
44
- lsb[:id] = $1
45
- when /^Description:\s+(.+)$/
46
- lsb[:description] = $1
47
- when /^Release:\s+(.+)$/
48
- lsb[:release] = $1
49
- when /^Codename:\s+(.+)$/
50
- lsb[:codename] = $1
51
- else
52
- lsb[:id] = line
25
+ if File.exists?("/etc/lsb-release")
26
+ File.open("/etc/lsb-release").each do |line|
27
+ case line
28
+ when /^DISTRIB_ID=["']?(.+?)["']?$/
29
+ lsb[:id] = $1
30
+ when /^DISTRIB_RELEASE=["']?(.+?)["']?$/
31
+ lsb[:release] = $1
32
+ when /^DISTRIB_CODENAME=["']?(.+?)["']?$/
33
+ lsb[:codename] = $1
34
+ when /^DISTRIB_DESCRIPTION=["']?(.+?)["']?$/
35
+ lsb[:description] = $1
36
+ end
53
37
  end
54
-
38
+ elsif File.exists?("/usr/bin/lsb_release")
39
+ # Fedora/Redhat, requires redhat-lsb package
40
+ so = shell_out("lsb_release -a")
41
+ so.stdout.lines do |line|
42
+ case line
43
+ when /^Distributor ID:\s+(.+)$/
44
+ lsb[:id] = $1
45
+ when /^Description:\s+(.+)$/
46
+ lsb[:description] = $1
47
+ when /^Release:\s+(.+)$/
48
+ lsb[:release] = $1
49
+ when /^Codename:\s+(.+)$/
50
+ lsb[:codename] = $1
51
+ else
52
+ lsb[:id] = line
53
+ end
54
+ end
55
+ else
56
+ Ohai::Log.debug("Skipping LSB, cannot find /etc/lsb-release or /usr/bin/lsb_release")
55
57
  end
56
58
  end
57
- else
58
- Ohai::Log.debug("Skipping LSB, cannot find /etc/lsb-release or /usr/bin/lsb_release")
59
59
  end
@@ -16,68 +16,72 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
- provides "memory"
19
+ Ohai.plugin(:Memory) do
20
+ provides "memory", "memory/swap"
20
21
 
21
- memory Mash.new
22
- memory[:swap] = Mash.new
22
+ collect_data(:linux) do
23
+ memory Mash.new
24
+ memory[:swap] = Mash.new
23
25
 
24
- File.open("/proc/meminfo").each do |line|
25
- case line
26
- when /^MemTotal:\s+(\d+) (.+)$/
27
- memory[:total] = "#{$1}#{$2}"
28
- when /^MemFree:\s+(\d+) (.+)$/
29
- memory[:free] = "#{$1}#{$2}"
30
- when /^Buffers:\s+(\d+) (.+)$/
31
- memory[:buffers] = "#{$1}#{$2}"
32
- when /^Cached:\s+(\d+) (.+)$/
33
- memory[:cached] = "#{$1}#{$2}"
34
- when /^Active:\s+(\d+) (.+)$/
35
- memory[:active] = "#{$1}#{$2}"
36
- when /^Inactive:\s+(\d+) (.+)$/
37
- memory[:inactive] = "#{$1}#{$2}"
38
- when /^HighTotal:\s+(\d+) (.+)$/
39
- memory[:high_total] = "#{$1}#{$2}"
40
- when /^HighFree:\s+(\d+) (.+)$/
41
- memory[:high_free] = "#{$1}#{$2}"
42
- when /^LowTotal:\s+(\d+) (.+)$/
43
- memory[:low_total] = "#{$1}#{$2}"
44
- when /^LowFree:\s+(\d+) (.+)$/
45
- memory[:low_free] = "#{$1}#{$2}"
46
- when /^Dirty:\s+(\d+) (.+)$/
47
- memory[:dirty] = "#{$1}#{$2}"
48
- when /^Writeback:\s+(\d+) (.+)$/
49
- memory[:writeback] = "#{$1}#{$2}"
50
- when /^AnonPages:\s+(\d+) (.+)$/
51
- memory[:anon_pages] = "#{$1}#{$2}"
52
- when /^Mapped:\s+(\d+) (.+)$/
53
- memory[:mapped] = "#{$1}#{$2}"
54
- when /^Slab:\s+(\d+) (.+)$/
55
- memory[:slab] = "#{$1}#{$2}"
56
- when /^SReclaimable:\s+(\d+) (.+)$/
57
- memory[:slab_reclaimable] = "#{$1}#{$2}"
58
- when /^SUnreclaim:\s+(\d+) (.+)$/
59
- memory[:slab_unreclaim] = "#{$1}#{$2}"
60
- when /^PageTables:\s+(\d+) (.+)$/
61
- memory[:page_tables] = "#{$1}#{$2}"
62
- when /^NFS_Unstable:\s+(\d+) (.+)$/
63
- memory[:nfs_unstable] = "#{$1}#{$2}"
64
- when /^Bounce:\s+(\d+) (.+)$/
65
- memory[:bounce] = "#{$1}#{$2}"
66
- when /^CommitLimit:\s+(\d+) (.+)$/
67
- memory[:commit_limit] = "#{$1}#{$2}"
68
- when /^Committed_AS:\s+(\d+) (.+)$/
69
- memory[:committed_as] = "#{$1}#{$2}"
70
- when /^VmallocTotal:\s+(\d+) (.+)$/
71
- memory[:vmalloc_total] = "#{$1}#{$2}"
72
- when /^VmallocUsed:\s+(\d+) (.+)$/
73
- memory[:vmalloc_used] = "#{$1}#{$2}"
74
- when /^VmallocChunk:\s+(\d+) (.+)$/
75
- memory[:vmalloc_chunk] = "#{$1}#{$2}"
76
- when /^SwapCached:\s+(\d+) (.+)$/
77
- memory[:swap][:cached] = "#{$1}#{$2}"
78
- when /^SwapTotal:\s+(\d+) (.+)$/
79
- memory[:swap][:total] = "#{$1}#{$2}"
80
- when /^SwapFree:\s+(\d+) (.+)$/
81
- memory[:swap][:free] = "#{$1}#{$2}"
26
+ File.open("/proc/meminfo").each do |line|
27
+ case line
28
+ when /^MemTotal:\s+(\d+) (.+)$/
29
+ memory[:total] = "#{$1}#{$2}"
30
+ when /^MemFree:\s+(\d+) (.+)$/
31
+ memory[:free] = "#{$1}#{$2}"
32
+ when /^Buffers:\s+(\d+) (.+)$/
33
+ memory[:buffers] = "#{$1}#{$2}"
34
+ when /^Cached:\s+(\d+) (.+)$/
35
+ memory[:cached] = "#{$1}#{$2}"
36
+ when /^Active:\s+(\d+) (.+)$/
37
+ memory[:active] = "#{$1}#{$2}"
38
+ when /^Inactive:\s+(\d+) (.+)$/
39
+ memory[:inactive] = "#{$1}#{$2}"
40
+ when /^HighTotal:\s+(\d+) (.+)$/
41
+ memory[:high_total] = "#{$1}#{$2}"
42
+ when /^HighFree:\s+(\d+) (.+)$/
43
+ memory[:high_free] = "#{$1}#{$2}"
44
+ when /^LowTotal:\s+(\d+) (.+)$/
45
+ memory[:low_total] = "#{$1}#{$2}"
46
+ when /^LowFree:\s+(\d+) (.+)$/
47
+ memory[:low_free] = "#{$1}#{$2}"
48
+ when /^Dirty:\s+(\d+) (.+)$/
49
+ memory[:dirty] = "#{$1}#{$2}"
50
+ when /^Writeback:\s+(\d+) (.+)$/
51
+ memory[:writeback] = "#{$1}#{$2}"
52
+ when /^AnonPages:\s+(\d+) (.+)$/
53
+ memory[:anon_pages] = "#{$1}#{$2}"
54
+ when /^Mapped:\s+(\d+) (.+)$/
55
+ memory[:mapped] = "#{$1}#{$2}"
56
+ when /^Slab:\s+(\d+) (.+)$/
57
+ memory[:slab] = "#{$1}#{$2}"
58
+ when /^SReclaimable:\s+(\d+) (.+)$/
59
+ memory[:slab_reclaimable] = "#{$1}#{$2}"
60
+ when /^SUnreclaim:\s+(\d+) (.+)$/
61
+ memory[:slab_unreclaim] = "#{$1}#{$2}"
62
+ when /^PageTables:\s+(\d+) (.+)$/
63
+ memory[:page_tables] = "#{$1}#{$2}"
64
+ when /^NFS_Unstable:\s+(\d+) (.+)$/
65
+ memory[:nfs_unstable] = "#{$1}#{$2}"
66
+ when /^Bounce:\s+(\d+) (.+)$/
67
+ memory[:bounce] = "#{$1}#{$2}"
68
+ when /^CommitLimit:\s+(\d+) (.+)$/
69
+ memory[:commit_limit] = "#{$1}#{$2}"
70
+ when /^Committed_AS:\s+(\d+) (.+)$/
71
+ memory[:committed_as] = "#{$1}#{$2}"
72
+ when /^VmallocTotal:\s+(\d+) (.+)$/
73
+ memory[:vmalloc_total] = "#{$1}#{$2}"
74
+ when /^VmallocUsed:\s+(\d+) (.+)$/
75
+ memory[:vmalloc_used] = "#{$1}#{$2}"
76
+ when /^VmallocChunk:\s+(\d+) (.+)$/
77
+ memory[:vmalloc_chunk] = "#{$1}#{$2}"
78
+ when /^SwapCached:\s+(\d+) (.+)$/
79
+ memory[:swap][:cached] = "#{$1}#{$2}"
80
+ when /^SwapTotal:\s+(\d+) (.+)$/
81
+ memory[:swap][:total] = "#{$1}#{$2}"
82
+ when /^SwapFree:\s+(\d+) (.+)$/
83
+ memory[:swap][:free] = "#{$1}#{$2}"
84
+ end
85
+ end
82
86
  end
83
87
  end
@@ -16,406 +16,387 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
- require 'ipaddr'
20
- provides "network", "counters/network"
21
-
22
- def encaps_lookup(encap)
23
- return "Loopback" if encap.eql?("Local Loopback") || encap.eql?("loopback")
24
- return "PPP" if encap.eql?("Point-to-Point Protocol")
25
- return "SLIP" if encap.eql?("Serial Line IP")
26
- return "VJSLIP" if encap.eql?("VJ Serial Line IP")
27
- return "IPIP" if encap.eql?("IPIP Tunnel")
28
- return "6to4" if encap.eql?("IPv6-in-IPv4")
29
- return "Ethernet" if encap.eql?("ether")
30
- encap
31
- end
19
+ Ohai.plugin(:Network) do
20
+ provides "network", "network/interfaces"
21
+ provides "counters/network", "counters/network/interfaces"
22
+ provides "ipaddress", "ip6address", "macaddress"
23
+
24
+ def linux_encaps_lookup(encap)
25
+ return "Loopback" if encap.eql?("Local Loopback") || encap.eql?("loopback")
26
+ return "PPP" if encap.eql?("Point-to-Point Protocol")
27
+ return "SLIP" if encap.eql?("Serial Line IP")
28
+ return "VJSLIP" if encap.eql?("VJ Serial Line IP")
29
+ return "IPIP" if encap.eql?("IPIP Tunnel")
30
+ return "6to4" if encap.eql?("IPv6-in-IPv4")
31
+ return "Ethernet" if encap.eql?("ether")
32
+ encap
33
+ end
32
34
 
33
- iface = Mash.new
34
- net_counters = Mash.new
35
-
36
- # Match the lead line for an interface from iproute2
37
- # 3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
38
- # The '@eth0:' portion doesn't exist on primary interfaces and thus is optional in the regex
39
- IPROUTE_INT_REGEX = /^(\d+): ([0-9a-zA-Z@:\.\-_]*?)(@[0-9a-zA-Z]+|):\s/
40
-
41
- if File.exist?("/sbin/ip")
42
-
43
- # families to get default routes from
44
- families = [
45
- {
46
- :name => "inet",
47
- :default_route => "0.0.0.0/0",
48
- :default_prefix => :default,
49
- :neighbour_attribute => :arp
50
- },
51
- {
52
- :name => "inet6",
53
- :default_route => "::/0",
54
- :default_prefix => :default_inet6,
55
- :neighbour_attribute => :neighbour_inet6
56
- }
57
- ]
58
-
59
- popen4("ip addr") do |pid, stdin, stdout, stderr|
60
- stdin.close
61
- cint = nil
62
- stdout.each do |line|
63
- if line =~ IPROUTE_INT_REGEX
64
- cint = $2
65
- iface[cint] = Mash.new
66
- if cint =~ /^(\w+)(\d+.*)/
67
- iface[cint][:type] = $1
68
- iface[cint][:number] = $2
69
- end
35
+ collect_data(:linux) do
36
+ require 'ipaddr'
37
+
38
+ iface = Mash.new
39
+ net_counters = Mash.new
40
+
41
+ network Mash.new unless network
42
+ network[:interfaces] = Mash.new unless network[:interfaces]
43
+ counters Mash.new unless counters
44
+ counters[:network] = Mash.new unless counters[:network]
45
+
46
+ # Match the lead line for an interface from iproute2
47
+ # 3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
48
+ # The '@eth0:' portion doesn't exist on primary interfaces and thus is optional in the regex
49
+ IPROUTE_INT_REGEX = /^(\d+): ([0-9a-zA-Z@:\.\-_]*?)(@[0-9a-zA-Z]+|):\s/
50
+
51
+ if File.exist?("/sbin/ip")
52
+
53
+ # families to get default routes from
54
+ families = [{
55
+ :name => "inet",
56
+ :default_route => "0.0.0.0/0",
57
+ :default_prefix => :default,
58
+ :neighbour_attribute => :arp
59
+ },{
60
+ :name => "inet6",
61
+ :default_route => "::/0",
62
+ :default_prefix => :default_inet6,
63
+ :neighbour_attribute => :neighbour_inet6
64
+ }]
65
+
66
+ so = shell_out("ip addr")
67
+ cint = nil
68
+ so.stdout.lines do |line|
69
+ if line =~ IPROUTE_INT_REGEX
70
+ cint = $2
71
+ iface[cint] = Mash.new
72
+ if cint =~ /^(\w+)(\d+.*)/
73
+ iface[cint][:type] = $1
74
+ iface[cint][:number] = $2
75
+ end
70
76
 
71
- if line =~ /mtu (\d+)/
72
- iface[cint][:mtu] = $1
73
- end
77
+ if line =~ /mtu (\d+)/
78
+ iface[cint][:mtu] = $1
79
+ end
74
80
 
75
- flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|LOWER_UP|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)/)
76
- if flags.length > 1
77
- iface[cint][:flags] = flags.flatten.uniq
78
- end
79
- end
80
- if line =~ /link\/(\w+) ([\da-f\:]+) /
81
- iface[cint][:encapsulation] = encaps_lookup($1)
82
- unless $2 == "00:00:00:00:00:00"
83
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
84
- iface[cint][:addresses][$2.upcase] = { "family" => "lladdr" }
81
+ flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|LOWER_UP|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)/)
82
+ if flags.length > 1
83
+ iface[cint][:flags] = flags.flatten.uniq
84
+ end
85
85
  end
86
- end
87
- if line =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/
88
- tmp_addr, tmp_prefix = $1, $3
89
- tmp_prefix ||= "32"
90
- original_int = nil
91
-
92
- # Are we a formerly aliased interface?
93
- if line =~ /#{cint}:(\d+)$/
94
- sub_int = $1
95
- alias_int = "#{cint}:#{sub_int}"
96
- original_int = cint
97
- cint = alias_int
86
+ if line =~ /link\/(\w+) ([\da-f\:]+) /
87
+ iface[cint][:encapsulation] = linux_encaps_lookup($1)
88
+ unless $2 == "00:00:00:00:00:00"
89
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
90
+ iface[cint][:addresses][$2.upcase] = { "family" => "lladdr" }
91
+ end
98
92
  end
93
+ if line =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/
94
+ tmp_addr, tmp_prefix = $1, $3
95
+ tmp_prefix ||= "32"
96
+ original_int = nil
97
+
98
+ # Are we a formerly aliased interface?
99
+ if line =~ /#{cint}:(\d+)$/
100
+ sub_int = $1
101
+ alias_int = "#{cint}:#{sub_int}"
102
+ original_int = cint
103
+ cint = alias_int
104
+ end
99
105
 
100
- iface[cint] = Mash.new unless iface[cint] # Create the fake alias interface if needed
101
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
102
- iface[cint][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix }
103
- iface[cint][:addresses][tmp_addr][:netmask] = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s
104
-
105
- if line =~ /peer (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
106
- iface[cint][:addresses][tmp_addr][:peer] = $1
107
- end
106
+ iface[cint] = Mash.new unless iface[cint] # Create the fake alias interface if needed
107
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
108
+ iface[cint][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix }
109
+ iface[cint][:addresses][tmp_addr][:netmask] = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s
108
110
 
109
- if line =~ /brd (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
110
- iface[cint][:addresses][tmp_addr][:broadcast] = $1
111
- end
111
+ if line =~ /peer (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
112
+ iface[cint][:addresses][tmp_addr][:peer] = $1
113
+ end
112
114
 
113
- if line =~ /scope (\w+)/
114
- iface[cint][:addresses][tmp_addr][:scope] = ($1.eql?("host") ? "Node" : $1.capitalize)
115
- end
115
+ if line =~ /brd (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
116
+ iface[cint][:addresses][tmp_addr][:broadcast] = $1
117
+ end
116
118
 
117
- # If we found we were an an alias interface, restore cint to its original value
118
- cint = original_int unless original_int.nil?
119
- end
120
- if line =~ /inet6 ([a-f0-9\:]+)\/(\d+) scope (\w+)/
121
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
122
- tmp_addr = $1
123
- iface[cint][:addresses][tmp_addr] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("host") ? "Node" : $3.capitalize) }
124
- end
125
- end
126
- end
119
+ if line =~ /scope (\w+)/
120
+ iface[cint][:addresses][tmp_addr][:scope] = ($1.eql?("host") ? "Node" : $1.capitalize)
121
+ end
127
122
 
128
- popen4("ip -d -s link") do |pid, stdin, stdout, stderr|
129
- stdin.close
130
- tmp_int = nil
131
- on_rx = true
132
- stdout.each do |line|
133
- if line =~ IPROUTE_INT_REGEX
134
- tmp_int = $2
135
- net_counters[tmp_int] = Mash.new unless net_counters[tmp_int]
123
+ # If we found we were an an alias interface, restore cint to its original value
124
+ cint = original_int unless original_int.nil?
125
+ end
126
+ if line =~ /inet6 ([a-f0-9\:]+)\/(\d+) scope (\w+)/
127
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
128
+ tmp_addr = $1
129
+ iface[cint][:addresses][tmp_addr] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("host") ? "Node" : $3.capitalize) }
130
+ end
136
131
  end
137
132
 
138
- if line =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
139
- int = on_rx ? :rx : :tx
140
- net_counters[tmp_int][int] = Mash.new unless net_counters[tmp_int][int]
141
- net_counters[tmp_int][int][:bytes] = $1
142
- net_counters[tmp_int][int][:packets] = $2
143
- net_counters[tmp_int][int][:errors] = $3
144
- net_counters[tmp_int][int][:drop] = $4
145
- if(int == :rx)
146
- net_counters[tmp_int][int][:overrun] = $5
147
- else
148
- net_counters[tmp_int][int][:carrier] = $5
149
- net_counters[tmp_int][int][:collisions] = $6
133
+ so = shell_out("ip -d -s link")
134
+ tmp_int = nil
135
+ on_rx = true
136
+ so.stdout.lines do |line|
137
+ if line =~ IPROUTE_INT_REGEX
138
+ tmp_int = $2
139
+ net_counters[tmp_int] = Mash.new unless net_counters[tmp_int]
150
140
  end
151
141
 
152
- on_rx = !on_rx
153
- end
142
+ if line =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
143
+ int = on_rx ? :rx : :tx
144
+ net_counters[tmp_int][int] = Mash.new unless net_counters[tmp_int][int]
145
+ net_counters[tmp_int][int][:bytes] = $1
146
+ net_counters[tmp_int][int][:packets] = $2
147
+ net_counters[tmp_int][int][:errors] = $3
148
+ net_counters[tmp_int][int][:drop] = $4
149
+ if(int == :rx)
150
+ net_counters[tmp_int][int][:overrun] = $5
151
+ else
152
+ net_counters[tmp_int][int][:carrier] = $5
153
+ net_counters[tmp_int][int][:collisions] = $6
154
+ end
154
155
 
155
- if line =~ /qlen (\d+)/
156
- net_counters[tmp_int][:tx] = Mash.new unless net_counters[tmp_int][:tx]
157
- net_counters[tmp_int][:tx][:queuelen] = $1
158
- end
159
-
160
- if line =~ /vlan id (\d+)/
161
- tmp_id = $1
162
- iface[tmp_int][:vlan] = Mash.new unless iface[tmp_int][:vlan]
163
- iface[tmp_int][:vlan][:id] = tmp_id
164
-
165
- vlan_flags = line.scan(/(REORDER_HDR|GVRP|LOOSE_BINDING)/)
166
- if vlan_flags.length > 0
167
- iface[tmp_int][:vlan][:flags] = vlan_flags.flatten.uniq
156
+ on_rx = !on_rx
168
157
  end
169
- end
170
-
171
- if line =~ /state (\w+)/
172
- iface[tmp_int]['state'] = $1.downcase
173
- end
174
158
 
159
+ if line =~ /qlen (\d+)/
160
+ net_counters[tmp_int][:tx] = Mash.new unless net_counters[tmp_int][:tx]
161
+ net_counters[tmp_int][:tx][:queuelen] = $1
162
+ end
175
163
 
176
- end
177
- end
164
+ if line =~ /vlan id (\d+)/
165
+ tmp_id = $1
166
+ iface[tmp_int][:vlan] = Mash.new unless iface[tmp_int][:vlan]
167
+ iface[tmp_int][:vlan][:id] = tmp_id
178
168
 
179
- families.each do |family|
180
- neigh_attr = family[:neighbour_attribute]
181
- default_prefix = family[:default_prefix]
182
-
183
- popen4("ip -f #{family[:name]} neigh show") do |pid, stdin, stdout, stderr|
184
- stdin.close
185
- stdout.each do |line|
186
- if line =~ /^([a-f0-9\:\.]+)\s+dev\s+([^\s]+)\s+lladdr\s+([a-fA-F0-9\:]+)/
187
- unless iface[$2]
188
- Ohai::Log.warn("neighbour list has entries for unknown interface #{iface[$2]}")
189
- next
169
+ vlan_flags = line.scan(/(REORDER_HDR|GVRP|LOOSE_BINDING)/)
170
+ if vlan_flags.length > 0
171
+ iface[tmp_int][:vlan][:flags] = vlan_flags.flatten.uniq
190
172
  end
191
- iface[$2][neigh_attr] = Mash.new unless iface[$2][neigh_attr]
192
- iface[$2][neigh_attr][$1] = $3.downcase
193
173
  end
194
- end
195
- end
196
174
 
197
- # checking the routing tables
198
- # why ?
199
- # 1) to set the default gateway and default interfaces attributes
200
- # 2) on some occasions, the best way to select node[:ipaddress] is to look at
201
- # the routing table source field.
202
- # 3) and since we're at it, let's populate some :routes attributes
203
- # (going to do that for both inet and inet6 addresses)
204
- popen4("ip -o -f #{family[:name]} route show") do |pid, stdin, stdout, stderr|
205
- stdin.close
206
- stdout.each do |line|
207
- line.strip!
208
- Ohai::Log.debug("Parsing #{line}")
209
- if line =~ /\\/
210
- parts = line.split('\\')
211
- route_dest = parts.shift.strip
212
- route_endings = parts
213
- elsif line =~ /^([^\s]+)\s(.*)$/
214
- route_dest = $1
215
- route_endings = [$2]
216
- else
217
- next
175
+ if line =~ /state (\w+)/
176
+ iface[tmp_int]['state'] = $1.downcase
218
177
  end
219
- route_endings.each do |route_ending|
220
- if route_ending =~ /\bdev\s+([^\s]+)\b/
221
- route_int = $1
222
- else
223
- Ohai::Log.debug("Skipping route entry without a device: '#{line}'")
224
- next
225
- end
178
+ end
226
179
 
227
- unless iface[route_int]
228
- Ohai::Log.debug("Skipping previously unseen interface from 'ip route show': #{route_int}")
229
- next
180
+ families.each do |family|
181
+ neigh_attr = family[:neighbour_attribute]
182
+ default_prefix = family[:default_prefix]
183
+
184
+ so = shell_out("ip -f #{family[:name]} neigh show")
185
+ so.stdout.lines do |line|
186
+ if line =~ /^([a-f0-9\:\.]+)\s+dev\s+([^\s]+)\s+lladdr\s+([a-fA-F0-9\:]+)/
187
+ unless iface[$2]
188
+ Ohai::Log.warn("neighbour list has entries for unknown interface #{iface[$2]}")
189
+ next
190
+ end
191
+ iface[$2][neigh_attr] = Mash.new unless iface[$2][neigh_attr]
192
+ iface[$2][neigh_attr][$1] = $3.downcase
230
193
  end
194
+ end
231
195
 
232
- route_entry = Mash.new( :destination => route_dest,
233
- :family => family[:name] )
234
- %w[via scope metric proto src].each do |k|
235
- route_entry[k] = $1 if route_ending =~ /\b#{k}\s+([^\s]+)\b/
196
+ # checking the routing tables
197
+ # why ?
198
+ # 1) to set the default gateway and default interfaces attributes
199
+ # 2) on some occasions, the best way to select node[:ipaddress] is to look at
200
+ # the routing table source field.
201
+ # 3) and since we're at it, let's populate some :routes attributes
202
+ # (going to do that for both inet and inet6 addresses)
203
+ so = shell_out("ip -f #{family[:name]} route show")
204
+ so.stdout.lines do |line|
205
+ if line =~ /^([^\s]+)\s(.*)$/
206
+ route_dest = $1
207
+ route_ending = $2
208
+ #
209
+ if route_ending =~ /\bdev\s+([^\s]+)\b/
210
+ route_int = $1
211
+ else
212
+ Ohai::Log.debug("Skipping route entry without a device: '#{line}'")
213
+ next
214
+ end
215
+
216
+ unless iface[route_int]
217
+ Ohai::Log.debug("Skipping previously unseen interface from 'ip route show': #{route_int}")
218
+ next
219
+ end
220
+
221
+ route_entry = Mash.new( :destination => route_dest,
222
+ :family => family[:name] )
223
+ %w[via scope metric proto src].each do |k|
224
+ route_entry[k] = $1 if route_ending =~ /\b#{k}\s+([^\s]+)\b/
225
+ end
226
+
227
+ # a sanity check, especially for Linux-VServer, OpenVZ and LXC:
228
+ # don't report the route entry if the src address isn't set on the node
229
+ next if route_entry[:src] and not iface[route_int][:addresses].has_key? route_entry[:src]
230
+
231
+ iface[route_int][:routes] = Array.new unless iface[route_int][:routes]
232
+ iface[route_int][:routes] << route_entry
236
233
  end
237
-
238
- # a sanity check, especially for Linux-VServer, OpenVZ and LXC:
239
- # don't report the route entry if the src address isn't set on the node
240
- next if route_entry[:src] and not iface[route_int][:addresses].has_key? route_entry[:src]
241
-
242
- iface[route_int][:routes] = Array.new unless iface[route_int][:routes]
243
- iface[route_int][:routes] << route_entry
244
234
  end
245
- end
246
- end
247
- # now looking at the routes to set the default attributes
248
- # for information, default routes can be of this form :
249
- # - default via 10.0.2.4 dev br0
250
- # - default dev br0 scope link
251
- # - default via 10.0.3.1 dev eth1 src 10.0.3.2 metric 10
252
- # - default via 10.0.4.1 dev eth2 src 10.0.4.2 metric 20
253
-
254
- # using a temporary var to hold routes and their interface name
255
- routes = iface.collect do |i,iv|
256
- iv[:routes].collect do |r|
257
- r.merge(:dev=>i) if r[:family] == family[:name]
258
- end.compact if iv[:routes]
259
- end.compact.flatten
260
-
261
- # using a temporary var to hold the default route
262
- # in case there are more than 1 default route, sort it by its metric
263
- # and return the first one
264
- # (metric value when unspecified is 0)
265
- default_route = routes.select do |r|
266
- r[:destination] == "default"
267
- end.sort do |x,y|
268
- (x[:metric].nil? ? 0 : x[:metric].to_i) <=> (y[:metric].nil? ? 0 : y[:metric].to_i)
269
- end.first
270
-
271
- if default_route.nil? or default_route.empty?
272
- Ohai::Log.debug("Unable to determine default #{family[:name]} interface")
273
- else
274
- network["#{default_prefix}_interface"] = default_route[:dev]
275
- Ohai::Log.debug("#{default_prefix}_interface set to #{default_route[:dev]}")
276
-
277
- # setting gateway to 0.0.0.0 or :: if the default route is a link level one
278
- network["#{default_prefix}_gateway"] = default_route[:via] ? default_route[:via] : family[:default_route].chomp("/0")
279
- Ohai::Log.debug("#{default_prefix}_gateway set to #{network["#{default_prefix}_gateway"]}")
280
-
281
- # since we're at it, let's populate {ip,mac,ip6}address with the best values
282
- # using the source field when it's specified :
283
- # 1) in the default route
284
- # 2) in the route entry used to reach the default gateway
285
- route = routes.select do |r|
286
- # selecting routes
287
- r[:src] and # it has a src field
288
- iface[r[:dev]] and # the iface exists
289
- iface[r[:dev]][:addresses].has_key? r[:src] and # the src ip is set on the node
290
- iface[r[:dev]][:addresses][r[:src]][:scope].downcase != "link" and # this isn't a link level addresse
291
- ( r[:destination] == "default" or
292
- ( default_route[:via] and # the default route has a gateway
293
- IPAddress(r[:destination]).include? IPAddress(default_route[:via]) # the route matches the gateway
294
- )
295
- )
296
- end.sort_by do |r|
297
- # sorting the selected routes:
298
- # - getting default routes first
299
- # - then sort by metric
300
- # - then by prefixlen
301
- [
302
- r[:destination] == "default" ? 0 : 1,
303
- r[:metric].nil? ? 0 : r[:metric].to_i,
304
- # for some reason IPAddress doesn't accept "::/0", it doesn't like prefix==0
305
- # just a quick workaround: use 0 if IPAddress fails
306
- begin
307
- IPAddress( r[:destination] == "default" ? family[:default_route] : r[:destination] ).prefix
308
- rescue
309
- 0
310
- end
311
- ]
312
- end.first
313
-
314
- unless route.nil? or route.empty?
315
- if family[:name] == "inet"
316
- ipaddress route[:src]
317
- macaddress iface[route[:dev]][:addresses].select{|k,v| v["family"]=="lladdr"}.first.first unless iface[route[:dev]][:flags].include? "NOARP"
235
+ # now looking at the routes to set the default attributes
236
+ # for information, default routes can be of this form :
237
+ # - default via 10.0.2.4 dev br0
238
+ # - default dev br0 scope link
239
+ # - default via 10.0.3.1 dev eth1 src 10.0.3.2 metric 10
240
+ # - default via 10.0.4.1 dev eth2 src 10.0.4.2 metric 20
241
+
242
+ # using a temporary var to hold routes and their interface name
243
+ routes = iface.collect do |i,iv|
244
+ iv[:routes].collect do |r|
245
+ r.merge(:dev=>i) if r[:family] == family[:name]
246
+ end.compact if iv[:routes]
247
+ end.compact.flatten
248
+
249
+ # using a temporary var to hold the default route
250
+ # in case there are more than 1 default route, sort it by its metric
251
+ # and return the first one
252
+ # (metric value when unspecified is 0)
253
+ default_route = routes.select do |r|
254
+ r[:destination] == "default"
255
+ end.sort do |x,y|
256
+ (x[:metric].nil? ? 0 : x[:metric].to_i) <=> (y[:metric].nil? ? 0 : y[:metric].to_i)
257
+ end.first
258
+
259
+ if default_route.nil? or default_route.empty?
260
+ Ohai::Log.debug("Unable to determine default #{family[:name]} interface")
318
261
  else
319
- ip6address route[:src]
262
+ network["#{default_prefix}_interface"] = default_route[:dev]
263
+ Ohai::Log.debug("#{default_prefix}_interface set to #{default_route[:dev]}")
264
+
265
+ # setting gateway to 0.0.0.0 or :: if the default route is a link level one
266
+ network["#{default_prefix}_gateway"] = default_route[:via] ? default_route[:via] : family[:default_route].chomp("/0")
267
+ Ohai::Log.debug("#{default_prefix}_gateway set to #{network["#{default_prefix}_gateway"]}")
268
+
269
+ # since we're at it, let's populate {ip,mac,ip6}address with the best values
270
+ # using the source field when it's specified :
271
+ # 1) in the default route
272
+ # 2) in the route entry used to reach the default gateway
273
+ route = routes.select do |r|
274
+ # selecting routes
275
+ r[:src] and # it has a src field
276
+ iface[r[:dev]] and # the iface exists
277
+ iface[r[:dev]][:addresses].has_key? r[:src] and # the src ip is set on the node
278
+ iface[r[:dev]][:addresses][r[:src]][:scope].downcase != "link" and # this isn't a link level addresse
279
+ ( r[:destination] == "default" or
280
+ ( default_route[:via] and # the default route has a gateway
281
+ IPAddress(r[:destination]).include? IPAddress(default_route[:via]) # the route matches the gateway
282
+ )
283
+ )
284
+ end.sort_by do |r|
285
+ # sorting the selected routes:
286
+ # - getting default routes first
287
+ # - then sort by metric
288
+ # - then by prefixlen
289
+ [
290
+ r[:destination] == "default" ? 0 : 1,
291
+ r[:metric].nil? ? 0 : r[:metric].to_i,
292
+ # for some reason IPAddress doesn't accept "::/0", it doesn't like prefix==0
293
+ # just a quick workaround: use 0 if IPAddress fails
294
+ begin
295
+ IPAddress( r[:destination] == "default" ? family[:default_route] : r[:destination] ).prefix
296
+ rescue
297
+ 0
298
+ end
299
+ ]
300
+ end.first
301
+
302
+ unless route.nil? or route.empty?
303
+ if family[:name] == "inet"
304
+ ipaddress route[:src]
305
+ macaddress iface[route[:dev]][:addresses].select{|k,v| v["family"]=="lladdr"}.first.first unless iface[route[:dev]][:flags].include? "NOARP"
306
+ else
307
+ ip6address route[:src]
308
+ end
309
+ end
320
310
  end
321
311
  end
322
- end
323
- end
324
312
 
325
- else
313
+ else
326
314
 
327
- begin
328
- route_result = from("route -n \| grep -m 1 ^0.0.0.0").split(/[ \t]+/)
329
- network[:default_gateway], network[:default_interface] = route_result.values_at(1,7)
330
- rescue Ohai::Exceptions::Exec
331
- Ohai::Log.debug("Unable to determine default interface")
332
- end
315
+ begin
316
+ so = shell_out("route -n")
317
+ route_result = so.stdout.split($/).grep( /^0.0.0.0/ )[0].split( /[ \t]+/ )
318
+ network[:default_gateway], network[:default_interface] = route_result.values_at(1,7)
319
+ rescue Ohai::Exceptions::Exec
320
+ Ohai::Log.debug("Unable to determine default interface")
321
+ end
333
322
 
334
- popen4("ifconfig -a") do |pid, stdin, stdout, stderr|
335
- stdin.close
336
- cint = nil
337
- stdout.each do |line|
338
- tmp_addr = nil
339
- # dev_valid_name in the kernel only excludes slashes, nulls, spaces
340
- # http://git.kernel.org/?p=linux/kernel/git/stable/linux-stable.git;a=blob;f=net/core/dev.c#l851
341
- if line =~ /^([0-9a-zA-Z@\.\:\-_]+)\s+/
342
- cint = $1
343
- iface[cint] = Mash.new
344
- if cint =~ /^(\w+)(\d+.*)/
345
- iface[cint][:type] = $1
346
- iface[cint][:number] = $2
323
+ so = shell_out("ifconfig -a")
324
+ cint = nil
325
+ so.stdout.lines do |line|
326
+ tmp_addr = nil
327
+ # dev_valid_name in the kernel only excludes slashes, nulls, spaces
328
+ # http://git.kernel.org/?p=linux/kernel/git/stable/linux-stable.git;a=blob;f=net/core/dev.c#l851
329
+ if line =~ /^([0-9a-zA-Z@\.\:\-_]+)\s+/
330
+ cint = $1
331
+ iface[cint] = Mash.new
332
+ if cint =~ /^(\w+)(\d+.*)/
333
+ iface[cint][:type] = $1
334
+ iface[cint][:number] = $2
335
+ end
336
+ end
337
+ if line =~ /Link encap:(Local Loopback)/ || line =~ /Link encap:(.+?)\s/
338
+ iface[cint][:encapsulation] = linux_encaps_lookup($1)
339
+ end
340
+ if line =~ /HWaddr (.+?)\s/
341
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
342
+ iface[cint][:addresses][$1] = { "family" => "lladdr" }
343
+ end
344
+ if line =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
345
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
346
+ iface[cint][:addresses][$1] = { "family" => "inet" }
347
+ tmp_addr = $1
348
+ end
349
+ if line =~ /inet6 addr: ([a-f0-9\:]+)\/(\d+) Scope:(\w+)/
350
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
351
+ iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("Host") ? "Node" : $3) }
352
+ end
353
+ if line =~ /Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
354
+ iface[cint][:addresses][tmp_addr]["broadcast"] = $1
355
+ end
356
+ if line =~ /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
357
+ iface[cint][:addresses][tmp_addr]["netmask"] = $1
358
+ end
359
+ flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)\s/)
360
+ if flags.length > 1
361
+ iface[cint][:flags] = flags.flatten
362
+ end
363
+ if line =~ /MTU:(\d+)/
364
+ iface[cint][:mtu] = $1
365
+ end
366
+ if line =~ /P-t-P:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
367
+ iface[cint][:peer] = $1
368
+ end
369
+ if line =~ /RX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)/
370
+ net_counters[cint] = Mash.new unless net_counters[cint]
371
+ net_counters[cint][:rx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "frame" => $5 }
372
+ end
373
+ if line =~ /TX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)/
374
+ net_counters[cint][:tx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "carrier" => $5 }
375
+ end
376
+ if line =~ /collisions:(\d+)/
377
+ net_counters[cint][:tx]["collisions"] = $1
378
+ end
379
+ if line =~ /txqueuelen:(\d+)/
380
+ net_counters[cint][:tx]["queuelen"] = $1
381
+ end
382
+ if line =~ /RX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
383
+ net_counters[cint][:rx]["bytes"] = $1
384
+ end
385
+ if line =~ /TX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
386
+ net_counters[cint][:tx]["bytes"] = $1
347
387
  end
348
388
  end
349
- if line =~ /Link encap:(Local Loopback)/ || line =~ /Link encap:(.+?)\s/
350
- iface[cint][:encapsulation] = encaps_lookup($1)
351
- end
352
- if line =~ /HWaddr (.+?)\s/
353
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
354
- iface[cint][:addresses][$1] = { "family" => "lladdr" }
355
- end
356
- if line =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
357
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
358
- iface[cint][:addresses][$1] = { "family" => "inet" }
359
- tmp_addr = $1
360
- end
361
- if line =~ /inet6 addr: ([a-f0-9\:]+)\/(\d+) Scope:(\w+)/
362
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
363
- iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("Host") ? "Node" : $3) }
364
- end
365
- if line =~ /Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
366
- iface[cint][:addresses][tmp_addr]["broadcast"] = $1
367
- end
368
- if line =~ /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
369
- iface[cint][:addresses][tmp_addr]["netmask"] = $1
370
- end
371
- flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)\s/)
372
- if flags.length > 1
373
- iface[cint][:flags] = flags.flatten
374
- end
375
- if line =~ /MTU:(\d+)/
376
- iface[cint][:mtu] = $1
377
- end
378
- if line =~ /P-t-P:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
379
- iface[cint][:peer] = $1
380
- end
381
- if line =~ /RX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)/
382
- net_counters[cint] = Mash.new unless net_counters[cint]
383
- net_counters[cint][:rx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "frame" => $5 }
384
- end
385
- if line =~ /TX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)/
386
- net_counters[cint][:tx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "carrier" => $5 }
387
- end
388
- if line =~ /collisions:(\d+)/
389
- net_counters[cint][:tx]["collisions"] = $1
390
- end
391
- if line =~ /txqueuelen:(\d+)/
392
- net_counters[cint][:tx]["queuelen"] = $1
393
- end
394
- if line =~ /RX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
395
- net_counters[cint][:rx]["bytes"] = $1
396
- end
397
- if line =~ /TX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
398
- net_counters[cint][:tx]["bytes"] = $1
399
- end
400
- end
401
- end
402
-
403
389
 
404
- popen4("arp -an") do |pid, stdin, stdout, stderr|
405
- stdin.close
406
- stdout.each do |line|
407
- if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) \[(\w+)\] on ([0-9a-zA-Z\.\:\-]+)/
408
- next unless iface[$4] # this should never happen
409
- iface[$4][:arp] = Mash.new unless iface[$4][:arp]
410
- iface[$4][:arp][$1] = $2.downcase
390
+ so = shell_out("arp -an")
391
+ so.stdout.lines do |line|
392
+ if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) \[(\w+)\] on ([0-9a-zA-Z\.\:\-]+)/
393
+ next unless iface[$4] # this should never happen
394
+ iface[$4][:arp] = Mash.new unless iface[$4][:arp]
395
+ iface[$4][:arp][$1] = $2.downcase
396
+ end
411
397
  end
412
398
  end
399
+ counters[:network][:interfaces] = net_counters
400
+ network["interfaces"] = iface
413
401
  end
414
-
415
402
  end
416
-
417
-
418
- counters[:network][:interfaces] = net_counters
419
-
420
- network["interfaces"] = iface
421
-