forked from snoj/itsnmptrap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (129 loc) · 3.76 KB
/
index.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
144
145
146
147
148
149
150
151
152
var snmp = require('snmp-native');
var snmprouter = function(opts) {
opts = opts || {};
opts.once = opts.once || false;
this.opts = opts;
this._traps = [];
this._filters = [];
};
snmprouter.prototype.use = function(oid, cb) {
if(typeof oid === 'function') {
cb = oid;
oid = /./;
}
this._addtostore('_filters', oid, {}, cb);
return this;
};
snmprouter.prototype.trap = function(_oid, _opts, _callback) {
var oid, opts, callback;
switch(arguments.length) {
case 1:
if(typeof _oid !== 'function')
throw "[Error: must provide a callback for a trap route]";
oid = /./;
opts = {};
callback = oid;
break;
case 2:
if(typeof _oid === 'string' || (_oid instanceof RegExp)) {
oid = _oid;
opts = {};
} else {
oid = /./;
opts = _oid;
}
callback = _opts;
break;
default:
oid = _oid;
opts = _opts;
callback = _callback;
}
this._addtostore('_traps', oid, opts, callback);
return this;
};
snmprouter.prototype._addtostore = function(store, oid, opts, callback) {
store = store || '_traps';
opts.once = opts.once || this.opts.once || false;
var raddr = /./;
if(/@/.test(oid)) {
var splitoid = oid.split('@');
raddr = splitoid[0]; //.replace(/\./g, "\\.");
oid = splitoid[1];
if(require('net').isIP(raddr)) raddr = new RegExp("^" + raddr.replace(/\./g, "\\.") + "$");
else raddr = new RegExp(raddr);
}
this[store].push({raddr: raddr, oid: oid, callback: callback, opts: opts});
};
snmprouter.prototype.findcb = function(remote, oid, store) {
store = store || "_traps";
var self = this;
var cbs = this[store].filter(function(v) {
if(v.oid instanceof RegExp && v.oid.test(oid) && v.raddr.test(remote)) {
return true;
} else if(typeof v.oid === 'string' && v.oid === oid && v.raddr.test(remote)) {
return true;
}
return false;
}).sort(function(a, b) {
var rtn = 0;
if(a.opts.once && !b.opts.once) return -1;
var ord = (a.oid.length || a.oid.toString().length) - (b.oid.length || b.oid.toString().length);
rtn = -ord;
return rtn;
});
return cbs;
};
snmprouter.prototype._filter = function(trap, endcb) {
var filters = this.findcb(trap.remote, trap.oid, '_filters');
if(filters.length > 0)
snmprouter._filterwalk(trap, filters, 0, endcb);
else
endcb(trap);
};
snmprouter._filterwalk = function(trap, list, i, end) {
if(typeof list[i] === 'undefined') {
end(trap) //, list, i-1);
return;
}
list[i].callback.call(null, trap, function(ok) {
if((typeof ok === 'boolean' && ok !== false) || typeof ok === 'undefined')
setImmediate(snmprouter._filterwalk.bind(null, trap, list, i+1, end));
});
};
snmprouter.prototype.runmsg = function(rawmsg, remote) {
var self = this;
var msg = snmp.parse(rawmsg);
var ran = {};
msg.pdu.varbinds.forEach(function(vb) {
var oid = vb.oid.join('.');
if(ran[oid]) return;
var trap = {
oid:oid
, value: vb.value
, varbind: vb
, msg: msg
, remote: remote
};
self._filter(trap, function() {
self.findcb(remote.address, oid).forEach(function(v) {
if(ran[oid] && v.opts.once) return;
ran[oid] = true;
v.callback({oid:oid, value: vb.value, varbind: vb, msg: msg, remote: remote})
});
});
});
};
snmprouter.prototype.listen = function(port, address, callback) {
var self = this;
address = address || "0.0.0.0";
var addresstype = 'udp' + require('net').isIP(address);
self._dgram = require('dgram').createSocket(addresstype, function(msg, remote) {
self.runmsg(msg, remote);
});
self._dgram.bind(port, address);
};
snmprouter.prototype.close = function() {
this._dgram.close();
};
module.exports = snmprouter;