diff options
author | Tim Smith <tsmith@chef.io> | 2017-09-01 12:23:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-01 12:23:35 -0700 |
commit | ed190dcb201e4c154177e8bb5fb5e6a5c70bf88e (patch) | |
tree | 9c6c9d059b88141335868a4d1ae4770a5fd45278 | |
parent | 1b511f066ecfc07763f7a09ffbfc76ad61d1092f (diff) | |
parent | f61bde8484599a2206e9a71e60a9c83f0d4544fd (diff) | |
download | ohai-ed190dcb201e4c154177e8bb5fb5e6a5c70bf88e.tar.gz |
Merge pull request #1049 from rmcleod8/aix_uptime_is_wrong_af
changing method to gather AIX uptime
-rw-r--r-- | lib/ohai/plugins/aix/uptime.rb | 35 | ||||
-rw-r--r-- | spec/unit/plugins/aix/uptime_spec.rb | 16 |
2 files changed, 32 insertions, 19 deletions
diff --git a/lib/ohai/plugins/aix/uptime.rb b/lib/ohai/plugins/aix/uptime.rb index ce463753..8adc78d0 100644 --- a/lib/ohai/plugins/aix/uptime.rb +++ b/lib/ohai/plugins/aix/uptime.rb @@ -22,16 +22,31 @@ Ohai.plugin(:Uptime) do collect_data(:aix) do require "date" - # Example output: - # $ who -b - # . system boot Jul 9 17:51 - so = shell_out("who -b") - so.stdout.lines.each do |line| - if line =~ /.* boot (.+)/ - uptime_seconds Time.now.to_i - DateTime.parse($1 + " #{Time.now.zone}").strftime("%s").to_i - uptime seconds_to_human(uptime_seconds) - break - end + # below we're going to assume that PID 1 is init (this is true 99.99999% of the time) + # output will look like this + # 1148-20:54:50 + # This reads as 1148 days, 20 hours, 54 minutes, 50 seconds since the process was started (elapsed) + # who -b does not return the YEAR, so we need something more concrete + so = shell_out("LC_ALL=POSIX ps -o etime= -p 1").stdout + + # Here we'll check our shell_out for a dash, which indicates there is a # of days involved + # We'll chunk off the days, hours (where applicable), minutes, seconds into seperate vars + # We also need to do this because ps -o etime= will not display days if the machine has been up for less than 24 hours + # If the machine has been up for less than one hour, the shell_out will not output hours hence our else + # see here: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix.cmds4/ps.htm#ps__row-d3e109655 + d = nil + h = nil + case so + when /^\d+-\d/ + (d, h, m, s) = so.split(/[-:]/) + when /^\d+:\d+:\d/ + (h, m, s) = so.split(/[:]/) + else + (m, s) = so.split(/[:]/) end + elapsed_seconds = ((d.to_i * 86400) + (h.to_i * 3600) + (m.to_i * 60) + s.to_i) + + uptime_seconds Time.now.to_i - elapsed_seconds + uptime seconds_to_human(elapsed_seconds) end end diff --git a/spec/unit/plugins/aix/uptime_spec.rb b/spec/unit/plugins/aix/uptime_spec.rb index d04403f8..018bcf22 100644 --- a/spec/unit/plugins/aix/uptime_spec.rb +++ b/spec/unit/plugins/aix/uptime_spec.rb @@ -23,19 +23,17 @@ describe Ohai::System, "Aix plugin uptime" do before(:each) do @plugin = get_plugin("aix/uptime") allow(@plugin).to receive(:collect_os).and_return(:aix) - allow(Time).to receive_message_chain(:now, :to_i).and_return(1412072511) - allow(Time).to receive_message_chain(:now, :zone).and_return("IST") - allow(DateTime).to receive_message_chain(:parse, :strftime, :to_i).and_return(1411561320) - allow(@plugin).to receive(:shell_out).with("who -b").and_return(mock_shell_out(0, " . system boot Sep 24 17:52", nil)) - + allow(@plugin).to receive(:shell_out).and_call_original + allow(Time).to receive_message_chain(:now, :to_i).and_return(1504287957) + allow(@plugin).to receive(:shell_out).with("LC_ALL=POSIX ps -o etime= -p 1").and_return(mock_shell_out(0, "1148-20:54:50", nil)) @plugin.run end - it "should set uptime_seconds to uptime" do - expect(@plugin[:uptime_seconds]).to eq(511191) + it "should set uptime_seconds to uptime with days" do + expect(@plugin[:uptime_seconds]).to eq(1405025467) end - it "should set uptime to a human readable date" do - expect(@plugin[:uptime]).to eq("5 days 21 hours 59 minutes 51 seconds") + it "should set uptime to a human readable date with days" do + expect(@plugin[:uptime]).to eq("1148 days 20 hours 54 minutes 50 seconds") end end |