UNPKG

domotz-remote-pawn

Version:

Domotz Agent

126 lines (113 loc) 4.9 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * 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 VALID_LINE_RE = /\s+\d+\.\|--\s+([^ ]+)\s+([^\s]+)\s+[^\s]+\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/i; module.exports.factory = function (dto, resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); function getItems(radix, fields) { var hsh = {}; hsh[radix + 'pktloss'] = fields[2].replace('%', ''); hsh[radix + 'pinglast'] = fields[3]; hsh[radix + 'pingavg'] = fields[4]; hsh[radix + 'pingbest'] = fields[5]; hsh[radix + 'pingworst'] = fields[6]; hsh[radix + 'pingstdv'] = fields[7]; return hsh; } function parseOutput(data, packets, destination) { var lines = data.toString().split(/\r?\n/); var count = 0; var items = {}; lines.forEach(function (item) { var fields = item.match(VALID_LINE_RE); if (!fields || fields[1] === '???') { myConsole.silly('Discarding MTR line => %s', item); return; } myConsole.silly('Including MTR line => %s', item); switch (count) { case 0: resourceLocator.lodash.extend(items, getItems('int.', fields)); myConsole.debug('MTR - Internal detected: %s', fields); count++; break; case 1: resourceLocator.lodash.extend(items, getItems('isp.', fields)); myConsole.debug('MTR - ISP detected: %s', fields); count++; break; default: resourceLocator.lodash.extend(items, getItems('dst.', fields)); break; } }); items.rawdata = JSON.stringify(data.toString()); items.target = destination; items['num.packets'] = packets; return items; } function getMtrExecutable(count, destination) { var args; if (resourceLocator.utils.platform.isWindows()) { args = ' -r -t 2 -c ' + count + ' ' + destination; } else { args = '--report --report-wide --no-dns -4 --report-cycles ' + count + ' ' + destination; } return (resourceLocator.utils.platform.needsSudo() ? 'sudo ' : '') + 'domotz_mtr ' + args; } return { getMtrInfo: function (destination, count) { var command = getMtrExecutable(count, destination); return resourceLocator.utils.proc .exec(command) .then(function (stdout) { return parseOutput(stdout, count, destination); }) .catch(function (err) { return { status: 500, message: err.message }; }); }, mtrInfoToDTO: function (mtrInfo) { if (mtrInfo.rawdata) { mtrInfo.rawdata = mtrInfo.rawdata.split('\\n'); } var map = { 'dst.pingavg': 'destination.ping.average', 'dst.pingbest': 'destination.ping.best', 'dst.pinglast': 'destination.ping.last', 'dst.pingstdv': 'destination.ping.std_dev', 'dst.pingworst': 'destination.ping.worst', 'dst.pktloss': 'destination.loss_ratio', 'int.pingavg': 'internal.ping.average', 'int.pingbest': 'internal.ping.best', 'int.pinglast': 'internal.ping.last', 'int.pingstdv': 'internal.ping.std_dev', 'int.pingworst': 'internal.ping.worst', 'int.pktloss': 'internal.loss_ratio', 'isp.pingavg': 'isp.ping.average', 'isp.pingbest': 'isp.ping.best', 'isp.pinglast': 'isp.ping.last', 'isp.pingstdv': 'isp.ping.std_dev', 'isp.pingworst': 'isp.ping.worst', 'isp.pktloss': 'isp.loss_ratio', rawdata: 'raw', target: 'target', 'num.packets': 'num.packets', }; return dto.mapArrayToDTO(mtrInfo, map); }, }; };