From 6b7662235c6d3772a02ef0bcfb8035841746e9ae Mon Sep 17 00:00:00 2001 From: okopop Date: Wed, 23 Oct 2024 20:38:08 +0200 Subject: [PATCH] Add fallback solution for Last Reboot fact openSUSE dont support "uptime --since", maybe others too, make a complicated one to save them all. --- README.md | 2 +- lib/facter/motd_last_reboot.rb | 9 ++++++++- spec/classes/motd_spec.rb | 2 +- templates/motd.epp | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 523c431..0cf3df1 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ motd::warn: "This OS version is EOL since XX-YY-ZZ" ================== Motd by Puppet ============================== Hostname.........: testhest.example.com Platform.........: vmware, 2 CPU, 3.47 GiB RAM -Operatingsystem..: Red Hat Enterprise Linux release 8.10 (Ootpa) +Operating System.: Red Hat Enterprise Linux release 8.10 (Ootpa) Puppet Agent.....: 7.18.0 Last reboot......: 2024-10-19 01:22 Information......: Product X | Production | Team Y diff --git a/lib/facter/motd_last_reboot.rb b/lib/facter/motd_last_reboot.rb index 9400336..fb19aa1 100644 --- a/lib/facter/motd_last_reboot.rb +++ b/lib/facter/motd_last_reboot.rb @@ -1,6 +1,13 @@ Facter.add(:motd_last_reboot) do + # N.B. seconds can flip back and forth and create unwanted changes, skip them setcode do + # This becomes empty when uptime command does not have "--since" implemented lastboot = Facter::Core::Execution.execute('/usr/bin/uptime --since') - lastboot.slice(0..-4) # to 4th char from the end + if !lastboot.empty? + lastboot.slice(0..-4) # to 4th char from the end + else + # This is a solid fallback when uptime command does not work as expected + Facter::Core::Execution.execute('date -d "@$(($(date +%s) - $(awk \'{print int($1)}\' /proc/uptime)))" +"%Y-%m-%d %H:%M"') + end end end diff --git a/spec/classes/motd_spec.rb b/spec/classes/motd_spec.rb index 0e8522d..7e65934 100644 --- a/spec/classes/motd_spec.rb +++ b/spec/classes/motd_spec.rb @@ -20,7 +20,7 @@ context 'with default_facts.yml' do it { is_expected.to contain_file('/etc/motd').with_content(%r{Hostname.........: testhest.example.com}) } it { is_expected.to contain_file('/etc/motd').with_content(%r{Platform.........: vmware, 2 CPU, 3.64 GiB RAM}) } - it { is_expected.to contain_file('/etc/motd').with_content(%r{Operatingsystem..: }) } + it { is_expected.to contain_file('/etc/motd').with_content(%r{Operating System.: }) } it { is_expected.to contain_file('/etc/motd').with_content(%r{Puppet Agent.....: 7.18.0}) } it { is_expected.to contain_file('/etc/motd').with_content(%r{Last reboot......: \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}}) } end diff --git a/templates/motd.epp b/templates/motd.epp index 9a457c3..d3e5efb 100644 --- a/templates/motd.epp +++ b/templates/motd.epp @@ -1,7 +1,7 @@ ================== Motd by Puppet ============================== Hostname.........: <%= $facts['networking']['fqdn'] %> Platform.........: <%= $facts['virtual'] %>, <%= $facts['processors']['count'] %> CPU, <%= $facts['memory']['system']['total'] %> RAM -Operatingsystem..: <%= $facts['os']['distro']['description'] %> +Operating System.: <%= $facts['os']['distro']['description'] %> Puppet Agent.....: <%= $facts['puppetversion'] %> Last reboot......: <%= $motd_last_reboot %> <% if $motd::info { -%>