raumstatus/node/ip-poll.js

55 lines
1.4 KiB
JavaScript

var redis = require("redis");
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;
var redisprefix = "ippoll:";
var IpPoll = function(switchaddr, hostsaddr) {
var self = this;
var redisClient = redis.createClient();
var regexp = /\(([0-9]+) hosts* up\)/;
// var nmap = "nmap -n -sP -T5 --host-timeout 10ms ";
var nmap = "nmap -n -sP -T5 ";
redisClient.on("connect", function () {
console.log("connected to redis");
self.emit('ready');
});
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 num = parseInt(matches[1]);
redisClient.zadd('onlinecount', Date.now(), num, function() {
self.emit('doneCount', num);
});
}
}
});
};
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");
}
});
};
};
util.inherits(IpPoll, EventEmitter);
module.exports = IpPoll;