snmp.node.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. }
  180. else if(service.snmp_oids_cleaned === 0) {
  181. service.snmp_oids_cleaned = 1;
  182. // the second time, keep only values
  183. service.snmp_oids = new Array();
  184. var oid_keys = Object.keys(service.snmp_oids_index);
  185. var oid_keys_len = oid_keys.length;
  186. while(oid_keys_len--) {
  187. if (service.snmp_oids_index[oid_keys[oid_keys_len]].type === 'value')
  188. service.snmp_oids.push(oid_keys[oid_keys_len]);
  189. }
  190. }
  191. },
  192. getdata: function(service, index, ok, failed, callback) {
  193. var __DEBUG = netdata.options.DEBUG;
  194. var that = this;
  195. if(index >= service.snmp_oids.length) {
  196. callback((ok > 0)?{ ok: ok, failed: failed }:null);
  197. return;
  198. }
  199. var slice;
  200. if(service.snmp_oids.length <= service.request.max_request_size) {
  201. slice = service.snmp_oids;
  202. index = service.snmp_oids.length;
  203. }
  204. else if(service.snmp_oids.length - index <= service.request.max_request_size) {
  205. slice = service.snmp_oids.slice(index, service.snmp_oids.length);
  206. index = service.snmp_oids.length;
  207. }
  208. else {
  209. slice = service.snmp_oids.slice(index, index + service.request.max_request_size);
  210. index += service.request.max_request_size;
  211. }
  212. if(__DEBUG === true)
  213. netdata.debug(service.module.name + ': ' + service.name + ': making ' + slice.length + ' entries request, max is: ' + service.request.max_request_size);
  214. service.snmp_session.get(slice, function(error, varbinds) {
  215. if(error) {
  216. service.error('Received error = ' + netdata.stringify(error) + ' varbinds = ' + netdata.stringify(varbinds));
  217. // make all values null
  218. var len = slice.length;
  219. while(len--)
  220. service.snmp_oids_index[slice[len]].value = null;
  221. }
  222. else {
  223. if(__DEBUG === true)
  224. netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
  225. var varbinds_len = varbinds.length;
  226. for(var i = 0; i < varbinds_len ; i++) {
  227. var value = null;
  228. if(net_snmp.isVarbindError(varbinds[i])) {
  229. if(__DEBUG === true)
  230. netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
  231. service.error('OID ' + varbinds[i].oid + ' gave error: ' + net_snmp.varbindError(varbinds[i]));
  232. value = null;
  233. failed++;
  234. }
  235. else {
  236. // test fom Counter64
  237. // varbinds[i].type = net_snmp.ObjectType.Counter64;
  238. // varbinds[i].value = new Buffer([0x34, 0x49, 0x2e, 0xdc, 0xd1]);
  239. switch(varbinds[i].type) {
  240. case net_snmp.ObjectType.OctetString:
  241. if (service.snmp_oids_index[varbinds[i].oid].type !== 'title' && service.snmp_oids_index[varbinds[i].oid].type !== 'name') {
  242. // parse floating point values, exposed as strings
  243. value = parseFloat(varbinds[i].value) * 1000;
  244. 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)");
  245. }
  246. else {
  247. // just use the string
  248. value = varbinds[i].value;
  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 string)");
  250. }
  251. break;
  252. case net_snmp.ObjectType.Counter64:
  253. // copy the buffer
  254. value = '0x' + varbinds[i].value.toString('hex');
  255. 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)");
  256. break;
  257. case net_snmp.ObjectType.Integer:
  258. case net_snmp.ObjectType.Counter:
  259. case net_snmp.ObjectType.Gauge:
  260. default:
  261. value = varbinds[i].value;
  262. 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)");
  263. break;
  264. }
  265. ok++;
  266. }
  267. if(value !== null) {
  268. switch(service.snmp_oids_index[varbinds[i].oid].type) {
  269. case 'title': service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value; break;
  270. case 'name' : service.snmp_oids_index[varbinds[i].oid].link.name = value.toString().replace(/\W/g, '_'); break;
  271. case 'value': service.snmp_oids_index[varbinds[i].oid].link.value = value; 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. if(__DEBUG === true)
  295. 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));
  296. // create the SNMP session
  297. service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);
  298. if(__DEBUG === true)
  299. netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
  300. // if we later need traps, this is how to do it:
  301. //service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
  302. // if(error) console.error('trap error: ' + netdata.stringify(error));
  303. //});
  304. }
  305. // do it, get the SNMP values for the sessions we need
  306. this.getdata(service, 0, 0, 0, callback);
  307. }
  308. };
  309. var snmp = {
  310. name: __filename,
  311. enable_autodetect: true,
  312. update_every: 1,
  313. base_priority: 50000,
  314. charts: {},
  315. processResponse: function(service, data) {
  316. if(data !== null) {
  317. if(service.added !== true)
  318. service.commit();
  319. var chart_keys = Object.keys(service.request.charts);
  320. var chart_keys_len = chart_keys.length;
  321. for(var i = 0; i < chart_keys_len; i++) {
  322. var c = chart_keys[i];
  323. var chart = snmp.charts[c];
  324. if(typeof chart === 'undefined') {
  325. chart = service.chart(c, service.request.charts[c]);
  326. snmp.charts[c] = chart;
  327. }
  328. service.begin(chart);
  329. var dimensions = service.request.charts[c].dimensions;
  330. var dim_keys = Object.keys(dimensions);
  331. var dim_keys_len = dim_keys.length;
  332. for(var j = 0; j < dim_keys_len ; j++) {
  333. var d = dim_keys[j];
  334. if (dimensions[d].value !== null) {
  335. if(typeof dimensions[d].offset === 'number' && typeof dimensions[d].value === 'number')
  336. service.set(d, dimensions[d].value + dimensions[d].offset);
  337. else
  338. service.set(d, dimensions[d].value);
  339. }
  340. }
  341. service.end();
  342. }
  343. }
  344. },
  345. // module.serviceExecute()
  346. // this function is called only from this module
  347. // its purpose is to prepare the request and call
  348. // netdata.serviceExecute()
  349. serviceExecute: function(conf) {
  350. var __DEBUG = netdata.options.DEBUG;
  351. if(__DEBUG === true)
  352. netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', update_every: ' + conf.update_every);
  353. var service = netdata.service({
  354. name: conf.hostname,
  355. request: conf,
  356. update_every: conf.update_every,
  357. module: this,
  358. processor: netdata.processors.snmp
  359. });
  360. // multiply the charts, if required
  361. var chart_keys = Object.keys(service.request.charts);
  362. var chart_keys_len = chart_keys.length;
  363. for( var i = 0; i < chart_keys_len ; i++ ) {
  364. var c = chart_keys[i];
  365. var service_request_chart = service.request.charts[c];
  366. if(__DEBUG === true)
  367. netdata.debug(this.name + ': snmp hostname: ' + conf.hostname + ', examining chart: ' + c);
  368. if(typeof service_request_chart.update_every === 'undefined')
  369. service_request_chart.update_every = service.update_every;
  370. if(typeof service_request_chart.multiply_range !== 'undefined') {
  371. var from = service_request_chart.multiply_range[0];
  372. var to = service_request_chart.multiply_range[1];
  373. var prio = service_request_chart.priority || 1;
  374. if(prio < snmp.base_priority) prio += snmp.base_priority;
  375. while(from <= to) {
  376. var id = c + from.toString();
  377. var chart = extend(true, {}, service_request_chart);
  378. chart.title += from.toString();
  379. if(typeof chart.titleoid !== 'undefined')
  380. chart.titleoid += from.toString();
  381. chart.priority = prio++;
  382. var dim_keys = Object.keys(chart.dimensions);
  383. var dim_keys_len = dim_keys.length;
  384. for(var j = 0; j < dim_keys_len ; j++) {
  385. var d = dim_keys[j];
  386. chart.dimensions[d].oid += from.toString();
  387. if(typeof chart.dimensions[d].oidname !== 'undefined')
  388. chart.dimensions[d].oidname += from.toString();
  389. }
  390. service.request.charts[id] = chart;
  391. from++;
  392. }
  393. delete service.request.charts[c];
  394. }
  395. else {
  396. if(service.request.charts[c].priority < snmp.base_priority)
  397. service.request.charts[c].priority += snmp.base_priority;
  398. }
  399. }
  400. service.execute(this.processResponse);
  401. },
  402. configure: function(config) {
  403. var added = 0;
  404. if(typeof config.max_request_size === 'undefined')
  405. config.max_request_size = 50;
  406. if(typeof(config.servers) !== 'undefined') {
  407. var len = config.servers.length;
  408. while(len--) {
  409. if(typeof config.servers[len].update_every === 'undefined')
  410. config.servers[len].update_every = this.update_every;
  411. if(typeof config.servers[len].max_request_size === 'undefined')
  412. config.servers[len].max_request_size = config.max_request_size;
  413. this.serviceExecute(config.servers[len]);
  414. added++;
  415. }
  416. }
  417. return added;
  418. },
  419. // module.update()
  420. // this is called repeatidly to collect data, by calling
  421. // service.execute()
  422. update: function(service, callback) {
  423. service.execute(function(serv, data) {
  424. service.module.processResponse(serv, data);
  425. callback();
  426. });
  427. }
  428. };
  429. module.exports = snmp;