summaryrefslogtreecommitdiffstats
path: root/node.d/snmp.node.js
blob: 21615f6237e9e9564a3f309dbccf4443ef9db28d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
'use strict';

// This program will connect to one or more SNMP Agents

// example configuration in /etc/netdata/snmp.conf
/*
{
	"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": {
					"title": "Switch Bandwidth for port 1",
					"units": "kilobits/s",
					"type": "area",
					"priority": 1,
					"dimensions": {
						"in": {
							"oid": ".1.3.6.1.2.1.2.2.1.10.1",
							"algorithm": "incremental",
							"multiplier": 8,
							"divisor": 1024
						},
						"out": {
							"oid": ".1.3.6.1.2.1.2.2.1.16.1",
							"algorithm": "incremental",
							"multiplier": -8,
							"divisor": 1024
						}
					}
				},
				"snmp_switch.bandwidth_port2": {
					"title": "Switch Bandwidth for port 2",
					"units": "kilobits/s",
					"type": "area",
					"priority": 1,
					"dimensions": {
						"in": {
							"oid": ".1.3.6.1.2.1.2.2.1.10.2",
							"algorithm": "incremental",
							"multiplier": 8,
							"divisor": 1024
						},
						"out": {
							"oid": ".1.3.6.1.2.1.2.2.1.16.2",
							"algorithm": "incremental",
							"multiplier": -8,
							"divisor": 1024
						}
					}
				}
			}
		}
	]
}
*/

// You can also give ranges of charts like the following.
// This will append 1-24 to id, title, oid (on each dimension)
// so that 24 charts will be created.
/*
{
	"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": {
					"title": "Switch Bandwidth for port ",
					"units": "kilobits/s",
					"type": "area",
					"priority": 1,
					"multiply_range": [ 1, 24 ],
					"dimensions": {
						"in": {
							"oid": ".1.3.6.1.2.1.2.2.1.10.",
							"algorithm": "incremental",
							"multiplier": 8,
							"divisor": 1024
						},
						"out": {
							"oid": ".1.3.6.1.2.1.2.2.1.16.",
							"algorithm": "incremental",
							"multiplier": -8,
							"divisor": 1024
						}
					}
				}
			}
		}
	]
}
*/

var net_snmp = require('net-snmp');
var extend = require('extend');
var netdata = require('netdata');

if(netdata.options.DEBUG === true) netdata.debug('loaded ' + __filename + ' plugin');

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;
	},

	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

			if(netdata.options.DEBUG === true)
				netdata.debug(service.module.name + ': ' + service.name + ': preparing ' + this.name + ' OIDs');

			// build an index of all OIDs
			service.snmp_oids_index = {};
			for(var c in service.request.charts) {
				// for each chart

				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 + ", OID name: " + oidname);

					// link it to the point we need to set the value to
					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));

			// now create the array of OIDs needed by net-snmp
			service.snmp_oids = new Array();
			for(var o in service.snmp_oids_index)
				service.snmp_oids.push(o);

			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;

			// 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);
		}
	},

	getdata: function(service, index, ok, failed, callback) {
		var that = this;

		if(index >= service.snmp_oids.length) {
			callback((ok > 0)?{ ok: ok, failed: failed }:null);
			return;
		}

		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;
		}

		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 = slice.length;
				while(len--)
					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]));
						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);

						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');
			}
			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);
	}
};

var snmp = {
	name: __filename,
	enable_autodetect: true,
	update_every: 1,
	base_priority: 50000,

	charts: {},

	processResponse: function(service, data) {
		if(data !== null) {
			if(service.added !== true)
				service.commit();

			for(var c in service.request.charts) {
				var chart = snmp.charts[c];

				if(typeof chart === 'undefined') {
					chart = service.chart(c, service.request.charts[c]);
					snmp.charts[c] = chart;
				}

				service.begin(chart);
				
				for( var d in service.request.charts[c].dimensions )
					if(service.request.charts[c].dimensions[d].value !== null)
						service.set(d, service.request.charts[c].dimensions[d].value);

				service.end();
			}
		}
	},

	// module.serviceExecute()
	// this function is called only from this module
	// its purpose is to prepare the request and call
	// netdata.serviceExecute()
	serviceExecute: function(conf) {
		if(netdata.options.DEBUG === true)
			netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', update_every: ' + conf.update_every);

		var service = netdata.service({
			name: conf.hostname,
			request: conf,
			update_every: conf.update_every,
			module: this,
			processor: netdata.processors.snmp
		});

		// multiply the charts, if required
		for(var c in service.request.charts) {
			if(netdata.options.DEBUG === true)
				netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', examining chart: ' + c);

			if(typeof service.request.charts[c].update_every === 'undefined')
				service.request.charts[c].update_every = service.update_every;

			if(typeof service.request.charts[c].multiply_range !== 'undefined') {
				var from = service.request.charts[c].multiply_range[0];
				var to = service.request.charts[c].multiply_range[1];
				var prio = service.request.charts[c].priority || 1;

				if(prio < snmp.base_priority) prio += snmp.base_priority;

				while(from <= to) {
					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++;
				}

				delete service.request.charts[c];
			}
			else {
				if(service.request.charts[c].priority < snmp.base_priority)
					service.request.charts[c].priority += snmp.base_priority;
			}
		}

		service.execute(this.processResponse);
	},

	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++;
			}
		}

		return added;
	},

	// module.update()
	// this is called repeatidly to collect data, by calling
	// service.execute()
	update: function(service, callback) {
		service.execute(function(serv, data) {
			service.module.processResponse(serv, data);
			callback();
		});
	},
};

module.exports = snmp;