snmp.node.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. 'use strict';
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. // netdata snmp module
  4. // This program will connect to one or more SNMP Agents
  5. //
  6. // example configuration in /etc/netdata/node.d/snmp.conf
  7. /*
  8. {
  9. "enable_autodetect": false,
  10. "update_every": 5,
  11. "max_request_size": 50,
  12. "servers": [
  13. {
  14. "hostname": "10.11.12.8",
  15. "community": "public",
  16. "update_every": 10,
  17. "max_request_size": 50,
  18. "options": { "timeout": 10000 },
  19. "charts": {
  20. "snmp_switch.bandwidth_port1": {
  21. "title": "Switch Bandwidth for port 1",
  22. "units": "kilobits/s",
  23. "type": "area",
  24. "priority": 1,
  25. "dimensions": {
  26. "in": {
  27. "oid": ".1.3.6.1.2.1.2.2.1.10.1",
  28. "algorithm": "incremental",
  29. "multiplier": 8,
  30. "divisor": 1024,
  31. "offset": 0
  32. },
  33. "out": {
  34. "oid": ".1.3.6.1.2.1.2.2.1.16.1",
  35. "algorithm": "incremental",
  36. "multiplier": -8,
  37. "divisor": 1024,
  38. "offset": 0
  39. }
  40. }
  41. },
  42. "snmp_switch.bandwidth_port2": {
  43. "title": "Switch Bandwidth for port 2",
  44. "units": "kilobits/s",
  45. "type": "area",
  46. "priority": 1,
  47. "dimensions": {
  48. "in": {
  49. "oid": ".1.3.6.1.2.1.2.2.1.10.2",
  50. "algorithm": "incremental",
  51. "multiplier": 8,
  52. "divisor": 1024,
  53. "offset": 0
  54. },
  55. "out": {
  56. "oid": ".1.3.6.1.2.1.2.2.1.16.2",
  57. "algorithm": "incremental",
  58. "multiplier": -8,
  59. "divisor": 1024,
  60. "offset": 0
  61. }
  62. }
  63. }
  64. }
  65. }
  66. ]
  67. }
  68. */
  69. // You can also give ranges of charts like the following.
  70. // This will append 1-24 to id, title, oid (on each dimension)
  71. // so that 24 charts will be created.
  72. /*
  73. {
  74. "enable_autodetect": false,
  75. "update_every": 10,
  76. "max_request_size": 50,
  77. "servers": [
  78. {
  79. "hostname": "10.11.12.8",
  80. "community": "public",
  81. "update_every": 10,
  82. "max_request_size": 50,
  83. "options": { "timeout": 20000 },
  84. "charts": {
  85. "snmp_switch.bandwidth_port": {
  86. "title": "Switch Bandwidth for port ",
  87. "units": "kilobits/s",
  88. "type": "area",
  89. "priority": 1,
  90. "multiply_range": [ 1, 24 ],
  91. "dimensions": {
  92. "in": {
  93. "oid": ".1.3.6.1.2.1.2.2.1.10.",
  94. "algorithm": "incremental",
  95. "multiplier": 8,
  96. "divisor": 1024,
  97. "offset": 0
  98. },
  99. "out": {
  100. "oid": ".1.3.6.1.2.1.2.2.1.16.",
  101. "algorithm": "incremental",
  102. "multiplier": -8,
  103. "divisor": 1024,
  104. "offset": 0
  105. }
  106. }
  107. }
  108. }
  109. }
  110. ]
  111. }
  112. */
  113. var net_snmp = require('net-snmp');
  114. var extend = require('extend');
  115. var netdata = require('netdata');
  116. if (netdata.options.DEBUG === true) netdata.debug('loaded', __filename, ' plugin');
  117. netdata.processors.snmp = {
  118. name: 'snmp',
  119. fixoid: function (oid) {
  120. if (typeof oid !== 'string')
  121. return oid;
  122. if (oid.charAt(0) === '.')
  123. return oid.substring(1, oid.length);
  124. return oid;
  125. },
  126. prepare: function (service) {
  127. var __DEBUG = netdata.options.DEBUG;
  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
  130. if (__DEBUG === true)
  131. netdata.debug(service.module.name + ': ' + service.name + ': preparing ' + this.name + ' OIDs');
  132. // build an index of all OIDs
  133. service.snmp_oids_index = {};
  134. var chart_keys = Object.keys(service.request.charts);
  135. var chart_keys_len = chart_keys.length;
  136. while (chart_keys_len--) {
  137. var c = chart_keys[chart_keys_len];
  138. var chart = service.request.charts[c];
  139. // for each chart
  140. if (__DEBUG === true)
  141. netdata.debug(service.module.name + ': ' + service.name + ': indexing ' + this.name + ' chart: ' + c);
  142. if (typeof chart.titleoid !== 'undefined') {
  143. service.snmp_oids_index[this.fixoid(chart.titleoid)] = {
  144. type: 'title',
  145. link: chart
  146. };
  147. }
  148. var dim_keys = Object.keys(chart.dimensions);
  149. var dim_keys_len = dim_keys.length;
  150. while (dim_keys_len--) {
  151. var d = dim_keys[dim_keys_len];
  152. var dim = chart.dimensions[d];
  153. // for each dimension in the chart
  154. var oid = this.fixoid(dim.oid);
  155. var oidname = this.fixoid(dim.oidname);
  156. if (__DEBUG === true)
  157. 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] = {
  160. type: 'value',
  161. link: dim
  162. };
  163. if (typeof oidname !== 'undefined')
  164. service.snmp_oids_index[oidname] = {
  165. type: 'name',
  166. link: dim
  167. };
  168. // and set the value to null
  169. dim.value = null;
  170. }
  171. }
  172. if (__DEBUG === true)
  173. netdata.debug(service.module.name + ': ' + service.name + ': indexed ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids_index));
  174. // now create the array of OIDs needed by net-snmp
  175. service.snmp_oids = Object.keys(service.snmp_oids_index);
  176. if (__DEBUG === true)
  177. netdata.debug(service.module.name + ': ' + service.name + ': final list of ' + this.name + ' OIDs: ' + netdata.stringify(service.snmp_oids));
  178. service.snmp_oids_cleaned = 0;
  179. } else if (service.snmp_oids_cleaned === 0) {
  180. service.snmp_oids_cleaned = 1;
  181. // the second time, keep only values
  182. service.snmp_oids = new Array();
  183. var oid_keys = Object.keys(service.snmp_oids_index);
  184. var oid_keys_len = oid_keys.length;
  185. while (oid_keys_len--) {
  186. if (service.snmp_oids_index[oid_keys[oid_keys_len]].type === 'value')
  187. service.snmp_oids.push(oid_keys[oid_keys_len]);
  188. }
  189. }
  190. },
  191. getdata: function (service, index, ok, failed, callback) {
  192. var __DEBUG = netdata.options.DEBUG;
  193. var that = this;
  194. if (index >= service.snmp_oids.length) {
  195. callback((ok > 0) ? {ok: ok, failed: failed} : null);
  196. return;
  197. }
  198. var slice;
  199. if (service.snmp_oids.length <= service.request.max_request_size) {
  200. slice = service.snmp_oids;
  201. index = service.snmp_oids.length;
  202. } else if (service.snmp_oids.length - index <= service.request.max_request_size) {
  203. slice = service.snmp_oids.slice(index, service.snmp_oids.length);
  204. index = service.snmp_oids.length;
  205. } else {
  206. slice = service.snmp_oids.slice(index, index + service.request.max_request_size);
  207. index += service.request.max_request_size;
  208. }
  209. if (__DEBUG === true)
  210. netdata.debug(service.module.name + ': ' + service.name + ': making ' + slice.length + ' entries request, max is: ' + service.request.max_request_size);
  211. service.snmp_session.get(slice, function (error, varbinds) {
  212. if (error) {
  213. service.error('Received error = ' + netdata.stringify(error) + ' varbinds = ' + netdata.stringify(varbinds));
  214. // make all values null
  215. var len = slice.length;
  216. while (len--)
  217. service.snmp_oids_index[slice[len]].value = null;
  218. } else {
  219. if (__DEBUG === true)
  220. netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
  221. var varbinds_len = varbinds.length;
  222. for (var i = 0; i < varbinds_len; i++) {
  223. var value = null;
  224. if (net_snmp.isVarbindError(varbinds[i])) {
  225. if (__DEBUG === true)
  226. netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
  227. service.error('OID ' + varbinds[i].oid + ' gave error: ' + net_snmp.varbindError(varbinds[i]));
  228. value = null;
  229. failed++;
  230. } else {
  231. // test fom Counter64
  232. // varbinds[i].type = net_snmp.ObjectType.Counter64;
  233. // varbinds[i].value = new Buffer([0x34, 0x49, 0x2e, 0xdc, 0xd1]);
  234. switch (varbinds[i].type) {
  235. case net_snmp.ObjectType.OctetString:
  236. if (service.snmp_oids_index[varbinds[i].oid].type !== 'title' && service.snmp_oids_index[varbinds[i].oid].type !== 'name') {
  237. // parse floating point values, exposed as strings
  238. value = parseFloat(varbinds[i].value) * 1000;
  239. 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)");
  240. } else {
  241. // just use the string
  242. value = varbinds[i].value;
  243. 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)");
  244. }
  245. break;
  246. case net_snmp.ObjectType.Counter64:
  247. // copy the buffer
  248. value = '0x' + varbinds[i].value.toString('hex');
  249. 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)");
  250. break;
  251. case net_snmp.ObjectType.Integer:
  252. case net_snmp.ObjectType.Counter:
  253. case net_snmp.ObjectType.Gauge:
  254. default:
  255. value = varbinds[i].value;
  256. 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)");
  257. break;
  258. }
  259. ok++;
  260. }
  261. if (value !== null) {
  262. switch (service.snmp_oids_index[varbinds[i].oid].type) {
  263. case 'title':
  264. service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value;
  265. break;
  266. case 'name' :
  267. service.snmp_oids_index[varbinds[i].oid].link.name = value.toString().replace(/\W/g, '_');
  268. break;
  269. case 'value':
  270. service.snmp_oids_index[varbinds[i].oid].link.value = value;
  271. break;
  272. }
  273. }
  274. }
  275. if (__DEBUG === true)
  276. netdata.debug(service.module.name + ': ' + service.name + ': finished ' + service.module.name + ' with ' + ok + ' successful and ' + failed + ' failed values');
  277. }
  278. that.getdata(service, index, ok, failed, callback);
  279. });
  280. },
  281. process: function (service, callback) {
  282. var __DEBUG = netdata.options.DEBUG;
  283. this.prepare(service);
  284. if (service.snmp_oids.length === 0) {
  285. // no OIDs found for this service
  286. if (__DEBUG === true)
  287. service.error('no OIDs to process.');
  288. callback(null);
  289. return;
  290. }
  291. if (typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
  292. // no SNMP session has been created for this service
  293. // the SNMP session is just the initialization of NET-SNMP
  294. var snmp_version = (service.request.options && service.request.options.version)
  295. ? service.request.options.version
  296. : net_snmp.Version1;
  297. if (snmp_version === net_snmp.Version3) {
  298. if (__DEBUG === true)
  299. 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));
  300. // create the SNMP session
  301. service.snmp_session = net_snmp.createV3Session(service.request.hostname, service.request.user, service.request.options);
  302. } else {
  303. if (__DEBUG === true)
  304. 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));
  305. // create the SNMP session
  306. service.snmp_session = net_snmp.createSession(service.request.hostname, service.request.community, service.request.options);
  307. }
  308. if (__DEBUG === true)
  309. netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
  310. // if we later need traps, this is how to do it:
  311. //service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
  312. // if(error) console.error('trap error: ' + netdata.stringify(error));
  313. //});
  314. }
  315. // do it, get the SNMP values for the sessions we need
  316. this.getdata(service, 0, 0, 0, callback);
  317. }
  318. };
  319. var snmp = {
  320. name: __filename,
  321. enable_autodetect: true,
  322. update_every: 1,
  323. base_priority: 50000,
  324. charts: {},
  325. processResponse: function (service, data) {
  326. if (data !== null) {
  327. if (service.added !== true)
  328. service.commit();
  329. var chart_keys = Object.keys(service.request.charts);
  330. var chart_keys_len = chart_keys.length;
  331. for (var i = 0; i < chart_keys_len; i++) {
  332. var c = chart_keys[i];
  333. var chart = snmp.charts[c];
  334. if (typeof chart === 'undefined') {
  335. chart = service.chart(c, service.request.charts[c]);
  336. snmp.charts[c] = chart;
  337. }
  338. service.begin(chart);
  339. var dimensions = service.request.charts[c].dimensions;
  340. var dim_keys = Object.keys(dimensions);
  341. var dim_keys_len = dim_keys.length;
  342. for (var j = 0; j < dim_keys_len; j++) {
  343. var d = dim_keys[j];
  344. if (dimensions[d].value !== null) {
  345. if (typeof dimensions[d].offset === 'number' && typeof dimensions[d].value === 'number')
  346. service.set(d, dimensions[d].value + dimensions[d].offset);
  347. else
  348. service.set(d, dimensions[d].value);
  349. }
  350. }
  351. service.end();
  352. }
  353. }
  354. },
  355. // module.serviceExecute()
  356. // this function is called only from this module
  357. // its purpose is to prepare the request and call
  358. // netdata.serviceExecute()
  359. serviceExecute: function (conf) {
  360. var __DEBUG = netdata.options.DEBUG;
  361. if (__DEBUG === true)
  362. netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', update_every: ' + conf.update_every);
  363. var service = netdata.service({
  364. name: conf.hostname,
  365. request: conf,
  366. update_every: conf.update_every,
  367. module: this,
  368. processor: netdata.processors.snmp
  369. });
  370. // multiply the charts, if required
  371. var chart_keys = Object.keys(service.request.charts);
  372. var chart_keys_len = chart_keys.length;
  373. for (var i = 0; i < chart_keys_len; i++) {
  374. var c = chart_keys[i];
  375. var service_request_chart = service.request.charts[c];
  376. if (__DEBUG === true)
  377. netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', examining chart: ' + c);
  378. if (typeof service_request_chart.update_every === 'undefined')
  379. service_request_chart.update_every = service.update_every;
  380. if (typeof service_request_chart.multiply_range !== 'undefined') {
  381. var from = service_request_chart.multiply_range[0];
  382. var to = service_request_chart.multiply_range[1];
  383. var prio = service_request_chart.priority || 1;
  384. if (prio < snmp.base_priority) prio += snmp.base_priority;
  385. while (from <= to) {
  386. var id = c + from.toString();
  387. var chart = extend(true, {}, service_request_chart);
  388. chart.title += from.toString();
  389. if (typeof chart.titleoid !== 'undefined')
  390. chart.titleoid += from.toString();
  391. chart.priority = prio++;
  392. var dim_keys = Object.keys(chart.dimensions);
  393. var dim_keys_len = dim_keys.length;
  394. for (var j = 0; j < dim_keys_len; j++) {
  395. var d = dim_keys[j];
  396. chart.dimensions[d].oid += from.toString();
  397. if (typeof chart.dimensions[d].oidname !== 'undefined')
  398. chart.dimensions[d].oidname += from.toString();
  399. }
  400. service.request.charts[id] = chart;
  401. from++;
  402. }
  403. delete service.request.charts[c];
  404. } else {
  405. if (service.request.charts[c].priority < snmp.base_priority)
  406. service.request.charts[c].priority += snmp.base_priority;
  407. }
  408. }
  409. service.execute(this.processResponse);
  410. },
  411. configure: function (config) {
  412. var added = 0;
  413. if (typeof config.max_request_size === 'undefined')
  414. config.max_request_size = 50;
  415. if (typeof (config.servers) !== 'undefined') {
  416. var len = config.servers.length;
  417. while (len--) {
  418. if (typeof config.servers[len].update_every === 'undefined')
  419. config.servers[len].update_every = this.update_every;
  420. if (typeof config.servers[len].max_request_size === 'undefined')
  421. config.servers[len].max_request_size = config.max_request_size;
  422. this.serviceExecute(config.servers[len]);
  423. added++;
  424. }
  425. }
  426. return added;
  427. },
  428. // module.update()
  429. // this is called repeatedly to collect data, by calling
  430. // service.execute()
  431. update: function (service, callback) {
  432. service.execute(function (serv, data) {
  433. service.module.processResponse(serv, data);
  434. callback();
  435. });
  436. }
  437. };
  438. module.exports = snmp;