-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
vnstat-dumpdb.js
143 lines (116 loc) · 2.95 KB
/
vnstat-dumpdb.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Name: vnstat-dumpdb
Description: Get vnStat data and config in Node.js
Author: Franklin van de Meent (https://frankl.in)
Source & docs: https://github.com/fvdm/nodejs-vnstat-dumpdb
Feedback: https://github.com/fvdm/nodejs-vnstat-dumpdb/issues
License: Unlicense (see LICENSE file)
*/
var exec = require ('child_process') .exec;
var set = {
bin: 'vnstat',
iface: null,
config: {}
};
/**
* Make and callback an error
*
* @callback callback
* @param message {string} - Error.message
* @param err {Error, null} - Error.error
* @param details {mixed} - Error.details
* @param callback {function} - `function (err) {}`
* @returns {void}
*/
function doError (message, err, details, callback) {
var error = new Error (message);
error.error = err;
error.details = details;
callback (error);
}
/**
* Get vnStat config
*
* @callback callback
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function getConfig (callback) {
exec (set.bin + ' --showconfig', function (err, text) {
var config = {};
var line;
var i;
if (err) {
doError ('no config', err, text, callback);
return;
}
text = text.split ('\n');
for (i = 0; i < text.length; i++) {
line = text [i] .trim ();
if (line.substr (0, 1) !== '#') {
line.replace (/(\w+)\s+(.+)/, function (s, key, val) {
config [key] = val.slice (0, 1) === '"' ? val.slice (1, -1) : val;
});
}
}
callback (null, config);
});
}
/**
* Get stats database
*
* @callback callback
* @param [iface] {string} - Limit data to one interface
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function getStats (iface, callback) {
var i;
if (typeof iface === 'function') {
callback = iface;
iface = set.iface;
}
exec (set.bin + ' --json', function (err, json, stderr) {
if (err) {
err.stderr = stderr;
doError ('command failed', err, json, callback);
return;
}
try {
json = JSON.parse (json);
} catch (e) {
doError ('invalid data', e, json, callback);
return;
}
if (iface) {
for (i = 0; i < json.interfaces.length; i++) {
if (json.interfaces [i] .id === iface) {
callback (null, json.interfaces [i]);
return;
}
}
doError ('invalid interface', { iface }, json, callback);
return;
}
callback (null, json.interfaces);
});
}
/**
* Configuration
*
* @param [setup] {object}
* @param [setup.bin] {string=vnstat} - Command of or path to vnstat binary
* @param [setup.iface] {string} - Select interface, defaults to all (null)
* @returns {object} - Module interface methods
*/
module.exports = function (setup) {
if (setup instanceof Object) {
set.bin = setup.bin || set.bin;
set.iface = setup.iface || set.iface;
}
return {
getStats: getStats,
getConfig: getConfig,
set: set
};
};