Compare commits

...

4 Commits

5 changed files with 68 additions and 8 deletions

View File

@ -164,9 +164,13 @@
fetch("?content", { credentials: "same-origin" })
.then(function(response) {
if (response.ok) return response.text();
throw new Error();
})
.then(function(html) {
if (html) document.getElementById("content").innerHTML = html;
document.getElementById("content").innerHTML = html;
})
.catch(function() {
document.getElementById("content").innerHTML = "Error";
});
</script>
<% end %>

View File

@ -62,12 +62,16 @@ if Process.uid != 0
exit(1)
end
FileUtils.mkdir_p(File.dirname(SOCKET_PATH))
FileUtils.rm_f(SOCKET_PATH)
if ENV["LISTEN_FDS"]
server = UNIXServer.for_fd(3)
else
FileUtils.mkdir_p(File.dirname(SOCKET_PATH))
FileUtils.rm_f(SOCKET_PATH)
server = UNIXServer.new(SOCKET_PATH)
File.chmod(0660, SOCKET_PATH)
FileUtils.chown(nil, "apache", SOCKET_PATH)
server = UNIXServer.new(SOCKET_PATH)
File.chmod(0660, SOCKET_PATH)
FileUtils.chown(nil, "apache", SOCKET_PATH)
end
Signal.trap("TERM") { server.close }
Signal.trap("INT") { server.close }

View File

@ -3,7 +3,9 @@
require "cgi"
require "erb"
require "digest"
require "json"
require "securerandom"
require "socket"
ASSETS_DIR = File.join(__dir__, "../assets")
DATA_DIR = File.join(__dir__, "../data")
@ -81,9 +83,39 @@ end
if cgi.params.key?("content")
if authenticated
sleep 5
html = ""
begin
sock = UNIXSocket.new("/run/malpd/malpd.sock")
sock.puts "info"
info = JSON.parse(sock.gets)
sock.close
rescue
cgi.out("type" => "text/html", "charset" => "UTF-8") do
"<p>Content placeholder</p>"
"Could not connect to malpd socket"
end
exit
end
info.each do |entry|
case entry["type"]
when "drive"
dt = entry["drivetype"]
model = CGI.escapeHTML(entry["model"])
capacity = CGI.escapeHTML(entry["capacity"])
html << <<~HTML
<div class="card ok">
<div class="card-header">
<span class="card-title">#{model} <span class="drive-type #{dt}">#{dt.upcase}</span></span>
</div>
<div class="card-sub">#{capacity}</div>
</div>
HTML
end
end
cgi.out("type" => "text/html", "charset" => "UTF-8") do
html
end
else
cgi.out("Status" => "403 Forbidden", "type" => "text/plain", "charset" => "UTF-8") do

10
systemd/malpd.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=MALP monitoring daemon
Requires=malpd.socket
[Service]
Type=simple
ExecStart=/var/www/malp/bin/malpd
[Install]
WantedBy=multi-user.target

10
systemd/malpd.socket Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=malpd Unix socket
[Socket]
ListenStream=/run/malpd/malpd.sock
SocketGroup=apache
SocketMode=0660
[Install]
WantedBy=sockets.target