Add downtime logging

This commit is contained in:
Aelita4 2022-08-24 10:39:08 +02:00
parent 473e7df43e
commit f30a04c32e
Signed by: Aelita4
GPG Key ID: F8EC95519509D1D5
2 changed files with 51 additions and 1 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');