-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·34 lines (27 loc) · 999 Bytes
/
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
#!/usr/bin/env node
const fs = require('fs');
const http = require('http');
const readline = require('readline');
const Metric = require('./metric');
const DEFAULT_INPUT_FILE_PATH = '/dev/ttyAMA0';
const metrics = {};
const [,, inputFilePath = DEFAULT_INPUT_FILE_PATH, port = 9850] = process.argv;
readline.createInterface(fs.createReadStream(inputFilePath))
.on('line', (input) => {
const metric = Metric.fromEURIDISLine(input);
if (metric && metric.monitored) {
metrics[metric.name] = metric;
}
});
console.log(`Reading from ${inputFilePath}...`);
const server = http.createServer((req, res) => {
if (req.url !== '/metrics' || req.method !== 'GET') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
return;
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(Object.values(metrics).map(metric => metric.toPrometheusMetric()).join('\n'));
});
server.listen(port);
console.log(`Listening on port ${port}...`);