mirror of https://github.com/Aelita4/sshmon.git
Add downtime counter
This commit is contained in:
parent
7e25c2162e
commit
aa84456054
31
src/index.js
31
src/index.js
|
@ -12,12 +12,15 @@ const ping = async host => {
|
||||||
|
|
||||||
const app = new express();
|
const app = new express();
|
||||||
|
|
||||||
app.get("/", async (req, res) => {
|
const pings = new Map();
|
||||||
if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]");
|
|
||||||
const addresses = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}))
|
|
||||||
|
|
||||||
const timeoutDelay = 60000;
|
if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]");
|
||||||
|
let addresses = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}))
|
||||||
|
addresses.forEach(a => pings.set(a, -1));
|
||||||
|
const timeoutDelay = 60000;
|
||||||
|
|
||||||
|
app.get("/", async (req, res) => {
|
||||||
|
addresses = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}))
|
||||||
let out = `<html><head><script>const addresses = ["${addresses.join('", "')}"];
|
let out = `<html><head><script>const addresses = ["${addresses.join('", "')}"];
|
||||||
function clock() {
|
function clock() {
|
||||||
|
|
||||||
|
@ -61,8 +64,8 @@ app.get("/", async (req, res) => {
|
||||||
b.innerHTML = '<span style="color:blue;">PINGING...</span>'
|
b.innerHTML = '<span style="color:blue;">PINGING...</span>'
|
||||||
await fetch('http://localhost:8080/ping/' + a)
|
await fetch('http://localhost:8080/ping/' + a)
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((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 + ")")) + '</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((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince) + '</span>'
|
||||||
});
|
});
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@ -96,11 +99,23 @@ app.get("/", async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/ping/:ip", async (req, res) => {
|
app.get("/ping/:ip", async (req, res) => {
|
||||||
const r = await ping(req.params.ip);
|
const ip = req.params.ip;
|
||||||
|
const r = await ping(ip);
|
||||||
|
if(!r.alive && pings.get(ip) === -1) pings.set(ip, Date.now());
|
||||||
|
else if(r.alive) pings.set(ip, -1);
|
||||||
res.setHeader('Content-Type', 'application/json');
|
res.setHeader('Content-Type', 'application/json');
|
||||||
res.end(JSON.stringify(r));
|
res.end(JSON.stringify(r));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get("/downtime/:ip", async (req, res) => {
|
||||||
|
const ip = req.params.ip;
|
||||||
|
|
||||||
|
const json = { downSince: pings.get(ip) };
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
res.end(JSON.stringify(json));
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/addIP/:ip", async (req, res) => {
|
app.get("/addIP/:ip", async (req, res) => {
|
||||||
if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]");
|
if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]");
|
||||||
const json = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}));
|
const json = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}));
|
||||||
|
|
Loading…
Reference in New Issue