From d4f98b1f60109577a08fcc473dd26f6c5b6026e2 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 11 Feb 2023 13:39:24 +0100 Subject: [PATCH] Use new /api/info/metrics endpoint to generate (settings level dependent) metrics on the first tab of the settings page Signed-off-by: DL6ER --- scripts/pi-hole/js/settings-system.js | 60 ++++----- scripts/pi-hole/js/settings.js | 4 +- settings.php | 179 ++++++++++++++++++-------- 3 files changed, 151 insertions(+), 92 deletions(-) diff --git a/scripts/pi-hole/js/settings-system.js b/scripts/pi-hole/js/settings-system.js index ebc4f45e..f26516a3 100644 --- a/scripts/pi-hole/js/settings-system.js +++ b/scripts/pi-hole/js/settings-system.js @@ -43,49 +43,35 @@ function updateHostInfo() { }); } -var cacheinfoTimer = null; +// Walk nested objects, create a dash-separated global key and assign the value +// to the corresponding element (add percentage for DNS replies) +function setMetrics(data, prefix) { + for (const [key, val] of Object.entries(data)) { + if (typeof val === "object") { + setMetrics(val, prefix + key + "-"); + } else if (prefix === "sysinfo-dns-replies-") { + // Compute and display percentage of DNS replies in addition to the absolute value + $("#" + prefix + key).text(val + " (" + ((100 * val) / data.sum).toFixed(1) + "%)"); + } else { + $("#" + prefix + key).text(val); + } + } +} + +var metricsTimer = null; // eslint-disable-next-line no-unused-vars -function updateCacheInfo() { +function updateMetrics() { $.ajax({ - url: "/api/info/cache", + url: "/api/info/metrics", }) .done(function (data) { - var cache = data.cache; - $("#sysinfo-cache-size").text(cache.size); - $("#sysinfo-cache-inserted").text(cache.inserted); - $("#sysinfo-cache-evicted").text(cache.evicted); - $("#sysinfo-cache-expired").text(cache.expired); - $("#sysinfo-cache-immortal").text(cache.immortal); - $("#sysinfo-cache-valid-a").text(cache.valid.a); - $("#sysinfo-cache-valid-aaaa").text(cache.valid.aaaa); - $("#sysinfo-cache-valid-cname").text(cache.valid.cname); - $("#sysinfo-cache-valid-srv").text(cache.valid.srv); - $("#sysinfo-cache-valid-ds").text(cache.valid.ds); - $("#sysinfo-cache-valid-dnskey").text(cache.valid.dnskey); - $("#sysinfo-cache-valid-other").text(cache.valid.other); + var metrics = data.metrics; + setMetrics(metrics, "sysinfo-"); - var total = - cache.optimized + cache.local + cache.auth + cache.extra.unanswered + cache.extra.forwarded; - $("#sysinfo-replies-optimized").text( - cache.optimized + " (" + ((100 * cache.optimized) / total).toFixed(1) + "%)" - ); - $("#sysinfo-replies-local").text( - cache.local + " (" + ((100 * cache.local) / total).toFixed(1) + "%)" - ); - $("#sysinfo-replies-auth").text( - cache.auth + " (" + ((100 * cache.auth) / total).toFixed(1) + "%)" - ); - $("#sysinfo-replies-extra-unanswered").text( - cache.extra.unanswered + " (" + ((100 * cache.extra.unanswered) / total).toFixed(1) + "%)" - ); - $("#sysinfo-replies-extra-forwarded").text( - cache.extra.forwarded + " (" + ((100 * cache.extra.forwarded) / total).toFixed(1) + "%)" - ); - - $("#sysinfo-dns-overlay").hide(); + $("#sysinfo-metrics-overlay").hide(); // Update every 10 seconds - clearTimeout(cacheinfoTimer); - cacheinfoTimer = setTimeout(updateCacheInfo, 10000); + clearTimeout(metricsTimer); + metricsTimer = setTimeout(updateMetrics, 10000); }) .fail(function (data) { apiFailure(data); diff --git a/scripts/pi-hole/js/settings.js b/scripts/pi-hole/js/settings.js index c64d4f0f..5be7fda0 100644 --- a/scripts/pi-hole/js/settings.js +++ b/scripts/pi-hole/js/settings.js @@ -5,11 +5,11 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global updateHostInfo:false, updateCacheInfo:false, createDynamicConfigTabs:false */ +/* global updateHostInfo:false, updateMetrics:false, createDynamicConfigTabs:false */ $(function () { updateHostInfo(); - updateCacheInfo(); + updateMetrics(); createDynamicConfigTabs(); }); diff --git a/settings.php b/settings.php index dec66e34..55269771 100644 --- a/settings.php +++ b/settings.php @@ -129,7 +129,7 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array('sysadmin', 'dns', 'dhcp
-

DNS Information

+

Server metrics

@@ -147,73 +147,73 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array('sysadmin', 'dns', 'dhcp DNS cache size: -   +   DNS cache insertions: -   +   DNS cache evictions: -   +   DNS cache expiries: -   +   Immortal DNS cache entries: -   +   - + Valid A records in cache: -   +   - + Valid AAAA records in cache: -   +   - + Valid CNAME records in cache: -   +   - + Valid SRV records in cache: -   +   - + Valid DS records in cache: -   +   - + Valid DNSKEY records in cache: -   +   - + Other valid records in cache: -   +   @@ -237,31 +237,130 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array('sysadmin', 'dns', 'dhcp Local replies: -   +   Forwarded queries: -   +   Unanswered queries: -   +   Cache optimizer replies: -   +   - + Authoritative replies: -   +   + + + +
+
+
+
+ +
+
+
+

DHCP server

+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ DHCPDISCOVER: +  
+ DHCPOFFER: +  
+ DHCPREQUEST: +  
+ DHCPACK: +  
+ DHCPNAK: +  
+ DHCPDECLINE: +  
+ DHCPINFORM: +  
+ DHCPRELEASE: +  
+ DHCPNOANSWER: +  
+ BOOTP: +  
+ PXE: +  
+ Allocated / pruned IPv4 leases: +   / +   +
+ Allocated / pruned IPv6 leases: +   / +   +
@@ -272,7 +371,7 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array('sysadmin', 'dns', 'dhcp
-
+
@@ -311,32 +410,6 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array('sysadmin', 'dns', 'dhcp
- -
- - -
-
- - -
-
- - - -
-
- - -
-
- - -
-
- - -