-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
337 lines (291 loc) · 11.1 KB
/
index.js
File metadata and controls
337 lines (291 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
module.exports = function(app) {
let plugin = {};
let unsubscribes = [];
plugin.id = 'signalk-logviewer';
plugin.name = 'Log Viewer';
plugin.description = 'View and filter SignalK server logs';
plugin.schema = {
type: 'object',
properties: {
maxLines: {
type: 'number',
title: 'Maximum lines to retrieve',
default: 2000,
minimum: 100,
maximum: 50000
}
}
};
plugin.start = function(options) {
app.debug('Plugin started');
// Function to convert TAI64N timestamp to readable format
function convertTAI64N(tai64nStr) {
try {
// Remove @ prefix
const hex = tai64nStr.substring(1);
// Parse the hex string (first 16 chars are seconds since epoch + TAI offset)
const hexSeconds = hex.substring(0, 16);
const seconds = parseInt(hexSeconds, 16);
// TAI64N starts at 1970-01-01 00:00:10 TAI (10 seconds offset)
// Convert to Unix timestamp (subtract the TAI offset: 2^62 + 10)
const TAI_OFFSET = Math.pow(2, 62) + 10;
const unixSeconds = seconds - TAI_OFFSET;
// Create date object
const date = new Date(unixSeconds * 1000);
return date.toISOString();
} catch (err) {
return tai64nStr; // Return original if conversion fails
}
}
// Function to extract original timestamp and message
function processLogLine(line) {
// Check if line starts with @4 (TAI64N format)
if (line.startsWith('@4')) {
const parts = line.split(' ');
if (parts.length > 0) {
return {
original: line,
timestamp: parts[0],
message: parts.slice(1).join(' ')
};
}
}
return {
original: line,
timestamp: null,
message: line
};
}
// Try to get logs from Victron Cerbo (Venus OS)
function getLogsFromVictronCerbo(numLines) {
const venusLogPath = '/data/log/signalk-server/current';
try {
app.debug('Attempting to read Victron Cerbo log at:', venusLogPath);
// Try to check file accessibility
fs.accessSync(venusLogPath, fs.constants.R_OK);
app.debug('Found Victron Cerbo log at:', venusLogPath);
// Read file and get last N lines
const stats = fs.statSync(venusLogPath);
const fileSize = stats.size;
app.debug('Victron log file size:', fileSize, 'bytes');
// Read last chunk of file (should be enough for numLines)
const chunkSize = Math.min(500 * numLines, fileSize); // ~500 bytes per line estimate
const buffer = Buffer.alloc(chunkSize);
const fd = fs.openSync(venusLogPath, 'r');
const startPos = Math.max(0, fileSize - chunkSize);
app.debug('Reading', chunkSize, 'bytes from position', startPos);
fs.readSync(fd, buffer, 0, chunkSize, startPos);
fs.closeSync(fd);
const data = buffer.toString('utf8');
const allLines = data.split('\n').filter(line => line.trim());
const lastLines = allLines.slice(-numLines);
app.debug('Found', allLines.length, 'total lines, returning last', lastLines.length);
// Process TAI64N timestamps - keep structured data
const processedLines = lastLines.map(processLogLine);
app.debug('Successfully read', processedLines.length, 'lines from Victron Cerbo');
return {
lines: processedLines,
path: venusLogPath,
source: 'victron-cerbo',
hasTAI64N: true
};
} catch (err) {
app.debug('Victron Cerbo log not accessible:', err.message);
return null;
}
}
// Try to get logs from journalctl if running as systemd service
function getLogsFromJournalctl(numLines) {
try {
const output = execSync(`journalctl -u signalk -n ${numLines} --no-pager --output=cat`, {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024 // 10MB buffer
});
const lines = output.trim().split('\n');
return lines.map(line => ({
original: line,
timestamp: null,
message: line
}));
} catch (err) {
app.debug('journalctl not available:', err.message);
return null;
}
}
// Try to get logs from file
function getLogsFromFile(numLines) {
try {
// Check common log file locations
const homeDir = require('os').homedir();
const possiblePaths = [
path.join(homeDir, '.signalk', 'logs', 'signalk-server.log'),
path.join(homeDir, '.signalk', 'signalk-server.log'),
'/var/log/signalk/signalk-server.log',
'/var/log/signalk.log'
];
for (const logPath of possiblePaths) {
if (fs.existsSync(logPath)) {
app.debug('Found log file at:', logPath);
const data = fs.readFileSync(logPath, 'utf8');
const lines = data.trim().split('\n').slice(-numLines);
return {
lines: lines.map(line => ({
original: line,
timestamp: null,
message: line
})),
path: logPath
};
}
}
return null;
} catch (err) {
app.debug('Error reading log file:', err.message);
return null;
}
}
// Check if we're running on Victron Venus OS (Cerbo/Octo/etc)
function isCerboSystem() {
try {
// Method 1: Check for Venus OS device hostnames + /data directory
const hostname = require('os').hostname();
app.debug('Hostname:', hostname);
// Venus OS devices have specific hostnames:
// - einstein = Cerbo GX
// - beaglebone = Octo GX
// - venus = Venus GX
const venusHostnames = ['einstein', 'beaglebone', 'venus'];
if (venusHostnames.includes(hostname) && fs.existsSync('/data')) {
app.debug(`Detected Venus OS device (hostname: ${hostname})`);
return true;
}
// Method 2: Check /etc/venus-release (if it exists on Venus OS)
if (fs.existsSync('/etc/venus-release')) {
app.debug('Detected Venus OS (/etc/venus-release exists)');
return true;
}
// Method 3: Check /etc/version (Venus OS has this file)
if (fs.existsSync('/etc/version') && fs.existsSync('/data')) {
try {
const version = fs.readFileSync('/etc/version', 'utf8').trim();
// Venus OS version format is typically a date like "20250915120900"
if (version.length === 14 && /^\d+$/.test(version)) {
app.debug('Detected Venus OS (/etc/version format + /data directory)');
return true;
}
} catch (err) {
app.debug('Could not read /etc/version:', err.message);
}
}
// Method 4: Check /etc/os-release for Venus OS identifier
if (fs.existsSync('/etc/os-release')) {
try {
const osRelease = fs.readFileSync('/etc/os-release', 'utf8');
if (osRelease.includes('venus') || osRelease.includes('Venus')) {
app.debug('Detected Venus OS (from /etc/os-release)');
return true;
}
} catch (err) {
app.debug('Could not read /etc/os-release:', err.message);
}
}
app.debug('Not a Cerbo/Venus OS system');
return false;
} catch (err) {
app.error('Error detecting Cerbo system:', err.message);
return false;
}
}
// Register API endpoint for logs
app.get('/signalk-logviewer/api/logs', (req, res) => {
const numLines = parseInt(req.query.lines) || options.maxLines || 2000;
const maxLines = Math.min(numLines, 50000);
let isCerbo = isCerboSystem();
try {
let result = null;
// Try Victron Cerbo first (Venus OS)
app.debug('Checking for Victron Cerbo logs...');
result = getLogsFromVictronCerbo(maxLines);
if (result) {
app.debug('Returning Victron Cerbo logs');
// If we successfully read from Cerbo path, it's definitely a Cerbo
isCerbo = true;
return res.json({
lines: result.lines,
path: result.path,
count: result.lines.length,
source: result.source,
hasTAI64N: result.hasTAI64N,
isCerbo: true
});
}
// If getLogsFromVictronCerbo was attempted but failed, and we haven't detected Cerbo yet,
// it might still be a Cerbo with permission issues. Check if the Cerbo log path exists
// but just couldn't be read
if (!isCerbo && fs.existsSync('/data/log/signalk-server')) {
app.debug('Detected Cerbo system (log directory exists but not readable)');
isCerbo = true;
}
// Try journalctl (most common for systemd installations)
app.debug('Trying journalctl...');
let lines = getLogsFromJournalctl(maxLines);
let source = 'journalctl';
let logPath = 'journalctl -u signalk';
// If journalctl fails, try log files
if (!lines) {
app.debug('Trying log files...');
const fileResult = getLogsFromFile(maxLines);
if (fileResult) {
lines = fileResult.lines;
logPath = fileResult.path;
source = 'file';
}
}
// If still no logs, return error
if (!lines || lines.length === 0) {
app.error('Could not find logs anywhere');
const errorMessage = isCerbo
? 'Victron Venus OS users (Cerbo GX / Octo GX / Venus GX)'
: 'Could not find logs';
const suggestion = isCerbo
? 'SSH as root to your device and execute:\nchown -R signalk:signalk /data/log/signalk-server\n\nFor persistent solution and increasing of log file (survives reboot):\n\nSee README.md of this plugin.'
: 'Check that SignalK is logging and accessible';
return res.status(404).json({
error: errorMessage,
message: 'Tried Victron Cerbo, journalctl and common log file locations',
suggestion: suggestion,
isCerbo: isCerbo
});
}
res.json({
lines: lines,
path: logPath,
count: lines.length,
source: source,
hasTAI64N: false,
isCerbo: isCerbo
});
} catch (error) {
app.error('Error reading logs:', error);
res.status(500).json({
error: error.message,
details: 'Could not fetch logs'
});
}
});
// Serve the HTML file
app.get('/plugins/signalk-logviewer/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.setPluginStatus('Running - Access at /plugins/signalk-logviewer/');
};
plugin.stop = function() {
unsubscribes.forEach(f => f());
unsubscribes = [];
app.debug('Plugin stopped');
};
return plugin;
};