3 // This program will connect to one or more SNMP Agents
5 // example configuration in /etc/netdata/snmp.conf
8 "enable_autodetect": false,
10 "max_request_size": 50,
13 "hostname": "10.11.12.8",
14 "community": "public",
16 "max_request_size": 50,
17 "options": { "timeout": 10000 },
19 "snmp_switch.bandwidth_port1": {
20 "title": "Switch Bandwidth for port 1",
21 "units": "kilobits/s",
26 "oid": ".1.3.6.1.2.1.2.2.1.10.1",
27 "algorithm": "incremental",
32 "oid": ".1.3.6.1.2.1.2.2.1.16.1",
33 "algorithm": "incremental",
39 "snmp_switch.bandwidth_port2": {
40 "title": "Switch Bandwidth for port 2",
41 "units": "kilobits/s",
46 "oid": ".1.3.6.1.2.1.2.2.1.10.2",
47 "algorithm": "incremental",
52 "oid": ".1.3.6.1.2.1.2.2.1.16.2",
53 "algorithm": "incremental",
65 // You can also give ranges of charts like the following.
66 // This will append 1-24 to id, title, oid (on each dimension)
67 // so that 24 charts will be created.
70 "enable_autodetect": false,
72 "max_request_size": 50,
75 "hostname": "10.11.12.8",
76 "community": "public",
78 "max_request_size": 50,
79 "options": { "timeout": 20000 },
81 "snmp_switch.bandwidth_port": {
82 "title": "Switch Bandwidth for port ",
83 "units": "kilobits/s",
86 "multiply_range": [ 1, 24 ],
89 "oid": ".1.3.6.1.2.1.2.2.1.10.",
90 "algorithm": "incremental",
95 "oid": ".1.3.6.1.2.1.2.2.1.16.",
96 "algorithm": "incremental",
108 var net_snmp = require('net-snmp');
109 var extend = require('extend');
110 var netdata = require('netdata');
112 if(netdata.options.DEBUG === true) netdata.debug('loaded ' + __filename + ' plugin');
114 netdata.processors.snmp = {
117 fixoid: function(oid) {
118 if(typeof oid !== 'string')
121 if(oid.charAt(0) === '.')
122 return oid.substring(1, oid.length);
127 prepare: function(service) {
128 if(typeof service.snmp_oids === 'undefined' || service.snmp_oids === null || service.snmp_oids.length === 0) {
129 // this is the first time we see this service
131 if(netdata.options.DEBUG === true)
132 netdata.debug(service.module.name + ': ' + service.name + ': preparing ' + this.name + ' OIDs');
134 // build an index of all OIDs
135 service.snmp_oids_index = {};
136 for(var c in service.request.charts) {
139 if(netdata.options.DEBUG === true)
140 netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c);
142 if(typeof service.request.charts[c].titleoid !== 'undefined') {
143 service.snmp_oids_index[this.fixoid(service.request.charts[c].titleoid)] = {
145 link: service.request.charts[c]
149 for(var d in service.request.charts[c].dimensions) {
150 // for each dimension in the chart
152 var oid = this.fixoid(service.request.charts[c].dimensions[d].oid);
153 var oidname = this.fixoid(service.request.charts[c].dimensions[d].oidname);
155 if(netdata.options.DEBUG === true)
156 netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c + ', dimension: ' + d + ', OID: ' + oid + ", OID name: " + oidname);
158 // link it to the point we need to set the value to
159 service.snmp_oids_index[oid] = {
161 link: service.request.charts[c].dimensions[d]
164 if(typeof oidname !== 'undefined')
165 service.snmp_oids_index[oidname] = {
167 link: service.request.charts[c].dimensions[d]
170 // and set the value to null
171 service.request.charts[c].dimensions[d].value = null;
175 if(netdata.options.DEBUG === true)
176 netdata.debug(service.module.name + ': ' + service.name + ': indexed ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids_index));
178 // now create the array of OIDs needed by net-snmp
179 service.snmp_oids = new Array();
180 for(var o in service.snmp_oids_index)
181 service.snmp_oids.push(o);
183 if(netdata.options.DEBUG === true)
184 netdata.debug(service.module.name + ': ' + service.name + ': final list of ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids));
186 service.snmp_oids_cleaned = 0;
188 else if(service.snmp_oids_cleaned === 0) {
189 service.snmp_oids_cleaned = 1;
191 // the second time, keep only values
192 service.snmp_oids = new Array();
193 for(var o in service.snmp_oids_index)
194 if(service.snmp_oids_index[o].type === 'value')
195 service.snmp_oids.push(o);
199 getdata: function(service, index, ok, failed, callback) {
202 if(index >= service.snmp_oids.length) {
203 callback((ok > 0)?{ ok: ok, failed: failed }:null);
208 if(service.snmp_oids.length <= service.request.max_request_size) {
209 slice = service.snmp_oids;
210 index = service.snmp_oids.length;
212 else if(service.snmp_oids.length - index <= service.request.max_request_size) {
213 slice = service.snmp_oids.slice(index, service.snmp_oids.length);
214 index = service.snmp_oids.length;
217 slice = service.snmp_oids.slice(index, index + service.request.max_request_size);
218 index += service.request.max_request_size;
221 if(netdata.options.DEBUG === true)
222 netdata.debug(service.module.name + ': ' + service.name + ': making ' + slice.length + ' entries request, max is: ' + service.request.max_request_size);
224 service.snmp_session.get(slice, function(error, varbinds) {
226 service.error('Received error = ' + netdata.stringify(error) + ' varbinds = ' + netdata.stringify(varbinds));
228 // make all values null
229 var len = slice.length;
231 service.snmp_oids_index[slice[len]].value = null;
234 if(netdata.options.DEBUG === true)
235 netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
237 for(var i = 0; i < varbinds.length; i++) {
240 if(net_snmp.isVarbindError(varbinds[i])) {
241 if(netdata.options.DEBUG === true)
242 netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
244 service.error('OID ' + varbinds[i].oid + ' gave error: ' + snmp.varbindError(varbinds[i]));
249 if(netdata.options.DEBUG === true)
250 netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + " = " + varbinds[i].value);
252 value = varbinds[i].value;
257 switch(service.snmp_oids_index[varbinds[i].oid].type) {
258 case 'title': service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value; break;
259 case 'name' : service.snmp_oids_index[varbinds[i].oid].link.name = value; break;
260 case 'value': service.snmp_oids_index[varbinds[i].oid].link.value = value; break;
265 if(netdata.options.DEBUG === true)
266 netdata.debug(service.module.name + ': ' + service.name + ': finished ' + service.module.name + ' with ' + ok + ' successful and ' + failed + ' failed values');
268 that.getdata(service, index, ok, failed, callback);
272 process: function(service, callback) {
273 this.prepare(service);
275 if(service.snmp_oids.length === 0) {
276 // no OIDs found for this service
278 if(netdata.options.DEBUG === true)
279 service.error('no OIDs to process.');
285 if(typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
286 // no SNMP session has been created for this service
287 // the SNMP session is just the initialization of NET-SNMP
289 if(netdata.options.DEBUG === true)
290 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));
292 // create the SNMP session
293 service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);
295 if(netdata.options.DEBUG === true)
296 netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
298 // if we later need traps, this is how to do it:
299 //service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
300 // if(error) console.error('trap error: ' + netdata.stringify(error));
304 // do it, get the SNMP values for the sessions we need
305 this.getdata(service, 0, 0, 0, callback);
311 enable_autodetect: true,
313 base_priority: 50000,
317 processResponse: function(service, data) {
319 if(service.added !== true)
322 for(var c in service.request.charts) {
323 var chart = snmp.charts[c];
325 if(typeof chart === 'undefined') {
326 chart = service.chart(c, service.request.charts[c]);
327 snmp.charts[c] = chart;
330 service.begin(chart);
332 for( var d in service.request.charts[c].dimensions )
333 if(service.request.charts[c].dimensions[d].value !== null)
334 service.set(d, service.request.charts[c].dimensions[d].value);
341 // module.serviceExecute()
342 // this function is called only from this module
343 // its purpose is to prepare the request and call
344 // netdata.serviceExecute()
345 serviceExecute: function(conf) {
346 if(netdata.options.DEBUG === true)
347 netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', update_every: ' + conf.update_every);
349 var service = netdata.service({
352 update_every: conf.update_every,
354 processor: netdata.processors.snmp
357 // multiply the charts, if required
358 for(var c in service.request.charts) {
359 if(netdata.options.DEBUG === true)
360 netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', examining chart: ' + c);
362 if(typeof service.request.charts[c].update_every === 'undefined')
363 service.request.charts[c].update_every = service.update_every;
365 if(typeof service.request.charts[c].multiply_range !== 'undefined') {
366 var from = service.request.charts[c].multiply_range[0];
367 var to = service.request.charts[c].multiply_range[1];
368 var prio = service.request.charts[c].priority || 1;
370 if(prio < snmp.base_priority) prio += snmp.base_priority;
373 var id = c + from.toString();
374 var chart = extend(true, {}, service.request.charts[c]);
375 chart.title += from.toString();
377 if(typeof chart.titleoid !== 'undefined')
378 chart.titleoid += from.toString();
380 chart.priority = prio++;
381 for(var d in chart.dimensions) {
382 chart.dimensions[d].oid += from.toString();
384 if(typeof chart.dimensions[d].oidname !== 'undefined')
385 chart.dimensions[d].oidname += from.toString();
387 service.request.charts[id] = chart;
391 delete service.request.charts[c];
394 if(service.request.charts[c].priority < snmp.base_priority)
395 service.request.charts[c].priority += snmp.base_priority;
399 service.execute(this.processResponse);
402 configure: function(config) {
405 if(typeof config.max_request_size === 'undefined')
406 config.max_request_size = 50;
408 if(typeof(config.servers) !== 'undefined') {
409 var len = config.servers.length;
411 if(typeof config.servers[len].update_every === 'undefined')
412 config.servers[len].update_every = this.update_every;
414 if(typeof config.servers[len].max_request_size === 'undefined')
415 config.servers[len].max_request_size = config.max_request_size;
417 this.serviceExecute(config.servers[len]);
426 // this is called repeatidly to collect data, by calling
428 update: function(service, callback) {
429 service.execute(function(serv, data) {
430 service.module.processResponse(serv, data);
436 module.exports = snmp;