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"
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'
leases=$(cat /var/lib/misc/dnsmasq.leases | cut -d ' ' -f2-4)
for lease in $leases; do
mac=$(echo $lease | cut -d ' ' -f1)
ip=$(echo $lease | cut -d ' ' -f2)
hostname=$(echo $lease | cut -d ' ' -f3)
mac=$(echo "$lease" | cut -d ' ' -f1)
ip=$(echo "$lease" | cut -d ' ' -f2)
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+="<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>$ip</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>"
fi
done

View File

@ -4,12 +4,10 @@ if [[ "$REQUEST_PATH_SANE" =~ ^refresh ]]; then
ip=$(echo "$POST_DATA" | cut -d '=' -f2)
body="{\n\"time\": \"$(date +%T)\",\n"
body+="\"status\": \"unknown\"\n}\n"
body+="\"status\": \"ok\"\n}\n"
cat <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$(echo -ne $body)
EOF
header=$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
printf "%b\n%b" "$header" "$body"
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);
var request = new XMLHttpRequest();
request.addEventListener("load", function () {
var response;
console.log(this.status, this.responseText);
if (this.status !== 200) {
return;
}
response = JSON.parse(this.responseText);
if (response.status === "up") {
host.status_node.innerHTML = "&#10003;";
} else if (response.status === "unknown") {
} else if (response.status === "unknown" && wol) {
setTimeout(function () {
request_status(host);
}, 1000);
request_status(host, wol);
}, 1000 + Math.random() * 1000);
} else {
host.status_node.innerHTML = "X";
}
@ -23,7 +22,7 @@ var request_status = function (host) {
request.open("GET", "status/" + host.ip);
request.send();
}
var request_refresh = function (host) {
var request_refresh = function (host, wol) {
console.log("Refresh " + host.ip);
var request = new XMLHttpRequest();
@ -34,6 +33,7 @@ var request_refresh = function (host) {
return;
}
response = JSON.parse(this.responseText);
console.log(response);
if (response.status === "ok") {
host.status_node.innerHTML = "&#10003;";
} else {
@ -47,8 +47,8 @@ var request_refresh = function (host) {
request.send(params);
setTimeout(function () {
request_status(host);
}, 1000);
request_status(host, wol);
}, 4000 + Math.random() * 2000);
}
var request_wol = function (host) {
@ -77,8 +77,8 @@ var request_wol = function (host) {
request.send(params);
setTimeout(function () {
request_refresh(host);
}, 1000);
request_refresh(host, true);
}, 5000);
}
window.onload = function () {
@ -109,8 +109,10 @@ window.onload = function () {
host_bound.wol_button.onclick = function (e) {
request_wol(host_bound);
}
setTimeout(function () {
request_refresh(host_bound, false);
}, Math.random() * 2000);
}) (host);
request_refresh(host);
}
}