network-performance-monitor
Version:
A comprehensive network performance monitoring tool that continuously tests and tracks your network's performance over time
254 lines • 9.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const database_1 = require("./database");
const scheduler_1 = require("./scheduler");
const pidfile_1 = require("./pidfile");
const webServer_1 = require("./webServer");
commander_1.program
.name('network-performance-monitor')
.description('Network performance testing tool with DNS, ping, website, and speed tests')
.version('1.2.0');
commander_1.program
.command('start')
.description('Start the network performance monitor daemon')
.option('-p, --port <port>', 'Port to run the web UI on', '3000')
.option('--test-mode', 'Enable test mode with configurable speed test intervals')
.option('--speed-test-interval <ms>', 'Speed test interval in milliseconds (test mode only)', '10000')
.action(async (options) => {
const pidFile = new pidfile_1.PidFile();
if (pidFile.isDaemonRunning()) {
console.error('Daemon is already running');
process.exit(1);
}
console.log('Starting daemon...');
const daemonPath = path.join(__dirname, 'daemon.js');
const args = ['--port', options.port];
if (options.testMode) {
args.push('--test-mode');
if (options.speedTestInterval) {
args.push('--speed-test-interval', options.speedTestInterval);
}
}
const daemon = (0, child_process_1.spawn)('node', [daemonPath, ...args], {
detached: true,
stdio: 'ignore'
});
daemon.unref();
console.log(`Daemon started successfully (Web UI on port ${options.port})`);
});
commander_1.program
.command('stop')
.description('Stop the network performance monitor daemon')
.action(async () => {
const pidFile = new pidfile_1.PidFile();
const 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);
}
try {
process.kill(pid, 'SIGTERM');
console.log('Stopping daemon...');
// Wait for daemon to stop gracefully (max 5 seconds)
let attempts = 0;
while (attempts < 50 && pidFile.isProcessRunning(pid)) {
await new Promise(resolve => setTimeout(resolve, 100));
attempts++;
}
if (pidFile.isProcessRunning(pid)) {
console.error('Daemon did not stop gracefully, forcing...');
process.kill(pid, 'SIGKILL');
await new Promise(resolve => setTimeout(resolve, 500));
}
console.log('Daemon stopped successfully');
}
catch (error) {
console.error('Failed to stop daemon:', error);
process.exit(1);
}
});
commander_1.program
.command('restart')
.description('Restart the network performance monitor daemon')
.option('-p, --port <port>', 'Port to run the web UI on', '3000')
.option('--test-mode', 'Enable test mode with configurable speed test intervals')
.option('--speed-test-interval <ms>', 'Speed test interval in milliseconds (test mode only)', '10000')
.action(async (options) => {
const pidFile = new pidfile_1.PidFile();
// Stop the daemon if it's running
if (pidFile.isDaemonRunning()) {
const pid = pidFile.read();
console.log('Stopping daemon...');
try {
process.kill(pid, 'SIGTERM');
// Wait for daemon to stop (max 5 seconds)
let attempts = 0;
while (attempts < 50 && pidFile.isProcessRunning(pid)) {
await new Promise(resolve => setTimeout(resolve, 100));
attempts++;
}
if (pidFile.isProcessRunning(pid)) {
console.error('Failed to stop daemon gracefully, forcing...');
process.kill(pid, 'SIGKILL');
await new Promise(resolve => setTimeout(resolve, 500));
}
console.log('Daemon stopped');
}
catch (error) {
console.error('Error stopping daemon:', error);
}
}
// Start the daemon
console.log('Starting daemon...');
const daemonPath = path.join(__dirname, 'daemon.js');
const args = ['--port', options.port];
if (options.testMode) {
args.push('--test-mode');
if (options.speedTestInterval) {
args.push('--speed-test-interval', options.speedTestInterval);
}
}
const daemon = (0, child_process_1.spawn)('node', [daemonPath, ...args], {
detached: true,
stdio: 'ignore'
});
daemon.unref();
console.log(`Daemon restarted successfully (Web UI on port ${options.port})`);
});
commander_1.program
.command('status')
.description('Check if the daemon is running')
.action(() => {
const pidFile = new pidfile_1.PidFile();
if (pidFile.isDaemonRunning()) {
const pid = pidFile.read();
console.log(`Daemon is running (PID: ${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(async (options) => {
const regularInterval = parseInt(options.regular) * 1000;
const speedTestInterval = parseInt(options.speedtest) * 1000;
const duration = parseInt(options.duration) * 1000;
console.log(`Starting test mode:
Regular checks: every ${options.regular}s
Speed tests: every ${options.speedtest}s
Duration: ${options.duration}s`);
const db = new database_1.Database();
await db.initialize();
const scheduler = new scheduler_1.Scheduler(db, {
regularCheckInterval: regularInterval,
speedTestInterval: speedTestInterval,
testMode: true
});
await scheduler.start();
// Stop after duration
setTimeout(async () => {
console.log('\\nTest duration completed, stopping...');
await scheduler.stop();
db.close();
process.exit(0);
}, duration);
// Handle interruption
process.on('SIGINT', async () => {
console.log('\\nInterrupted, stopping...');
await scheduler.stop();
db.close();
process.exit(0);
});
});
commander_1.program
.command('run-once')
.description('Run all tests once and exit')
.action(async () => {
console.log('Running all tests once...');
const db = new database_1.Database();
await db.initialize();
const scheduler = new scheduler_1.Scheduler(db, {
regularCheckInterval: 60000,
speedTestInterval: 3600000
});
// Access private methods through a workaround
const schedulerAny = scheduler;
console.log('\\nRunning regular tests (DNS, Ping, Website)...');
await schedulerAny.runRegularTests();
console.log('\\nRunning speed test...');
await schedulerAny.runSpeedTest('manual');
db.close();
console.log('\\nAll tests completed');
});
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(async (options) => {
const port = parseInt(options.port);
console.log(`Starting web UI server on port ${port}...`);
const db = new database_1.Database();
await db.initialize();
const webServer = new webServer_1.WebServer(db, port);
webServer.start();
// Keep the process running
process.on('SIGINT', () => {
console.log('\\nShutting down web server...');
db.close();
process.exit(0);
});
});
// If no command was provided, default to 'start'
const args = process.argv.slice();
if (args.length === 2) {
args.push('start');
}
commander_1.program.parse(args);
//# sourceMappingURL=index.js.map