Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cisco-device-inventory.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2013 Stephen Vickers
  2. var snmp = require ("../");
  3. var util = require ("util");
  4. if (process.argv.length < 5) {
  5. console.log ("usage: cisco-device-inventory <target> <community> <version>");
  6. process.exit (1);
  7. }
  8. var target = process.argv[2];
  9. var community = process.argv[3];
  10. var version = (process.argv[4] == "2c") ? snmp.Version2c : snmp.Version1;
  11. function pollDeviceDetails (session, device, pollCb) {
  12. console.log ("getting system properties...");
  13. var oids = ["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.1.6.0"];
  14. session.get (oids, function (error, varbinds) {
  15. if (error) {
  16. pollCb (error, null);
  17. } else {
  18. for (var i = 0; i < varbinds.length; i++) {
  19. if (snmp.isVarbindError (varbinds[i])) {
  20. console.error (snmp.varbindError (varbinds[i]));
  21. return;
  22. }
  23. }
  24. device.name = varbinds[0].value.toString ();
  25. device.description = varbinds[1].value.toString ();
  26. device.location = varbinds[2].value.toString ();
  27. pollDeviceInterfaces (session, device, pollCb);
  28. }
  29. });
  30. }
  31. function pollDeviceDevices (session, device, pollCb) {
  32. function pollVlans () {
  33. if (device.vlandIds.length > 0) {
  34. var vlanId = device.vlandIds.pop ();
  35. var session = snmp.createSession (target,
  36. community + "@" + vlanId, {version: version});
  37. console.log ("getting macs for vlan " + vlanId + "...");
  38. // dot1dTpFdbTable: dot1dTpFdbPort
  39. var oid = "1.3.6.1.2.1.17.4.3";
  40. session.tableColumns (oid, [2], function (error, table) {
  41. if (error) {
  42. pollCb (error, null);
  43. } else {
  44. for (var index in table) {
  45. var mac = new Buffer (index.split (".")).toString ("hex");
  46. var row = table[index];
  47. delete table[index];
  48. table[mac] = row;
  49. }
  50. device.devices[vlanId] = table;
  51. if (device.vlandIds.length > 0) {
  52. pollVlans ();
  53. } else {
  54. delete device.vlandIds;
  55. pollCb (null, device);
  56. }
  57. }
  58. });
  59. }
  60. }
  61. console.log ("getting macs...");
  62. if (device.vlandIds.length > 0) {
  63. pollVlans ();
  64. } else {
  65. delete device.vlandIds;
  66. pollCb (null, device);
  67. }
  68. }
  69. function pollDeviceInterfaces (session, device, pollCb) {
  70. console.log ("getting interfaces...");
  71. // ifTable: ifDescr, ifType, ifPhysAddress
  72. session.tableColumns ("1.3.6.1.2.1.2.2", [2, 3, 6], function (error, table) {
  73. if (error) {
  74. pollCb (error, null);
  75. } else {
  76. device.intefaces = table;
  77. pollDeviceVlans (session, device, pollCb);
  78. }
  79. });
  80. }
  81. function pollDeviceVlans (session, device, pollCb) {
  82. console.log ("getting vlans...");
  83. // vtpVlanTable: vtpVlanType
  84. session.tableColumns ("1.3.6.1.4.1.9.9.46.1.3.1", [3], function (error, table) {
  85. if (error) {
  86. pollCb (error, null);
  87. } else {
  88. device.vlans = table;
  89. device.vlandIds = [];
  90. device.devices = {};
  91. for (var index in table) {
  92. var match = index.match (/(\d+)$/);
  93. if (match) {
  94. var vlanId = match[1];
  95. var row = table[index];
  96. delete table[index];
  97. table[vlanId] = row;
  98. if (table[vlanId][3] == 1)
  99. device.vlandIds.push (vlanId);
  100. }
  101. }
  102. pollDeviceDevices (session, device, pollCb);
  103. }
  104. });
  105. }
  106. function pollDevice (session, device, pollCb) {
  107. pollDeviceDetails (session, device, pollCb);
  108. }
  109. var session = snmp.createSession (target, community, {version: version});
  110. pollDevice (session, {}, function (error, device) {
  111. if (error) {
  112. console.error (error.toString ());
  113. } else {
  114. console.warn (util.inspect (device, {depth: 3}));
  115. }
  116. });