UNPKG

domotz-remote-pawn

Version:

Domotz Agent

126 lines (104 loc) 5.26 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz UK LLP * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ const DEFAULT_MAX_REPETITIONS = 24; var common = require('./../../snmp/common.js'); var snmpSubtrees = function (message, resourceLocator) { var snmp = resourceLocator.netSnmp; var cmdUtils = resourceLocator.utils.command; var cid = message.correlationId; var options = message.payload; var oidsMapping = cmdUtils.extractFromOptions(options, 'oids', {}); var host = cmdUtils.extractFromOptions(options, 'host', 'localhost'); var community = cmdUtils.extractFromOptions(options, 'community', 'public'); var oids = common.extractOids(oidsMapping); var transformer = message.customTransformer ? message.customTransformer : common.transformer; // Adjust the default var maxRepetitions = options.maxRepetitions || DEFAULT_MAX_REPETITIONS; options.version = options.version || snmp.Version2c; delete options.maxRepetitions; return { execute: function (callback) { var response = {oids:{}}; var waiting = {}; for (var j = 0; j < oids.length; j++) { oid = oids[j]; response.oids[oid] = {error: 'Unrecognized oid'}; } var snmpSession = snmp.createSession(host, community, options); snmpSession.on("error", function (error) { var errorMessage = error.toString(); console.error("- SNMP Subtree - (socket error) %s", errorMessage); snmpSession.close(); }); function done(oid) { return function (error) { console.debug("MESSAGE - (%s): SNMP Subtrees => response received for oid: %s", cid, oid); if (error) { console.info("MESSAGE - (%s): SNMP Subtrees => error retrieving oid: %s => %s", cid, oid, error.message); response.oids[oid] = {'error': error}; } waiting[oid] = false; for (var i = 0; i < oids.length; i++) { if (waiting[oids[i]]) { console.debug("MESSAGE - (%s): SNMP Subtrees => still waiting for oid: %s", cid, oids[i]); return; } } console.debug("MESSAGE - (%s): SNMP Subtrees => command execution concluded, yielding result: %s", cid, JSON.stringify(response)); snmpSession.close(); callback(response); }; } function valueArrived(oid) { return function (varbinds) { console.debug("MESSAGE - (%s): SNMP Subtrees => varbind callback invoked for oid: %s", cid, oid); if (response.oids[oid].error !== undefined ) { response.oids[oid] = {}; } for (var i = 0; i < varbinds.length; i++) { if (snmp.isVarbindError (varbinds[i])) { console.warn ("MESSAGE - (%s): SNMP Subtrees => error retrieving oid: %s => varbind error: %s", cid, oid, snmp.varbindError(varbinds[i])); response.oids[oid] = {'error': snmp.varbindError (varbinds[i])}; break; } else { var sub_oid = varbinds[i].oid.substring(oid.length + 1); var value = transformer(oidsMapping[oid])(varbinds[i].value); console.debug("MESSAGE - (%s): SNMP value arrived: [%s].[%s]: %s", cid, oid, sub_oid, value); response.oids[oid][sub_oid] = value; } } }; } var oid; for (i = 0; i < oids.length; i++) { oid = oids[i]; snmpSession.subtree(oid, maxRepetitions, valueArrived(oid), done(oid)); } console.debug("MESSAGE - (%s): SNMP Subtrees => request to host %s for oids: %s", cid, host, JSON.stringify(oids)); // Initialise structures for multi oid retrieval for (var i = 0; i < oids.length; i++) { waiting[oids[i]] = true; } }, describe: function () { return "snmpSubtrees - " + host + ' - ' + JSON.stringify(oids); } }; }; module.exports.command = snmpSubtrees;