diff options
author | Tim Smith <tsmith@chef.io> | 2017-09-06 07:16:54 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-06 07:16:54 -0700 |
commit | 819a7e39019126474761698c1d3e2a40f114b7fc (patch) | |
tree | b98c50b3a6d5b580f34053bdfaeef0734e8a5a92 | |
parent | ed190dcb201e4c154177e8bb5fb5e6a5c70bf88e (diff) | |
parent | 094ccf036ec26d16219ab65386f4368c8773ef71 (diff) | |
download | ohai-819a7e39019126474761698c1d3e2a40f114b7fc.tar.gz |
Merge pull request #1047 from jeunito/bug/1011
Add error handling in Linux filesystem plugin
-rw-r--r-- | lib/ohai/plugins/linux/filesystem.rb | 86 | ||||
-rw-r--r-- | spec/unit/plugins/linux/filesystem_spec.rb | 12 |
2 files changed, 59 insertions, 39 deletions
diff --git a/lib/ohai/plugins/linux/filesystem.rb b/lib/ohai/plugins/linux/filesystem.rb index 4e8fd0de..e15e94f2 100644 --- a/lib/ohai/plugins/linux/filesystem.rb +++ b/lib/ohai/plugins/linux/filesystem.rb @@ -95,52 +95,60 @@ Ohai.plugin(:Filesystem) do fs = Mash.new # Grab filesystem data from df - so = shell_out("df -P") - so.stdout.each_line do |line| - case line - when /^Filesystem\s+1024-blocks/ - next - when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\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 + begin + so = shell_out("df -P") + so.stdout.each_line do |line| + case line + when /^Filesystem\s+1024-blocks/ + next + when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\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 - end - # Grab filesystem inode data from df - so = shell_out("df -iP") - so.stdout.each_line do |line| - case line - when /^Filesystem\s+Inodes/ - next - when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/ - key = "#{$1},#{$6}" - fs[key] ||= Mash.new - fs[key][:device] = $1 - fs[key][:total_inodes] = $2 - fs[key][:inodes_used] = $3 - fs[key][:inodes_available] = $4 - fs[key][:inodes_percent_used] = $5 - fs[key][:mount] = $6 + # Grab filesystem inode data from df + so = shell_out("df -iP") + so.stdout.each_line do |line| + case line + when /^Filesystem\s+Inodes/ + next + when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/ + key = "#{$1},#{$6}" + fs[key] ||= Mash.new + fs[key][:device] = $1 + fs[key][:total_inodes] = $2 + fs[key][:inodes_used] = $3 + fs[key][:inodes_available] = $4 + fs[key][:inodes_percent_used] = $5 + fs[key][:mount] = $6 + end end + rescue Ohai::Exceptions::Exec + Ohai::Log.warn("df is not available") end # Grab mount information from /bin/mount - so = shell_out("mount") - so.stdout.each_line do |line| - if line =~ /^(.+?) on (.+?) type (.+?) \((.+?)\)$/ - key = "#{$1},#{$2}" - fs[key] = Mash.new unless fs.has_key?(key) - fs[key][:device] = $1 - fs[key][:mount] = $2 - fs[key][:fs_type] = $3 - fs[key][:mount_options] = $4.split(",") + begin + so = shell_out("mount") + so.stdout.each_line do |line| + if line =~ /^(.+?) on (.+?) type (.+?) \((.+?)\)$/ + key = "#{$1},#{$2}" + fs[key] = Mash.new unless fs.has_key?(key) + fs[key][:device] = $1 + fs[key][:mount] = $2 + fs[key][:fs_type] = $3 + fs[key][:mount_options] = $4.split(",") + end end + rescue Ohai::Exceptions::Exec + Ohai::Log.warn("mount is not available") end # We used to try to decide if we wanted to run lsblk or blkid diff --git a/spec/unit/plugins/linux/filesystem_spec.rb b/spec/unit/plugins/linux/filesystem_spec.rb index 96382b1e..65265cd4 100644 --- a/spec/unit/plugins/linux/filesystem_spec.rb +++ b/spec/unit/plugins/linux/filesystem_spec.rb @@ -27,6 +27,8 @@ describe Ohai::System, "Linux filesystem plugin" do allow(plugin).to receive(:shell_out).with("df -iP").and_return(mock_shell_out(0, "", "")) allow(plugin).to receive(:shell_out).with("mount").and_return(mock_shell_out(0, "", "")) allow(plugin).to receive(:which).with("lsblk").and_return(nil) + allow(plugin).to receive(:which).with("df").and_return("/bin/df") + allow(plugin).to receive(:which).with("mount").and_return("/bin/mount") allow(plugin).to receive(:which).with("blkid").and_return("/sbin/blkid") allow(plugin).to receive(:shell_out).with("/sbin/blkid", timeout: 60).and_return(mock_shell_out(0, "", "")) @@ -529,4 +531,14 @@ BLKID_TYPE expect(plugin[:filesystem]["by_mountpoint"]["/mnt"][:devices]).to eq(["/dev/sdb1", "/dev/sdc1"]) end end + + %w{df mount}.each do |command| + describe "when #{command} does not exist" do + it "logs warning about #{command} missing" do + allow(plugin).to receive(:shell_out).with(/#{command}/).and_raise(Ohai::Exceptions::Exec) + expect(Ohai::Log).to receive(:warn).with("#{command} is not available") + plugin.run + end + end + end end |