uptime fact now returns seconds
uptime_{days,minutes,hours,seconds} facts also available

Now includes support for Linux, OSX/BSD, and Windows

Limited tests also added

Signed-off-by: James Turnbull <[email protected]>
---
 lib/facter/uptime.rb         |   28 +++++++++++++++++++---------
 lib/facter/util/uptime.rb    |   41 +++++++++++++++++++++++------------------
 spec/unit/data/darwin_uptime |    1 +
 spec/unit/data/linux_uptime  |    1 +
 spec/unit/util/uptime.rb     |   23 +++++++++++++++++++++++
 5 files changed, 67 insertions(+), 27 deletions(-)
 create mode 100644 spec/unit/data/darwin_uptime
 create mode 100644 spec/unit/data/linux_uptime
 create mode 100755 spec/unit/util/uptime.rb

diff --git a/lib/facter/uptime.rb b/lib/facter/uptime.rb
index 3a3bc86..4d43ab7 100644
--- a/lib/facter/uptime.rb
+++ b/lib/facter/uptime.rb
@@ -7,14 +7,24 @@ Facter.add(:uptime) do
     end
 end
 
-if FileTest.exists?("/proc/uptime")
-    uptime = Facter::Util::Uptime.get_uptime
+Facter.add(:uptime) do
+    confine :operatingsystem => %w{FreeBSD NetBSD OpenBSD Darwin}
+    setcode do
+        Facter::Util::Uptime.get_uptime_bsd
+    end
+end
+
+Facter.add(:uptime) do
+    confine :operatingsystem => :windows
+    setcode do
+        Facter::Util::Uptime.get_uptime_win
+    end
+end
 
-    %w{days hours seconds}.each do |label|
-        Facter.add("uptime_" + label) do
-            setcode do
-                Facter::Util::Uptime.get_uptime_period(uptime, label)
-            end 
-        end 
-    end 
+%w{days hours seconds}.each do |label|
+    Facter.add("uptime_" + label) do
+        setcode do
+            Facter::Util::Uptime.get_uptime_period(Facter.value(:uptime), 
label)
+        end
+    end
 end
diff --git a/lib/facter/util/uptime.rb b/lib/facter/util/uptime.rb
index c1e339b..a9020dc 100644
--- a/lib/facter/util/uptime.rb
+++ b/lib/facter/util/uptime.rb
@@ -1,32 +1,37 @@
 # A module to gather uptime facts
 #
 module Facter::Util::Uptime
+
+    require 'time'
+
     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"
-        end
+        return_uptime(Time.parse(`who -b 2>/dev/null`))
+    end
+
+    def self.get_uptime_win
+        require 'Win32API'
+        getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
+        return_uptime(Time.at(getTickCount.call() / 1000.0))
+    end
+
+    def self.get_uptime_bsd
+        return_uptime(Time.at(`sysctl -b kern.boottime 
2>/dev/null`.unpack('L').first))
     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.return_uptime(time)
+      (Time.now - time).to_i
     end
 
     def self.get_uptime_period(seconds, label)
         case label
         when 'days'
-            value = seconds / 86400
+           seconds / 86400
         when 'hours'
-            value = seconds / 3600
+           seconds / 3600
+        when 'minutes'
+           seconds / 60
         when 'seconds'
-            seconds
-        end     
+           seconds
+        end
     end
-end   
+end
diff --git a/spec/unit/data/darwin_uptime b/spec/unit/data/darwin_uptime
new file mode 100644
index 0000000..d3a5394
--- /dev/null
+++ b/spec/unit/data/darwin_uptime
@@ -0,0 +1 @@
+Thu Jun 24 00:53:31 -0700 2010
diff --git a/spec/unit/data/linux_uptime b/spec/unit/data/linux_uptime
new file mode 100644
index 0000000..9ee9f97
--- /dev/null
+++ b/spec/unit/data/linux_uptime
@@ -0,0 +1 @@
+         system boot  2010-06-17 13:59
diff --git a/spec/unit/util/uptime.rb b/spec/unit/util/uptime.rb
new file mode 100755
index 0000000..af00c42
--- /dev/null
+++ b/spec/unit/util/uptime.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'facter/util/uptime'
+
+describe Facter::Util::Uptime do
+
+    it "should return a date on Darwin" do
+        sample_output_file = File.dirname(__FILE__) + '/../data/darwin_uptime'
+        darwin_uptime = File.new(sample_output_file).read().chomp!
+        Facter::Util::Uptime.stubs(:get_uptime_bsd).returns(darwin_uptime)
+        Facter::Util::Uptime.get_uptime_bsd.should == 'Thu Jun 24 00:53:31 
-0700 2010'
+    end
+
+    it "should return a date on Linux" do
+        sample_output_file = File.dirname(__FILE__) + '/../data/linux_uptime'
+        linux_uptime = File.new(sample_output_file).read().chomp!
+        Facter::Util::Uptime.stubs(:get_uptime_linux).returns(linux_uptime)
+        Facter::Util::Uptime.get_uptime_linux.should == '         system boot  
2010-06-17 13:59'
+    end
+
+end
-- 
1.6.6.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to