class Facter::Util::Facts::UptimeParser

Constants

SECS_IN_AN_HOUR
SECS_IN_A_DAY
SECS_IN_A_MINUTE

Public Class Methods

uptime_seconds_unix() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 16
def uptime_seconds_unix
  uptime_proc_uptime || uptime_sysctl || uptime_executable
end

Private Class Methods

calculate_days(output) click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 97
def calculate_days(output)
  return unless output =~ /(\d+) day(?:s|\(s\))?,/

  SECS_IN_A_DAY * Regexp.last_match(1).to_i
end
calculate_days_hours(output) click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 83
def calculate_days_hours(output)
  return unless output =~ /(\d+) day(?:s|\(s\))?,\s+(\d+) hr(?:s|\(s\))?,/

  SECS_IN_A_DAY * Regexp.last_match(1).to_i +
    SECS_IN_AN_HOUR * Regexp.last_match(2).to_i
end
calculate_days_hours_minutes(output) click to toggle source

Regexp handles Solaris, AIX, HP-UX, and Tru64. ‘day(?:s|(s))?’ says maybe ‘day’, ‘days’,

or 'day(s)', and don't set $2.
# File lib/facter/util/facts/uptime_parser.rb, line 75
def calculate_days_hours_minutes(output)
  return unless output =~ /(\d+) day(?:s|\(s\))?,?\s+(\d+):-?(\d+)/

  SECS_IN_A_DAY * Regexp.last_match(1).to_i +
    SECS_IN_AN_HOUR * Regexp.last_match(2).to_i +
    SECS_IN_A_MINUTE * Regexp.last_match(3).to_i
end
calculate_days_minutes(output) click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 90
def calculate_days_minutes(output)
  return unless output =~ /(\d+) day(?:s|\(s\))?,\s+(\d+) min(?:s|\(s\))?,/

  SECS_IN_A_DAY * Regexp.last_match(1).to_i +
    SECS_IN_A_MINUTE * Regexp.last_match(2).to_i
end
calculate_hours(output) click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 113
def calculate_hours(output)
  return unless output =~ /(\d+) hr(?:s|\(s\))?,/

  SECS_IN_AN_HOUR * Regexp.last_match(1).to_i
end
calculate_hours_minutes(output) click to toggle source

must anchor to ‘up’ to avoid matching time of day at beginning of line. Certain versions of uptime on Solaris may insert a ‘-’ into the minutes field.

# File lib/facter/util/facts/uptime_parser.rb, line 106
def calculate_hours_minutes(output)
  return unless output =~ /up\s+(\d+):-?(\d+),/

  SECS_IN_AN_HOUR * Regexp.last_match(1).to_i +
    SECS_IN_A_MINUTE * Regexp.last_match(2).to_i
end
calculate_minutes(output) click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 119
def calculate_minutes(output)
  return unless output =~ /(\d+) min(?:s|\(s\))?,/

  SECS_IN_A_MINUTE * Regexp.last_match(1).to_i
end
compute_uptime(time) click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 68
def compute_uptime(time)
  (Time.now - time).to_i
end
output_calculator_methods() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 56
def output_calculator_methods
  %i[
    calculate_days_hours_minutes
    calculate_days_hours
    calculate_days_minutes
    calculate_days
    calculate_hours_minutes
    calculate_hours
    calculate_minutes
  ]
end
uptime_executable() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 34
def uptime_executable
  output = Facter::Core::Execution.execute(uptime_executable_cmd, logger: @log)

  return unless output

  up = 0
  output_calculator_methods.find { |method| up = send(method, output) }
  up || 0
end
uptime_executable_cmd() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 52
def uptime_executable_cmd
  'uptime'
end
uptime_file() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 44
def uptime_file
  '/proc/uptime'
end
uptime_proc_uptime() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 22
def uptime_proc_uptime
  output = Facter::Core::Execution.execute("/bin/cat #{uptime_file}", logger: @log)

  output.chomp.split(' ').first.to_i unless output.empty?
end
uptime_sysctl() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 28
def uptime_sysctl
  output = Facter::Core::Execution.execute("sysctl -n #{uptime_sysctl_variable}", logger: @log)

  compute_uptime(Time.at(output.match(/\d+/)[0].to_i)) unless output.empty?
end
uptime_sysctl_variable() click to toggle source
# File lib/facter/util/facts/uptime_parser.rb, line 48
def uptime_sysctl_variable
  'kern.boottime'
end