forked from tombatossals/gps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_users.js
executable file
·52 lines (44 loc) · 1.75 KB
/
monitor_users.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
'use strict';
var util = require('util'),
getConnectedUsers = require('./mikrotik').getConnectedUsers,
getNodesByName = require('./common').getNodesByName,
INTERVAL = require('./common').INTERVAL,
updateNode = require('./common').updateNode,
Q = require('q');
function monitorOmnitikUsers(node) {
var deferred = Q.defer();
getConnectedUsers(node).then(function(users) {
if (users && users.hasOwnProperty('good')) {
if (users && users.good && users.bad && !isNaN(users.good + users.bad)) {
node.connectedUsers = users.good + users.bad;
}
node.save(function() {
console.log(util.format('PUTVAL "%s/node/connected_users" interval=%s N:%s:%s', node.name, INTERVAL, users.good, users.bad));
deferred.resolve(util.format('PUTVAL "%s/node/connected_users" interval=%s N:%s:%s', node.name, INTERVAL, users.good, users.bad));
});
} else {
deferred.resolve(util.format('Can\'t read wireless connected users on %s', node.name));
}
}).fail(function(error) {
deferred.reject(util.format('Can\'t read wireless connected users on %s', node.name));
});
return deferred.promise;
}
function execute(nodes) {
var deferred = Q.defer();
getNodesByName(nodes).then(function(nodes) {
var promises = [];
nodes.forEach(function(node) {
if (node.omnitik === true) {
promises.push(monitorOmnitikUsers(node));
}
});
Q.allSettled(promises).then(function(results) {
deferred.resolve(results);
});
}).fail(function(error) {
deferred.reject().done();
});
return deferred.promise;
}
module.exports.execute = execute;