Compare commits

..

2 Commits

Author SHA1 Message Date
Aelita4 41995c90c2
Change time output formatting 2022-08-24 10:44:13 +02:00
Aelita4 f30a04c32e
Add downtime logging 2022-08-24 10:39:08 +02:00
3 changed files with 54 additions and 4 deletions

View File

@ -12,7 +12,7 @@
3. Create database and tables 3. Create database and tables
- Script does not create database and tables by default - Script does not create database and tables by default
- Create database `sshmon` with 2 tables: - Create database `sshmon` with 3 tables:
<table> <table>
<thead> <thead>
<tr> <tr>
@ -66,6 +66,40 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<table>
<thead>
<tr>
<th colspan=3>downtime</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>Type</td>
<td>Extra</td>
</tr>
<tr>
<td>id</td>
<td>int</td>
<td>AUTO_INCREMENT PRIMARY KEY</td>
</tr>
<tr>
<td>ip</td>
<td>varchar(255)</td>
<td></td>
</tr>
<tr>
<td>time</td>
<td>datetime</td>
<td></td>
</tr>
<tr>
<td>status</td>
<td>varchar(255)</td>
<td></td>
</tr>
</tbody>
</table>
4. Register new account in app 4. Register new account in app
5. Login 5. Login

View File

@ -1,4 +1,5 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { connection } from '../index.js';
export default { export default {
method: "GET", method: "GET",
@ -6,6 +7,21 @@ export default {
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {
const ip = req.params.ip; const ip = req.params.ip;
const r = await data.ping(ip); const r = await data.ping(ip);
if((Number(data.pings.get(ip).split("_")[0]) + (1000 * 60 * 5)) < Date.now()) {
if(!r.alive) connection.query("SELECT * FROM downtime WHERE ip=? AND date=? AND status=?", [ip, new Date(Number(data.pings.get(ip).split("_")[0])).toISOString().slice(0, 19).replace('T', ' '), "DOWN"], (err, results, fields) => {
if(err) throw err;
if(results.length === 0) connection.query("INSERT INTO downtime VALUES (NULL, ?, ?, ?)", [ip, new Date(Number(data.pings.get(ip).split("_")[0])).toISOString().slice(0, 19).replace('T', ' '), "DOWN"], (errr, resultss, fieldss) => {
if(errr) throw errr;
});
});
else connection.query("SELECT * FROM downtime WHERE ip=? AND date=? AND status=?", [ip, new Date(Number(data.pings.get(ip).split("_")[0])).toISOString().slice(0, 19).replace('T', ' '), "UP"], (err, results, fields) => {
if(err) throw err;
if(results.length === 0) connection.query("INSERT INTO downtime VALUES (NULL, ?, ?, ?)", [ip, new Date(Number(data.pings.get(ip).split("_")[0])).toISOString().slice(0, 19).replace('T', ' '), "UP"], (errr, resultss, fieldss) => {
if(errr) throw errr;
});
});
}
if(!r.alive && data.pings.get(ip).split("_")[1] === "1") data.pings.set(ip, `${Date.now()}_0`); if(!r.alive && data.pings.get(ip).split("_")[1] === "1") data.pings.set(ip, `${Date.now()}_0`);
else if(r.alive) data.pings.set(ip, `${Date.now()}_1`); else if(r.alive) data.pings.set(ip, `${Date.now()}_1`);
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');

View File

@ -25,8 +25,8 @@
async function aaa() { async function aaa() {
const date = new Date(); const date = new Date();
document.getElementById("main").innerHTML = "Last checked: " + date; document.getElementById("main").innerHTML = "Last checked: " + date.toLocaleString();
document.getElementById("eta").innerHTML = "Next scheduled check: " + new Date(date.getTime() + <%= timeoutDelay %>); document.getElementById("eta").innerHTML = "Next scheduled check: " + new Date(date.getTime() + <%= timeoutDelay %>).toLocaleString();
addresses.forEach(async a => { addresses.forEach(async a => {
const b = document.getElementById(a + "_status"); const b = document.getElementById(a + "_status");
if(!b) return; if(!b) return;
@ -34,7 +34,7 @@
await fetch('http://localhost:8080/ping/' + a) await fetch('http://localhost:8080/ping/' + a)
.then((response) => response.json()) .then((response) => response.json())
.then(async (data) => { .then(async (data) => {
b.innerHTML = data.alive ? '<span style="color:green;">UP (' + data.numeric_host + ") " + data.time + 'ms</span>' : '<span style="color:red;">DOWN' + ((data.host == "unknown") ? " (UNKNOWN HOST)" : (" (" + data.numeric_host + ")")) + " since " + new Date(Number((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince)) + '</span>' b.innerHTML = data.alive ? '<span style="color:green;">UP (' + data.numeric_host + ") " + data.time + 'ms</span>' : '<span style="color:red;">DOWN' + ((data.host == "unknown") ? " (UNKNOWN HOST)" : (" (" + data.numeric_host + ")")) + " since " + new Date(Number((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince)).toLocaleString() + '</span>'
}); });
}) })
setTimeout(aaa, <%= timeoutDelay %>) setTimeout(aaa, <%= timeoutDelay %>)