From 72764a67f7878d238c6cebd55468f13ea33227d3 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Thu, 15 Dec 2022 10:04:58 +0000 Subject: [PATCH] Show active (paging) alerts --- src/dashboard.js | 56 +++++++++++++++++++++++++++++++++++++++++++++--- src/index.html | 2 +- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/dashboard.js b/src/dashboard.js index 515eb7a..5d99f87 100644 --- a/src/dashboard.js +++ b/src/dashboard.js @@ -8,6 +8,20 @@ const prom = new PrometheusDriver({ const accountsQuery = "sum(max(accounts_count) by (shard,type,status)) by (type)"; +const alertsURL = "https://alerts.autistici.org/alerts.json"; + +const alertsQuery = { + filters: ["@state=active", "severity=page"], + gridLabel: "", + gridSortReverse: false, + gridLimits: {}, + sortOrder: "", + sortLabel: "", + sortReverse: false, + defaultGroupLimit: 5, + groupLimits: {} +}; + const accountNiceNames = { mail: "email accounts", web: "websites", @@ -33,16 +47,52 @@ function queryAccounts(el) { .catch((err) => { dashboardError('Failed to fetch account metrics', err); }); -}; +} + +function queryAlerts(el) { + fetch(alertsURL, { + method: 'POST', + body: JSON.stringify(alertsQuery), + credentials: 'include' + }) + .then((resp) => { + if (!resp.ok) { + throw new Error('HTTP request failed'); + } + return resp.json(); + }) + .then((result) => { + if (result.status != "success") { + throw new Error('JSON API request failed'); + } + var numFiring = result.totalAlerts; + + var txt = 'No active alerts.'; + if (numFiring > 0) { + txt = '' + numFiring + ' active alerts!'; + } + var p = document.createElement('p'); + p.appendChild(document.createTextNode(txt)); + el.append(p); + }) + .catch((err) => { + dashboardError('Failed to fetch active alerts', err); + }); +} function dashboardError(msg, err) { var p = document.createElement('p'); - p.appendChild(document.createTextNode(msg + ': ' + err.error)); + var errmsg = err.error; + if (!errmsg) { + errmsg = err.message; + } + p.appendChild(document.createTextNode(msg + ': ' + errmsg)); document.getElementById('errors').append(p); -}; +} window.addEventListener('load', () => { console.log('Populating data items...'); queryAccounts(document.getElementById('accountInfo')); + queryAlerts(document.getElementById('alertInfo')); }); diff --git a/src/index.html b/src/index.html index 1b349f2..4502491 100644 --- a/src/index.html +++ b/src/index.html @@ -72,7 +72,7 @@ body { <h3>System information</h3> - <div id="sysInfo"></div> + <div id="alertInfo"></div> <ul> <li><a href="https://admin.autistici.org/">service-dashboard</a> - map of services and hosts</li> -- GitLab