diff options
author | Thom May <thom@may.lt> | 2018-05-10 12:06:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-10 12:06:31 +0100 |
commit | b736f660956dcf81a5bf57966b5122f395627190 (patch) | |
tree | c4292461eed2bb150733e931ffb07551dfa53167 | |
parent | 25e8deffeb2fc0e6fd81eade9e1e2cc4d819bbed (diff) | |
parent | 63f4fb6a2bf362de7466b603151340afcdf4ecd9 (diff) | |
download | ohai-b736f660956dcf81a5bf57966b5122f395627190.tar.gz |
Merge pull request #1181 from jaymzh/fix_filesystem
[filesystem] Unify plugins, bring BSD into the modern age
-rw-r--r-- | lib/ohai/plugins/bsd/filesystem.rb | 75 | ||||
-rw-r--r-- | lib/ohai/plugins/darwin/filesystem.rb | 107 | ||||
-rw-r--r-- | lib/ohai/plugins/filesystem.rb (renamed from lib/ohai/plugins/linux/filesystem.rb) | 128 | ||||
-rw-r--r-- | spec/unit/plugins/bsd/filesystem_spec.rb | 13 | ||||
-rw-r--r-- | spec/unit/plugins/darwin/filesystem_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/plugins/linux/filesystem_spec.rb | 2 |
6 files changed, 142 insertions, 185 deletions
diff --git a/lib/ohai/plugins/bsd/filesystem.rb b/lib/ohai/plugins/bsd/filesystem.rb deleted file mode 100644 index 4842d3fc..00000000 --- a/lib/ohai/plugins/bsd/filesystem.rb +++ /dev/null @@ -1,75 +0,0 @@ -# -# Author:: Adam Jacob (<adam@chef.io>) -# Author:: Tim Smith (<tsmith@chef.io>) -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -Ohai.plugin(:Filesystem) do - provides "filesystem" - - collect_data(:freebsd, :openbsd, :netbsd, :dragonflybsd) do - fs = Mash.new - - # Grab filesystem data from df - so = shell_out("df") - so.stdout.lines do |line| - case line - when /^Filesystem/ - next - when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/ - filesystem = $1 - fs[filesystem] = Mash.new - fs[filesystem][:kb_size] = $2 - fs[filesystem][:kb_used] = $3 - fs[filesystem][:kb_available] = $4 - fs[filesystem][:percent_used] = $5 - fs[filesystem][:mount] = $6 - end - end - - # inode parsing from 'df -iP' - so = shell_out("df -iP") - so.stdout.lines do |line| - case line - when /^Filesystem/ # skip the header - next - when /^(.+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+(\d+)\s+(\d+)\s+(\d+)%(.+)$/ - filesystem = $1.strip - fs[filesystem] ||= Mash.new - fs[filesystem][:inodes_used] = $6 - fs[filesystem][:inodes_available] = $7 - fs[filesystem][:total_inodes] = ($6.to_i + $7.to_i).to_s - fs[filesystem][:inodes_percent_used] = $8 - fs[filesystem][:mount] = $9.strip - end - end - - # Grab mount information from mount - so = shell_out("mount -l") - so.stdout.lines do |line| - if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ - filesystem = $1 - fs[filesystem] = Mash.new unless fs.has_key?(filesystem) - fs[filesystem][:mount] = $2 - fs[filesystem][:fs_type] = $3 - fs[filesystem][:mount_options] = $4.split(/,\s*/) - end - end - - # Set the filesystem data - filesystem fs - end -end diff --git a/lib/ohai/plugins/darwin/filesystem.rb b/lib/ohai/plugins/darwin/filesystem.rb deleted file mode 100644 index 07fbb5c9..00000000 --- a/lib/ohai/plugins/darwin/filesystem.rb +++ /dev/null @@ -1,107 +0,0 @@ -# -# Author:: Phil Dibowitz (<phil@ipom.com>) -# Author:: Benjamin Black (<bb@chef.io>) -# Copyright:: Copyright (c) 2009-2016 Chef Software, Inc. -# Copyright:: Copyright (c) 2015 Facebook, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -Ohai.plugin(:Filesystem) do - provides "filesystem" - - def generate_device_view(fs) - view = {} - fs.each_value do |entry| - view[entry[:device]] = Mash.new unless view[entry[:device]] - entry.each do |key, val| - next if %w{device mount}.include?(key) - view[entry[:device]][key] = val - end - if entry[:mount] - view[entry[:device]][:mounts] = [] unless view[entry[:device]][:mounts] - view[entry[:device]][:mounts] << entry[:mount] - end - end - view - end - - def generate_mountpoint_view(fs) - view = {} - fs.each_value do |entry| - next unless entry[:mount] - view[entry[:mount]] = Mash.new unless view[entry[:mount]] - entry.each do |key, val| - next if %w{mount device}.include?(key) - view[entry[:mount]][key] = val - end - if entry[:device] - view[entry[:mount]][:devices] = [] unless view[entry[:mount]][:devices] - view[entry[:mount]][:devices] << entry[:device] - end - end - view - end - - collect_data(:darwin) do - fs = Mash.new - block_size = 0 - # on new versions of OSX, -i is default, on old versions it's not, so - # specifying it gets consistent output - so = shell_out("df -i") - so.stdout.each_line do |line| - case line - when /^Filesystem\s+(\d+)-/ - block_size = $1.to_i - next - when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(\d+)\s+(\d+)\s+(\d+%)\s+(.+)$/ - key = "#{$1},#{$9}" - fs[key] = Mash.new - fs[key][:block_size] = block_size - fs[key][:device] = $1 - fs[key][:kb_size] = ($2.to_i / (1024 / block_size)).to_s - fs[key][:kb_used] = ($3.to_i / (1024 / block_size)).to_s - fs[key][:kb_available] = ($4.to_i / (1024 / block_size)).to_s - fs[key][:percent_used] = $5 - fs[key][:inodes_used] = $6 - fs[key][:inodes_available] = $7 - fs[key][:total_inodes] = ($6.to_i + $7.to_i).to_s - fs[key][:inodes_percent_used] = $8 - fs[key][:mount] = $9 - end - end - - so = shell_out("mount") - so.stdout.lines do |line| - if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ - key = "#{$1},#{$2}" - fs[key] = Mash.new unless fs.has_key?(key) - fs[key][:mount] = $2 - fs[key][:fs_type] = $3 - fs[key][:mount_options] = $4.split(/,\s*/) - end - end - - by_pair = fs - by_device = generate_device_view(fs) - by_mountpoint = generate_mountpoint_view(fs) - - fs_data = Mash.new - fs_data["by_device"] = by_device - fs_data["by_mountpoint"] = by_mountpoint - fs_data["by_pair"] = by_pair - - filesystem fs_data - end -end diff --git a/lib/ohai/plugins/linux/filesystem.rb b/lib/ohai/plugins/filesystem.rb index a0f1a159..f1332b62 100644 --- a/lib/ohai/plugins/linux/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -90,6 +90,15 @@ Ohai.plugin(:Filesystem) do view end + def generate_deprecated_view(fs) + view = generate_device_view(fs) + view.each do |device, entry| + view[device][:mount] = entry[:mounts].first + view[device].delete(:mounts) + end + view + end + collect_data(:linux) do fs = Mash.new @@ -250,4 +259,123 @@ Ohai.plugin(:Filesystem) do # Set the filesystem data filesystem fs_data end + + collect_data(:freebsd, :openbsd, :netbsd, :dragonflybsd) do + fs = Mash.new + + # Grab filesystem data from df + so = shell_out("df") + so.stdout.lines do |line| + case line + when /^Filesystem/ + next + when /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(\S+)$/ + key = "#{$1},#{$6}" + fs[key] = Mash.new + fs[key][:device] = $1 + fs[key][:kb_size] = $2 + fs[key][:kb_used] = $3 + fs[key][:kb_available] = $4 + fs[key][:percent_used] = $5 + fs[key][:mount] = $6 + end + end + + # inode parsing from 'df -iP' + so = shell_out("df -iP") + so.stdout.lines do |line| + case line + when /^Filesystem/ # skip the header + next + when /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+(\d+)\s+(\d+)\s+(\d+)%\s+(\S+)$/ + key = "#{$1},#{$9}" + fs[key] ||= Mash.new + fs[key][:device] = $1 + fs[key][:inodes_used] = $6 + fs[key][:inodes_available] = $7 + fs[key][:total_inodes] = ($6.to_i + $7.to_i).to_s + fs[key][:inodes_percent_used] = $8 + fs[key][:mount] = $9 + end + end + + # Grab mount information from mount + so = shell_out("mount -l") + so.stdout.lines do |line| + if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ + key = "#{$1},#{$2}" + fs[key] ||= Mash.new + fs[key][:device] = $1 + fs[key][:mount] = $2 + fs[key][:fs_type] = $3 + fs[key][:mount_options] = $4.split(/,\s*/) + end + end + + # create views + by_pair = fs + by_device = generate_device_view(fs) + by_mountpoint = generate_mountpoint_view(fs) + + fs_data = Mash.new + fs_data["by_device"] = by_device + fs_data["by_mountpoint"] = by_mountpoint + fs_data["by_pair"] = by_pair + + # Set the filesystem data - BSD didn't do the conversion when everyone else + # did, so 15 will have both be the new API and 16 will drop the old API + filesystem generate_deprecated_view(fs) + filesystem2 fs_data + end + + collect_data(:darwin) do + fs = Mash.new + block_size = 0 + # on new versions of OSX, -i is default, on old versions it's not, so + # specifying it gets consistent output + so = shell_out("df -i") + so.stdout.each_line do |line| + case line + when /^Filesystem\s+(\d+)-/ + block_size = $1.to_i + next + when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(\d+)\s+(\d+)\s+(\d+%)\s+(.+)$/ + key = "#{$1},#{$9}" + fs[key] = Mash.new + fs[key][:block_size] = block_size + fs[key][:device] = $1 + fs[key][:kb_size] = ($2.to_i / (1024 / block_size)).to_s + fs[key][:kb_used] = ($3.to_i / (1024 / block_size)).to_s + fs[key][:kb_available] = ($4.to_i / (1024 / block_size)).to_s + fs[key][:percent_used] = $5 + fs[key][:inodes_used] = $6 + fs[key][:inodes_available] = $7 + fs[key][:total_inodes] = ($6.to_i + $7.to_i).to_s + fs[key][:inodes_percent_used] = $8 + fs[key][:mount] = $9 + end + end + + so = shell_out("mount") + so.stdout.lines do |line| + if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ + key = "#{$1},#{$2}" + fs[key] = Mash.new unless fs.has_key?(key) + fs[key][:mount] = $2 + fs[key][:fs_type] = $3 + fs[key][:mount_options] = $4.split(/,\s*/) + end + end + + by_pair = fs + by_device = generate_device_view(fs) + by_mountpoint = generate_mountpoint_view(fs) + + fs_data = Mash.new + fs_data["by_device"] = by_device + fs_data["by_mountpoint"] = by_mountpoint + fs_data["by_pair"] = by_pair + + filesystem fs_data + end end diff --git a/spec/unit/plugins/bsd/filesystem_spec.rb b/spec/unit/plugins/bsd/filesystem_spec.rb index 7b913734..e9a41235 100644 --- a/spec/unit/plugins/bsd/filesystem_spec.rb +++ b/spec/unit/plugins/bsd/filesystem_spec.rb @@ -20,7 +20,7 @@ require_relative "../../../spec_helper.rb" describe Ohai::System, "BSD filesystem plugin" do - let(:plugin) { get_plugin("bsd/filesystem") } + let(:plugin) { get_plugin("filesystem") } before(:each) do allow(plugin).to receive(:collect_os).and_return(:freebsd) @@ -55,41 +55,49 @@ DFi it "should set kb_size to value from df" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:kb_size]).to eq("9637788") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:kb_size]).to eq("9637788") end it "should set kb_used to value from df" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:kb_used]).to eq("3313504") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:kb_used]).to eq("3313504") end it "should set kb_available to value from df" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:kb_available]).to eq("5553264") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:kb_available]).to eq("5553264") end it "should set percent_used to value from df" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:percent_used]).to eq("37%") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:percent_used]).to eq("37%") end it "should set mount to value from df" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:mount]).to eq("/") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:mount]).to eq("/") end it "should set total_inodes to value from df -iP" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:total_inodes]).to eq("1043326") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:total_inodes]).to eq("1043326") end it "should set inodes_used to value from df -iP" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:inodes_used]).to eq("252576") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:inodes_used]).to eq("252576") end it "should set inodes_available to value from df -iP" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:inodes_available]).to eq("790750") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:inodes_available]).to eq("790750") end end @@ -110,16 +118,19 @@ MOUNT it "should set mount to value from mount" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:mount]).to eq("/") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:mount]).to eq("/") end it "should set fs_type to value from mount" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:fs_type]).to eq("ufs") + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:fs_type]).to eq("ufs") end it "should set mount_options to an array of values from mount" do plugin.run expect(plugin[:filesystem]["/dev/ada0p2"][:mount_options]).to eq(["local", "journaled soft-updates"]) + expect(plugin[:filesystem2]["by_pair"]["/dev/ada0p2,/"][:mount_options]).to eq(["local", "journaled soft-updates"]) end end diff --git a/spec/unit/plugins/darwin/filesystem_spec.rb b/spec/unit/plugins/darwin/filesystem_spec.rb index 63889dc0..17ffb071 100644 --- a/spec/unit/plugins/darwin/filesystem_spec.rb +++ b/spec/unit/plugins/darwin/filesystem_spec.rb @@ -19,7 +19,7 @@ require_relative "../../../spec_helper.rb" describe Ohai::System, "darwin filesystem plugin" do - let(:plugin) { get_plugin("darwin/filesystem") } + let(:plugin) { get_plugin("filesystem") } before(:each) do allow(plugin).to receive(:collect_os).and_return(:darwin) diff --git a/spec/unit/plugins/linux/filesystem_spec.rb b/spec/unit/plugins/linux/filesystem_spec.rb index c1ce9e7d..322d9718 100644 --- a/spec/unit/plugins/linux/filesystem_spec.rb +++ b/spec/unit/plugins/linux/filesystem_spec.rb @@ -19,7 +19,7 @@ require_relative "../../../spec_helper.rb" describe Ohai::System, "Linux filesystem plugin" do - let(:plugin) { get_plugin("linux/filesystem") } + let(:plugin) { get_plugin("filesystem") } before(:each) do allow(plugin).to receive(:collect_os).and_return(:linux) |