domotz-remote-pawn
Version:
Domotz Agent
146 lines (132 loc) • 5.51 kB
JavaScript
/** 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 TYPE_INTEGER = 2;
const TYPE_OID = 6;
function convertOIDS(oids) {
var convertedOIDS = [];
for (var i in oids) {
convertedOIDS[i] = oids[i]
.split('.')
.filter(function (s) {
return s.length > 0;
})
.map(function (s) {
return parseInt(s, 10);
});
}
return convertedOIDS;
}
function convertHexNumber(hexValue) {
if (hexValue === undefined || hexValue === null) {
return null;
}
var number = parseInt(hexValue, 16);
var maxValue = Math.pow(2, (hexValue.length / 2) * 8);
if (number > maxValue / 2 - 1) {
number = number - maxValue;
}
return number;
}
var snmpGetNative = function (message, resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
myConsole.info('SNMP_GET_NATIVE - (%s)', JSON.stringify(message));
var snmpNative = resourceLocator.snmpNative;
var cmdUtils = resourceLocator.utils.command;
var snmpCommon = resourceLocator.snmpCommon;
var cid = message.correlationId;
var options = message.payload;
var oidsMapping = cmdUtils.extractFromOptions(options, 'oids', {});
var host = options.host;
var community = options.community;
var oids = snmpCommon.extractOids(oidsMapping);
var transformer = message.customTransformer ? message.customTransformer : snmpCommon.transformer;
// Comparison to null is mandatory because snmp.Version1 = 0
if (options.version == null) {
//jshint ignore:line
options.version = snmpNative.Versions.SNMPv2c;
}
function extractOidsFromVarbinds(varbinds) {
myConsole.info('SNMP_GET_NATIVE - (%s): Got response - number of returned oids: %s', cid, varbinds.length);
var values = {};
varbinds.forEach(function (vb) {
var val;
if (vb.type === TYPE_INTEGER) {
val = convertHexNumber(vb.valueHex);
} else if (vb.type === TYPE_OID) {
val = vb.value.join('.').replace(/(.NaN).*/i, '');
} else {
val = vb.value;
}
var response_oid = vb.oid.join('.');
var value = transformer(oidsMapping[response_oid])(val);
values[response_oid] = value;
});
return { oids: values };
}
return {
execute: function (callback) {
var callbackCalled = false;
var responseCallback = function (error, varbinds) {
var response;
if (error) {
myConsole.debug('SNMP_GET_NATIVE - (%s): SNMP error received: %s', cid, error);
response = { error: error };
} else {
if (varbinds !== null && varbinds !== undefined) {
response = extractOidsFromVarbinds(varbinds);
} else {
response = { error: 'invalid SNMP response ' };
}
}
myConsole.debug('SNMP_GET_NATIVE - (%s): command execution concluded, yielding result: %s', cid, JSON.stringify(response));
try {
snmpNativeSession.close();
} catch (error) {
myConsole.error('SNMP_GET_NATIVE - cannot close session: ' + error);
} finally {
if (!callbackCalled) {
callbackCalled = true;
callback(response);
}
}
};
myConsole.debug('SNMP_NATIVE_GET - (%s): Sending SNMP get request to host %s for oids: %s', cid, host, JSON.stringify(oids));
var snmpNativeSession = new snmpNative.Session(options);
snmpNativeSession.on('error', function (error) {
var errorMessage = error.toString();
myConsole.error('SNMP_GET_NATIVE - (socket error) %s', errorMessage);
try {
snmpNativeSession.close();
} catch (error) {
myConsole.error('SNMP_GET_NATIVE - cannot close session: ' + error);
} finally {
if (!callbackCalled) {
callbackCalled = true;
callback({ error: errorMessage });
}
}
});
snmpNativeSession.getAll({ oids: convertOIDS(oids) }, responseCallback);
},
describe: function () {
return cid + ' - snmpGetNative - ' + host + ' - ' + JSON.stringify(oids);
},
};
};
module.exports.command = snmpGetNative;
module.exports.convertOIDS = convertOIDS;
module.exports.convertHexNumber = convertHexNumber;