-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (50 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const MONITOR_IP = '8.8.8.8'; // IP to ping
const DELAY = 5000; // milliseconds
const ping = require('ping');
const winston = require("winston");
const DailyRotateFile = require("winston-daily-rotate-file");
let wasDown = false;
const logFormat = winston.format.printf(
({ timestamp, status }) => {
return `${timestamp}: ${status}`;
}
);
let logger = winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss",
}),
logFormat
),
transports: [
new DailyRotateFile({
maxFiles: 10,
dirname: "logs/",
filename: "%DATE%.log",
datePattern: "YYYY-MM-DD",
}),
new winston.transports.Console(),
],
});
const log = status => {
logger.log({level: 'info', status});
}
const pingMe = ip => () => {
ping.sys.probe(ip, isAlive => {
if (isAlive) {
if (wasDown) {
log("Connection restored");
}
wasDown = false;
} else {
if (!wasDown) {
log("Connection lost");
}
wasDown = true;
}
})
}
log(`Pinging ${MONITOR_IP}!`);
setInterval(pingMe(MONITOR_IP), DELAY);