ohai 8.7.0 → 8.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -3
  3. data/lib/ohai/application.rb +3 -1
  4. data/lib/ohai/loader.rb +4 -3
  5. data/lib/ohai/mixin/dmi_decode.rb +47 -0
  6. data/lib/ohai/mixin/os.rb +2 -0
  7. data/lib/ohai/plugins/aix/kernel.rb +2 -1
  8. data/lib/ohai/plugins/{freebsd → bsd}/filesystem.rb +21 -3
  9. data/lib/ohai/plugins/bsd/virtualization.rb +80 -0
  10. data/lib/ohai/plugins/dragonflybsd/cpu.rb +58 -0
  11. data/lib/ohai/plugins/dragonflybsd/memory.rb +60 -0
  12. data/lib/ohai/plugins/dragonflybsd/network.rb +126 -0
  13. data/lib/ohai/plugins/dragonflybsd/os.rb +32 -0
  14. data/lib/ohai/plugins/dragonflybsd/platform.rb +29 -0
  15. data/lib/ohai/plugins/freebsd/cpu.rb +14 -8
  16. data/lib/ohai/plugins/freebsd/os.rb +1 -1
  17. data/lib/ohai/plugins/hostname.rb +1 -1
  18. data/lib/ohai/plugins/kernel.rb +12 -0
  19. data/lib/ohai/plugins/linux/virtualization.rb +8 -37
  20. data/lib/ohai/plugins/network_listeners.rb +5 -0
  21. data/lib/ohai/plugins/ps.rb +2 -2
  22. data/lib/ohai/plugins/solaris2/filesystem.rb +9 -1
  23. data/lib/ohai/plugins/uptime.rb +1 -1
  24. data/lib/ohai/plugins/virtualbox.rb +44 -0
  25. data/lib/ohai/plugins/windows/cpu.rb +26 -27
  26. data/lib/ohai/plugins/windows/virtualization.rb +29 -17
  27. data/lib/ohai/system.rb +1 -1
  28. data/lib/ohai/version.rb +1 -1
  29. data/ohai.gemspec +50 -0
  30. data/spec/functional/loader_spec.rb +53 -0
  31. data/spec/unit/loader_spec.rb +51 -53
  32. data/spec/unit/plugins/aix/kernel_spec.rb +5 -0
  33. data/spec/unit/plugins/bsd/filesystem_spec.rb +126 -0
  34. data/spec/unit/plugins/{freebsd → bsd}/virtualization_spec.rb +7 -4
  35. data/spec/unit/plugins/freebsd/cpu_spec.rb +60 -14
  36. data/spec/unit/plugins/linux/virtualization_spec.rb +58 -5
  37. data/spec/unit/plugins/solaris2/filesystem.rb +84 -0
  38. data/spec/unit/plugins/virtualbox_spec.rb +88 -0
  39. data/spec/unit/plugins/windows/cpu_spec.rb +3 -1
  40. data/spec/unit/plugins/windows/virtualization_spec.rb +182 -32
  41. metadata +21 -26
  42. data/lib/ohai/plugins/freebsd/virtualization.rb +0 -94
  43. data/lib/ohai/plugins/netbsd/filesystem.rb +0 -57
  44. data/lib/ohai/plugins/netbsd/virtualization.rb +0 -68
  45. data/lib/ohai/plugins/openbsd/filesystem.rb +0 -57
  46. data/lib/ohai/plugins/openbsd/virtualization.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48005ef0ac4164194df7bc906291139ce7227895
4
- data.tar.gz: 24932f3bfaa161f5d97e0131801b7f18e999a636
3
+ metadata.gz: 85e95271fa844c86e7109612c85d6e9dd4c76f0c
4
+ data.tar.gz: 41c785220f4364c44d6b8df5f267212e60c2854d
5
5
  SHA512:
6
- metadata.gz: f955882f73da2fc4ff1649f0664224bc6878ae78353883e94e08a36c379d2306da9fb2c632c3f0ed58d00a64ea2b271c98670356f0885410fc872695920c3755
7
- data.tar.gz: 80adff5dccf6c1f7ef8afaefda644fc820bcf13f7919277d89f7c0d93388720dd32e961e5296343ee2456f8a86543f3330133dd31699f6c2baf57988c7dc5053
6
+ metadata.gz: 468cc81d43c4067f7c34dd737fdd4d17acfd26e25082c58b157d228ba22e26534b44ba4741c887e60147461d675b42057a191f28cd4446f16305708066b32e55
7
+ data.tar.gz: 69873511516a5cad588aa6e241615eebb4b7eedcfdc26e40519018f5829af581700f8bb55441a8098d2bc0d407a5c4d8455b2b2432627bc902be785e924b1125
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # ohai
2
2
  [![Build Status Master](https://travis-ci.org/chef/ohai.svg?branch=master)](https://travis-ci.org/chef/ohai)
3
3
  [![Build Status Master](https://ci.appveyor.com/api/projects/status/github/chef/ohai?branch=master&svg=true&passingText=master%20-%20Ok&pendingText=master%20-%20Pending&failingText=master%20-%20Failing)](https://ci.appveyor.com/project/Chef/ohai/branch/master)
4
+ [![Gem Version](https://badge.fury.io/rb/ohai.svg)](https://badge.fury.io/rb/ohai)
4
5
 
5
6
 
6
7
  # DESCRIPTION:
@@ -16,8 +17,8 @@ Chef distributes ohai as a RubyGem. This README is for developers who
16
17
  want to modify the Ohai source code. For users who want to write plugins
17
18
  for Ohai, see the docs:
18
19
 
19
- * General documentation: http://docs.chef.io/ohai.html
20
- * Custom plugin documentation: http://docs.chef.io/ohai_custom.html
20
+ * General documentation: https://docs.chef.io/ohai.html
21
+ * Custom plugin documentation: https://docs.chef.io/ohai_custom.html
21
22
 
22
23
  # DEVELOPMENT:
23
24
 
@@ -67,7 +68,7 @@ Ohai has some Rake tasks for doing various things.
67
68
 
68
69
  Source:
69
70
 
70
- * http://github.com/chef/ohai/tree/master
71
+ * https://github.com/chef/ohai/tree/master
71
72
 
72
73
  Issues:
73
74
 
@@ -15,6 +15,7 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
 
18
+ require 'chef-config/path_helper'
18
19
  require 'chef-config/workstation_config_loader'
19
20
  require 'ohai'
20
21
  require 'ohai/log'
@@ -32,7 +33,8 @@ class Ohai::Application
32
33
  option :directory,
33
34
  :short => "-d DIRECTORY",
34
35
  :long => "--directory DIRECTORY",
35
- :description => "A directory to add to the Ohai search path"
36
+ :description => "A directory to add to the Ohai search path",
37
+ :proc => lambda { |path| Ohai::Config.platform_specific_path(path) }
36
38
 
37
39
  option :log_level,
38
40
  :short => "-l LEVEL",
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Claire McQuin (<claire@opscode.com>)
3
- # Copyright:: Copyright (c) 2013 Opscode, Inc.
2
+ # Author:: Claire McQuin (<claire@chef.io>)
3
+ # Copyright:: Copyright (c) 2013-2015 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,7 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
+ require 'chef-config/path_helper'
19
20
  require 'ohai/log'
20
21
  require 'ohai/mash'
21
22
  require 'ohai/dsl'
@@ -38,7 +39,7 @@ module Ohai
38
39
 
39
40
  # Finds all the *.rb files under the configured paths in :plugin_path
40
41
  def self.find_all_in(plugin_dir)
41
- Dir[File.join(plugin_dir, "**", "*.rb")].map do |file|
42
+ Dir[File.join(ChefConfig::PathHelper.escape_glob(plugin_dir), "**", "*.rb")].map do |file|
42
43
  new(file, plugin_dir)
43
44
  end
44
45
  end
@@ -0,0 +1,47 @@
1
+ #
2
+ # Author:: Tim Smith <tsmith@chef.io>
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ # http://www.dmo.ca/blog/detecting-virtualization-on-linux
19
+ module ::Ohai::Mixin::DmiDecode
20
+ def determine_guest(dmi_data)
21
+ dmi_data.each_line do |line|
22
+ case line
23
+ when /Manufacturer: Microsoft/
24
+ if dmi_data =~ /Product Name: Virtual Machine/
25
+ if dmi_data =~ /Version: (VS2005R2|6.0)/
26
+ return 'virtualpc'
27
+ elsif dmi_data =~ /Version: (7.0|Hyper-V)/
28
+ return 'hyperv'
29
+ elsif dmi_data =~ /Version: 5.0/
30
+ return 'virtualserver'
31
+ end
32
+ end
33
+ when /Manufacturer: VMware/
34
+ return 'vmware'
35
+ when /Manufacturer: Xen/
36
+ return 'xen'
37
+ when /Product Name: VirtualBox/
38
+ return 'vbox'
39
+ when /Product Name: OpenStack/
40
+ return 'openstack'
41
+ when /Manufacturer: QEMU|Product Name: (KVM|RHEV)/
42
+ return 'kvm'
43
+ end
44
+ end
45
+ return nil
46
+ end
47
+ end
@@ -39,6 +39,8 @@ module Ohai
39
39
  return "openbsd"
40
40
  when /netbsd(.*)$/
41
41
  return "netbsd"
42
+ when /dragonfly(.*)$/
43
+ return "dragonflybsd"
42
44
  when /solaris2/
43
45
  return "solaris2"
44
46
  when /mswin|mingw32|windows/
@@ -22,10 +22,11 @@ Ohai.plugin(:Kernel) do
22
22
  collect_data(:aix) do
23
23
  kernel Mash.new
24
24
 
25
- kernel[:name] = shell_out("uname -s").stdout.split($/)[0].downcase
25
+ kernel[:name] = shell_out("uname -s").stdout.split($/)[0].downcase
26
26
  kernel[:release] = shell_out("uname -r").stdout.split($/)[0]
27
27
  kernel[:version] = shell_out("uname -v").stdout.split($/)[0]
28
28
  kernel[:machine] = shell_out("uname -p").stdout.split($/)[0]
29
+ kernel[:bits] = shell_out("getconf KERNEL_BITMODE").stdout.strip
29
30
 
30
31
  modules = Mash.new
31
32
  so = shell_out("genkex -d")
@@ -1,6 +1,7 @@
1
1
  #
2
- # Author:: Adam Jacob (<adam@opscode.com>)
3
- # Copyright:: Copyright (c) 2008 Opscode, Inc.
2
+ # Author:: Adam Jacob (<adam@chef.io>)
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ # Copyright:: Copyright (c) 2008-2015 Chef Software, Inc.
4
5
  # License:: Apache License, Version 2.0
5
6
  #
6
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,7 +20,7 @@
19
20
  Ohai.plugin(:Filesystem) do
20
21
  provides "filesystem"
21
22
 
22
- collect_data(:freebsd) do
23
+ collect_data(:freebsd, :openbsd, :netbsd, :dragonflybsd) do
23
24
  fs = Mash.new
24
25
 
25
26
  # Grab filesystem data from df
@@ -39,6 +40,23 @@ Ohai.plugin(:Filesystem) do
39
40
  end
40
41
  end
41
42
 
43
+ # inode parsing from 'df -iP'
44
+ so = shell_out("df -iP")
45
+ so.stdout.lines do |line|
46
+ case line
47
+ when /^Filesystem/ # skip the header
48
+ next
49
+ when /^(.+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+(\d+)\s+(\d+)\s+(\d+)%(.+)$/
50
+ filesystem = $1.strip
51
+ fs[filesystem] ||= Mash.new
52
+ fs[filesystem][:inodes_used] = $6
53
+ fs[filesystem][:inodes_available] = $7
54
+ fs[filesystem][:total_inodes] = ($6.to_i + $7.to_i).to_s
55
+ fs[filesystem][:inodes_percent_used] = $8
56
+ fs[filesystem][:mount] = $9.strip
57
+ end
58
+ end
59
+
42
60
  # Grab mount information from mount
43
61
  so = shell_out("mount -l")
44
62
  so.stdout.lines do |line|
@@ -0,0 +1,80 @@
1
+ #
2
+ # Author:: Bryan McLellan (btm@loftninjas.org)
3
+ # Copyright:: Copyright (c) 2009 Bryan McLellan
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'ohai/mixin/dmi_decode'
20
+
21
+ include Ohai::Mixin::DmiDecode
22
+
23
+ Ohai.plugin(:Virtualization) do
24
+ provides 'virtualization'
25
+
26
+ collect_data(:freebsd, :openbsd, :netbsd, :dragonflybsd) do
27
+
28
+ virtualization Mash.new unless virtualization
29
+ virtualization[:systems] = Mash.new unless virtualization[:systems]
30
+
31
+ # detect when in a jail or when a jail is actively running (not in stopped state)
32
+ so = shell_out("sysctl -n security.jail.jailed")
33
+ if so.stdout.split($/)[0].to_i == 1
34
+ virtualization[:system] = "jail"
35
+ virtualization[:role] = "guest"
36
+ virtualization[:systems][:jail] = 'guest'
37
+ end
38
+
39
+ so = shell_out('jls -n')
40
+ if (so.stdout || '').lines.count >= 1
41
+ virtualization[:system] = 'jail'
42
+ virtualization[:role] = 'host'
43
+ virtualization[:systems][:jail] = 'host'
44
+ end
45
+
46
+ # detect from modules
47
+ so = shell_out("#{Ohai.abs_path('/sbin/kldstat')}")
48
+ so.stdout.lines do |line|
49
+ case line
50
+ when /vboxdrv/
51
+ virtualization[:system] = 'vbox'
52
+ virtualization[:role] = 'host'
53
+ virtualization[:systems][:vbox] = 'host'
54
+ when /vboxguest/
55
+ virtualization[:system] = 'vbox'
56
+ virtualization[:role] = 'guest'
57
+ virtualization[:systems][:vbox] = 'guest'
58
+ end
59
+ end
60
+
61
+ # Detect KVM/QEMU from cpu, report as KVM
62
+ # hw.model: QEMU Virtual CPU version 0.9.1
63
+ so = shell_out('sysctl -n hw.model')
64
+ if so.stdout.split($/)[0] =~ /QEMU Virtual CPU|Common KVM processor|Common 32-bit KVM processor/
65
+ virtualization[:system] = 'kvm'
66
+ virtualization[:role] = 'guest'
67
+ virtualization[:systems][:kvm] = 'guest'
68
+ end
69
+
70
+ # parse dmidecode to discover various virtualization guests
71
+ if File.exist?('/usr/local/sbin/dmidecode') || File.exist?('/usr/pkg/sbin/dmidecode')
72
+ guest = determine_guest(shell_out('dmidecode').stdout)
73
+ if guest
74
+ virtualization[:system] = guest
75
+ virtualization[:role] = 'guest'
76
+ virtualization[:systems][guest.to_sym] = 'guest'
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,58 @@
1
+ #
2
+ # Author:: Bryan McLellan (btm@loftninjas.org)
3
+ # Copyright:: Copyright (c) 2008 Bryan McLellan
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ Ohai.plugin(:CPU) do
20
+ provides "cpu"
21
+
22
+ collect_data(:dragonflybsd) do
23
+ # all dmesg output for smp I can find only provides info about a single processor
24
+ # identical processors is probably a hardware requirement so we'll duplicate data for each cpu
25
+ # old examples: http://www.bnv-bamberg.de/home/ba3294/smp/rbuild/index.htm
26
+ cpuinfo = Mash.new
27
+ cpuinfo["flags"] = []
28
+
29
+ # /var/run/dmesg.boot
30
+ # CPU: Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz (3516.61-MHz K8-class CPU)
31
+ # Origin = "GenuineIntel" Id = 0x306a9 Family = 6 Model = 3a Stepping = 9
32
+ # Features=0x783fbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,MMX,FXSR,SSE,SSE2>
33
+ # Features2=0x209<SSE3,MON,SSSE3>
34
+ # AMD Features=0x28100800<SYSCALL,NX,RDTSCP,LM>
35
+ # AMD Features2=0x1<LAHF>
36
+
37
+ File.open("/var/run/dmesg.boot").each do |line|
38
+ case line
39
+ when /CPU:\s+(.+) \(([\d.]+).+\)/
40
+ cpuinfo["model_name"] = $1
41
+ cpuinfo["mhz"] = $2
42
+ when /Origin = "(.+)"\s+Id = (.+)\s+Stepping = (.+)/
43
+ cpuinfo["vendor_id"] = $1
44
+ cpuinfo["stepping"] = $3
45
+ # These _should_ match /AMD Features2?/ lines as well
46
+ when /Features=.+<(.+)>/
47
+ cpuinfo["flags"].concat($1.downcase.split(','))
48
+ # Features2=0x80000001<SSE3,<b31>>
49
+ when /Features2=[a-f\dx]+<(.+)>/
50
+ cpuinfo["flags"].concat($1.downcase.split(','))
51
+ end
52
+ end
53
+
54
+ cpu cpuinfo
55
+ so = shell_out("sysctl -n hw.ncpu")
56
+ cpu[:total] = so.stdout.split($/)[0].to_i
57
+ end
58
+ end
@@ -0,0 +1,60 @@
1
+ #
2
+ # Author:: Bryan McLellan (btm@loftninjas.org)
3
+ # Copyright:: Copyright (c) 2009 Bryan McLellan
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ Ohai.plugin(:Memory) do
20
+ provides "memory", "memory/swap"
21
+
22
+ collect_data(:dragonflybsd) do
23
+ memory Mash.new
24
+ memory[:swap] = Mash.new
25
+
26
+ # /usr/src/sys/sys/vmmeter.h
27
+ so = shell_out("sysctl -n vm.stats.vm.v_page_size")
28
+ memory[:page_size] = so.stdout.split($/)[0]
29
+ so = shell_out("sysctl -n vm.stats.vm.v_page_count")
30
+ memory[:page_count] = so.stdout.split($/)[0]
31
+ memory[:total] = memory[:page_size].to_i * memory[:page_count].to_i
32
+ so = shell_out("sysctl -n vm.stats.vm.v_free_count")
33
+ memory[:free] = memory[:page_size].to_i * so.stdout.split($/)[0].to_i
34
+ so = shell_out("sysctl -n vm.status.vm.v_active_count")
35
+ memory[:active] = memory[:page_size].to_i * so.stdout.split($/)[0].to_i
36
+ so = shell_out("sysctl -n vm.status.vm.v_inactive_count")
37
+ memory[:inactive] = memory[:page_size].to_i * so.stdout.split($/)[0].to_i
38
+ so = shell_out("sysctl -n vm.stats.vm.v_cache_count")
39
+ memory[:cache] = memory[:page_size].to_i * so.stdout.split($/)[0].to_i
40
+ so = shell_out("sysctl -n vm.stats.vm.v_wire_count")
41
+ memory[:wired] = memory[:page_size].to_i * so.stdout.split($/)[0].to_i
42
+ so = shell_out("sysctl -n vfs.bufspace")
43
+ memory[:buffers] = so.stdout.split($/)[0]
44
+
45
+ so = shell_out("swapinfo")
46
+ so.stdout.lines do |line|
47
+ # Device 1K-blocks Used Avail Capacity
48
+ # /dev/ad0s1b 253648 0 253648 0%
49
+ if line =~ /^([\d\w\/]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\%]+)/
50
+ mdev = $1
51
+ memory[:swap][mdev] = Mash.new
52
+ memory[:swap][mdev][:total] = $2
53
+ memory[:swap][mdev][:used] = $3
54
+ memory[:swap][mdev][:free] = $4
55
+ memory[:swap][mdev][:percent_free] = $5
56
+ end
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,126 @@
1
+ #
2
+ # Author:: Bryan McLellan (btm@loftninjas.org)
3
+ # Copyright:: Copyright (c) 2009 Bryan McLellan
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ Ohai.plugin(:Network) do
20
+ provides "network", "network/interfaces"
21
+ provides "counters/network", "counters/network/interfaces"
22
+
23
+ collect_data(:dragonflybsd) do
24
+ network Mash.new unless network
25
+ network[:interfaces] = Mash.new unless network[:interfaces]
26
+ counters Mash.new unless counters
27
+ counters[:network] = Mash.new unless counters[:network]
28
+
29
+ so = shell_out("route -n get default")
30
+ so.stdout.lines do |line|
31
+ if line =~ /(\w+): ([\w\.]+)/
32
+ case $1
33
+ when "gateway"
34
+ network[:default_gateway] = $2
35
+ when "interface"
36
+ network[:default_interface] = $2
37
+ end
38
+ end
39
+ end
40
+
41
+ iface = Mash.new
42
+ so = shell_out("#{ Ohai.abs_path( "/sbin/ifconfig" )} -a")
43
+ cint = nil
44
+ so.stdout.lines do |line|
45
+ if line =~ /^([0-9a-zA-Z\.]+):\s+/
46
+ cint = $1
47
+ iface[cint] = Mash.new
48
+ if cint =~ /^(\w+)(\d+.*)/
49
+ iface[cint][:type] = $1
50
+ iface[cint][:number] = $2
51
+ end
52
+ end
53
+ # call the family lladdr to match linux for consistency
54
+ if line =~ /\s+ether (.+?)\s/
55
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
56
+ iface[cint][:addresses][$1] = { "family" => "lladdr" }
57
+ end
58
+ if line =~ /\s+inet ([\d.]+) netmask ([\da-fx]+)\s*\w*\s*([\d.]*)/
59
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
60
+ # convert the netmask to decimal for consistency
61
+ netmask = "#{$2[2,2].hex}.#{$2[4,2].hex}.#{$2[6,2].hex}.#{$2[8,2].hex}"
62
+ if $3.empty?
63
+ iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => netmask }
64
+ else
65
+ # found a broadcast address
66
+ iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => netmask, "broadcast" => $3 }
67
+ end
68
+ end
69
+ if line =~ /\s+inet6 ([a-f0-9\:]+)%?(\w*)\s+prefixlen\s+(\d+)\s*\w*\s*([\da-fx]*)/
70
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
71
+ if $4.empty?
72
+ iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $3 }
73
+ else
74
+ # found a zone_id / scope
75
+ iface[cint][:addresses][$1] = { "family" => "inet6", "zoneid" => $2, "prefixlen" => $3, "scopeid" => $4 }
76
+ end
77
+ end
78
+ if line =~ /flags=\d+<(.+)>/
79
+ flags = $1.split(',')
80
+ iface[cint][:flags] = flags if flags.length > 0
81
+ end
82
+ if line =~ /metric: (\d+) mtu: (\d+)/
83
+ iface[cint][:metric] = $1
84
+ iface[cint][:mtu] = $2
85
+ end
86
+ end
87
+
88
+ so = shell_out("arp -an")
89
+ so.stdout.lines do |line|
90
+ if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/
91
+ next unless iface[$3] # this should never happen
92
+ iface[$3][:arp] = Mash.new unless iface[$3][:arp]
93
+ iface[$3][:arp][$1] = $2.downcase
94
+ end
95
+ end
96
+
97
+ network["interfaces"] = iface
98
+
99
+ net_counters = Mash.new
100
+ # From netstat(1), not sure of the implications:
101
+ # Show the state of all network interfaces or a single interface
102
+ # which have been auto-configured (interfaces statically configured
103
+ # into a system, but not located at boot time are not shown).
104
+ so = shell_out("netstat -ibdn")
105
+ so.stdout.lines do |line|
106
+ # Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Drop
107
+ # ed0 1500 <Link#1> 54:52:00:68:92:85 333604 26 151905886 175472 0 24897542 0 905
108
+ # $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
109
+ if line =~ /^([\w\.\*]+)\s+\d+\s+<Link#\d+>\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
110
+ net_counters[$1] = Mash.new unless net_counters[$1]
111
+ net_counters[$1]["rx"] = Mash.new unless net_counters[$1]["rx"]
112
+ net_counters[$1]["tx"] = Mash.new unless net_counters[$1]["tx"]
113
+ net_counters[$1]["rx"]["packets"] = $3
114
+ net_counters[$1]["rx"]["errors"] = $4
115
+ net_counters[$1]["rx"]["bytes"] = $5
116
+ net_counters[$1]["tx"]["packets"] = $6
117
+ net_counters[$1]["tx"]["errors"] = $7
118
+ net_counters[$1]["tx"]["bytes"] = $8
119
+ net_counters[$1]["tx"]["collisions"] = $9
120
+ net_counters[$1]["tx"]["dropped"] = $10
121
+ end
122
+ end
123
+
124
+ counters[:network][:interfaces] = net_counters
125
+ end
126
+ end