Add package update count and reboot pending to host server info card

This commit is contained in:
Josh Holtrop 2026-08-21 10:17:20 -04:00
parent a21bf2c215
commit 2c0da87fac
2 changed files with 74 additions and 2 deletions

View File

@ -169,6 +169,49 @@ def read_virtual_machines
end end
end end
def read_os_id
return nil unless File.exist?("/etc/os-release")
File.readlines("/etc/os-release").each do |line|
if line =~ /^ID=(.+)$/
return $1.strip.delete('"').downcase
end
end
nil
end
def apt_updates
out = `apt-get -s -o Debug::NoLocking=true upgrade 2>/dev/null`
count = 0
out.each_line do |line|
count += 1 if line.start_with?("Inst ")
end
count
end
def apt_reboot_pending
File.exist?("/var/run/reboot-required")
end
def dnf_updates
out = `dnf -q check-update 2>/dev/null`
status = $?.exitstatus
return 0 if status == 0
return nil unless status == 100
count = 0
out.each_line do |line|
line = line.strip
next if line.empty?
next if line.start_with?("Obsoleting", "Last metadata")
count += 1 if line.split(/\s+/).size >= 3
end
count
end
def dnf_reboot_pending
`dnf needs-restarting -r >/dev/null 2>&1`
$?.exitstatus == 1
end
def read_server_info def read_server_info
info = { "type" => "server" } info = { "type" => "server" }
@ -181,6 +224,15 @@ def read_server_info
end end
end end
case read_os_id
when "ubuntu", "debian"
info["updates"] = apt_updates
info["reboot_pending"] = apt_reboot_pending
when "almalinux", "rocky", "rhel", "centos", "fedora"
info["updates"] = dnf_updates
info["reboot_pending"] = dnf_reboot_pending
end
out = `stat -c %W / 2>/dev/null`.strip out = `stat -c %W / 2>/dev/null`.strip
if out =~ /^\d+$/ && out.to_i > 0 if out =~ /^\d+$/ && out.to_i > 0
info["install_date"] = Time.at(out.to_i).strftime("%Y-%m-%d") info["install_date"] = Time.at(out.to_i).strftime("%Y-%m-%d")

View File

@ -125,8 +125,19 @@ if cgi.params.key?("content")
html << %(<div>) html << %(<div>)
if server if server
srv_stats = []
if updates = server["updates"]
cls = updates == 0 ? "ok" : "warn"
srv_stats << [cls, "#{updates} update#{updates == 1 ? "" : "s"}"]
end
if server["reboot_pending"]
srv_stats << ["warn", "Reboot pending"]
end
srv_cls = srv_stats.any? { |c, _| c == "bad" } ? "bad" :
srv_stats.any? { |c, _| c == "warn" } ? "warn" : "ok"
html << %(<div class="section-label">Server</div>) html << %(<div class="section-label">Server</div>)
html << %(<div class="card ok"><div class="card-header"><span class="card-title">Server Info</span></div><div class="subitems">) html << %(<div class="card #{srv_cls}"><div class="card-header"><span class="card-title">Server Info</span></div><div class="subitems">)
if server["os_name"] if server["os_name"]
html << <<~HTML html << <<~HTML
@ -178,7 +189,16 @@ if cgi.params.key?("content")
HTML HTML
end end
html << %(</div></div>) html << %(</div>)
if srv_stats.any?
srv_stats_html = srv_stats.map { |c, txt|
%(<span class="stat #{c}">#{CGI.escapeHTML(txt)}</span>)
}.join
html << %(<div class="drive-stats" style="margin-top:0.45rem;padding:0 0.6rem;">#{srv_stats_html}</div>)
end
html << %(</div>)
end end
if filesystems.any? if filesystems.any?