@casoon/auditmysite
Version:
Professional website analysis suite with robust accessibility testing, Core Web Vitals performance monitoring, SEO analysis, and content optimization insights. Features isolated browser contexts, retry mechanisms, and comprehensive API endpoints for profe
262 lines • 9.1 kB
JavaScript
;
/**
* System Health Checker
* Provides comprehensive health monitoring for AuditMySite
*/
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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemHealthChecker = void 0;
const os_1 = __importDefault(require("os"));
const logger_1 = require("../logging/logger");
class SystemHealthChecker {
constructor() {
this.MEMORY_WARNING_THRESHOLD = 0.8; // 80%
this.MEMORY_CRITICAL_THRESHOLD = 0.9; // 90%
this.logger = new logger_1.Logger({ level: 'info' });
this.startTime = Date.now();
}
/**
* Get comprehensive system health status
*/
async getHealthStatus() {
const memoryCheck = this.checkMemory();
const cpuCheck = this.checkCPU();
const browserCheck = await this.checkBrowser();
const filesystemCheck = this.checkFilesystem();
// Determine overall status
const checks = [memoryCheck, cpuCheck, browserCheck, filesystemCheck];
const hasFailure = checks.some(check => check.status === 'fail');
const hasWarning = checks.some(check => check.status === 'warn');
let overallStatus;
if (hasFailure) {
overallStatus = 'unhealthy';
}
else if (hasWarning) {
overallStatus = 'degraded';
}
else {
overallStatus = 'healthy';
}
const totalMemory = os_1.default.totalmem();
const freeMemory = os_1.default.freemem();
const usedMemory = totalMemory - freeMemory;
return {
status: overallStatus,
timestamp: new Date().toISOString(),
uptime: Date.now() - this.startTime,
checks: {
memory: memoryCheck,
cpu: cpuCheck,
browser: browserCheck,
filesystem: filesystemCheck,
},
metrics: {
totalMemoryMB: Math.round(totalMemory / 1024 / 1024),
usedMemoryMB: Math.round(usedMemory / 1024 / 1024),
freeMemoryMB: Math.round(freeMemory / 1024 / 1024),
memoryUsagePercent: (usedMemory / totalMemory) * 100,
cpuLoadAverage: os_1.default.loadavg(),
processUptime: process.uptime(),
},
};
}
/**
* Check memory health
*/
checkMemory() {
const totalMemory = os_1.default.totalmem();
const freeMemory = os_1.default.freemem();
const usedMemory = totalMemory - freeMemory;
const usagePercent = usedMemory / totalMemory;
if (usagePercent >= this.MEMORY_CRITICAL_THRESHOLD) {
return {
status: 'fail',
message: 'Critical memory usage',
details: {
usagePercent: Math.round(usagePercent * 100),
threshold: this.MEMORY_CRITICAL_THRESHOLD * 100,
},
};
}
if (usagePercent >= this.MEMORY_WARNING_THRESHOLD) {
return {
status: 'warn',
message: 'High memory usage',
details: {
usagePercent: Math.round(usagePercent * 100),
threshold: this.MEMORY_WARNING_THRESHOLD * 100,
},
};
}
return {
status: 'pass',
message: 'Memory usage is healthy',
details: {
usagePercent: Math.round(usagePercent * 100),
},
};
}
/**
* Check CPU health
*/
checkCPU() {
const loadAvg = os_1.default.loadavg();
const cpuCount = os_1.default.cpus().length;
const load1min = loadAvg[0];
const loadPerCpu = load1min / cpuCount;
// Load average threshold: 0.7 per CPU core
if (loadPerCpu > 0.9) {
return {
status: 'fail',
message: 'Critical CPU load',
details: {
loadAverage: loadAvg,
cpuCount,
loadPerCpu: Math.round(loadPerCpu * 100) / 100,
},
};
}
if (loadPerCpu > 0.7) {
return {
status: 'warn',
message: 'High CPU load',
details: {
loadAverage: loadAvg,
cpuCount,
loadPerCpu: Math.round(loadPerCpu * 100) / 100,
},
};
}
return {
status: 'pass',
message: 'CPU load is healthy',
details: {
loadAverage: loadAvg,
cpuCount,
},
};
}
/**
* Check browser availability
*/
async checkBrowser() {
try {
// Check if Playwright browsers are installed
const { execSync } = await Promise.resolve().then(() => __importStar(require('child_process')));
try {
// Check if chromium is available
execSync('npx playwright --version', { stdio: 'ignore' });
return {
status: 'pass',
message: 'Playwright browser is available',
};
}
catch {
return {
status: 'fail',
message: 'Playwright browser is not installed',
details: {
suggestion: 'Run: npx playwright install chromium',
},
};
}
}
catch (error) {
return {
status: 'warn',
message: 'Unable to verify browser installation',
details: {
error: error instanceof Error ? error.message : String(error),
},
};
}
}
/**
* Check filesystem health
*/
checkFilesystem() {
try {
const tmpdir = os_1.default.tmpdir();
// Try to write to temp directory
const { writeFileSync, unlinkSync } = require('fs');
const testFile = `${tmpdir}/auditmysite-health-check-${Date.now()}.tmp`;
writeFileSync(testFile, 'health check', 'utf-8');
unlinkSync(testFile);
return {
status: 'pass',
message: 'Filesystem is writable',
details: {
tmpdir,
},
};
}
catch (error) {
return {
status: 'fail',
message: 'Filesystem write failed',
details: {
error: error instanceof Error ? error.message : String(error),
},
};
}
}
/**
* Get simplified health check (for quick checks)
*/
async isHealthy() {
const status = await this.getHealthStatus();
return status.status === 'healthy';
}
/**
* Log current health status
*/
async logHealthStatus() {
const status = await this.getHealthStatus();
if (status.status === 'healthy') {
this.logger.info('System health check: HEALTHY');
}
else if (status.status === 'degraded') {
this.logger.warn('System health check: DEGRADED', status.checks);
}
else {
this.logger.error('System health check: UNHEALTHY', status.checks);
}
}
}
exports.SystemHealthChecker = SystemHealthChecker;
//# sourceMappingURL=system-health-checker.js.map