201 lines
9.9 KiB
JavaScript
201 lines
9.9 KiB
JavaScript
// Generate realistic sample log files for testing
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const zlib = require('zlib');
|
|
|
|
const dir = path.join(__dirname);
|
|
|
|
// ---- Application Log (app.log) ----
|
|
const levels = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'TRACE'];
|
|
const components = ['auth', 'database', 'http', 'cache', 'scheduler', 'config', 'metrics', 'queue', 'storage', 'api'];
|
|
const users = ['admin@corp.com', 'alice@corp.com', 'bob@corp.com', 'charlie@corp.com', 'service-account'];
|
|
const endpoints = ['/api/users', '/api/orders', '/api/products', '/api/auth/login', '/api/health', '/api/metrics', '/api/search', '/api/upload', '/api/notifications', '/api/settings'];
|
|
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
|
|
const dbOps = ['SELECT', 'INSERT', 'UPDATE', 'DELETE'];
|
|
const tables = ['users', 'orders', 'products', 'sessions', 'audit_log', 'notifications'];
|
|
|
|
function rnd(arr) { return arr[Math.floor(Math.random() * arr.length)]; }
|
|
function rndInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
|
|
|
|
function genAppLog(lines) {
|
|
const result = [];
|
|
const base = new Date('2026-04-29T06:00:00Z');
|
|
|
|
for (let i = 0; i < lines; i++) {
|
|
const ts = new Date(base.getTime() + i * rndInt(100, 5000));
|
|
const iso = ts.toISOString();
|
|
const level = rnd(levels);
|
|
const comp = rnd(components);
|
|
let msg = '';
|
|
|
|
switch (level) {
|
|
case 'DEBUG':
|
|
const msgs = [
|
|
`Cache ${rnd(['hit', 'miss'])} for key: ${rnd(tables)}:${rndInt(1, 9999)}`,
|
|
`Query executed in ${rndInt(1, 500)}ms: ${rnd(dbOps)} FROM ${rnd(tables)}`,
|
|
`Token validation for user: ${rnd(users)}`,
|
|
`Connection pool stats: active=${rndInt(1, 10)}, idle=${rndInt(0, 5)}, waiting=${rndInt(0, 3)}`,
|
|
`Request headers: Accept=application/json, X-Request-ID=${rndInt(100000, 999999)}`,
|
|
`GC pause: ${rndInt(5, 50)}ms, heap: ${rndInt(200, 800)}MB`,
|
|
];
|
|
msg = rnd(msgs);
|
|
break;
|
|
case 'INFO':
|
|
const infoMsgs = [
|
|
`${rnd(methods)} ${rnd(endpoints)} - ${rnd([200, 201, 204])} (${rndInt(1, 200)}ms)`,
|
|
`User ${rnd(users)} authenticated successfully`,
|
|
`Scheduled task completed: ${rnd(['cleanup-sessions', 'sync-data', 'generate-report', 'send-notifications'])}`,
|
|
`Database connection established to ${rnd(['primary', 'replica-1', 'replica-2'])}`,
|
|
`Metrics: requests/min=${rndInt(500, 2000)}, errors/min=${rndInt(0, 10)}, avg_latency=${rndInt(10, 100)}ms`,
|
|
`File uploaded: ${rnd(['report', 'avatar', 'document', 'backup'])}_${rndInt(1, 999)}.${rnd(['pdf', 'png', 'csv', 'zip'])} (${rndInt(10, 5000)}KB)`,
|
|
`Queue processed: ${rndInt(10, 500)} messages in ${rndInt(100, 5000)}ms`,
|
|
];
|
|
msg = rnd(infoMsgs);
|
|
break;
|
|
case 'WARN':
|
|
const warnMsgs = [
|
|
`Slow query detected: ${rnd(dbOps)} FROM ${rnd(tables)} took ${rndInt(1000, 10000)}ms`,
|
|
`Rate limit approaching for IP ${rndInt(10, 192)}.${rndInt(0, 255)}.${rndInt(0, 255)}.${rndInt(1, 254)}: ${rndInt(80, 99)}% of limit`,
|
|
`Heap usage at ${rndInt(70, 95)}% (${rndInt(800, 1400)}MB / 1536MB)`,
|
|
`Retry attempt ${rndInt(1, 3)}/3 for external service: ${rnd(['payment-api', 'email-service', 'notification-hub'])}`,
|
|
`Certificate expires in ${rndInt(5, 30)} days for ${rnd(['api.example.com', 'auth.example.com'])}`,
|
|
`Deprecated API endpoint called: ${rnd(endpoints)} (v1)`,
|
|
`Connection pool exhausted, waiting for available connection`,
|
|
];
|
|
msg = rnd(warnMsgs);
|
|
break;
|
|
case 'ERROR':
|
|
const errMsgs = [
|
|
`${rnd([500, 502, 503])} Internal Server Error: ${rnd(methods)} ${rnd(endpoints)} - ${rnd(['NullPointerException', 'TimeoutException', 'ConnectionRefused', 'OutOfMemoryError'])}`,
|
|
`Database query failed: ${rnd(dbOps)} FROM ${rnd(tables)} - ${rnd(['deadlock detected', 'connection timeout', 'constraint violation'])}`,
|
|
`Authentication failed for ${rnd(users)}: ${rnd(['invalid credentials', 'account locked', 'token expired', 'MFA required'])}`,
|
|
`External service unavailable: ${rnd(['payment-api', 'email-service', 'cdn'])} (HTTP ${rnd([502, 503, 504])})`,
|
|
`File operation failed: ${rnd(['permission denied', 'disk full', 'file not found', 'I/O error'])}`,
|
|
`Queue message processing failed: ${rnd(['deserialization error', 'handler timeout', 'duplicate message'])}`,
|
|
];
|
|
msg = rnd(errMsgs);
|
|
break;
|
|
case 'FATAL':
|
|
const fatalMsgs = [
|
|
`Database connection lost to primary! Attempting failover...`,
|
|
`Out of memory: heap space exhausted at ${rndInt(1400, 1536)}MB`,
|
|
`Unrecoverable error in ${rnd(components)}: process will restart`,
|
|
`Disk space critical: ${rndInt(95, 99)}% used on /data`,
|
|
`SSL/TLS handshake failed: certificate chain broken`,
|
|
];
|
|
msg = rnd(fatalMsgs);
|
|
break;
|
|
case 'TRACE':
|
|
const traceMsgs = [
|
|
`Entering ${rnd(components)}.${rnd(['process', 'handle', 'validate', 'transform'])}() with args: [${rndInt(1, 100)}]`,
|
|
`HTTP request body: {"id":${rndInt(1, 9999)},"action":"${rnd(['create', 'update', 'delete'])}"}`,
|
|
`SQL: ${rnd(dbOps)} * FROM ${rnd(tables)} WHERE id = ${rndInt(1, 9999)} LIMIT 1`,
|
|
`Response serialization: ${rndInt(1, 50)}ms for ${rndInt(100, 10000)} bytes`,
|
|
];
|
|
msg = rnd(traceMsgs);
|
|
break;
|
|
}
|
|
|
|
result.push(`${iso} ${level.padEnd(5)} [${comp}] ${msg}`);
|
|
}
|
|
return result.join('\n');
|
|
}
|
|
|
|
// ---- Nginx Access Log ----
|
|
function genNginxLog(lines) {
|
|
const result = [];
|
|
const ips = ['192.168.1.100', '192.168.1.101', '10.0.0.50', '10.0.0.51', '172.16.0.10', '203.0.113.42', '198.51.100.7'];
|
|
const agents = [
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0',
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Safari/605.1.15',
|
|
'Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0',
|
|
'curl/8.4.0',
|
|
'python-requests/2.31.0',
|
|
'Googlebot/2.1 (+http://www.google.com/bot.html)',
|
|
];
|
|
const paths = ['/dashboard', '/api/users', '/api/orders', '/api/products', '/static/app.js', '/static/style.css', '/api/auth/login', '/api/health', '/favicon.ico', '/api/search?q=test'];
|
|
const statuses = [200, 200, 200, 200, 200, 201, 204, 301, 304, 400, 401, 403, 404, 500, 502, 503];
|
|
const base = new Date('2026-04-29T06:00:00Z');
|
|
|
|
for (let i = 0; i < lines; i++) {
|
|
const ts = new Date(base.getTime() + i * rndInt(500, 3000));
|
|
const dateStr = ts.toISOString().replace('T', ' ').replace(/\.\d+Z/, '');
|
|
const ip = rnd(ips);
|
|
const method = rnd(methods);
|
|
const p = rnd(paths);
|
|
const status = rnd(statuses);
|
|
const size = rndInt(0, 50000);
|
|
const agent = rnd(agents);
|
|
const ref = rnd(['-', 'https://example.com/dashboard', 'https://example.com/']);
|
|
result.push(`${ip} - - [${dateStr}] "${method} ${p} HTTP/1.1" ${status} ${size} "${ref}" "${agent}"`);
|
|
}
|
|
return result.join('\n');
|
|
}
|
|
|
|
// ---- Syslog ----
|
|
function genSyslog(lines) {
|
|
const result = [];
|
|
const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
|
|
const hosts = ['web-01', 'web-02', 'db-01', 'cache-01', 'lb-01'];
|
|
const procs = ['sshd', 'systemd', 'kernel', 'cron', 'nginx', 'postfix', 'dockerd', 'kubelet'];
|
|
|
|
for (let i = 0; i < lines; i++) {
|
|
const h = rndInt(0, 23);
|
|
const m = rndInt(0, 59);
|
|
const s = rndInt(0, 59);
|
|
const host = rnd(hosts);
|
|
const proc = rnd(procs);
|
|
const pid = rndInt(1000, 65000);
|
|
let msg = '';
|
|
|
|
switch (proc) {
|
|
case 'sshd':
|
|
msg = rnd([
|
|
`Accepted publickey for ${rnd(['root', 'deploy', 'admin'])} from ${rndInt(10, 192)}.${rndInt(0, 255)}.${rndInt(0, 255)}.${rndInt(1, 254)} port ${rndInt(40000, 65000)}`,
|
|
`Failed password for invalid user ${rnd(['test', 'admin', 'root'])} from ${rndInt(10, 192)}.${rndInt(0, 255)}.${rndInt(0, 255)}.${rndInt(1, 254)}`,
|
|
`Connection closed by authenticating user ${rnd(['deploy', 'admin'])} ${rndInt(10, 192)}.${rndInt(0, 255)}.${rndInt(0, 255)}.${rndInt(1, 254)} port ${rndInt(40000, 65000)}`,
|
|
]);
|
|
break;
|
|
case 'kernel':
|
|
msg = rnd([
|
|
`[${rndInt(10000, 99999)}.${rndInt(100, 999)}] TCP: request_sock_TCP: Possible SYN flooding on port ${rndInt(80, 8080)}`,
|
|
`[${rndInt(10000, 99999)}.${rndInt(100, 999)}] Out of memory: Killed process ${rndInt(1000, 9999)} (${rnd(['java', 'node', 'python'])})`,
|
|
`[${rndInt(10000, 99999)}.${rndInt(100, 999)}] EXT4-fs (sda1): mounted filesystem with ordered data mode`,
|
|
`[${rndInt(10000, 99999)}.${rndInt(100, 999)}] device eth0 entered promiscuous mode`,
|
|
]);
|
|
break;
|
|
default:
|
|
msg = rnd([
|
|
`Started ${rnd(['Daily cleanup', 'Log rotation', 'Health check', 'Backup'])} service`,
|
|
`Unit ${rnd(['nginx', 'docker', 'postgresql'])}.service ${rnd(['started', 'stopped', 'reloaded'])}`,
|
|
`Process ${rndInt(1000, 9999)} exited with status ${rnd([0, 1, 137])}`,
|
|
]);
|
|
}
|
|
|
|
result.push(`Apr 29 ${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')} ${host} ${proc}[${pid}]: ${msg}`);
|
|
}
|
|
return result.join('\n');
|
|
}
|
|
|
|
// ---- Generate files ----
|
|
console.log('Generating sample logs...');
|
|
|
|
const appLog = genAppLog(500);
|
|
fs.writeFileSync(path.join(dir, 'app.log'), appLog);
|
|
console.log(' ✓ app.log (500 lines)');
|
|
|
|
const nginxLog = genNginxLog(300);
|
|
fs.writeFileSync(path.join(dir, 'nginx-access.log'), nginxLog);
|
|
console.log(' ✓ nginx-access.log (300 lines)');
|
|
|
|
const syslog = genSyslog(200);
|
|
fs.writeFileSync(path.join(dir, 'syslog'), syslog);
|
|
console.log(' ✓ syslog (200 lines, no extension)');
|
|
|
|
// Gzip version
|
|
const gzipped = zlib.gzipSync(Buffer.from(genAppLog(100)));
|
|
fs.writeFileSync(path.join(dir, 'archived.log.gz'), gzipped);
|
|
console.log(' ✓ archived.log.gz (100 lines, gzipped)');
|
|
|
|
console.log('\nDone! All sample logs generated in', dir);
|