Node-Red configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

snmp-agent.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. var snmp = require ("../");
  2. var fs = require("fs");
  3. var getopts = require ("getopts");
  4. var options = getopts(process.argv.slice(2));
  5. var snmpOptions = {
  6. disableAuthorization: options.n,
  7. port: options.p,
  8. engineID: options.e,
  9. debug: options.d,
  10. address: null,
  11. accessControlModelType: snmp.AccessControlModelType.Simple
  12. };
  13. if (options.s) {
  14. var changes;
  15. // If there's a persistent store, make its specified changes
  16. try {
  17. changes = JSON.parse(fs.readFileSync("persistent.json"));
  18. for (var providerName in changes) {
  19. var change = changes[providerName];
  20. if (typeof change == "object") {
  21. // table row
  22. for (var rowIndex in change) {
  23. mib.addTableRow(providerName, change[rowIndex]);
  24. }
  25. } else {
  26. mib.setScalarValue(providerName, change);
  27. }
  28. }
  29. } catch (e) {
  30. console.log("Could not parse persistent storage");
  31. changes = {};
  32. }
  33. }
  34. var callback = function (error, data) {
  35. var needSave;
  36. if ( error ) {
  37. console.error (error);
  38. return;
  39. }
  40. console.log (JSON.stringify(data.pdu.varbinds, null, 2));
  41. // If the user didn't request testing persistent storage, we're done here
  42. if (! options.s) {
  43. return;
  44. }
  45. // Keep note of whether we need to save
  46. needSave = false;
  47. data.pdu.varbinds.forEach(
  48. (varbind) => {
  49. let index;
  50. let value;
  51. // If there was a request error, we don't need to do anything
  52. if ("errorStatus" in varbind || ! ("providerName" in varbind) ) {
  53. return;
  54. }
  55. if (varbind.autoCreated && "rowIndex" in varbind) {
  56. // Auto-create table row
  57. index = JSON.stringify(varbind.rowIndex);
  58. if ( ! changes[varbind.providerName] ) {
  59. changes[varbind.providerName] = {};
  60. }
  61. value = varbind.row;
  62. value = value.map((v) => v instanceof Buffer ? v.toString() : v);
  63. changes[varbind.providerName][index] = value;
  64. needSave = true;
  65. } else if (varbind.autoCreated) {
  66. // Auto-created scalar
  67. value = varbind.value;
  68. value = value instanceof Buffer ? value.toString() : value;
  69. changes[varbind.providerName] = value;
  70. needSave = true;
  71. } else if (varbind.deleted && "rowIndex" in varbind) {
  72. // Delete table row
  73. index = JSON.stringify(varbind.rowIndex);
  74. if (changes && changes[varbind.providerName] && changes[varbind.providerName][index]) {
  75. delete changes[varbind.providerName][index];
  76. if (Object.keys(changes[varbind.providerName]).length === 0) {
  77. delete changes[varbind.providerName];
  78. }
  79. }
  80. needSave = true;
  81. } else if ("requestValue" in varbind && "rowIndex" in varbind && "column" in varbind) {
  82. // Set a column value
  83. value = varbind.value;
  84. index = JSON.stringify(varbind.rowIndex);
  85. changes[varbind.providerName][index][varbind.columnPosition] =
  86. value instanceof Buffer ? value.toString() : value;
  87. needSave = true;
  88. } else if ("requestValue" in varbind) {
  89. // Set a scalar
  90. value = varbind.value;
  91. changes[varbind.providerName] =
  92. value instanceof Buffer ? value.toString() : value;
  93. needSave = true;
  94. } else {
  95. console.log("Ignoring varbind:" + JSON.stringify(varbind, null, " "));
  96. }
  97. });
  98. // Did we make any changes?
  99. if (needSave) {
  100. // Yup. Save 'em.
  101. fs.writeFileSync("persistent.json", JSON.stringify(changes, null, " "));
  102. }
  103. };
  104. var agent = snmp.createAgent(snmpOptions, callback);
  105. var authorizer = agent.getAuthorizer ();
  106. authorizer.addCommunity ("denied");
  107. authorizer.addCommunity ("public");
  108. authorizer.addCommunity ("private");
  109. authorizer.addUser ({
  110. name: "fred",
  111. level: snmp.SecurityLevel.noAuthNoPriv
  112. });
  113. authorizer.addUser ({
  114. name: "betty",
  115. level: snmp.SecurityLevel.authNoPriv,
  116. authProtocol: snmp.AuthProtocols.sha,
  117. authKey: "illhavesomeauth"
  118. });
  119. authorizer.addUser ({
  120. name: "wilma",
  121. level: snmp.SecurityLevel.authPriv,
  122. authProtocol: snmp.AuthProtocols.sha,
  123. authKey: "illhavesomeauth",
  124. privProtocol: snmp.PrivProtocols.des,
  125. privKey: "andsomepriv"
  126. });
  127. // console.log(JSON.stringify(agent.getAuthorizer().getUsers(), null, 2));
  128. var scalarProvider = {
  129. name: "sysDescr",
  130. type: snmp.MibProviderType.Scalar,
  131. oid: "1.3.6.1.2.1.1.1",
  132. scalarType: snmp.ObjectType.OctetString,
  133. maxAccess: snmp.MaxAccess['read-write'],
  134. constraints: {
  135. sizes: [
  136. { min: 1, max: 3 },
  137. { min: 5 }
  138. ]
  139. }
  140. };
  141. agent.registerProvider (scalarProvider);
  142. scalarProvider = {
  143. name: "snmpEnableAuthenTraps",
  144. type: snmp.MibProviderType.Scalar,
  145. oid: "1.3.6.1.2.1.11.30",
  146. scalarType: snmp.ObjectType.Integer,
  147. // createHandler: (provider) => 42,
  148. maxAccess: snmp.MaxAccess['read-create'],
  149. constraints: {
  150. ranges: [
  151. { min: 1, max: 3 },
  152. { min: 5 }
  153. ]
  154. },
  155. defVal: 1
  156. };
  157. agent.registerProvider (scalarProvider);
  158. var tableProvider = {
  159. name: "ifTable",
  160. type: snmp.MibProviderType.Table,
  161. oid: "1.3.6.1.2.1.2.2.1",
  162. // createHandler: (provider, action, row) => [ row[0], "Locally-created", 24, snmp.RowStatus[action] ],
  163. maxAccess: snmp.MaxAccess['not-accessible'],
  164. tableColumns: [
  165. {
  166. number: 1,
  167. name: "ifIndex",
  168. type: snmp.ObjectType.Integer,
  169. maxAccess: snmp.MaxAccess['read-only']
  170. },
  171. {
  172. number: 2,
  173. name: "ifDescr",
  174. type: snmp.ObjectType.OctetString,
  175. maxAccess: snmp.MaxAccess['read-write'],
  176. defVal: "Hello world!"
  177. },
  178. {
  179. number: 3,
  180. name: "ifType",
  181. type: snmp.ObjectType.Integer,
  182. maxAccess: snmp.MaxAccess['read-only'],
  183. constraints: {
  184. enumeration: {
  185. "1": "goodif",
  186. "2": "badif",
  187. "6": "someif",
  188. "24": "anotherif"
  189. }
  190. },
  191. defVal: 6
  192. },
  193. {
  194. number: 99,
  195. name: "ifStatus",
  196. type: snmp.ObjectType.Integer,
  197. maxAccess: snmp.MaxAccess['read-write'],
  198. rowStatus: true
  199. }
  200. ],
  201. tableIndex: [
  202. {
  203. columnName: "ifIndex"
  204. }
  205. ],
  206. handler: function ifTable (mibRequest) {
  207. // e.g. can update the table before responding to the request here
  208. mibRequest.done ();
  209. }
  210. };
  211. agent.registerProvider (tableProvider);
  212. var mib = agent.getMib ();
  213. // Modify defaults
  214. mib.setScalarDefaultValue ("snmpEnableAuthenTraps", 3);
  215. mib.setTableRowDefaultValues( "ifTable", [ undefined, "Hello world, amended!", 2, undefined ] );
  216. mib.setScalarValue ("sysDescr", "Rage inside the machine!");
  217. mib.addTableRow ("ifTable", [1, "lo", 24, 1]);
  218. mib.addTableRow ("ifTable", [2, "eth0", 6, 2]);
  219. // mib.deleteTableRow ("ifTable", [2]);
  220. // mib.unregisterProvider ("ifTable");
  221. // mib.unregisterProvider ("sysDescr");
  222. // var store = snmp.createModuleStore ();
  223. // var providers = store.getProviders ("IF-MIB");
  224. // mib.registerProviders (providers);
  225. //console.log (JSON.stringify (providers, null, 2));
  226. mib.dump ({
  227. leavesOnly: true,
  228. showProviders: true,
  229. showValues: true,
  230. showTypes: true
  231. });
  232. // var data = mib.getTableColumnDefinitions ("ifTable");
  233. // var data = mib.getTableCells ("ifTable", true);
  234. // var data = mib.getTableColumnCells ("ifTable", 2);
  235. // var data = mib.getTableRowCells ("ifTable", [1]);
  236. // mib.setTableSingleCell ("ifTable", 2, [2], "changed!");
  237. mib.setTableSingleCell ("ifTable", 3, [2], 99);
  238. var data = mib.getTableSingleCell ("ifTable", 3, [2]);
  239. // var data = mib.getScalarValue ("sysDescr");
  240. console.log(JSON.stringify (data, null, 2));
  241. var acm = authorizer.getAccessControlModel ();
  242. acm.setCommunityAccess ("denied", snmp.AccessLevel.None);
  243. acm.setCommunityAccess ("public", snmp.AccessLevel.ReadOnly);
  244. acm.setCommunityAccess ("private", snmp.AccessLevel.ReadWrite);
  245. acm.setUserAccess ("fred", snmp.AccessLevel.ReadWrite);
  246. console.log ("private = ", acm.getCommunityAccess ("private"));
  247. console.log (acm.getCommunitiesAccess ());