commit a65f0906347644b87ae386a99838e98640502775 Author: Lucas Pleß Date: Wed Oct 2 14:04:01 2013 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/node/bla.js b/node/bla.js new file mode 100644 index 0000000..6d1a3f7 --- /dev/null +++ b/node/bla.js @@ -0,0 +1,25 @@ + + + + + +client.on("error", function (err) { + console.log("Redis Error " + err); +}); + +client.on("connect", function () { + client.set("24:77:03:a9:f3:f4","lucas"); + client.set("3c:97:0e:22:b7:68","lucas"); + client.set("a0:0b:ba:c7:98:e5","lucas"); + client.set("b8:27:eb:7d:6f:d2","rpi2"); + client.set("b8:27:eb:7b:9b:9a","rpi3"); + client.set("a8:88:08:10:a0:c5","pascal"); + client.set("c8:97:9f:72:8f:67","fisch"); + + +}); + + + + + diff --git a/node/index.js b/node/index.js new file mode 100644 index 0000000..7bdb83b --- /dev/null +++ b/node/index.js @@ -0,0 +1,4 @@ + +var statusbot = require("./statusbot"); + +statusbot.start(); diff --git a/node/ip-poll.js b/node/ip-poll.js new file mode 100644 index 0000000..5fee0bf --- /dev/null +++ b/node/ip-poll.js @@ -0,0 +1,38 @@ +var util = require('util'); +var EventEmitter = require('events').EventEmitter; +var exec = require('child_process').exec + +var IpPoll = function(switchaddr, hostsaddr) { + var self = this; + + var regexp = /\(([0-9]+) hosts* up\)/; + var nmap = "nmap -n -sP -T5 --host-timeout 10ms "; + + 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) { + self.emit('doneCount', parseInt(matches[1])); + } + } + }); + } + + 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; diff --git a/node/package.json b/node/package.json new file mode 100644 index 0000000..afea5d5 --- /dev/null +++ b/node/package.json @@ -0,0 +1,13 @@ +{ + "name": "statusbot", + "description": "ctdo status bot", + "version": "0.0.1", + "private": true, + "dependencies": { + "express": "3.x", + "net-snmp": "x", + "redis": "x", + "underscore": "x" + }, + "main": "statusbot" +} diff --git a/node/snmp-mac.js b/node/snmp-mac.js new file mode 100644 index 0000000..fb94013 --- /dev/null +++ b/node/snmp-mac.js @@ -0,0 +1,88 @@ +var redis = require("redis"); +var snmp = require("net-snmp"); +var util = require('util'); +var EventEmitter = require('events').EventEmitter; +var _u = require("underscore"); + + +var SnmpMac = function(hostname, community) { + var self = this; + + var redisClient = redis.createClient(); + var session = snmp.createSession(hostname, community, { version: snmp.Version2c } ); + + var baseoid = "1.3.6.1.2.1.17.7.1.2.2.1.3"; + var regexp = /([0-9]+).([0-9]+).([0-9]+).([0-9]+).([0-9]+).([0-9]+)$/; + var names = []; + + redisClient.on("connect", function () { + redisClient.set("24:77:03:a9:f3:f4","lucas"); + redisClient.set("3c:97:0e:22:b7:68","lucas"); + redisClient.set("a0:0b:ba:c7:98:e5","lucas"); + redisClient.set("b8:27:eb:7d:6f:d2","rpi2"); + redisClient.set("b8:27:eb:7b:9b:9a","rpi3"); + redisClient.set("a8:88:08:10:a0:c5","pascal"); + redisClient.set("c8:97:9f:72:8f:67","fisch"); + redisClient.set("00:80:a3:91:39:1c","ripe-atlas-probe"); + redisClient.set("d4:ca:6d:33:cf:79","routerboard"); + + console.log("connected to redis"); + self.emit('ready'); + }); + + function getMacFromOID(oid, callback) { + var matches = regexp.exec(oid); + var mac = ""; + + if(matches != null) { + for(var i = 1; i < matches.length; i++) { + var num = parseInt(matches[i]); + if(num <= 15) mac += "0"; + mac += num.toString(16) + ":"; + } + mac = mac.substr(0, mac.length-1); + + callback(mac); + } + } + + function doneCb(error) { + if (error) + console.error(error.toString ()); + + self.emit('done', _u.uniq(names)); + } + + function feedCb(varbinds) { + for (var i = 0; i < varbinds.length; i++) { + if (snmp.isVarbindError(varbinds[i])) { + console.error(snmp.varbindError (varbinds[i])); + } else { + if(varbinds[i].value == "3") { // only valid arp entries + getMacFromOID(varbinds[i].oid, function(mac) { + redisClient.get(mac, function(err, reply) { + if(reply != null) { + names.push(reply); + } + }); + }); + } + } + } + } + + this.poll = function() { + names.length = 0; + session.subtree(baseoid, 20, feedCb, doneCb); + } + + this.stop = function() { + redisClient.quit(); + + } + +} + +util.inherits(SnmpMac, EventEmitter); + +module.exports = SnmpMac; diff --git a/node/statusbot.js b/node/statusbot.js new file mode 100644 index 0000000..d612694 --- /dev/null +++ b/node/statusbot.js @@ -0,0 +1,84 @@ +var _u = require("underscore"); +var express = require('express'), + app = express(); +var SnmpMac = require("./snmp-mac"); +var IpPoll = require("./ip-poll"); + +var spaceanswer = { + "api": "0.13", + "space": "Chaostreff Dortmund", + "logo": "http://www.chaostreff-dortmund.de/logo.jpg", + "url": "http://www.chaostreff-dortmund.de/", + "location": { + "address": "Braunschweiger Str 22, 44145 Dortmund, Germany", + "lon": 7.4649449, + "lat": 51.527611, + }, + "contact": { + "phone": "+49231 8 404 777", + "irc": "irc://irc.hackint.eu/#ctdo", + "ml": "ccc@chaostreff-dortmund.de", + "twitter": "@ctdo", + "issue_mail": "server-admin@chaostreff-dortmund.de" + }, + "issue_report_channels": [ "issue_email" ], + "state": { + "open": null, + "lastchange": 0 + } +}; + +var simpleanswer = { "state": "unknown", + "count": 0, + "names": [], + "lastchange": 0 +}; + +function start() { + var snmpMac = new SnmpMac("juni.ctdo.de", "ctdo23"); + var ippoll = new IpPoll("switch2.raum.ctdo.de","195.160.169.20-62 195.160.169.70-126"); + + snmpMac.on('done', function(res) { + console.log(res); + simpleanswer.names = res; + simpleanswer.lastchange = parseInt((new Date().getTime()) / 1000); + }); + + ippoll.on('doneCount', function(num) { + console.log("there are " + num + " hosts up"); + simpleanswer.count = num; + simpleanswer.lastchange = parseInt((new Date().getTime()) / 1000); + }); + + ippoll.on('doneState', function(state) { + console.log("room state is: " + state); + simpleanswer.state = state; + spaceanswer.state.open = state; + + spaceanswer.state.lastchange = parseInt((new Date().getTime()) / 1000); + simpleanswer.lastchange = spaceanswer.state.lastchange; + }); + + setInterval(function() { + snmpMac.poll(); + ippoll.pollCount(); + ippoll.pollState(); + }, 10000); + + + app.get('/api/spaceapi/v13', function(req, res) { + res.send(spaceanswer); + }); + app.get('/api/simple/v2', function(req, res) { + res.send(simpleanswer); + }); + + app.listen(3000); + + snmpMac.poll(); + ippoll.pollCount(); + ippoll.pollState(); +} + + +exports.start = start;