Skip to content

dgwynne/node-snmp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

snmp for node.js

i hate writing doco. the code below shows how it works though. it
queries all the devices on 192.168.1.0/24 for their interface info. in
my environment it was about to find 26 devices which had 3500 interfaces
in just under 15 seconds.

var snmp = require('snmp');
var mgr = snmp.createManager( { community: 'public', version: 2, retries: 10 } );

var agents = { };
process.on('exit', function() {
        console.log(agents);
        console.log(Object.keys(agents).length);
});

var oid2mib = {
        '1.3.6.1.2.1.2.2.1.2': 'ifDescr',
        '1.3.6.1.2.1.2.2.1.7': 'ifAdminStatus',
        '1.3.6.1.2.1.2.2.1.8': 'ifOperStatus',
        '1.3.6.1.2.1.31.1.1.1.1': 'ifName',
        '1.3.6.1.2.1.31.1.1.1.15': 'ifHighSpeed',
        '1.3.6.1.2.1.31.1.1.1.18': 'ifAlias',

        '1.3.6.1.2.1.31.1.1.1.6': 'ifHCInOctets',
        '1.3.6.1.2.1.31.1.1.1.10': 'ifHCOutOctets',

        '1.3.6.1.2.1.31.1.1.1.7': 'ifHCInUcastPkts',
        '1.3.6.1.2.1.31.1.1.1.11': 'ifHCOutUcastPkts',

        '1.3.6.1.2.1.31.1.1.1.8': 'ifHCInMulticastPkts',
        '1.3.6.1.2.1.31.1.1.1.12': 'ifHCOutMulticastPkts',

        '1.3.6.1.2.1.31.1.1.1.9': 'ifHCInBroadcastPkts',
        '1.3.6.1.2.1.31.1.1.1.13': 'ifHCOutBroadcastPkts',

        '1.3.6.1.2.1.2.2.1.13': 'ifInDiscards',
        '1.3.6.1.2.1.2.2.1.19': 'ifOutDiscards',

        '1.3.6.1.2.1.2.2.1.14': 'ifInErrors',
        '1.3.6.1.2.1.2.2.1.20': 'ifOutErrors'
};

function ml(err, res, agent) {
        if (err)
                return;

        if (typeof(agents[agent]) === 'undefined')
                agents[agent] = { };

        for (var i = 0; i < res.length; i++) {
                var o = res[i].oid.split('.');
                var idx = o.pop();
                var oid = o.join('.');

                if (typeof(agents[agent][idx]) === 'undefined')
                        agents[agent][idx] = { };

                agents[agent][idx][oid2mib[oid]] = res[i].value.toString();
        }
}

function m(agent) {
        mgr.get(agent, '1.3.6.1.2.1.1.3.0', function(e, r) {
                if (e)
                        return;

                for (var k in oid2mib) {
                        mgr.bulkGet(agent, k, function (e, r) { ml(e,r,agent); });
                }
        }, { retries: 3 });
}

for (i = 1; i < 255; i++)
        m('192.168.1.' + i);