List static leases and dynamic leases separately.

Also increased the delays between updates to cause less server
load.
Updated bashserv.
This commit is contained in:
Koray Yanik 2022-03-23 23:53:21 +00:00
parent 36e23424b1
commit 601ebc27e0
4 changed files with 52 additions and 23 deletions

@ -1 +1 @@
Subproject commit ef70aa40f9be1b8006f8ec1d9cbf17f93f722f1d Subproject commit 0c8aad069620421475301ebc680a2fed8ae829a8

View File

@ -41,16 +41,45 @@ if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then
body+="<link rel='stylesheet' href='main.css'></link>\n</head>\n" body+="<link rel='stylesheet' href='main.css'></link>\n</head>\n"
fi fi
body+="<body>\n<table>\n<tr><td></td><td>Hostname</td><td>IP Address</td><td>MAC Address</td><td>Wakeup</td></tr>\n" body+="<body>\n"
body+="<h3>Static leases</h3>\n"
body+="<table>\n<tr><td></td><td>Hostname</td><td>IP Address</td><td>MAC Address</td><td>Wakeup</td></tr>\n"
hosts=$(grep -i "^dhcp-host=" /etc/dnsmasq.conf | cut -d '=' -f2 | tr '[:upper:]' '[:lower:]')
for host in $hosts; do
mac=$(echo "$host" | cut -d ',' -f1)
ip=$(echo "$host" | cut -d ',' -f2)
hostname="?"
lease_hostname=$(grep -i "$mac" /var/lib/misc/dnsmasq.leases | cut -d ' ' -f4)
if [ -n "$lease_hostname" ]; then
hostname="$lease_hostname"
fi
body+="<tr class='host' data-ip='$ip' data-mac='$mac'>\n"
body+="<td class='status'>"
body+="<div 'class='fail'>X</div>"
body+="<div class='success' style='display: none;'>&#10003;</div>"
body+="<div class='undef' style='display: none;'>?</div>"
body+="</td>\n"
body+="<td>$hostname</td>"
body+="<td>$ip</td>"
body+="<td>$mac</td>"
body+="<td><button class='wol' value="$mac">&#x23fb;</button><td>\n"
body+="</tr>"
done
body+="</table>\n"
body+="<h3>Dynamic leases</h3>\n"
body+="<table>\n<tr><td></td><td>Hostname</td><td>IP Address</td><td>MAC Address</td><td>Wakeup</td></tr>\n"
IFS=$'\n' IFS=$'\n'
leases=$(cat /var/lib/misc/dnsmasq.leases | cut -d ' ' -f2-4) leases=$(cat /var/lib/misc/dnsmasq.leases | cut -d ' ' -f2-4)
for lease in $leases; do for lease in $leases; do
mac=$(echo $lease | cut -d ' ' -f1) mac=$(echo "$lease" | cut -d ' ' -f1)
ip=$(echo $lease | cut -d ' ' -f2) ip=$(echo "$lease" | cut -d ' ' -f2)
hostname=$(echo $lease | cut -d ' ' -f3) hostname=$(echo "$lease" | cut -d ' ' -f3)
if [ -n "$(grep -i "$mac" /etc/dnsmasq.conf)" ]; then if [ -z "$(grep -i "$mac" /etc/dnsmasq.conf)" ]; then
body+="<tr class='host' data-ip='$ip' data-mac='$mac'>\n" body+="<tr class='host' data-ip='$ip' data-mac='$mac'>\n"
body+="<td class='status'>" body+="<td class='status'>"
@ -62,7 +91,7 @@ if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then
body+="<td>$hostname</td>" body+="<td>$hostname</td>"
body+="<td>$ip</td>" body+="<td>$ip</td>"
body+="<td>$mac</td>" body+="<td>$mac</td>"
body+="<td><button class='wol' value="$mac">Wake up!</button><td>\n" body+="<td><button class='wol' value="$mac">&#x23fb;</button><td>\n"
body+="</tr>" body+="</tr>"
fi fi
done done

View File

@ -4,12 +4,10 @@ if [[ "$REQUEST_PATH_SANE" =~ ^refresh ]]; then
ip=$(echo "$POST_DATA" | cut -d '=' -f2) ip=$(echo "$POST_DATA" | cut -d '=' -f2)
body="{\n\"time\": \"$(date +%T)\",\n" body="{\n\"time\": \"$(date +%T)\",\n"
body+="\"status\": \"unknown\"\n}\n" body+="\"status\": \"ok\"\n}\n"
cat <<EOF header=$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c)) printf "%b\n%b" "$header" "$body"
$(echo -ne $body)
EOF
echo -ne "$body" > status/$ip echo -ne "$body" > status/$ip

View File

@ -1,20 +1,19 @@
var request_status = function (host) { var request_status = function (host, wol) {
console.log("Update " + host.ip); console.log("Update " + host.ip);
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.addEventListener("load", function () { request.addEventListener("load", function () {
var response; var response;
console.log(this.status, this.responseText);
if (this.status !== 200) { if (this.status !== 200) {
return; return;
} }
response = JSON.parse(this.responseText); response = JSON.parse(this.responseText);
if (response.status === "up") { if (response.status === "up") {
host.status_node.innerHTML = "&#10003;"; host.status_node.innerHTML = "&#10003;";
} else if (response.status === "unknown") { } else if (response.status === "unknown" && wol) {
setTimeout(function () { setTimeout(function () {
request_status(host); request_status(host, wol);
}, 1000); }, 1000 + Math.random() * 1000);
} else { } else {
host.status_node.innerHTML = "X"; host.status_node.innerHTML = "X";
} }
@ -23,7 +22,7 @@ var request_status = function (host) {
request.open("GET", "status/" + host.ip); request.open("GET", "status/" + host.ip);
request.send(); request.send();
} }
var request_refresh = function (host) { var request_refresh = function (host, wol) {
console.log("Refresh " + host.ip); console.log("Refresh " + host.ip);
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
@ -34,6 +33,7 @@ var request_refresh = function (host) {
return; return;
} }
response = JSON.parse(this.responseText); response = JSON.parse(this.responseText);
console.log(response);
if (response.status === "ok") { if (response.status === "ok") {
host.status_node.innerHTML = "&#10003;"; host.status_node.innerHTML = "&#10003;";
} else { } else {
@ -47,8 +47,8 @@ var request_refresh = function (host) {
request.send(params); request.send(params);
setTimeout(function () { setTimeout(function () {
request_status(host); request_status(host, wol);
}, 1000); }, 4000 + Math.random() * 2000);
} }
var request_wol = function (host) { var request_wol = function (host) {
@ -77,8 +77,8 @@ var request_wol = function (host) {
request.send(params); request.send(params);
setTimeout(function () { setTimeout(function () {
request_refresh(host); request_refresh(host, true);
}, 1000); }, 5000);
} }
window.onload = function () { window.onload = function () {
@ -109,8 +109,10 @@ window.onload = function () {
host_bound.wol_button.onclick = function (e) { host_bound.wol_button.onclick = function (e) {
request_wol(host_bound); request_wol(host_bound);
} }
setTimeout(function () {
request_refresh(host_bound, false);
}, Math.random() * 2000);
}) (host); }) (host);
request_refresh(host);
} }
} }