136 lines
4.0 KiB
JavaScript
136 lines
4.0 KiB
JavaScript
const express = require('express');
|
|
const http = require('http');
|
|
const WebSocket = require('ws');
|
|
const axios = require('axios');
|
|
const path = require('path');
|
|
|
|
const argv = require('yargs').argv;
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const wss = new WebSocket.Server({ server });
|
|
|
|
let logCache = new Set();
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
wss.on('connection', (ws) => {
|
|
console.log('Client connected:', ws._socket.remoteAddress);
|
|
|
|
ws.on('close', () => {
|
|
console.log('Client disconnected:', ws._socket.remoteAddress);
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket error:', error);
|
|
});
|
|
});
|
|
|
|
const debugMode = argv.debug || true;
|
|
|
|
setInterval(async () => {
|
|
let logs;
|
|
if (debugMode) {
|
|
try {
|
|
const fs = require('fs');
|
|
const data = fs.readFileSync('logsexample.out', 'utf8');
|
|
const logLines = data.split('\n');
|
|
const validLogs = [];
|
|
logLines.forEach((line, index) => {
|
|
if (line.trim() !== '') {
|
|
try {
|
|
const parsedLog = JSON.parse(line);
|
|
validLogs.push(parsedLog);
|
|
} catch (parseError) {
|
|
console.error(`Error parsing log line at index ${index}:`, line);
|
|
}
|
|
}
|
|
});
|
|
logs = validLogs;
|
|
} catch (error) {
|
|
console.error('Error reading logsexample.out:', error);
|
|
return;
|
|
}
|
|
} else {
|
|
const query = 'query=_time:4s relay received';
|
|
try {
|
|
const response = await axios.post('https://vmselect.riff.cc/select/logsql/query', {
|
|
data: query,
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
}
|
|
});
|
|
const logLines = response.data.split('\n');
|
|
const validLogs = [];
|
|
logLines.forEach((line, index) => {
|
|
if (line.trim() !== '') {
|
|
try {
|
|
const unescapedLine = line.replace(/\\"/g, '"');
|
|
const parsedLog = JSON.parse(unescapedLine);
|
|
validLogs.push(parsedLog);
|
|
} catch (parseError) {
|
|
console.error(`Error parsing log line at index ${index}:`, line);
|
|
}
|
|
}
|
|
});
|
|
logs = validLogs;
|
|
} catch (error) {
|
|
console.error('Error fetching logs:', error);
|
|
return;
|
|
}
|
|
|
|
if (Array.isArray(logs)) {
|
|
console.log('Processing logs array');
|
|
logs.forEach((log, index) => {
|
|
console.log(`Processing log at index ${index}`);
|
|
const msgMatch = log._msg.match(/msg_hash=0x[0-9a-fA-F]+/);
|
|
const timeMatch = log._msg.match(/receivedTime=\d+/);
|
|
const nodeIdMatch = log.kubernetes_pod_name.match(/nodes-(\d+)/);
|
|
|
|
if (msgMatch && timeMatch && nodeIdMatch) {
|
|
const msg_hash = msgMatch[0].split('=')[1];
|
|
const receivedTime = timeMatch[0].split('=')[1];
|
|
const nodeId = parseInt(nodeIdMatch[1], 10);
|
|
|
|
|
|
const logIdentifier = `${msg_hash}-${receivedTime}`;
|
|
if (!logCache.has(logIdentifier)) {
|
|
logCache.add(logIdentifier);
|
|
const logData = {
|
|
msg_hash,
|
|
receivedTime,
|
|
nodeId,
|
|
newNode: !logCache.has(`node-${nodeId}`)
|
|
};
|
|
|
|
logCache.add(`node-${nodeId}`);
|
|
|
|
const eventData = {
|
|
msg_hash: logData.msg_hash,
|
|
receivedTime: logData.receivedTime,
|
|
nodeId: logData.nodeId,
|
|
newNode: logData.newNode
|
|
};
|
|
|
|
wss.clients.forEach(client => {
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
console.log(`Sending event to client ${client._socket.remoteAddress}:`, eventData);
|
|
client.send(JSON.stringify(eventData));
|
|
}
|
|
});
|
|
} else {
|
|
console.log(`Duplicate log found: ${logIdentifier}`);
|
|
}
|
|
} else {
|
|
console.warn('Log did not match expected format');
|
|
}
|
|
});
|
|
} else {
|
|
console.error('Logs is not an array:', logs);
|
|
}
|
|
}
|
|
}, 3000);
|
|
|
|
server.listen(3000, () => {
|
|
console.log('Server is listening on port 3000');
|
|
});
|