From 2c0da87fac52ff79fd58a9bbdd45934445852017 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 21 Aug 2026 10:17:20 -0400 Subject: [PATCH] Add package update count and reboot pending to host server info card --- bin/malpd | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ cgi-bin/malp.rb | 24 +++++++++++++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/bin/malpd b/bin/malpd index d0c66b3..aef874c 100755 --- a/bin/malpd +++ b/bin/malpd @@ -169,6 +169,49 @@ def read_virtual_machines 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 info = { "type" => "server" } @@ -181,6 +224,15 @@ def read_server_info 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 if out =~ /^\d+$/ && out.to_i > 0 info["install_date"] = Time.at(out.to_i).strftime("%Y-%m-%d") diff --git a/cgi-bin/malp.rb b/cgi-bin/malp.rb index c9a4cdc..0ee2064 100755 --- a/cgi-bin/malp.rb +++ b/cgi-bin/malp.rb @@ -125,8 +125,19 @@ if cgi.params.key?("content") html << %(
) 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 << %() - html << %(
Server Info
) + html << %(
Server Info
) if server["os_name"] html << <<~HTML @@ -178,7 +189,16 @@ if cgi.params.key?("content") HTML end - html << %(
) + html << %(
) + + if srv_stats.any? + srv_stats_html = srv_stats.map { |c, txt| + %(#{CGI.escapeHTML(txt)}) + }.join + html << %(
#{srv_stats_html}
) + end + + html << %(
) end if filesystems.any?