forked from mvantassel/nzbget2influx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nzbget2influx.js
91 lines (73 loc) · 2.12 KB
/
nzbget2influx.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
const Influx = require('influx');
const nzbget = require('node-nzbget');
const checkInterval = process.env.UPDATE_INTERVAL_MS || 1000 * 30;
const influxClient = new Influx.InfluxDB({
host: process.env.INFLUX_HOST || 'localhost',
port: process.env.INFLUX_PORT || 8086,
protocol: process.env.INFLUX_PROTOCOL || 'http',
database: process.env.INFLUX_DB || 'nzbget'
});
const nzbgetConfig = {
host: process.env.NZBGET_HOST || 'localhost',
protocol: process.env.NZBGET_PROTOCOL ||'http',
port: process.env.NZBGET_PORT || 6789,
username: process.env.NZBGET_USERNAME || '',
password: process.env.NZBGET_PASSWORD || ''
};
function log(message) {
console.log(message);
}
function writeToInflux(seriesName, values, tags) {
let payload = {
fields: values
};
if (tags) {
payload.tags = tags;
}
return influxClient.writeMeasurement(seriesName, [payload]);
}
const ng = new nzbget({
url: `${nzbgetConfig.host}:${nzbgetConfig.port}`,
username: nzbgetConfig.username,
password: nzbgetConfig.password
});
function onGetNZBData(data) {
log(`${new Date()}: Parsing NZB Data`);
let nzbs = data.result;
nzbs.forEach(function(nzb) {
let value = {
name: nzb.NZBName,
size: nzb.FileSizeLo
};
let tags = {
status: nzb.Status,
category: nzb.Category
};
writeToInflux('nzb', value, tags).then(function() {
log(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`);
});
});
writeToInflux('nzbs', {
count: nzbs.length
}, null).then(function() {
log(`wrote ${nzbs.length} nzb data to influx: ${new Date()}`);
restart();
});
}
function handleError(err) {
log(`${new Date()}: Error`);
log(err);
}
function restart() {
// Every {checkInterval} seconds
setTimeout(getAllTheMetrics, checkInterval);
}
function getAllTheMetrics() {
ng.listgroups().then(onGetNZBData).catch(err => {
handleError(err);
restart();
});
}
log(`${new Date()}: Initialize NZB2Influx`);
getAllTheMetrics();