UNPKG

domotz-remote-pawn

Version:

Domotz Agent

121 lines (105 loc) 4.5 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/>. **/ module.exports.factory = function (_, procUtils, dto) { var domotzSbinDir = process.env.DOMOTZ_SBIN_DIR; function getItems(radix, fields) { var hsh = {}; hsh[radix + 'pktloss'] = fields[3].replace('%', ''); hsh[radix + 'pinglast'] = fields[5]; hsh[radix + 'pingavg'] = fields[6]; hsh[radix + 'pingbest'] = fields[7]; hsh[radix + 'pingworst'] = fields[8]; hsh[radix + 'pingstdv'] = fields[9]; 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.split(/[|\- ]+/); if (!/[0-9]+\.$/.test(fields[1]) || item.indexOf('???') !== -1) { console.silly('Discarding MTR line => %s', item); } else if (fields[1]) { console.debug('Including MTR line => %s', item); switch (count) { case 0: _.extend(items, getItems('int.', fields)); count ++; break; case 1: _.extend(items, getItems('isp.', fields)); count++; break; default: _.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) { return (process.getuid() === 0 ? '' : 'sudo ') + 'domotz_mtr --report --report-wide --no-dns -4 --report-cycles ' + count + ' ' + destination; } return { getMtrInfo: function (destination, count) { var command = getMtrExecutable(count, destination); return procUtils.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); } }; };