summaryrefslogtreecommitdiffstats
path: root/lib/facter/util
diff options
context:
space:
mode:
authorRein Henrichs <reinh@reinh.com>2010-08-02 17:28:45 -0700
committerRein Henrichs <reinh@reinh.com>2010-08-03 14:59:29 -0700
commita2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2 (patch)
tree45cb11ad615785f496cadb2b057a0a2c743a75e0 /lib/facter/util
parent1bd2ca29d8fd7d11e75096ceeeb704fe887cad31 (diff)
downloadfacter-a2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2.tar.gz
facter-a2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2.tar.xz
facter-a2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2.zip
[#2330] Uptime should not make redundant system calls
Rewrite of uptime facts and supporting utility methods. Works on unix, BSD, windows. No longer makes redundant system calls. Uses Facter::Util::Uptime utility methods: * Implemented uptime_seconds_unix using /proc/uptime or who -b on unix, sysctl on BSD. Added unit tests for the behaviors of get_uptime_seconds_unix: read from proc/uptime, read uptime from "sysctl -b kern.boottime", read uptime from "who -b", and return nil if nothing else works. * Implemented uptime_seconds_win using the Win32 API. Facts implemented: * uptime_{seconds,hours,days} Returns the respective integer value. * uptime Returns human readable uptime statistic that preserves original behavior. Examples: 3 days 1 day 5:08 hours 0:35 hours
Diffstat (limited to 'lib/facter/util')
-rw-r--r--lib/facter/util/uptime.rb66
1 files changed, 43 insertions, 23 deletions
diff --git a/lib/facter/util/uptime.rb b/lib/facter/util/uptime.rb
index c1e339b..f8a17db 100644
--- a/lib/facter/util/uptime.rb
+++ b/lib/facter/util/uptime.rb
@@ -1,32 +1,52 @@
+require 'time'
+
# A module to gather uptime facts
#
module Facter::Util::Uptime
- def self.get_uptime_simple
- time = Facter::Util::Resolution.exec('uptime')
- if time =~ /up\s*(\d+\s\w+)/
- $1
- elsif time =~ /up\s*(\d+:\d+)/
- $1 + " hours"
- else
- "unknown"
+ def self.get_uptime_seconds_unix
+ uptime_proc_uptime or uptime_sysctl or uptime_who_dash_b
+ end
+
+ def self.get_uptime_seconds_win
+ require 'Win32API'
+ getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
+ compute_uptime(Time.at(getTickCount.call() / 1000.0))
+ end
+
+ private
+
+ def self.uptime_proc_uptime
+ if File.exists? uptime_file
+ r = File.read uptime_file
+ r.split(" ").first.to_i
+ end
+ end
+
+ def self.uptime_sysctl
+ if (output = `#{uptime_sysctl_cmd}`) and $?.success?
+ compute_uptime(Time.at(output.unpack('L').first))
+ end
+ end
+
+ def self.uptime_who_dash_b
+ if (output = `#{uptime_who_cmd}`) and $?.success?
+ compute_uptime(Time.parse(output))
end
end
- def self.get_uptime
- r = IO.popen("/bin/cat /proc/uptime")
- uptime, idletime = r.readline.split(" ")
- r.close
- uptime_seconds = uptime.to_i
+ def self.compute_uptime(time)
+ (Time.now - time).to_i
+ end
+
+ def self.uptime_file
+ "/proc/uptime"
+ end
+
+ def self.uptime_sysctl_cmd
+ 'sysctl -b kern.boottime 2>/dev/null'
end
- def self.get_uptime_period(seconds, label)
- case label
- when 'days'
- value = seconds / 86400
- when 'hours'
- value = seconds / 3600
- when 'seconds'
- seconds
- end
+ def self.uptime_who_cmd
+ 'who -b 2>/dev/null'
end
-end
+end