The Puppet Labs Issue Tracker has Moved: https://tickets.puppetlabs.com

Bug #13535

Facter uptime can be wrong or negative on AIX

Added by Malcolm Howe about 2 years ago. Updated over 1 year ago.

Status:ClosedStart date:03/30/2012
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:library
Target version:1.6.14
Keywords: Affected Facter version:1.6.6
Branch:https://github.com/puppetlabs/facter/pull/333

We've Moved!

Ticket tracking is now hosted in JIRA: https://tickets.puppetlabs.com

This issue is currently not available for export. If you are experiencing the issue described below, please file a new ticket in JIRA. Once a new ticket has been created, please add a link to it that points back to this Redmine ticket.


Description

On AIX facter can show a negative uptime

uptime => -260 days
uptime_days => -260
uptime_hours => -6219
uptime_seconds => -22385831

This occurs because of the four methods for calculating uptime in facter/util/uptime.rb the only one that can be used on AIX is who -b. But on AIX who -b doesn’t display the year

$ who -b
.        system boot 14 Dec 10:30

so the current year is assumed which in this example results in a negative uptime. A possible solution would be to use the uptime (or equivalently w -u) command which produces similar output on AIX as it does on Linux

$ uptime
10:46AM   up 106 days,  23:18,  3 users,  load average: 0.03, 0.33, 0.53

For example

--- uptime.rb.save      2012-02-28 11:25:38.000000000 +0000
+++ uptime.rb   2012-03-30 11:03:05.000000000 +0100
@@ -4,7 +4,7 @@
#
module Facter::Util::Uptime
def self.get_uptime_seconds_unix
-     uptime_proc_uptime or uptime_sysctl or uptime_kstat or uptime_who_dash_b
+     uptime_proc_uptime or uptime_sysctl or uptime_kstat or uptime_uptime or uptime_who_dash_b
end
def self.get_uptime_seconds_win
@@ -37,6 +37,20 @@
end
end
+   def self.uptime_uptime
+     if output = Facter::Util::Resolution.exec("#{uptime_uptime_cmd} 2>/dev/null")
+       up=0
+       if output =~ /(\d+) days,\s+(\d+):(\d+)/
+           up=86400*$1.to_i + 3600*$2.to_i + 60*$3.to_i
+       elsif output =~ /(\d+):(\d+),/
+           up=3600*$1.to_i + 60*$2.to_i
+       elsif output =~ /(\d+) mins,/
+           up=($1.to_i*60)
+       end
+       up
+     end
+   end
+
def self.uptime_who_dash_b
if output = Facter::Util::Resolution.exec("#{uptime_who_cmd} 2>/dev/null")
compute_uptime(Time.parse(output))
@@ -57,6 +71,10 @@
def self.uptime_kstat_cmd
'kstat -p unix:::boot_time'
+   end
+
+   def self.uptime_uptime_cmd
+     'uptime'
end
def self.uptime_who_cmd

results in the facter output

uptime => 106 days
uptime_days => 106
uptime_hours => 2567
uptime_seconds => 9242280

History

#1 Updated by Ken Barber about 2 years ago

  • Category set to library
  • Status changed from Unreviewed to Accepted

#2 Updated by Alex Harvey over 1 year ago

  • Assignee set to Alex Harvey

I have reproduced this on Solaris 2.6 and facter 1.6.12-rc2 –

myhost[facter-1.6.12-rc2]# uname -s
SunOS
myhost[facter-1.6.12-rc2]# uname -r
5.6
myhost[facter-1.6.12-rc2]# export RUBYLIB=/var/tmp/facter-1.6.12-rc2/lib
myhost[facter-1.6.12-rc2]# ./bin/facter uptime
-106 days

The issue appears on versions of Solaris prior to Solaris 8, when the kstat command was introduced.

It also appears that the AIX implementation of who -b is the standard in all versions of commercial Unix –

Solaris 10 –

$ who -b
   .       system boot  May 31 17:42

HP-UX 11.31 –

$ who -b
   .       system boot  Nov  1 10:43

Tru64 V5.1 –

$ who -b
   .        system boot Jun 18 12:29

I believe Malcolm Howe’s suggestion to introduce a method ‘uptime_uptime’ to run before ‘uptime_who_dash_b’ will fix the problem on all versions of Unix.

#3 Updated by Alex Harvey over 1 year ago

While looking at the RSpec tests I have noted that who -b didn’t show the year in the file ./spec/fixtures/unit/util/uptime/who_b_boottime that is used in the existing RSpec test either:

$ cat ./spec/fixtures/unit/util/uptime/who_b_boottime
reboot   ~        Aug  1 14:13

I can’t work out what OS that is from – it doesn’t appear to be RHEL, CentOS or Ubuntu.

Meanwhile, the existing RSpec test in spec/unit/util/uptime_spec.rb only works because only the case of one hour in the future is considered:

it "should use 'who -b'" do
  who_b_output_file = my_fixture('who_b_boottime') # Aug 1 14:13
  Facter::Util::Uptime.stubs(:uptime_who_cmd).returns("cat \"#{who_b_output_file}\"")
  Time.stubs(:now).returns Time.parse("Aug 01 15:13") # one hour later
  Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60
end

#4 Updated by Alex Harvey over 1 year ago

I now know the who_b_boottime output is from a BSD OS, perhaps OS X, which gives me –

$ who -b
reboot   ~        Jul 28 16:34 

#5 Updated by Alex Harvey over 1 year ago

  • Status changed from Accepted to In Topic Branch Pending Review
  • Branch set to https://github.com/alexharv074/facter/tree/ticket/1.6.x/13535_facter_uptime_can_be_wrong_on_AIX

I have submitted a patch now. See also discussion on puppet developers mailing list under thread ‘who -b etc in facter’s uptime (#13535)’.

#6 Updated by Alex Harvey over 1 year ago

This is the link to the puppet developers mailing list discussion: https://groups.google.com/d/msg/puppet-dev/–/Zj0P4fHpEUMJ

#7 Updated by Jeff McCune over 1 year ago

Alex, I’m reviewing this now.

Do you mind if I amend your work (you’ll still be the author, I’ll just be the committer) to rename uptime_uptime to uptime_command?

-Jeff

#8 Updated by Jeff McCune over 1 year ago

  • Assignee changed from Alex Harvey to Jeff McCune

I’m working on this now.

-Jeff

#9 Updated by Jeff McCune over 1 year ago

  • Branch changed from https://github.com/alexharv074/facter/tree/ticket/1.6.x/13535_facter_uptime_can_be_wrong_on_AIX to https://github.com/puppetlabs/facter/pull/333

I re-worked a bit of this in my own branch. Alex remains as the author, I’ll be the committer in the history.

https://github.com/puppetlabs/facter/pull/333

#10 Updated by Jeff McCune over 1 year ago

  • Status changed from In Topic Branch Pending Review to Merged - Pending Release
  • Assignee deleted (Jeff McCune)

Merged

Merged into 1.6.x, 2.x, and master as 8063ffe

Thank you

Alex, thank you very much for this contribution. Sorry it took so long to merge it into the main release branches. Please let me know if you have any questions or concerns.

This fix should be released with Facter 1.6.14 and 2.0.0

-Jeff

#11 Updated by Jeff McCune over 1 year ago

  • Target version set to 1.6.14

#12 Updated by Alex Harvey over 1 year ago

Thanks Jeff, and I agree with your amendments to the code.

#13 Updated by Matthaus Owens over 1 year ago

  • Status changed from Merged - Pending Release to Closed

Released in Facter 1.6.14

Also available in: Atom PDF