Sha256: d6a3b36ceaab3b17099d6958d7ce6fdcc5be9b97af227cc7bbed2aef47e26ad7

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Linux
      class DockerUptime < BaseResolver
        @fact_list ||= {}
        class << self
          private

          def post_resolve(fact_name)
            @fact_list.fetch(fact_name) { detect_uptime(fact_name) }
          end

          def detect_uptime(fact_name)
            days, hours, minutes, seconds = extract_uptime_from_docker
            total_seconds = convert_to_seconds(days, hours, minutes, seconds)
            @fact_list = Utils::UptimeHelper.create_uptime_hash(total_seconds)

            @fact_list[fact_name]
          end

          def extract_uptime_from_docker
            # time format [dd-][hh:]mm:ss
            time = Facter::Core::Execution.execute('ps -o etime= -p "1"', logger: log)
            extracted_time = time.split(/[-:]/)

            reversed_time = extracted_time.reverse
            seconds = reversed_time[0].to_i
            minutes = reversed_time[1].to_i
            hours = reversed_time[2].to_i
            days = reversed_time[3].to_i

            [days, hours, minutes, seconds]
          end

          def convert_to_seconds(days, hours, minutes, seconds)
            days * 24 * 3600 + hours * 3600 + minutes * 60 + seconds
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
facter-4.0.44 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.43 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.42 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.41 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.40 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.39 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.38 lib/facter/resolvers/linux/docker_uptime.rb
facter-4.0.37 lib/facter/resolvers/linux/docker_uptime.rb