Add package update count and reboot pending to host server info card
This commit is contained in:
parent
a21bf2c215
commit
2c0da87fac
52
bin/malpd
52
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")
|
||||
|
||||
@ -125,8 +125,19 @@ if cgi.params.key?("content")
|
||||
html << %(<div>)
|
||||
|
||||
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="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"]
|
||||
html << <<~HTML
|
||||
@ -178,7 +189,16 @@ if cgi.params.key?("content")
|
||||
HTML
|
||||
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
|
||||
|
||||
if filesystems.any?
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user