summaryrefslogtreecommitdiffstats
path: root/lib/facter/uptime.rb
blob: 7aeeb3986be5fd4289ed79280a8d8b54d9d3aa45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Fact: uptime
#
# Purpose: return the system uptime in a human readable format.
#
# Resolution:
#   Does basic maths on the "uptime_seconds" fact to return a count of
#   days, hours and minutes of uptime
#
# Caveats:
#

require 'facter/util/uptime'

Facter.add(:uptime) do
  setcode do
    seconds = Facter.fact(:uptime_seconds).value

    unless seconds
      "unknown"
    else
      days    = seconds / (60 * 60 * 24)
      hours   = seconds / (60 * 60) % 24
      minutes = seconds / 60 % 60

      case days
      when 0 then "#{hours}:#{"%02d" % minutes} hours"
      when 1 then '1 day'
      else "#{days} days"
      end
    end

  end
end