]> arthur.barton.de Git - netdata.git/commitdiff
snmp now supports max request size, also can retreive chart titles and dimension...
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Wed, 17 Feb 2016 22:57:02 +0000 (00:57 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Wed, 17 Feb 2016 22:57:02 +0000 (00:57 +0200)
node.d/snmp.node.js

index 8368303009be2a92f3b73cdc8054998690f5dce7..53e5a335a874f774bad2716985a2520a4f5b14e8 100755 (executable)
@@ -7,11 +7,13 @@
 {
        "enable_autodetect": false,
        "update_every": 5,
+       "max_request_size": 50,
        "servers": [
                {
                        "hostname": "10.11.12.8",
                        "community": "public",
                        "update_every": 10,
+                       "max_request_size": 50,
                        "options": { "timeout": 10000 },
                        "charts": {
                                "snmp_switch.bandwidth_port1": {
 {
        "enable_autodetect": false,
        "update_every": 10,
+       "max_request_size": 50,
        "servers": [
                {
                        "hostname": "10.11.12.8",
                        "community": "public",
                        "update_every": 10,
+                       "max_request_size": 50,
                        "options": { "timeout": 20000 },
                        "charts": {
                                "snmp_switch.bandwidth_port": {
@@ -111,13 +115,16 @@ netdata.processors.snmp = {
        name: 'snmp',
 
        fixoid: function(oid) {
+               if(typeof oid !== 'string')
+                       return oid;
+
                if(oid.charAt(0) === '.')
                        return oid.substring(1, oid.length);
 
                return oid;
        },
 
-       process: function(service, callback) {
+       prepare: function(service) {
                if(typeof service.snmp_oids === 'undefined' || service.snmp_oids === null || service.snmp_oids.length === 0) {
                        // this is the first time we see this service
 
@@ -132,21 +139,39 @@ netdata.processors.snmp = {
                                if(netdata.options.DEBUG === true)
                                        netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c);
 
+                               if(typeof service.request.charts[c].titleoid !== 'undefined') {
+                                               service.snmp_oids_index[this.fixoid(service.request.charts[c].titleoid)] = {
+                                                       type: 'title',
+                                                       link: service.request.charts[c]
+                                               };
+                                       }
+
                                for(var d in service.request.charts[c].dimensions) {
                                        // for each dimension in the chart
 
                                        var oid = this.fixoid(service.request.charts[c].dimensions[d].oid);
+                                       var oidname = this.fixoid(service.request.charts[c].dimensions[d].oidname);
                                        
                                        if(netdata.options.DEBUG === true)
-                                               netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c + ', dimension: ' + d + ', OID: ' + oid);
+                                               netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c + ', dimension: ' + d + ', OID: ' + oid + ", OID name: " + oidname);
 
                                        // link it to the point we need to set the value to
-                                       service.snmp_oids_index[oid] = service.request.charts[c].dimensions[d];
+                                       service.snmp_oids_index[oid] = {
+                                               type: 'value',
+                                               link: service.request.charts[c].dimensions[d]
+                                       };
+
+                                       if(typeof oidname !== 'undefined')
+                                               service.snmp_oids_index[oidname] = {
+                                                       type: 'name',
+                                                       link: service.request.charts[c].dimensions[d]
+                                               };
 
                                        // and set the value to null
                                        service.request.charts[c].dimensions[d].value = null;
                                }
                        }
+
                        if(netdata.options.DEBUG === true)
                                netdata.debug(service.module.name + ': ' + service.name + ': indexed ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids_index));
 
@@ -157,77 +182,127 @@ netdata.processors.snmp = {
 
                        if(netdata.options.DEBUG === true)
                                netdata.debug(service.module.name + ': ' + service.name + ': final list of ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids));
+
+                       service.snmp_oids_cleaned = 0;
                }
+               else if(service.snmp_oids_cleaned === 0) {
+                       service.snmp_oids_cleaned = 1;
 
-               if(service.snmp_oids.length === 0) {
-                       // no OIDs found for this service
+                       // the second time, keep only values
+                       service.snmp_oids = new Array();
+                       for(var o in service.snmp_oids_index)
+                               if(service.snmp_oids_index[o].type === 'value')
+                                       service.snmp_oids.push(o);
+               }
+       },
 
-                       if(netdata.options.DEBUG === true)
-                               service.error('no OIDs to process.');
+       getdata: function(service, index, ok, failed, callback) {
+               var that = this;
 
-                       callback(null);
+               if(index >= service.snmp_oids.length) {
+                       callback((ok > 0)?{ ok: ok, failed: failed }:null);
                        return;
                }
 
-               if(typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
-                       // no SNMP session has been created for this service
-                       // the SNMP session is just the initialization of NET-SNMP
-
-                       if(netdata.options.DEBUG === true)
-                               netdata.debug(service.module.name + ': ' + service.name + ': opening ' + this.name + ' session on ' + service.request.hostname + ' community ' + service.request.community + ' options ' + netdata.stringify(service.request.options));
-
-                       // create the SNMP session
-                       service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);
-
-                       if(netdata.options.DEBUG === true)
-                               netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
-
-                       // if we later need traps, this is how to do it:
-                       //service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
-                       //      if(error) console.error('trap error: ' + netdata.stringify(error));
-                       //});
+               var slice;
+               if(service.snmp_oids.length <= service.request.max_request_size) {
+                       slice = service.snmp_oids;
+                       index = service.snmp_oids.length;
+               }
+               else if(service.snmp_oids.length - index <= service.request.max_request_size) {
+                       slice = service.snmp_oids.slice(index, service.snmp_oids.length);
+                       index = service.snmp_oids.length;
+               }
+               else {
+                       slice = service.snmp_oids.slice(index, index + service.request.max_request_size);
+                       index += service.request.max_request_size;
                }
 
-               // do it, get the SNMP values for the sessions we need
-               service.snmp_session.get(service.snmp_oids, function(error, varbinds) {
-                       var ok = 0, failed = 0;
+               if(netdata.options.DEBUG === true)
+                       netdata.debug(service.module.name + ': ' + service.name + ': making ' + slice.length + ' entries request, max is: ' + service.request.max_request_size);
 
+               service.snmp_session.get(slice, function(error, varbinds) {
                        if(error) {
                                service.error('Received error = ' + netdata.stringify(error) + ' varbinds = ' + netdata.stringify(varbinds));
 
                                // make all values null
-                               var len = service.snmp_oids.length;
+                               var len = slice.length;
                                while(len--)
-                                       service.snmp_oids_index[service.snmp_oids[len]].value = null;
+                                       service.snmp_oids_index[slice[len]].value = null;
                        }
                        else {
                                if(netdata.options.DEBUG === true)
                                        netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
 
                                for(var i = 0; i < varbinds.length; i++) {
+                                       var value = null;
+
                                        if(net_snmp.isVarbindError(varbinds[i])) {
                                                if(netdata.options.DEBUG === true)
                                                        netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
 
                                                service.error('OID ' + varbinds[i].oid + ' gave error: ' + snmp.varbindError(varbinds[i]));
-                                               service.snmp_oids_index[varbinds[i].oid].value = null;
+                                               value = null;
                                                failed++;
                                        }
                                        else {
                                                if(netdata.options.DEBUG === true)
                                                        netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + " = " + varbinds[i].value);
 
-                                               service.snmp_oids_index[varbinds[i].oid].value = varbinds[i].value;
+                                               value = varbinds[i].value;
                                                ok++;
                                        }
+
+                                       if(value !== null) {
+                                               switch(service.snmp_oids_index[varbinds[i].oid].type) {
+                                                       case 'title': service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value; break;
+                                                       case 'name' : service.snmp_oids_index[varbinds[i].oid].link.name = value; break;
+                                                       case 'value': service.snmp_oids_index[varbinds[i].oid].link.value = value; break;
+                                               }
+                                       }
                                }
 
                                if(netdata.options.DEBUG === true)
                                        netdata.debug(service.module.name + ': ' + service.name + ': finished ' + service.module.name + ' with ' + ok + ' successful and ' + failed + ' failed values');
                        }
-                       
-                       callback((ok > 0)?{ ok: ok, failed: failed }:null);
+                       that.getdata(service, index, ok, failed, callback);
                });
+       },
+
+       process: function(service, callback) {
+               this.prepare(service);
+
+               if(service.snmp_oids.length === 0) {
+                       // no OIDs found for this service
+
+                       if(netdata.options.DEBUG === true)
+                               service.error('no OIDs to process.');
+
+                       callback(null);
+                       return;
+               }
+
+               if(typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
+                       // no SNMP session has been created for this service
+                       // the SNMP session is just the initialization of NET-SNMP
+
+                       if(netdata.options.DEBUG === true)
+                               netdata.debug(service.module.name + ': ' + service.name + ': opening ' + this.name + ' session on ' + service.request.hostname + ' community ' + service.request.community + ' options ' + netdata.stringify(service.request.options));
+
+                       // create the SNMP session
+                       service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);
+
+                       if(netdata.options.DEBUG === true)
+                               netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
+
+                       // if we later need traps, this is how to do it:
+                       //service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
+                       //      if(error) console.error('trap error: ' + netdata.stringify(error));
+                       //});
+               }
+
+               // do it, get the SNMP values for the sessions we need
+               this.getdata(service, 0, 0, 0, callback);
        }
 };
 
@@ -298,9 +373,16 @@ var snmp = {
                                        var id = c + from.toString();
                                        var chart = extend(true, {}, service.request.charts[c]);
                                        chart.title += from.toString();
+                                       
+                                       if(typeof chart.titleoid !== 'undefined')
+                                               chart.titleoid += from.toString();
+
                                        chart.priority = prio++;
                                        for(var d in chart.dimensions) {
                                                chart.dimensions[d].oid += from.toString();
+
+                                               if(typeof chart.dimensions[d].oidname !== 'undefined')
+                                                       chart.dimensions[d].oidname += from.toString();
                                        }
                                        service.request.charts[id] = chart;
                                        from++;
@@ -316,12 +398,18 @@ var snmp = {
        configure: function(config) {
                var added = 0;
 
+               if(typeof config.max_request_size === 'undefined')
+                       config.max_request_size = 50;
+
                if(typeof(config.servers) !== 'undefined') {
                        var len = config.servers.length;
                        while(len--) {
                                if(typeof config.servers[len].update_every === 'undefined')
                                        config.servers[len].update_every = this.update_every;
 
+                               if(typeof config.servers[len].max_request_size === 'undefined')
+                                       config.servers[len].max_request_size = config.max_request_size;
+
                                this.serviceExecute(config.servers[len]);
                                added++;
                        }