2013-10-20 22:04:58 +00:00
|
|
|
var redis = require("redis");
|
2013-10-02 12:04:01 +00:00
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2013-10-20 22:04:58 +00:00
|
|
|
var exec = require('child_process').exec;
|
|
|
|
|
|
|
|
var redisprefix = "ippoll:";
|
2013-10-02 12:04:01 +00:00
|
|
|
|
|
|
|
var IpPoll = function(switchaddr, hostsaddr) {
|
2013-11-26 20:11:05 +00:00
|
|
|
var self = this;
|
2013-10-02 12:04:01 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
var redisClient = redis.createClient();
|
|
|
|
var regexp = /\(([0-9]+) hosts* up\)/;
|
2014-07-23 01:40:48 +00:00
|
|
|
var nmap = "nmap -n -sP ";
|
2013-10-20 22:04:58 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
redisClient.on("connect", function () {
|
2015-09-16 13:03:02 +00:00
|
|
|
console.log("IP-Poll: connected to redis");
|
2013-11-26 20:11:05 +00:00
|
|
|
self.emit('ready');
|
|
|
|
});
|
2013-11-26 19:41:40 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
this.pollCount = function() {
|
|
|
|
exec(nmap + "--min-hostgroup 10 " + hostsaddr, function (error, stdout, stderr) {
|
|
|
|
if(error == null) {
|
|
|
|
var matches = regexp.exec(stdout);
|
|
|
|
if(matches != null && matches.length == 2) {
|
|
|
|
var time = Date.now();
|
2013-10-22 12:01:50 +00:00
|
|
|
|
2014-07-23 00:16:15 +00:00
|
|
|
redisClient.zremrangebyscore(redisprefix + 'onlinecount', "-inf", time - 7*24*60*1000);
|
2013-10-20 22:04:58 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
var num = matches[1];
|
2013-10-22 12:01:50 +00:00
|
|
|
|
2014-07-23 00:16:15 +00:00
|
|
|
redisClient.zadd(redisprefix + 'onlinecount', time, time + "|" + num, function() {
|
|
|
|
self.emit('doneCount', parseInt(num));
|
2013-11-26 20:11:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-11-26 19:41:40 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
this.pollState = function() {
|
|
|
|
exec(nmap + switchaddr, function (error, stdout, stderr) {
|
|
|
|
if(error == null) {
|
|
|
|
var matches = regexp.exec(stdout);
|
|
|
|
if(matches != null && matches.length == 2) {
|
|
|
|
self.emit('doneState', matches[1] == "1");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.emit('doneState', "unknown");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-10-21 23:40:57 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
this.getHistory = function(start, end, callback) {
|
2013-10-21 23:40:57 +00:00
|
|
|
|
2014-07-23 00:16:15 +00:00
|
|
|
redisClient.zrangebyscore(redisprefix + 'onlinecount', "-inf", "+inf", function(err, replies) {
|
2013-11-26 20:11:05 +00:00
|
|
|
var data = [];
|
2013-10-22 12:01:50 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
replies.forEach(function (reply, i) {
|
|
|
|
var line = reply.split('|');
|
|
|
|
data.push( { at: moment(parseInt(line[0])).format(), value: line[1] });
|
|
|
|
});
|
2013-10-21 23:40:57 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
};
|
2013-10-20 22:04:58 +00:00
|
|
|
};
|
2013-10-02 12:04:01 +00:00
|
|
|
|
|
|
|
util.inherits(IpPoll, EventEmitter);
|
|
|
|
|
|
|
|
module.exports = IpPoll;
|