calmh / node-snmp-native

Native Javascript SNMP library for Node.js
MIT License
252 stars 65 forks source link

Added Support to getSubtreeBulk (more efficient than getSubtree); #4

Closed fmgdias closed 12 years ago

fmgdias commented 12 years ago

Added Suppert to Hide Value Raw and Value Hex; Change to Use PDU Request asn1ber.pduTypes (GetRequestPDU, GetNextRequestPDU, GetBulkRequestPDU)

fmgdias commented 12 years ago

Source to Teste:

[code] //Biblioteca var util = require('util');

//Pacotes var date_utils = require('date-utils'); var snmp = require('snmp-native');

//Snmp var snmpSession = new snmp.Session({ host : 'localhost', community : 'public' }); snmpSession.get({ "oid": '.1.3.6.1.2.1.1.5.0' /* sysName / }, function (err, snmpVarbinds) { if ( err ) { console.log("err:",err); } else { console.log("get:",snmpVarbinds); } / snmpSession.close(); / }); snmpSession.getNext({ "oid": '.1.3.6.1.2.1.2.2.1.2' / ifDescr / }, function (err, snmpVarbinds) { if ( err ) { console.log("err:",err); } else { console.log("getNext:",snmpVarbinds); } / snmpSession.close(); / }); snmpSession.getSubtree({ "oid": '.1.3.6.1.2.1.2.2.1.2' / ifDescr / }, function (err, snmpVarbinds) { if ( err ) { console.log("err:",err); } else { console.log("getSubtree:",snmpVarbinds); } / snmpSession.close(); / }); snmpSession.getNextBulk({ "oid": '.1.3.6.1.2.1.2.2.1.2' / ifDescr / }, function (err, snmpVarbinds) { if ( err ) { console.log("err:",err); } else { console.log("getNextBulk:",snmpVarbinds); } / snmpSession.close(); _/ });
/_/ snmpSession.getSubtreeBulk({ "oid": '.1.3.6.1.2.1.2.2.1.2' / ifDescr / }, function (err, snmpVarbinds) { if ( err ) { console.log("err:",err); } else { console.log("getSubtreeBulk:",snmpVarbinds); } / snmpSession.close(); / }); /*/ [/code]

fmgdias commented 12 years ago

new patch for [code]

Session.prototype.getSubtreeBulk = function (options, callback) { var self = this, vbs = [];

_.defaults(options, self.options);
parseOids(options);

if (!options.oid) {
    return callback(null, []);
}

options.startOid = options.oid;

// Helper to check whether `oid` in inside the tree rooted at
// `root` or not.
function inTree(root, oid) {
    var i;
    if (oid.length <= root.length) {
        return false;
    }
    for (i = 0; i < root.length; i++) {
        if (oid[i] !== root[i]) {
            return false;
        }
    }
    return true;
}

// Helper to handle the result of getNext and call the user's callback
// as appropriate. The callback will see one of the following patterns:
//  - callback([an Error object], undefined) -- an error ocurred.
//  - callback(null, [a Packet object]) -- data from under the tree.
//  - callback(null, null) -- end of tree.
function result(error, varbinds) {
    var i;

    if (error) {
        callback(error);
    } else {
        var next = null;
        for (i = 0; i < varbinds.length; i++) {
            if (inTree(options.startOid, varbinds[i].oid)) {
                if (varbinds[i].value === 'endOfMibView' || varbinds[i].value === 'noSuchObject' || varbinds[i].value === 'noSuchInstance') {
                    next = null;
                } else {
                    vbs.push(varbinds[i]);
                    next = { oid: varbinds[i].oid };
                }
            } else {
                next = null;
            }
        }
        if ( next != null ) {
            _.defaults(next, options);
            self.getNextBulk(next, result);
        } else {
            callback(null, vbs);
        }
    }
}

self.getNextBulk(options, result);

};

[/code]

calmh commented 12 years ago

Hi! Thanks for the pull request, getBulk would be sweet to have in there. I have a few comments though;

fmgdias commented 12 years ago

ok, thk man...