UNPKG

domotz-remote-pawn

Version:

Domotz Agent

119 lines (111 loc) 4.62 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/>. * * Created by Mohamed Kallel <mohamed.kallel@iovision.io> on 08/04/2020. * This is used for checking blocked nmap processes more than 15 minutes * */ var exec = require('child_process').exec; var q = require('q'); var taskToPromise = function (command) { var deferred = q.defer(); exec(command, function (err, stdout, stderr) { if (err || stderr) { return deferred.resolve(''); } deferred.resolve(stdout); }); return deferred.promise; }; var knownLongRunningProcesses = {}; function addLongRunningProcess(pid, description) { pid = parseInt(pid, 10); knownLongRunningProcesses[pid] = description; } function removeLongRunningProcess(pid) { pid = parseInt(pid, 10); delete knownLongRunningProcesses[pid]; } function _isLongRunningProcess(pid) { pid = parseInt(pid, 10); return knownLongRunningProcesses[pid] !== undefined; } var cleanBlockedProcess = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs('PROCESS_CLEANER'); // this cleanup code might be related to https://github.com/nmap/nmap/issues/2104 (fixed in nmap 7.90) myConsole.info('Start finding and killing blocked `nmap` processes'); return taskToPromise('WMIC PROCESS GET NAME, CREATIONDATE, ProcessID | findstr /i "domotz_nmap.exe domotz_bash.exe"') .then(function (res) { // example of output // 20200408211724.960575+120 domotz_nmap.exe if (res) { const pids = []; const lines = res.split('\n'); lines.forEach(function (line) { line = line.trim(); if (line === '') { return; } myConsole.debug('Nmap Process found: ' + line); const year = line.substring(0, 4); const month = line.substring(4, 6); const day = line.substring(6, 8); const hour = line.substring(8, 10); const minutes = line.substring(10, 12); const seconds = line.substring(12, 14); const timestamp = new Date(year + '-' + month + '-' + day + 'T' + hour + ':' + minutes + ':' + seconds).getTime(); const diff = new Date().getTime() - timestamp; if (diff > 15 * 60 * 1000) { const pid = line.split(/\s+/)[2]; if (!_isLongRunningProcess(pid)) { pids.push(pid); } else { myConsole.info( 'Process %s (%s) will not be killed since it is a registered long running process', pid, knownLongRunningProcesses[pid] ); } } }); return pids; } return []; }) .then(function (pids) { if (!pids.length) { myConsole.info('No stalled processes found'); return null; } myConsole.info('pid to kill: %s', pids); const promises = []; pids.forEach(function (pid) { promises.push(taskToPromise('taskkill /PID ' + pid + ' /F')); }); return q.all(promises); }) .then(function () { var message = 'Cleaning blocked processes ended'; myConsole.info(message); return message; }) .catch(function (err) { return err; }); }; module.exports.handler = cleanBlockedProcess; module.exports.addLongRunningProcess = addLongRunningProcess; module.exports.removeLongRunningProcess = removeLongRunningProcess;