UNPKG

network-performance-monitor

Version:

A comprehensive network performance monitoring tool that continuously tests and tracks your network's performance over time

319 lines (318 loc) 14.3 kB
#!/usr/bin/env node "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var commander_1 = require("commander"); var child_process_1 = require("child_process"); var path = require("path"); var database_1 = require("./database"); var scheduler_1 = require("./scheduler"); var pidfile_1 = require("./pidfile"); var webServer_1 = require("./webServer"); commander_1.program .name('network-performance-tester') .description('Network performance testing tool with DNS, ping, website, and speed tests') .version('1.0.0'); commander_1.program .command('start') .description('Start the network performance tester daemon') .action(function () { return __awaiter(void 0, void 0, void 0, function () { var pidFile, daemonPath, daemon; return __generator(this, function (_a) { pidFile = new pidfile_1.PidFile(); if (pidFile.isDaemonRunning()) { console.error('Daemon is already running'); process.exit(1); } console.log('Starting daemon...'); daemonPath = path.join(__dirname, 'daemon.js'); daemon = (0, child_process_1.spawn)('node', [daemonPath], { detached: true, stdio: 'ignore' }); daemon.unref(); console.log('Daemon started successfully'); return [2 /*return*/]; }); }); }); commander_1.program .command('stop') .description('Stop the network performance tester daemon') .action(function () { return __awaiter(void 0, void 0, void 0, function () { var pidFile, pid, attempts, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: pidFile = new pidfile_1.PidFile(); pid = pidFile.read(); if (pid === null) { console.error('No daemon is running'); process.exit(1); } if (!pidFile.isProcessRunning(pid)) { console.error('Daemon PID file exists but process is not running'); pidFile.remove(); process.exit(1); } _a.label = 1; case 1: _a.trys.push([1, 7, , 8]); process.kill(pid, 'SIGTERM'); console.log('Stopping daemon...'); attempts = 0; _a.label = 2; case 2: if (!(attempts < 50 && pidFile.isProcessRunning(pid))) return [3 /*break*/, 4]; return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })]; case 3: _a.sent(); attempts++; return [3 /*break*/, 2]; case 4: if (!pidFile.isProcessRunning(pid)) return [3 /*break*/, 6]; console.error('Daemon did not stop gracefully, forcing...'); process.kill(pid, 'SIGKILL'); return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 500); })]; case 5: _a.sent(); _a.label = 6; case 6: console.log('Daemon stopped successfully'); return [3 /*break*/, 8]; case 7: error_1 = _a.sent(); console.error('Failed to stop daemon:', error_1); process.exit(1); return [3 /*break*/, 8]; case 8: return [2 /*return*/]; } }); }); }); commander_1.program .command('restart') .description('Restart the network performance tester daemon') .action(function () { return __awaiter(void 0, void 0, void 0, function () { var pidFile, pid, attempts, error_2, daemonPath, daemon; return __generator(this, function (_a) { switch (_a.label) { case 0: pidFile = new pidfile_1.PidFile(); if (!pidFile.isDaemonRunning()) return [3 /*break*/, 8]; pid = pidFile.read(); console.log('Stopping daemon...'); _a.label = 1; case 1: _a.trys.push([1, 7, , 8]); process.kill(pid, 'SIGTERM'); attempts = 0; _a.label = 2; case 2: if (!(attempts < 50 && pidFile.isProcessRunning(pid))) return [3 /*break*/, 4]; return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })]; case 3: _a.sent(); attempts++; return [3 /*break*/, 2]; case 4: if (!pidFile.isProcessRunning(pid)) return [3 /*break*/, 6]; console.error('Failed to stop daemon gracefully, forcing...'); process.kill(pid, 'SIGKILL'); return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 500); })]; case 5: _a.sent(); _a.label = 6; case 6: console.log('Daemon stopped'); return [3 /*break*/, 8]; case 7: error_2 = _a.sent(); console.error('Error stopping daemon:', error_2); return [3 /*break*/, 8]; case 8: // Start the daemon console.log('Starting daemon...'); daemonPath = path.join(__dirname, 'daemon.js'); daemon = (0, child_process_1.spawn)('node', [daemonPath], { detached: true, stdio: 'ignore' }); daemon.unref(); console.log('Daemon restarted successfully'); return [2 /*return*/]; } }); }); }); commander_1.program .command('status') .description('Check if the daemon is running') .action(function () { var pidFile = new pidfile_1.PidFile(); if (pidFile.isDaemonRunning()) { var pid = pidFile.read(); console.log("Daemon is running (PID: ".concat(pid, ")")); } else { console.log('Daemon is not running'); } }); commander_1.program .command('test') .description('Run in test mode with configurable intervals') .option('-r, --regular <seconds>', 'Regular check interval in seconds', '1') .option('-s, --speedtest <seconds>', 'Speed test interval in seconds', '10') .option('-d, --duration <seconds>', 'Test duration in seconds', '60') .action(function (options) { return __awaiter(void 0, void 0, void 0, function () { var regularInterval, speedTestInterval, duration, db, scheduler; return __generator(this, function (_a) { switch (_a.label) { case 0: regularInterval = parseInt(options.regular) * 1000; speedTestInterval = parseInt(options.speedtest) * 1000; duration = parseInt(options.duration) * 1000; console.log("Starting test mode:\n Regular checks: every ".concat(options.regular, "s\n Speed tests: every ").concat(options.speedtest, "s\n Duration: ").concat(options.duration, "s")); db = new database_1.Database(); return [4 /*yield*/, db.initialize()]; case 1: _a.sent(); scheduler = new scheduler_1.Scheduler(db, { regularCheckInterval: regularInterval, speedTestInterval: speedTestInterval, testMode: true }); return [4 /*yield*/, scheduler.start()]; case 2: _a.sent(); // Stop after duration setTimeout(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: console.log('\\nTest duration completed, stopping...'); return [4 /*yield*/, scheduler.stop()]; case 1: _a.sent(); db.close(); process.exit(0); return [2 /*return*/]; } }); }); }, duration); // Handle interruption process.on('SIGINT', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: console.log('\\nInterrupted, stopping...'); return [4 /*yield*/, scheduler.stop()]; case 1: _a.sent(); db.close(); process.exit(0); return [2 /*return*/]; } }); }); }); return [2 /*return*/]; } }); }); }); commander_1.program .command('run-once') .description('Run all tests once and exit') .action(function () { return __awaiter(void 0, void 0, void 0, function () { var db, scheduler, schedulerAny; return __generator(this, function (_a) { switch (_a.label) { case 0: console.log('Running all tests once...'); db = new database_1.Database(); return [4 /*yield*/, db.initialize()]; case 1: _a.sent(); scheduler = new scheduler_1.Scheduler(db, { regularCheckInterval: 60000, speedTestInterval: 3600000 }); schedulerAny = scheduler; console.log('\\nRunning regular tests (DNS, Ping, Website)...'); return [4 /*yield*/, schedulerAny.runRegularTests()]; case 2: _a.sent(); console.log('\\nRunning speed test...'); return [4 /*yield*/, schedulerAny.runSpeedTest('manual')]; case 3: _a.sent(); db.close(); console.log('\\nAll tests completed'); return [2 /*return*/]; } }); }); }); commander_1.program .command('ui') .description('Launch the web UI for viewing performance data') .option('-p, --port <port>', 'Port to run the web server on', '3000') .action(function (options) { return __awaiter(void 0, void 0, void 0, function () { var port, db, webServer; return __generator(this, function (_a) { switch (_a.label) { case 0: port = parseInt(options.port); console.log("Starting web UI server on port ".concat(port, "...")); db = new database_1.Database(); return [4 /*yield*/, db.initialize()]; case 1: _a.sent(); webServer = new webServer_1.WebServer(db, port); webServer.start(); // Keep the process running process.on('SIGINT', function () { console.log('\\nShutting down web server...'); db.close(); process.exit(0); }); return [2 /*return*/]; } }); }); }); // If no command was provided, default to 'start' var args = process.argv.slice(); if (args.length === 2) { args.push('start'); } commander_1.program.parse(args);