diff options
author | Jose Asuncion <jose.asuncion@gmail.com> | 2017-08-21 17:41:32 -0700 |
---|---|---|
committer | Jose Asuncion <jose.asuncion@gmail.com> | 2017-08-21 17:43:08 -0700 |
commit | 1bbc6997308dfabe9d83ed4b89dde91b12979fbc (patch) | |
tree | 08a08be3bacf68f97b71ecb2d4d899d936e2428b | |
parent | 74ae9b00ba4698a4097759506b3f844ee7f94e2d (diff) | |
download | ohai-1bbc6997308dfabe9d83ed4b89dde91b12979fbc.tar.gz |
add error handling
Signed-off-by: Jose Asuncion <jeunito@gmail.com>
-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..27d407a9 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 + if which("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 + 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 + else + 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(",") + if which("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(",") + end end + else + 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..72aa8f02 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 event" do + allow(plugin).to receive(:which).with(command).and_return(nil) + expect(Ohai::Log).to receive(:warn).with("#{command} is not available") + plugin.run + end + end + end end |