diff --git a/bin/malpd b/bin/malpd index ae9024a..53ab682 100755 --- a/bin/malpd +++ b/bin/malpd @@ -166,6 +166,52 @@ def read_virtual_machines end end +def read_server_info + info = { "type" => "server" } + + if File.exist?("/etc/os-release") + File.readlines("/etc/os-release").each do |line| + if line =~ /^PRETTY_NAME="(.+)"$/ + info["os_name"] = $1 + break + end + end + 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") + elsif File.exist?("/etc/machine-id") + info["install_date"] = File.mtime("/etc/machine-id").strftime("%Y-%m-%d") + end + + if File.exist?("/proc/cpuinfo") + cpuinfo = File.read("/proc/cpuinfo") + if cpuinfo =~ /^model name\s*:\s*(.+)$/ + info["cpu_model"] = $1.strip + end + cores = cpuinfo.scan(/^processor\s*:/).size + info["cpu_cores"] = cores if cores > 0 + end + + if File.exist?("/proc/meminfo") + mem = {} + File.read("/proc/meminfo").each_line do |line| + if line =~ /^(\w+):\s+(\d+)/ + mem[$1] = $2.to_i * 1024 + end + end + if (total = mem["MemTotal"]) + available = mem["MemAvailable"] || + (mem["MemFree"].to_i + mem["Buffers"].to_i + mem["Cached"].to_i) + info["mem_total"] = total + info["mem_used"] = total - available + end + end + + info +end + def read_ups out = `apcaccess 2>/dev/null` return nil unless $?.success? && !out.empty? @@ -232,6 +278,8 @@ def handle_info(conn) info.concat(read_filesystems) info.concat(read_virtual_machines) + info << read_server_info + if (ups = read_ups) info << ups end diff --git a/cgi-bin/malp.rb b/cgi-bin/malp.rb index bb21251..52728b0 100755 --- a/cgi-bin/malp.rb +++ b/cgi-bin/malp.rb @@ -112,9 +112,67 @@ if cgi.params.key?("content") drives = info.select { |e| e["type"] == "drive" } filesystems = info.select { |e| e["type"] == "filesystem" } vms = info.select { |e| e["type"] == "vm" } + server = info.find { |e| e["type"] == "server" } html << %(
) + if server + html << %(
Server
) + html << %(
Server Info
) + + if server["os_name"] + html << <<~HTML +
+
OS
+ #{CGI.escapeHTML(server["os_name"])} +
+ HTML + end + + if server["install_date"] + html << <<~HTML +
+
Installed
+ #{CGI.escapeHTML(server["install_date"])} +
+ HTML + end + + cpu_model = server["cpu_model"] + cpu_cores = server["cpu_cores"] + if cpu_model || cpu_cores + cpu_text = [ + cpu_model, + cpu_cores ? "#{cpu_cores} thread#{cpu_cores == 1 ? "" : "s"}" : nil + ].compact.join(" · ") + html << <<~HTML +
+
CPU
+ #{CGI.escapeHTML(cpu_text)} +
+ HTML + end + + mem_total = server["mem_total"].to_i + mem_used = server["mem_used"].to_i + if mem_total > 0 + mem_pct_f = mem_used * 100.0 / mem_total + mem_pct = mem_pct_f.round + mem_cls = mem_pct_f >= 90 ? "bad" : mem_pct_f >= 75 ? "warn" : "ok" + html << <<~HTML +
+
+
Memory
+ #{mem_pct}% · #{CGI.escapeHTML(human_capacity(mem_used))} / #{CGI.escapeHTML(human_capacity(mem_total))} +
+
+
+ HTML + end + + html << %(
) + end + if vms.any? running = vms.count { |v| v["state"].to_s == "running" } total = vms.size