UNPKG

network-performance-monitor

Version:

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

107 lines 4.12 kB
"use strict"; 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 }); exports.PingTest = void 0; const ping = __importStar(require("ping")); const config_1 = require("./config"); const logger_1 = require("./logger"); class PingTest { db; constructor(db) { this.db = db; } async testHost(host, networkName) { const result = { host, latency: null, packetLoss: null, error: null, success: false }; try { const res = await ping.promise.probe(host, { timeout: 10, extra: ['-c', '4'] // Send 4 packets }); if (res.alive) { result.latency = res.avg ? parseFloat(res.avg) : null; result.packetLoss = res.packetLoss ? parseFloat(res.packetLoss) : 0; result.success = true; } else { result.error = 'Host unreachable'; } } catch (error) { result.error = error instanceof Error ? error.message : 'Unknown error'; } // Log the result (0, logger_1.logTestResult)('PING', host, result.success, result.latency || undefined, result.error || undefined); // Store in database await this.db.insertPingResult(host, result.latency, result.packetLoss, result.error, networkName); // Update baseline if successful if (result.success && result.latency !== null) { await this.db.updateBaseline('ping', host, result.latency); } return result; } async testAllHosts(networkName) { const results = []; // Use DNS servers as ping targets for (const server of config_1.config.dns.servers) { const result = await this.testHost(server, networkName); results.push(result); } return results; } async checkForAnomalies(results) { let anomalyDetected = false; for (const result of results) { if (!result.success || result.latency === null) { // Any failure is an anomaly anomalyDetected = true; continue; } const baseline = await this.db.getBaseline('ping', result.host); if (baseline !== null && result.latency > baseline * config_1.config.thresholds.latencyMultiplier) { anomalyDetected = true; (0, logger_1.logTestResult)('PING_ANOMALY', result.host, false, result.latency, `Latency ${result.latency}ms exceeds threshold (baseline: ${baseline}ms)`); } } return anomalyDetected; } } exports.PingTest = PingTest; //# sourceMappingURL=pingTest.js.map