function RequestFailedError(message, status) { this.name = "RequestFailedError"; this.message = message; this.status = status; Error.captureStackTrace(this, RequestFailedError); }
n/a
function Error() { [native code] }
n/a
function RequestInvalidError(message) { this.name = "RequestInvalidError"; this.message = message; Error.captureStackTrace(this, RequestInvalidError); }
n/a
function RequestTimedOutError(message) { this.name = "RequestTimedOutError"; this.message = message; Error.captureStackTrace(this, RequestTimedOutError); }
n/a
function ResponseInvalidError(message) { this.name = "ResponseInvalidError"; this.message = message; Error.captureStackTrace(this, ResponseInvalidError); }
n/a
Session = function (target, community, options) { this.target = target || "127.0.0.1"; this.community = community || "public"; this.version = (options && options.version) ? options.version : Version1; this.transport = (options && options.transport) ? options.transport : "udp4"; this.port = (options && options.port ) ? options.port : 161; this.trapPort = (options && options.trapPort ) ? options.trapPort : 162; this.retries = (options && (options.retries || options.retries == 0)) ? options.retries : 1; this.timeout = (options && options.timeout) ? options.timeout : 5000; this.sourceAddress = (options && options.sourceAddress ) ? options.sourceAddress : undefined; this.sourcePort = (options && options.sourcePort ) ? parseInt(options.sourcePort) : undefined; this.reqs = {}; this.reqCount = 0; this.dgram = dgram.createSocket (this.transport); this.dgram.unref(); var me = this; this.dgram.on ("message", me.onMsg.bind (me)); this.dgram.on ("close", me.onClose.bind (me)); this.dgram.on ("error", me.onError.bind (me)); if (this.sourceAddress || this.sourcePort) this.dgram.bind (this.sourcePort, this.sourceAddress); }
n/a
createSession = function (target, community, options) { return new Session (target, community, options); }
...
## Version 1.1.17 - 21/03/2016
* Correct reference to non-existant `req` variable in the `Session` objects
constructor (should be `this`)
## Version 1.1.18 - 15/05/2015
* Correct argument number and names to the `snmp.createSession()` function
* Add missing braces to an example in the README.md file
## Version 1.1.19 - 26/08/2016
* Remove 64bit integer check to ensure a maximum of 8 bytes are given in send
and received messages
...
function isVarbindError(varbind) { return !!(varbind.type == ObjectType.NoSuchObject || varbind.type == ObjectType.NoSuchInstance || varbind.type == ObjectType.EndOfMibView); }
...
error occurred
* `varbinds` - Array of varbinds, will not be provided if an error occurred
The varbind in position N in the `varbinds` array will correspond to the OID
in position N in the `oids` array in the request.
Each varbind must be checked for an error condition using the
`snmp.isVarbindError()` function when using SNMP version 2c.
The following example fetches values for the sysName (`1.3.6.1.2.1.1.5.0`) and
sysLocation (`1.3.6.1.2.1.1.6.0`) OIDs:
var oids = ["1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.1.6.0"];
session.get (oids, function (error, varbinds) {
...
function varbindError(varbind) { return (ObjectType[varbind.type] || "NotAnError") + ": " + varbind.oid; }
...
* `table` - Object containing object references representing conceptual
rows keyed by index (e.g. for the ifTable table rows are keyed by ifIndex),
each row object will contain values keyed by column number, will not be
provided if an error occurred
If an error occurs with any varbind returned by `subtree()` no table will be
passed to the `callback` function. The reason for failure, and the related
OID string (as returned from a call to the `snmp.varbindError()` function),
will be passed to the `callback` function in the `error` argument as an
instance of the `RequestFailedError` class.
The following example fetches the ifTable (`1.3.6.1.2.1.2.2`) table:
var oid = "1.3.6.1.2.1.2.2";
...
function readInt(buffer) { return readUint (buffer, true); }
n/a
function readUint(buffer, isSigned) { buffer.readByte (); var length = buffer.readByte (); var value = 0; var signedBitSet = false; if (length > 5) { throw new RangeError ("Integer too long '" + length + "'"); } else if (length == 5) { if (buffer.readByte () !== 0) throw new RangeError ("Integer too long '" + length + "'"); length = 4; } for (var i = 0; i < length; i++) { value *= 256; value += buffer.readByte (); if (isSigned && i <= 0) { if ((value & 0x80) == 0x80) signedBitSet = true; } } if (signedBitSet) value -= (1 << (i * 8)); return value; }
n/a
function RequestFailedError(message, status) { this.name = "RequestFailedError"; this.message = message; this.status = status; Error.captureStackTrace(this, RequestFailedError); }
n/a
function Error() { [native code] }
n/a
function Error() { [native code] }
n/a
function captureStackTrace() { [native code] }
...
/*****************************************************************************
** Exception class definitions
**/
function ResponseInvalidError (message) {
this.name = "ResponseInvalidError";
this.message = message;
Error.captureStackTrace(this, ResponseInvalidError);
}
util.inherits (ResponseInvalidError, Error);
function RequestInvalidError (message) {
this.name = "RequestInvalidError";
this.message = message;
Error.captureStackTrace(this, RequestInvalidError);
...
function RequestInvalidError(message) { this.name = "RequestInvalidError"; this.message = message; Error.captureStackTrace(this, RequestInvalidError); }
n/a
function Error() { [native code] }
n/a
function RequestTimedOutError(message) { this.name = "RequestTimedOutError"; this.message = message; Error.captureStackTrace(this, RequestTimedOutError); }
n/a
function Error() { [native code] }
n/a
function ResponseInvalidError(message) { this.name = "ResponseInvalidError"; this.message = message; Error.captureStackTrace(this, ResponseInvalidError); }
n/a
function Error() { [native code] }
n/a
Session = function (target, community, options) { this.target = target || "127.0.0.1"; this.community = community || "public"; this.version = (options && options.version) ? options.version : Version1; this.transport = (options && options.transport) ? options.transport : "udp4"; this.port = (options && options.port ) ? options.port : 161; this.trapPort = (options && options.trapPort ) ? options.trapPort : 162; this.retries = (options && (options.retries || options.retries == 0)) ? options.retries : 1; this.timeout = (options && options.timeout) ? options.timeout : 5000; this.sourceAddress = (options && options.sourceAddress ) ? options.sourceAddress : undefined; this.sourcePort = (options && options.sourcePort ) ? parseInt(options.sourcePort) : undefined; this.reqs = {}; this.reqCount = 0; this.dgram = dgram.createSocket (this.transport); this.dgram.unref(); var me = this; this.dgram.on ("message", me.onMsg.bind (me)); this.dgram.on ("close", me.onClose.bind (me)); this.dgram.on ("error", me.onError.bind (me)); if (this.sourceAddress || this.sourcePort) this.dgram.bind (this.sourcePort, this.sourceAddress); }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
cancelRequests = function (error) { var id; for (id in this.reqs) { var req = this.reqs[id]; this.unregisterRequest (req.id); req.responseCb (error); } }
n/a
close = function () { this.dgram.close (); return this; }
n/a
get = function (oids, responseCb) { function feedCb (req, message) { var pdu = message.pdu; var varbinds = []; if (req.message.pdu.varbinds.length != pdu.varbinds.length) { req.responseCb (new ResponseInvalidError ("Requested OIDs do not " + "match response OIDs")); } else { for (var i = 0; i < req.message.pdu.varbinds.length; i++) { if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) { req.responseCb (new ResponseInvalidError ("OID '" + req.message.pdu.varbinds[i].oid + "' in request at positiion '" + i + "' does not " + "match OID '" + pdu.varbinds[i].oid + "' in response " + "at position '" + i + "'")); return; } else { varbinds.push (pdu.varbinds[i]); } } req.responseCb (null, varbinds); } } var pduVarbinds = []; for (var i = 0; i < oids.length; i++) { var varbind = { oid: oids[i] }; pduVarbinds.push (varbind); } this.simpleGet (GetRequestPdu, feedCb, pduVarbinds, responseCb); return this; }
n/a
getBulk = function () { var oids, nonRepeaters, maxRepetitions, responseCb; if (arguments.length >= 4) { oids = arguments[0]; nonRepeaters = arguments[1]; maxRepetitions = arguments[2]; responseCb = arguments[3]; } else if (arguments.length >= 3) { oids = arguments[0]; nonRepeaters = arguments[1]; maxRepetitions = 10; responseCb = arguments[2]; } else { oids = arguments[0]; nonRepeaters = 0; maxRepetitions = 10; responseCb = arguments[1]; } function feedCb (req, message) { var pdu = message.pdu; var varbinds = []; var i = 0; // first walk through and grab non-repeaters if (pdu.varbinds.length < nonRepeaters) { req.responseCb (new ResponseInvalidError ("Varbind count in " + "response '" + pdu.varbinds.length + "' is less than " + "non-repeaters '" + nonRepeaters + "' in request")); } else { for ( ; i < nonRepeaters; i++) { if (isVarbindError (pdu.varbinds[i])) { varbinds.push (pdu.varbinds[i]); } else if (! oidFollowsOid (req.message.pdu.varbinds[i].oid, pdu.varbinds[i].oid)) { req.responseCb (new ResponseInvalidError ("OID '" + req.message.pdu.varbinds[i].oid + "' in request at " + "positiion '" + i + "' does not precede " + "OID '" + pdu.varbinds[i].oid + "' in response " + "at position '" + i + "'")); return; } else { varbinds.push (pdu.varbinds[i]); } } } var repeaters = req.message.pdu.varbinds.length - nonRepeaters; // secondly walk through and grab repeaters if (pdu.varbinds.length % (repeaters)) { req.responseCb (new ResponseInvalidError ("Varbind count in " + "response '" + pdu.varbinds.length + "' is not a " + "multiple of repeaters '" + repeaters + "' plus non-repeaters '" + nonRepeaters + "' in request")); } else { while (i < pdu.varbinds.length) { for (var j = 0; j < repeaters; j++, i++) { var reqIndex = nonRepeaters + j; var respIndex = i; if (isVarbindError (pdu.varbinds[respIndex])) { if (! varbinds[reqIndex]) varbinds[reqIndex] = []; varbinds[reqIndex].push (pdu.varbinds[respIndex]); } else if (! oidFollowsOid ( req.message.pdu.varbinds[reqIndex].oid, pdu.varbinds[respIndex].oid)) { req.responseCb (new ResponseInvalidError ("OID '" + req.message.pdu.varbinds[reqIndex].oid + "' in request at positiion '" + (reqIndex) + "' does not precede OID '" + pdu.varbinds[respIndex].oid + "' in response at position '" + (respIndex) + "'")); return; } else { if (! varbinds[reqIndex]) varbinds[reqIndex] = []; varbinds[reqIndex].push (pdu.varbinds[respIndex]); } } } } req.responseCb (null, varbinds); } var pduVarbinds = []; for (var i = 0; i < oids.length; i++) { var varbind = { oid: oids[i] }; pduVarbinds.push (varbind); } var options = { nonRepeaters: nonRepeaters, maxRepetitions: maxRepetitions }; this.simpleGet (GetBulkRequestPdu, feedCb, pduVarbinds, responseCb, options); return this; }
n/a
getNext = function (oids, responseCb) { function feedCb (req, message) { var pdu = message.pdu; var varbinds = []; if (req.message.pdu.varbinds.length != pdu.varbinds.length) { req.responseCb (new ResponseInvalidError ("Requested OIDs do not " + "match response OIDs")); } else { for (var i = 0; i < req.message.pdu.varbinds.length; i++) { if (isVarbindError (pdu.varbinds[i])) { varbinds.push (pdu.varbinds[i]); } else if (! oidFollowsOid (req.message.pdu.varbinds[i].oid, pdu.varbinds[i].oid)) { req.responseCb (new ResponseInvalidError ("OID '" + req.message.pdu.varbinds[i].oid + "' in request at " + "positiion '" + i + "' does not precede " + "OID '" + pdu.varbinds[i].oid + "' in response " + "at position '" + i + "'")); return; } else { varbinds.push (pdu.varbinds[i]); } } req.responseCb (null, varbinds); } } var pduVarbinds = []; for (var i = 0; i < oids.length; i++) { var varbind = { oid: oids[i] }; pduVarbinds.push (varbind); } this.simpleGet (GetNextRequestPdu, feedCb, pduVarbinds, responseCb); return this; }
n/a
inform = function () {
var typeOrOid = arguments[0];
var varbinds, options = {}, responseCb;
/**
** Support the following signatures:
**
** typeOrOid, varbinds, options, callback
** typeOrOid, varbinds, callback
** typeOrOid, options, callback
** typeOrOid, callback
**/
if (arguments.length >= 4) {
varbinds = arguments[1];
options = arguments[2];
responseCb = arguments[3];
} else if (arguments.length >= 3) {
if (arguments[1].constructor != Array) {
varbinds = [];
options = arguments[1];
responseCb = arguments[2];
} else {
varbinds = arguments[1];
responseCb = arguments[2];
}
} else {
varbinds = [];
responseCb = arguments[1];
}
function feedCb (req, message) {
var pdu = message.pdu;
var varbinds = [];
if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
req.responseCb (new ResponseInvalidError ("Inform OIDs do not "
+ "match response OIDs"));
} else {
for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
req.responseCb (new ResponseInvalidError ("OID '"
+ req.message.pdu.varbinds[i].oid
+ "' in inform at positiion '" + i + "' does not "
+ "match OID '" + pdu.varbinds[i].oid + "' in response "
+ "at position '" + i + "'"));
return;
} else {
varbinds.push (pdu.varbinds[i]);
}
}
req.responseCb (null, varbinds);
}
}
if (typeof typeOrOid != "string")
typeOrOid = "1.3.6.1.6.3.1.1.5." + (typeOrOid + 1);
var pduVarbinds = [
{
oid: "1.3.6.1.2.1.1.3.0",
type: ObjectType.TimeTicks,
value: options.upTime || Math.floor (process.uptime () * 100)
},
{
oid: "1.3.6.1.6.3.1.1.4.1.0",
type: ObjectType.OID,
value: typeOrOid
}
];
for (var i = 0; i < varbinds.length; i++) {
var varbind = {
oid: varbinds[i].oid,
type: varbinds[i].type,
value: varbinds[i].value
};
pduVarbinds.push (varbind);
}
options.port = this.trapPort;
this.simpleGet (InformRequestPdu, feedCb, pduVarbinds, responseCb, options);
return this;
}
n/a
onClose = function () { this.cancelRequests (new Error ("Socket forcibly closed")); this.emit ("close"); }
n/a
onError = function (error) { this.emit (error); }
n/a
onMsg = function (buffer, remote) { try { var message = new ResponseMessage (buffer); var req = this.unregisterRequest (message.pdu.id); if (! req) return; try { if (message.version != req.message.version) { req.responseCb (new ResponseInvalidError ("Version in request '" + req.message.version + "' does not match version in " + "response '" + message.version)); } else if (message.community != req.message.community) { req.responseCb (new ResponseInvalidError ("Community '" + req.message.community + "' in request does not match " + "community '" + message.community + "' in response")); } else if (message.pdu.type == PduType.GetResponse) { req.onResponse (req, message); } else { req.responseCb (new ResponseInvalidError ("Unknown PDU type '" + message.pdu.type + "' in response")); } } catch (error) { req.responseCb (error); } } catch (error) { this.emit("error", error); } }
n/a
onSimpleGetResponse = function (req, message) { var pdu = message.pdu; if (pdu.errorStatus > 0) { var statusString = ErrorStatus[pdu.errorStatus] || ErrorStatus.GeneralError; var statusCode = ErrorStatus[statusString] || ErrorStatus[ErrorStatus.GeneralError]; if (pdu.errorIndex <= 0 || pdu.errorIndex > pdu.varbinds.length) { req.responseCb (new RequestFailedError (statusString, statusCode)); } else { var oid = pdu.varbinds[pdu.errorIndex - 1].oid; var error = new RequestFailedError (statusString + ": " + oid, statusCode); req.responseCb (error); } } else { req.feedCb (req, message); } }
n/a
registerRequest = function (req) { if (! this.reqs[req.id]) { this.reqs[req.id] = req; if (this.reqCount <= 0) this.dgram.ref(); this.reqCount++; } var me = this; req.timer = setTimeout (function () { if (req.retries-- > 0) { me.send (req); } else { me.unregisterRequest (req.id); req.responseCb (new RequestTimedOutError ( "Request timed out")); } }, req.timeout); }
n/a
send = function (req, noWait) { try { var me = this; var buffer = req.message.toBuffer (); this.dgram.send (buffer, 0, buffer.length, req.port, this.target, function (error, bytes) { if (error) { req.responseCb (error); } else { if (noWait) { req.responseCb (null); } else { me.registerRequest (req); } } }); } catch (error) { req.responseCb (error); } return this; }
...
* Corrected a few instances of the parameter named `requestCallback` to some
methods in the README.md file which should have been `feedCallback`
* Null type is used for varbinds with a 0 value
* Correct instances of snmp.Type to snmp.ObjectType in the README.md file
## Version 1.1.10 - 01/12/2013
* Error handler in the `dgram.send()` callback in the `send()` method was
creating a new instance of the `Error` class from the `error` parameter, but
it was already an instance of the `Error` class (thanks Ray Solomon)
* Add stack traces to Error classes exported by this module (thanks Ray
Solomon)
* Allow users to specify `0` retries when creating a session (thanks Ray
Solomon)
* Update the list of SNMP version 1 related RFCs we adhere to in the
...
set = function (varbinds, responseCb) { function feedCb (req, message) { var pdu = message.pdu; var varbinds = []; if (req.message.pdu.varbinds.length != pdu.varbinds.length) { req.responseCb (new ResponseInvalidError ("Requested OIDs do not " + "match response OIDs")); } else { for (var i = 0; i < req.message.pdu.varbinds.length; i++) { if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) { req.responseCb (new ResponseInvalidError ("OID '" + req.message.pdu.varbinds[i].oid + "' in request at positiion '" + i + "' does not " + "match OID '" + pdu.varbinds[i].oid + "' in response " + "at position '" + i + "'")); return; } else { varbinds.push (pdu.varbinds[i]); } } req.responseCb (null, varbinds); } } var pduVarbinds = []; for (var i = 0; i < varbinds.length; i++) { var varbind = { oid: varbinds[i].oid, type: varbinds[i].type, value: varbinds[i].value }; pduVarbinds.push (varbind); } this.simpleGet (SetRequestPdu, feedCb, pduVarbinds, responseCb); return this; }
n/a
simpleGet = function (pduClass, feedCb, varbinds, responseCb, options) { var req = {}; try { var id = _generateId (); var pdu = new pduClass (id, varbinds, options); var message = new RequestMessage (this.version, this.community, pdu); req = { id: id, message: message, responseCb: responseCb, retries: this.retries, timeout: this.timeout, onResponse: this.onSimpleGetResponse, feedCb: feedCb, port: (options && options.port) ? options.port : this.port }; this.send (req); } catch (error) { if (req.responseCb) req.responseCb (error); } }
n/a
subtree = function () { var me = this; var oid = arguments[0]; var maxRepetitions, feedCb, doneCb; if (arguments.length < 4) { maxRepetitions = 20; feedCb = arguments[1]; doneCb = arguments[2]; } else { maxRepetitions = arguments[1]; feedCb = arguments[2]; doneCb = arguments[3]; } var req = { feedCb: feedCb, doneCb: doneCb, maxRepetitions: maxRepetitions, baseOid: oid }; this.walk (oid, maxRepetitions, subtreeCb.bind (me, req), doneCb); return this; }
n/a
table = function () { var me = this; var oid = arguments[0]; var maxRepetitions, responseCb; if (arguments.length < 3) { responseCb = arguments[1]; maxRepetitions = 20; } else { maxRepetitions = arguments[1]; responseCb = arguments[2]; } var req = { responseCb: responseCb, maxRepetitions: maxRepetitions, baseOid: oid, rowOid: oid + ".1.", table: {} }; this.subtree (oid, maxRepetitions, tableFeedCb.bind (me, req), tableResponseCb.bind (me, req)); return this; }
n/a
tableColumns = function () { var me = this; var oid = arguments[0]; var columns = arguments[1]; var maxRepetitions, responseCb; if (arguments.length < 4) { responseCb = arguments[2]; maxRepetitions = 20; } else { maxRepetitions = arguments[2]; responseCb = arguments[3]; } var req = { responseCb: responseCb, maxRepetitions: maxRepetitions, baseOid: oid, rowOid: oid + ".1.", columns: columns.slice(0), table: {} }; if (req.columns.length > 0) { var column = req.columns.pop (); this.subtree (req.rowOid + column, maxRepetitions, tableColumnsFeedCb.bind (me, req), tableColumnsResponseCb.bind (me, req)); } return this; }
n/a
trap = function () {
var req = {};
try {
var typeOrOid = arguments[0];
var varbinds, options = {}, responseCb;
/**
** Support the following signatures:
**
** typeOrOid, varbinds, options, callback
** typeOrOid, varbinds, agentAddr, callback
** typeOrOid, varbinds, callback
** typeOrOid, agentAddr, callback
** typeOrOid, options, callback
** typeOrOid, callback
**/
if (arguments.length >= 4) {
varbinds = arguments[1];
if (typeof arguments[2] == "string") {
options.agentAddr = arguments[2];
} else if (arguments[2].constructor != Array) {
options = arguments[2];
}
responseCb = arguments[3];
} else if (arguments.length >= 3) {
if (typeof arguments[1] == "string") {
varbinds = [];
options.agentAddr = arguments[1];
} else if (arguments[1].constructor != Array) {
varbinds = [];
options = arguments[1];
} else {
varbinds = arguments[1];
agentAddr = null;
}
responseCb = arguments[2];
} else {
varbinds = [];
responseCb = arguments[1];
}
var pdu, pduVarbinds = [];
for (var i = 0; i < varbinds.length; i++) {
var varbind = {
oid: varbinds[i].oid,
type: varbinds[i].type,
value: varbinds[i].value
};
pduVarbinds.push (varbind);
}
var id = _generateId ();
if (this.version == Version2c) {
if (typeof typeOrOid != "string")
typeOrOid = "1.3.6.1.6.3.1.1.5." + (typeOrOid + 1);
pduVarbinds.unshift (
{
oid: "1.3.6.1.2.1.1.3.0",
type: ObjectType.TimeTicks,
value: options.upTime || Math.floor (process.uptime () * 100)
},
{
oid: "1.3.6.1.6.3.1.1.4.1.0",
type: ObjectType.OID,
value: typeOrOid
}
);
pdu = new TrapV2Pdu (id, pduVarbinds, options);
} else {
pdu = new TrapPdu (typeOrOid, pduVarbinds, options);
}
var message = new RequestMessage (this.version, this.community, pdu);
req = {
id: id,
message: message,
responseCb: responseCb,
port: this.trapPort
};
this.send (req, true);
} catch (error) {
if (req.responseCb)
req.responseCb (error);
}
return this;
}
n/a
unregisterRequest = function (id) { var req = this.reqs[id]; if (req) { delete this.reqs[id]; clearTimeout (req.timer); delete req.timer; this.reqCount--; if (this.reqCount <= 0) this.dgram.unref(); return req; } else { return null; } }
n/a
walk = function () { var me = this; var oid = arguments[0]; var maxRepetitions, feedCb, doneCb, baseOid; if (arguments.length < 4) { maxRepetitions = 20; feedCb = arguments[1]; doneCb = arguments[2]; } else { maxRepetitions = arguments[1]; feedCb = arguments[2]; doneCb = arguments[3]; } var req = { maxRepetitions: maxRepetitions, feedCb: feedCb, doneCb: doneCb }; if (this.version == Version2c) this.getBulk ([oid], 0, maxRepetitions, walkCb.bind (me, req)); else this.getNext ([oid], walkCb.bind (me, req)); return this; }
n/a