node.d/snmp.node.js: format code (#7816)
Ilya Mashchenko committed
Jan 23, 2020 at 01:42 UTC
920019f4a77977fcb69a4abeedf76dc62e055ad9
1 file changed
+94
-95
collectors/node.d.plugin/snmp/snmp.node.js
+94
-95
@@ -117,53 +117,53 @@ var net_snmp = require('net-snmp');
117
var extend = require('extend');
118
var netdata = require('netdata');
119
120
-if(netdata.options.DEBUG === true) netdata.debug('loaded', __filename, ' plugin');
120
+if (netdata.options.DEBUG === true) netdata.debug('loaded', __filename, ' plugin');
121
122
netdata.processors.snmp = {
123
name: 'snmp',
124
125
- fixoid: function(oid) {
126
- if(typeof oid !== 'string')
125
+ fixoid: function (oid) {
126
+ if (typeof oid !== 'string')
127
return oid;
128
129
- if(oid.charAt(0) === '.')
129
+ if (oid.charAt(0) === '.')
130
return oid.substring(1, oid.length);
131
132
return oid;
133
},
134
135
- prepare: function(service) {
135
+ prepare: function (service) {
136
var __DEBUG = netdata.options.DEBUG;
137
138
- if(typeof service.snmp_oids === 'undefined' || service.snmp_oids === null || service.snmp_oids.length === 0) {
138
+ if (typeof service.snmp_oids === 'undefined' || service.snmp_oids === null || service.snmp_oids.length === 0) {
139
// this is the first time we see this service
140
141
- if(__DEBUG === true)
141
+ if (__DEBUG === true)
142
netdata.debug(service.module.name + ': ' + service.name + ': preparing ' + this.name + ' OIDs');
143
144
// build an index of all OIDs
145
service.snmp_oids_index = {};
146
var chart_keys = Object.keys(service.request.charts);
147
var chart_keys_len = chart_keys.length;
148
- while(chart_keys_len--) {
148
+ while (chart_keys_len--) {
149
var c = chart_keys[chart_keys_len];
150
var chart = service.request.charts[c];
151
152
// for each chart
153
154
- if(__DEBUG === true)
154
+ if (__DEBUG === true)
155
netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c);
156
157
- if(typeof chart.titleoid !== 'undefined') {
158
- service.snmp_oids_index[this.fixoid(chart.titleoid)] = {
159
- type: 'title',
160
- link: chart
161
- };
162
- }
157
+ if (typeof chart.titleoid !== 'undefined') {
158
+ service.snmp_oids_index[this.fixoid(chart.titleoid)] = {
159
+ type: 'title',
160
+ link: chart
161
+ };
162
+ }
163
164
var dim_keys = Object.keys(chart.dimensions);
165
var dim_keys_len = dim_keys.length;
166
- while(dim_keys_len--) {
166
+ while (dim_keys_len--) {
167
var d = dim_keys[dim_keys_len];
168
var dim = chart.dimensions[d];
169
@@ -172,7 +172,7 @@ netdata.processors.snmp = {
172
var oid = this.fixoid(dim.oid);
173
var oidname = this.fixoid(dim.oidname);
174
175
- if(__DEBUG === true)
175
+ if (__DEBUG === true)
176
netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c + ', dimension: ' + d + ', OID: ' + oid + ", OID name: " + oidname);
177
178
// link it to the point we need to set the value to
@@ -181,7 +181,7 @@ netdata.processors.snmp = {
181
link: dim
182
};
183
184
- if(typeof oidname !== 'undefined')
184
+ if (typeof oidname !== 'undefined')
185
service.snmp_oids_index[oidname] = {
186
type: 'name',
187
link: dim
@@ -192,18 +192,17 @@ netdata.processors.snmp = {
192
}
193
}
194
195
- if(__DEBUG === true)
195
+ if (__DEBUG === true)
196
netdata.debug(service.module.name + ': ' + service.name + ': indexed ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids_index));
197
198
// now create the array of OIDs needed by net-snmp
199
service.snmp_oids = Object.keys(service.snmp_oids_index);
200
201
- if(__DEBUG === true)
201
+ if (__DEBUG === true)
202
netdata.debug(service.module.name + ': ' + service.name + ': final list of ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids));
203
204
service.snmp_oids_cleaned = 0;
205
- }
206
- else if(service.snmp_oids_cleaned === 0) {
205
+ } else if (service.snmp_oids_cleaned === 0) {
206
service.snmp_oids_cleaned = 1;
207
208
// the second time, keep only values
@@ -211,87 +210,82 @@ netdata.processors.snmp = {
210
service.snmp_oids = new Array();
211
var oid_keys = Object.keys(service.snmp_oids_index);
212
var oid_keys_len = oid_keys.length;
214
- while(oid_keys_len--) {
213
+ while (oid_keys_len--) {
214
if (service.snmp_oids_index[oid_keys[oid_keys_len]].type === 'value')
215
service.snmp_oids.push(oid_keys[oid_keys_len]);
216
}
217
}
218
},
219
221
- getdata: function(service, index, ok, failed, callback) {
220
+ getdata: function (service, index, ok, failed, callback) {
221
var __DEBUG = netdata.options.DEBUG;
222
var that = this;
223
225
- if(index >= service.snmp_oids.length) {
226
- callback((ok > 0)?{ ok: ok, failed: failed }:null);
224
+ if (index >= service.snmp_oids.length) {
225
+ callback((ok > 0) ? {ok: ok, failed: failed} : null);
226
return;
227
}
228
229
var slice;
231
- if(service.snmp_oids.length <= service.request.max_request_size) {
230
+ if (service.snmp_oids.length <= service.request.max_request_size) {
231
slice = service.snmp_oids;
232
index = service.snmp_oids.length;
234
- }
235
- else if(service.snmp_oids.length - index <= service.request.max_request_size) {
233
+ } else if (service.snmp_oids.length - index <= service.request.max_request_size) {
234
slice = service.snmp_oids.slice(index, service.snmp_oids.length);
235
index = service.snmp_oids.length;
238
- }
239
- else {
236
+ } else {
237
slice = service.snmp_oids.slice(index, index + service.request.max_request_size);
238
index += service.request.max_request_size;
239
}
240
244
- if(__DEBUG === true)
241
+ if (__DEBUG === true)
242
netdata.debug(service.module.name + ': ' + service.name + ': making ' + slice.length + ' entries request, max is: ' + service.request.max_request_size);
243
247
- service.snmp_session.get(slice, function(error, varbinds) {
248
- if(error) {
244
+ service.snmp_session.get(slice, function (error, varbinds) {
245
+ if (error) {
246
service.error('Received error = ' + netdata.stringify(error) + ' varbinds = ' + netdata.stringify(varbinds));
247
248
// make all values null
249
var len = slice.length;
253
- while(len--)
250
+ while (len--)
251
service.snmp_oids_index[slice[len]].value = null;
255
- }
256
- else {
257
- if(__DEBUG === true)
252
+ } else {
253
+ if (__DEBUG === true)
254
netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
255
256
var varbinds_len = varbinds.length;
261
- for(var i = 0; i < varbinds_len ; i++) {
257
+ for (var i = 0; i < varbinds_len; i++) {
258
var value = null;
259
264
- if(net_snmp.isVarbindError(varbinds[i])) {
265
- if(__DEBUG === true)
260
+ if (net_snmp.isVarbindError(varbinds[i])) {
261
+ if (__DEBUG === true)
262
netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
263
264
service.error('OID ' + varbinds[i].oid + ' gave error: ' + net_snmp.varbindError(varbinds[i]));
265
value = null;
266
failed++;
271
- }
272
- else {
267
+ } else {
268
// test fom Counter64
269
// varbinds[i].type = net_snmp.ObjectType.Counter64;
270
// varbinds[i].value = new Buffer([0x34, 0x49, 0x2e, 0xdc, 0xd1]);
271
277
- switch(varbinds[i].type) {
272
+ switch (varbinds[i].type) {
273
case net_snmp.ObjectType.OctetString:
274
if (service.snmp_oids_index[varbinds[i].oid].type !== 'title' && service.snmp_oids_index[varbinds[i].oid].type !== 'name') {
275
// parse floating point values, exposed as strings
276
value = parseFloat(varbinds[i].value) * 1000;
282
- if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as float in string)");
283
- }
284
- else {
277
+ if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof (varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as float in string)");
278
+ } else {
279
// just use the string
280
value = varbinds[i].value;
287
- if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as string)");
281
+ if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof (varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as string)");
282
}
283
break;
284
285
case net_snmp.ObjectType.Counter64:
286
// copy the buffer
287
value = '0x' + varbinds[i].value.toString('hex');
294
- if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as buffer)");
288
+ if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof (varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as buffer)");
289
break;
290
291
case net_snmp.ObjectType.Integer:
@@ -299,45 +293,51 @@ netdata.processors.snmp = {
293
case net_snmp.ObjectType.Gauge:
294
default:
295
value = varbinds[i].value;
302
- if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as number)");
296
+ if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof (varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as number)");
297
break;
298
}
299
300
ok++;
301
}
302
309
- if(value !== null) {
310
- switch(service.snmp_oids_index[varbinds[i].oid].type) {
311
- case 'title': service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value; break;
312
- case 'name' : service.snmp_oids_index[varbinds[i].oid].link.name = value.toString().replace(/\W/g, '_'); break;
313
- case 'value': service.snmp_oids_index[varbinds[i].oid].link.value = value; break;
303
+ if (value !== null) {
304
+ switch (service.snmp_oids_index[varbinds[i].oid].type) {
305
+ case 'title':
306
+ service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value;
307
+ break;
308
+ case 'name' :
309
+ service.snmp_oids_index[varbinds[i].oid].link.name = value.toString().replace(/\W/g, '_');
310
+ break;
311
+ case 'value':
312
+ service.snmp_oids_index[varbinds[i].oid].link.value = value;
313
+ break;
314
}
315
}
316
}
317
318
- if(__DEBUG === true)
318
+ if (__DEBUG === true)
319
netdata.debug(service.module.name + ': ' + service.name + ': finished ' + service.module.name + ' with ' + ok + ' successful and ' + failed + ' failed values');
320
}
321
that.getdata(service, index, ok, failed, callback);
322
});
323
},
324
325
- process: function(service, callback) {
325
+ process: function (service, callback) {
326
var __DEBUG = netdata.options.DEBUG;
327
328
this.prepare(service);
329
330
- if(service.snmp_oids.length === 0) {
330
+ if (service.snmp_oids.length === 0) {
331
// no OIDs found for this service
332
333
- if(__DEBUG === true)
333
+ if (__DEBUG === true)
334
service.error('no OIDs to process.');
335
336
callback(null);
337
return;
338
}
339
340
- if(typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
340
+ if (typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
341
// no SNMP session has been created for this service
342
// the SNMP session is just the initialization of NET-SNMP
343
@@ -345,21 +345,21 @@ netdata.processors.snmp = {
345
? service.request.options.version
346
: net_snmp.Version1;
347
348
- if(snmp_version === net_snmp.Version3) {
349
- if(__DEBUG === true)
348
+ if (snmp_version === net_snmp.Version3) {
349
+ if (__DEBUG === true)
350
netdata.debug(service.module.name + ': ' + service.name + ': opening ' + this.name + ' session on ' + service.request.hostname + ' user ' + service.request.user + ' options ' + netdata.stringify(service.request.options));
351
352
// create the SNMP session
353
- service.snmp_session = net_snmp.createV3Session (service.request.hostname, service.request.user, service.request.options);
353
+ service.snmp_session = net_snmp.createV3Session(service.request.hostname, service.request.user, service.request.options);
354
} else {
355
- if(__DEBUG === true)
355
+ if (__DEBUG === true)
356
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));
357
358
// create the SNMP session
359
- service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);
359
+ service.snmp_session = net_snmp.createSession(service.request.hostname, service.request.community, service.request.options);
360
}
361
362
- if(__DEBUG === true)
362
+ if (__DEBUG === true)
363
netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
364
365
// if we later need traps, this is how to do it:
@@ -381,18 +381,18 @@ var snmp = {
381
382
charts: {},
383
384
- processResponse: function(service, data) {
385
- if(data !== null) {
386
- if(service.added !== true)
384
+ processResponse: function (service, data) {
385
+ if (data !== null) {
386
+ if (service.added !== true)
387
service.commit();
388
389
var chart_keys = Object.keys(service.request.charts);
390
var chart_keys_len = chart_keys.length;
391
- for(var i = 0; i < chart_keys_len; i++) {
391
+ for (var i = 0; i < chart_keys_len; i++) {
392
var c = chart_keys[i];
393
394
var chart = snmp.charts[c];
395
- if(typeof chart === 'undefined') {
395
+ if (typeof chart === 'undefined') {
396
chart = service.chart(c, service.request.charts[c]);
397
snmp.charts[c] = chart;
398
}
@@ -402,11 +402,11 @@ var snmp = {
402
var dimensions = service.request.charts[c].dimensions;
403
var dim_keys = Object.keys(dimensions);
404
var dim_keys_len = dim_keys.length;
405
- for(var j = 0; j < dim_keys_len ; j++) {
405
+ for (var j = 0; j < dim_keys_len; j++) {
406
var d = dim_keys[j];
407
408
if (dimensions[d].value !== null) {
409
- if(typeof dimensions[d].offset === 'number' && typeof dimensions[d].value === 'number')
409
+ if (typeof dimensions[d].offset === 'number' && typeof dimensions[d].value === 'number')
410
service.set(d, dimensions[d].value + dimensions[d].offset);
411
else
412
service.set(d, dimensions[d].value);
@@ -422,10 +422,10 @@ var snmp = {
422
// this function is called only from this module
423
// its purpose is to prepare the request and call
424
// netdata.serviceExecute()
425
- serviceExecute: function(conf) {
425
+ serviceExecute: function (conf) {
426
var __DEBUG = netdata.options.DEBUG;
427
428
- if(__DEBUG === true)
428
+ if (__DEBUG === true)
429
netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', update_every: ' + conf.update_every);
430
431
var service = netdata.service({
@@ -439,41 +439,41 @@ var snmp = {
439
// multiply the charts, if required
440
var chart_keys = Object.keys(service.request.charts);
441
var chart_keys_len = chart_keys.length;
442
- for( var i = 0; i < chart_keys_len ; i++ ) {
442
+ for (var i = 0; i < chart_keys_len; i++) {
443
var c = chart_keys[i];
444
var service_request_chart = service.request.charts[c];
445
446
- if(__DEBUG === true)
446
+ if (__DEBUG === true)
447
netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', examining chart: ' + c);
448
449
- if(typeof service_request_chart.update_every === 'undefined')
449
+ if (typeof service_request_chart.update_every === 'undefined')
450
service_request_chart.update_every = service.update_every;
451
452
- if(typeof service_request_chart.multiply_range !== 'undefined') {
452
+ if (typeof service_request_chart.multiply_range !== 'undefined') {
453
var from = service_request_chart.multiply_range[0];
454
var to = service_request_chart.multiply_range[1];
455
var prio = service_request_chart.priority || 1;
456
457
- if(prio < snmp.base_priority) prio += snmp.base_priority;
457
+ if (prio < snmp.base_priority) prio += snmp.base_priority;
458
459
- while(from <= to) {
459
+ while (from <= to) {
460
var id = c + from.toString();
461
var chart = extend(true, {}, service_request_chart);
462
chart.title += from.toString();
463
464
- if(typeof chart.titleoid !== 'undefined')
464
+ if (typeof chart.titleoid !== 'undefined')
465
chart.titleoid += from.toString();
466
467
chart.priority = prio++;
468
469
var dim_keys = Object.keys(chart.dimensions);
470
var dim_keys_len = dim_keys.length;
471
- for(var j = 0; j < dim_keys_len ; j++) {
471
+ for (var j = 0; j < dim_keys_len; j++) {
472
var d = dim_keys[j];
473
474
chart.dimensions[d].oid += from.toString();
475
476
- if(typeof chart.dimensions[d].oidname !== 'undefined')
476
+ if (typeof chart.dimensions[d].oidname !== 'undefined')
477
chart.dimensions[d].oidname += from.toString();
478
}
479
service.request.charts[id] = chart;
@@ -481,9 +481,8 @@ var snmp = {
481
}
482
483
delete service.request.charts[c];
484
- }
485
- else {
486
- if(service.request.charts[c].priority < snmp.base_priority)
484
+ } else {
485
+ if (service.request.charts[c].priority < snmp.base_priority)
486
service.request.charts[c].priority += snmp.base_priority;
487
}
488
}
@@ -491,19 +490,19 @@ var snmp = {
490
service.execute(this.processResponse);
491
},
492
494
- configure: function(config) {
493
+ configure: function (config) {
494
var added = 0;
495
497
- if(typeof config.max_request_size === 'undefined')
496
+ if (typeof config.max_request_size === 'undefined')
497
config.max_request_size = 50;
498
500
- if(typeof(config.servers) !== 'undefined') {
499
+ if (typeof (config.servers) !== 'undefined') {
500
var len = config.servers.length;
502
- while(len--) {
503
- if(typeof config.servers[len].update_every === 'undefined')
501
+ while (len--) {
502
+ if (typeof config.servers[len].update_every === 'undefined')
503
config.servers[len].update_every = this.update_every;
504
506
- if(typeof config.servers[len].max_request_size === 'undefined')
505
+ if (typeof config.servers[len].max_request_size === 'undefined')
506
config.servers[len].max_request_size = config.max_request_size;
507
508
this.serviceExecute(config.servers[len]);
@@ -517,8 +516,8 @@ var snmp = {
516
// module.update()
517
// this is called repeatidly to collect data, by calling
518
// service.execute()
520
- update: function(service, callback) {
521
- service.execute(function(serv, data) {
519
+ update: function (service, callback) {
520
+ service.execute(function (serv, data) {
521
service.module.processResponse(serv, data);
522
callback();
523
});