-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (64 loc) · 2.94 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Monitors a Raspberry Pi UART, combining the stream of received bytes into single-line messages, determining a log
* level based on regular expressions, saving the messages to rotating log files while prefixing each message with a
* timestamp, and optionally sending notifications to Slack and/or Telegram for some minimum log level, when the UART
* has been inactive or has not seen specific messages for too long, or for some basic reports.
*/
'use strict';
const raspi = require('raspi');
const Serial = require('raspi-serial').Serial;
const LogService = require('./log-service');
const NotificationService = require('./notification-service');
const WatchdogService = require('./watchdog-service');
const ReportingService = require('./reporting-service');
const config = require('./config');
const logService = new LogService(config.logfiles);
logService.warn('[monitor] Starting UART monitor');
const notificationService = new NotificationService(config.notifications);
notificationService.warn('Starting UART monitor');
// The watchdogs and reporters only send notifications to Telegram and/or Slack (if configured), not to the log files
const watchdogService = new WatchdogService(config.watchdogs, notificationService);
const reportingService = new ReportingService(config.reporters, notificationService);
/**
* Gets (guesses) a log level based on the message's text, using the regular expressions from the configuration.
*/
function getLevel(msg) {
return Object.keys(config.levels).find(level => {
const patterns = config.levels[level];
return patterns.include.some(p => p.test(msg)) && !patterns.exclude.some(p => p.test(msg))
}
) || 'debug';
}
let buffer = '';
/**
* Combines series of bytes into single-line messages, guesses a log level, and passes the messages to the file logger
* and notification services.
*/
function log(data) {
// To debug the raw stream, don't use `console.log` as that will output newlines, even when logging a partial line.
// Instead, use `stdout` to output only the newlines that the UART receives: process.stdout.write(data);
buffer += data.toString();
if (buffer.indexOf('\n') > -1) {
const lines = buffer.split('\n');
// Log whatever complete lines are in the buffer, so: up to the last newline
for (let i = 0; i < lines.length - 1; i++) {
const msg = lines[i];
const level = getLevel(msg);
logService.log(level, msg);
notificationService.log(level, msg);
watchdogService.message(msg);
reportingService.message(msg);
}
// The message following the last newline might not be complete yet; handle at a later time
buffer = lines[lines.length - 1];
}
}
raspi.init(() => {
const serial = new Serial(config.serial);
serial.open(() => {
serial.on('data', data => {
watchdogService.heartbeat();
log(data);
});
});
});