UNPKG

domotz-remote-pawn

Version:

Domotz Agent

95 lines (79 loc) 4.07 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/>. **/ var common = require('./../../snmp/common.js'); var snmpGet = 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 options.version = options.version || snmp.Version2c; return { execute: function (callback) { console.debug('SNMP_GET - (%s): Sending SNMP get request to host %s for oids: %s', cid, host, JSON.stringify(oids)); var snmpSession = snmp.createSession(host, community, options); snmpSession.on("error", function (error) { var errorMessage = error.toString(); console.error("- SNMP Get - (socket error) %s", errorMessage); snmpSession.close(); callback({'error': errorMessage}); }); snmpSession.get(oids, function (error, response) { if (error) { if(common.isEmpty(error) && response === undefined){ console.info("SNMP_GET - (%s): warn: ignoring callback since error is empty and response is undefined", cid); return; } else { console.warn("SNMP_GET - (%s): SNMP error received: %s", cid, error); response = {"error": error}; } } else if (response !== null && response !== undefined ) { console.info("SNMP_GET - (%s): Got response - number of returned oids: %s", cid, response.length); var values = {}; for (var i = 0; i < response.length; i++) { var oid = response[i].oid; if (!(snmp.isVarbindError(response[i]))) { var value = transformer(oidsMapping[oid])(response[i].value); console.debug("SNMP_GET - (%s): - SNMP value retrieved: %s=%s", cid, oid, value); values[oid] = value; } else { console.warn ("SNMP_GET - (%s): SNMP get - error retrieving oid: %s => varbind error: s", cid, oid, snmp.varbindError(response[i])); } } response = {"oids": values}; } else { response = {'error': "invalid SNMP response"}; } console.debug("SNMP_GET - (%s): command execution concluded, yielding result: %s", cid, JSON.stringify(response)); snmpSession.close(); callback(response); }); }, describe: function () { return cid + " - snmpGet - " + host + ' - ' + JSON.stringify(oids); } }; }; module.exports.command = snmpGet;