diff options
author | Tim Smith <tsmith@chef.io> | 2017-06-01 13:58:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-01 13:58:40 -0700 |
commit | 484d8b538d547096ed4472ca5a795d21d0d902db (patch) | |
tree | 6468f60a2e1e54550b63f4bdecebde31147b87b6 | |
parent | be0772889c83e34151f3c7d3421cbf8ad4680c17 (diff) | |
parent | f835ca6beafe91623b0f66ede802e3ebb98b9131 (diff) | |
download | ohai-484d8b538d547096ed4472ca5a795d21d0d902db.tar.gz |
Merge pull request #1001 from chef/zpool
Update zpools plugin to work on Linux/BSD
-rw-r--r-- | lib/ohai/plugins/zpools.rb (renamed from lib/ohai/plugins/solaris2/zpools.rb) | 55 | ||||
-rw-r--r-- | spec/unit/plugins/solaris2/zpools_spec.rb | 153 | ||||
-rw-r--r-- | spec/unit/plugins/zpools_spec.rb | 242 |
3 files changed, 281 insertions, 169 deletions
diff --git a/lib/ohai/plugins/solaris2/zpools.rb b/lib/ohai/plugins/zpools.rb index 9356c29c..7e518663 100644 --- a/lib/ohai/plugins/solaris2/zpools.rb +++ b/lib/ohai/plugins/zpools.rb @@ -1,6 +1,6 @@ # # Author:: Jason J. W. Williams (williamsjj@digitar.com) -# Copyright:: Copyright (c) 2011-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2011-2017 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,42 +18,65 @@ Ohai.plugin(:Zpools) do provides "zpools" + depends "platform_family" - collect_data(:solaris2) do - pools = Mash.new + # If zpool status doesn't know about a field it returns '-'. + # We don't want to fill a field with that + def sanitize_value(value) + value == "-" ? nil : value + end + def gather_pool_info + pools = Mash.new # Grab ZFS zpools overall health and attributes so = shell_out("zpool list -H -o name,size,alloc,free,cap,dedup,health,version") so.stdout.lines do |line| case line when /^([-_0-9A-Za-z]*)\s+([.0-9]+[MGTPE])\s+([.0-9]+[MGTPE])\s+([.0-9]+[MGTPE])\s+(\d+%)\s+([.0-9]+x)\s+([-_0-9A-Za-z]+)\s+(\d+|-)$/ + Ohai::Log.debug("Plugin Zpools: Parsing zpool list line: #{line.chomp}") pools[$1] = Mash.new - pools[$1][:pool_size] = $2 - pools[$1][:pool_allocated] = $3 - pools[$1][:pool_free] = $4 - pools[$1][:capacity_used] = $5 - pools[$1][:dedup_factor] = $6 - pools[$1][:health] = $7 - pools[$1][:zpool_version] = $8 + pools[$1][:pool_size] = sanitize_value($2) + pools[$1][:pool_allocated] = sanitize_value($3) + pools[$1][:pool_free] = sanitize_value($4) + pools[$1][:capacity_used] = sanitize_value($5) + pools[$1][:dedup_factor] = sanitize_value($6) + pools[$1][:health] = sanitize_value($7) + pools[$1][:zpool_version] = sanitize_value($8) end end + pools + end + + collect_data(:solaris2, :linux, :freebsd, :openbsd, :netbsd, :dragonflybsd) do + pools = gather_pool_info # Grab individual health for devices in the zpools pools.keys.each do |pool| pools[pool][:devices] = Mash.new + # Run "zpool status" as non-root user (adm) so that # the command won't try to open() each device which can # hang the command if any of the disks are bad. - so = shell_out("su adm -c \"zpool status #{pool}\"") + if platform_family == "solaris2" + command = "su adm -c \"zpool status #{pool}\"" + else + command = "zpool status #{pool}" + end + + so = shell_out(command) so.stdout.lines do |line| case line - when /^\s+(c[-_a-zA-Z0-9]+)\s+([-_a-zA-Z0-9]+)\s+(\d+)\s+(\d+)\s+(\d+)$/ + # linux: http://rubular.com/r/J3wQC6E2lH + # solaris: http://rubular.com/r/FqOBzUQQ4p + # freebsd: http://rubular.com/r/RYkMNlytXl + when /^\s+((sd|c|ad|da)[-_a-zA-Z0-9]+)\s+([-_a-zA-Z0-9]+)\s+(\d+)\s+(\d+)\s+(\d+)$/ + Ohai::Log.debug("Plugin Zpools: Parsing zpool status line: #{line.chomp}") pools[pool][:devices][$1] = Mash.new - pools[pool][:devices][$1][:state] = $2 + pools[pool][:devices][$1][:state] = $3 pools[pool][:devices][$1][:errors] = Mash.new - pools[pool][:devices][$1][:errors][:read] = $3 - pools[pool][:devices][$1][:errors][:write] = $4 - pools[pool][:devices][$1][:errors][:checksum] = $5 + pools[pool][:devices][$1][:errors][:read] = $4 + pools[pool][:devices][$1][:errors][:write] = $5 + pools[pool][:devices][$1][:errors][:checksum] = $6 end end end diff --git a/spec/unit/plugins/solaris2/zpools_spec.rb b/spec/unit/plugins/solaris2/zpools_spec.rb deleted file mode 100644 index fdc94bc4..00000000 --- a/spec/unit/plugins/solaris2/zpools_spec.rb +++ /dev/null @@ -1,153 +0,0 @@ -# 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. -# - -require_relative "../../../spec_helper.rb" - -describe Ohai::System, "Solaris 2.x zpool plugin" do - before(:each) do - @plugin = get_plugin("solaris2/zpools") - allow(@plugin).to receive(:collect_os).and_return(:solaris2) - - @zpool_status_rpool = <<-EOSR -pool: rpool -state: ONLINE -scan: resilvered 65.6G in 0h8m with 0 errors on Fri Jun 6 14:22:40 2014 -config: - - NAME STATE READ WRITE CKSUM - rpool ONLINE 0 0 0 - mirror-0 ONLINE 0 0 0 - c3t5d0s0 ONLINE 0 0 0 - c3t4d0s0 ONLINE 0 0 0 - -errors: No known data errors -EOSR - - @zpool_status_tank = <<-EOST -pool: tank -state: ONLINE -scan: scrub repaired 0 in 0h0m with 0 errors on Fri Jun 6 14:43:40 2014 -config: - - NAME STATE READ WRITE CKSUM - tank ONLINE 0 0 0 - raidz2-0 ONLINE 0 0 0 - c1t50014EE209D1DBA9d0 ONLINE 0 0 0 - c1t50014EE20A0ECED2d0 ONLINE 0 0 0 - c1t50014EE20A106BFFd0 ONLINE 0 0 0 - c1t50014EE20A1423E8d0 ONLINE 0 0 0 - c1t50014EE20A145447d0 ONLINE 0 0 0 - c1t50014EE20A29EE56d0 ONLINE 0 0 0 - raidz2-1 ONLINE 0 0 0 - c1t50014EE20A2B984Cd0 ONLINE 0 0 0 - c1t50014EE20A2BBC78d0 ONLINE 0 0 0 - c1t50014EE20A2BDCA9d0 ONLINE 0 0 0 - c1t50014EE25F697DC4d0 ONLINE 0 0 0 - c1t50014EE25F698BECd0 ONLINE 0 0 0 - c1t50014EE25F6998DAd0 ONLINE 0 0 0 -EOST - @zpool_out = <<-EOZO -rpool 109G 66.2G 42.8G 60% 1.00x ONLINE 34 -tank 130T 4.91M 130T 0% 1.00x ONLINE 34 -EOZO - allow(@plugin).to receive(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0, @zpool_out, "")) - allow(@plugin).to receive(:shell_out).with("su adm -c \"zpool status rpool\"").and_return(mock_shell_out(0, @zpool_status_rpool, "")) - allow(@plugin).to receive(:shell_out).with("su adm -c \"zpool status tank\"").and_return(mock_shell_out(0, @zpool_status_tank, "")) - end - - describe "On Solaris2 Common" do - it "Should have entries for both zpools" do - @plugin.run - expect(@plugin[:zpools][:rpool]).to be - expect(@plugin[:zpools][:tank]).to be - end - - it "Should have the correct pool size" do - @plugin.run - expect(@plugin[:zpools][:rpool][:pool_size]).to match("109G") - expect(@plugin[:zpools][:tank][:pool_size]).to match("130T") - end - - it "Should have the correct pool allocated size" do - @plugin.run - expect(@plugin[:zpools][:rpool][:pool_allocated]).to match("66.2G") - expect(@plugin[:zpools][:tank][:pool_allocated]).to match("4.91M") - end - - it "Should have the correct pool free size" do - @plugin.run - expect(@plugin[:zpools][:rpool][:pool_free]).to match("42.8G") - expect(@plugin[:zpools][:tank][:pool_free]).to match("130T") - end - - it "Should have the correct capacity_used" do - @plugin.run - expect(@plugin[:zpools][:rpool][:capacity_used]).to match("60%") - expect(@plugin[:zpools][:tank][:capacity_used]).to match("0%") - end - - it "Should have the correct dedup_factor" do - @plugin.run - expect(@plugin[:zpools][:rpool][:dedup_factor]).to match("1.00x") - expect(@plugin[:zpools][:tank][:dedup_factor]).to match("1.00x") - end - - it "Should have the correct health" do - @plugin.run - expect(@plugin[:zpools][:rpool][:health]).to match("ONLINE") - expect(@plugin[:zpools][:tank][:health]).to match("ONLINE") - end - - it "Should have the correct number of devices" do - @plugin.run - expect(@plugin[:zpools][:rpool][:devices].keys.size).to match(2) - expect(@plugin[:zpools][:tank][:devices].keys.size).to match(12) - end - end - - describe "On OmniOS_151006" do - before(:each) do - @zpool_out = <<-EOZO -rpool 109G 66.2G 42.8G 60% 1.00x ONLINE - -tank 130T 4.91M 130T 0% 1.00x ONLINE - -EOZO - allow(@plugin).to receive(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0, @zpool_out, "")) - end - - it "Won't have a version number" do - @plugin.run - expect(@plugin[:zpools][:rpool][:zpool_version]).to match("-") - expect(@plugin[:zpools][:tank][:zpool_version]).to match("-") - end - - end - - describe "On Solaris_11.1" do - before(:each) do - @zpool_out = <<-EOZO -rpool 109G 66.2G 42.8G 60% 1.00x ONLINE 34 -tank 130T 4.91M 130T 0% 1.00x ONLINE 34 -EOZO - allow(@plugin).to receive(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0, @zpool_out, "")) - end - - it "Should have a version number" do - @plugin.run - expect(@plugin[:zpools][:rpool][:zpool_version]).to match("34") - expect(@plugin[:zpools][:tank][:zpool_version]).to match("34") - end - - end -end diff --git a/spec/unit/plugins/zpools_spec.rb b/spec/unit/plugins/zpools_spec.rb new file mode 100644 index 00000000..b99a45ac --- /dev/null +++ b/spec/unit/plugins/zpools_spec.rb @@ -0,0 +1,242 @@ +# 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. +# + +require_relative "../../spec_helper.rb" + +describe Ohai::System, "zpools plugin" do + let(:plugin) { get_plugin("zpools") } + + context "on Linux" do + let(:zpool_status_tank) do + <<-EOST + pool: tank + state: ONLINE + scan: scrub repaired 0 in 0h0m with 0 errors on Fri Jun 6 14:43:40 2014 + config: + + NAME STATE READ WRITE CKSUM + tank ONLINE 0 0 0 + raidz2-0 ONLINE 0 0 0 + sdc ONLINE 0 0 0 + sdd ONLINE 0 0 0 + sde ONLINE 0 0 0 + sdf ONLINE 0 0 0 + sdg ONLINE 0 0 0 + sdh ONLINE 0 0 0 + raidz2-1 ONLINE 0 0 0 + sdi ONLINE 0 0 0 + sdj ONLINE 0 0 0 + sdk ONLINE 0 0 0 + sdl ONLINE 0 0 0 + sdm ONLINE 0 0 0 + sdn ONLINE 0 0 0 +EOST + end + let(:zpool_out) do + <<-EOZO +rpool 109G 66.2G 42.8G 60% 1.00x ONLINE - +tank 130T 4.91M 130T 0% 1.00x ONLINE - +EOZO + end + let(:zpool_status_rpool) do + <<-EOSR + pool: rpool + state: ONLINE + scan: none requested + config: + + NAME STATE READ WRITE CKSUM + rpool ONLINE 0 0 0 + mirror-0 ONLINE 0 0 0 + sda ONLINE 0 0 0 + sdb ONLINE 0 0 0 + + errors: No known data errors +EOSR + end + before do + allow(plugin).to receive(:platform_family).and_return("rhel") + allow(plugin).to receive(:collect_os).and_return(:linux) + allow(plugin).to receive(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0, zpool_out, "")) + allow(plugin).to receive(:shell_out).with("zpool status rpool").and_return(mock_shell_out(0, zpool_status_rpool, "")) + allow(plugin).to receive(:shell_out).with("zpool status tank").and_return(mock_shell_out(0, zpool_status_tank, "")) + end + + it "Has entries for both zpools" do + plugin.run + expect(plugin[:zpools][:rpool]).to be + expect(plugin[:zpools][:tank]).to be + end + + it "Has the correct pool size" do + plugin.run + expect(plugin[:zpools][:rpool][:pool_size]).to match("109G") + expect(plugin[:zpools][:tank][:pool_size]).to match("130T") + end + + it "Has the correct pool allocated size" do + plugin.run + expect(plugin[:zpools][:rpool][:pool_allocated]).to match("66.2G") + expect(plugin[:zpools][:tank][:pool_allocated]).to match("4.91M") + end + + it "Has the correct pool free size" do + plugin.run + expect(plugin[:zpools][:rpool][:pool_free]).to match("42.8G") + expect(plugin[:zpools][:tank][:pool_free]).to match("130T") + end + + it "Has the correct capacity_used" do + plugin.run + expect(plugin[:zpools][:rpool][:capacity_used]).to match("60%") + expect(plugin[:zpools][:tank][:capacity_used]).to match("0%") + end + + it "Has the correct dedup_factor" do + plugin.run + expect(plugin[:zpools][:rpool][:dedup_factor]).to match("1.00x") + expect(plugin[:zpools][:tank][:dedup_factor]).to match("1.00x") + end + + it "Has the correct health" do + plugin.run + expect(plugin[:zpools][:rpool][:health]).to match("ONLINE") + expect(plugin[:zpools][:tank][:health]).to match("ONLINE") + end + + it "Has the correct number of devices" do + plugin.run + expect(plugin[:zpools][:rpool][:devices].keys.size).to match(2) + expect(plugin[:zpools][:tank][:devices].keys.size).to match(12) + end + + it "Won't have a version number" do + plugin.run + expect(plugin[:zpools][:rpool][:zpool_version]).to be_nil + expect(plugin[:zpools][:tank][:zpool_version]).to be_nil + end + end + + context "on Solaris2" do + let(:zpool_status_tank) do + <<-EOST +pool: tank +state: ONLINE +scan: scrub repaired 0 in 0h0m with 0 errors on Fri Jun 6 14:43:40 2014 +config: + + NAME STATE READ WRITE CKSUM + tank ONLINE 0 0 0 + raidz2-0 ONLINE 0 0 0 + c1t50014EE209D1DBA9d0 ONLINE 0 0 0 + c1t50014EE20A0ECED2d0 ONLINE 0 0 0 + c1t50014EE20A106BFFd0 ONLINE 0 0 0 + c1t50014EE20A1423E8d0 ONLINE 0 0 0 + c1t50014EE20A145447d0 ONLINE 0 0 0 + c1t50014EE20A29EE56d0 ONLINE 0 0 0 + raidz2-1 ONLINE 0 0 0 + c1t50014EE20A2B984Cd0 ONLINE 0 0 0 + c1t50014EE20A2BBC78d0 ONLINE 0 0 0 + c1t50014EE20A2BDCA9d0 ONLINE 0 0 0 + c1t50014EE25F697DC4d0 ONLINE 0 0 0 + c1t50014EE25F698BECd0 ONLINE 0 0 0 + c1t50014EE25F6998DAd0 ONLINE 0 0 0 +EOST + end + let(:zpool_out) do + <<-EOZO +rpool 109G 66.2G 42.8G 60% 1.00x ONLINE 34 +tank 130T 4.91M 130T 0% 1.00x ONLINE 34 +EOZO + end + let(:zpool_status_rpool) do + <<-EOSR +pool: rpool +state: ONLINE +scan: resilvered 65.6G in 0h8m with 0 errors on Fri Jun 6 14:22:40 2014 +config: + + NAME STATE READ WRITE CKSUM + rpool ONLINE 0 0 0 + mirror-0 ONLINE 0 0 0 + c3t5d0s0 ONLINE 0 0 0 + c3t4d0s0 ONLINE 0 0 0 + +errors: No known data errors +EOSR + end + before do + allow(plugin).to receive(:platform_family).and_return("solaris2") + allow(plugin).to receive(:collect_os).and_return(:solaris2) + allow(plugin).to receive(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0, zpool_out, "")) + allow(plugin).to receive(:shell_out).with("su adm -c \"zpool status rpool\"").and_return(mock_shell_out(0, zpool_status_rpool, "")) + allow(plugin).to receive(:shell_out).with("su adm -c \"zpool status tank\"").and_return(mock_shell_out(0, zpool_status_tank, "")) + end + + it "Has entries for both zpools" do + plugin.run + expect(plugin[:zpools][:rpool]).to be + expect(plugin[:zpools][:tank]).to be + end + + it "Has the correct pool size" do + plugin.run + expect(plugin[:zpools][:rpool][:pool_size]).to match("109G") + expect(plugin[:zpools][:tank][:pool_size]).to match("130T") + end + + it "Has the correct pool allocated size" do + plugin.run + expect(plugin[:zpools][:rpool][:pool_allocated]).to match("66.2G") + expect(plugin[:zpools][:tank][:pool_allocated]).to match("4.91M") + end + + it "Has the correct pool free size" do + plugin.run + expect(plugin[:zpools][:rpool][:pool_free]).to match("42.8G") + expect(plugin[:zpools][:tank][:pool_free]).to match("130T") + end + + it "Has the correct capacity_used" do + plugin.run + expect(plugin[:zpools][:rpool][:capacity_used]).to match("60%") + expect(plugin[:zpools][:tank][:capacity_used]).to match("0%") + end + + it "Has the correct dedup_factor" do + plugin.run + expect(plugin[:zpools][:rpool][:dedup_factor]).to match("1.00x") + expect(plugin[:zpools][:tank][:dedup_factor]).to match("1.00x") + end + + it "Has the correct health" do + plugin.run + expect(plugin[:zpools][:rpool][:health]).to match("ONLINE") + expect(plugin[:zpools][:tank][:health]).to match("ONLINE") + end + + it "Has the correct number of devices" do + plugin.run + expect(plugin[:zpools][:rpool][:devices].keys.size).to match(2) + expect(plugin[:zpools][:tank][:devices].keys.size).to match(12) + end + + it "Has a version number" do + plugin.run + expect(plugin[:zpools][:rpool][:zpool_version]).to match("34") + expect(plugin[:zpools][:tank][:zpool_version]).to match("34") + end + end +end |