Improve log handling and email reporting

Refactored log buffer management to use 'allBuffer' for complete log storage and updated email content to include raw logs. Enhanced log table formatting and removed status from email header. Commented out interface up/down rules in helper.ts to adjust log rule detection.
This commit is contained in:
nguyentrungthat 2025-12-23 09:36:46 +07:00
parent 0af1cd8747
commit 48a50b5628
2 changed files with 41 additions and 40 deletions

View File

@ -169,15 +169,15 @@ export default class LineConnection {
lineNumber,
status: 'connected',
})
// this.checkLog()
this.checkLog()
resolve()
}, 1000)
})
this.client.on('data', (data) => {
let message = this.connecting ? cleanData(data.toString()) : data.toString()
// const lines = this.bufferLog.push(data)
// lines.forEach(this.handleLogLine)
const lines = this.bufferLog.push(data)
lines.forEach(this.handleLogLine)
let rawData = ''
if (this.isRunningScript) {
this.waitingScenario = true
@ -806,31 +806,31 @@ export default class LineConnection {
try {
if (this.config.status !== 'connected') {
clearInterval(interval)
this.session.clear()
this.bufferLog.clear()
return
}
const result = this.session.finalize()
if (result.errors.length === 0) return
if (result.errors.length === 0) {
this.session.clear()
this.bufferLog.clear()
return
}
// console.log('===== TEST RESULT =====')
// console.log('STATUS:', result.status)
// console.log('SUMMARY:', result.summary)
// result.errors.forEach((err, idx) => {
// console.log(`\n[${idx + 1}] ${err.level} - ${err.ruleId}`)
// console.log('Message:', err.message)
// console.log('Log:', err.evidence.raw)
// })
const detectLog = await this.detectLogWithAI(this.bufferLog.buffer)
const detectLog = await this.detectLogWithAI(this.bufferLog.allBuffer)
// console.log(detectLog)
const tableHTML = this.buildEmailContent(result, detectLog)
await sendMessageToMail(
'andrew.ng@apactech.io',
`[${result.status}] - [${this.config.stationName} - Line: ${this.config.lineNumber}] - Cisco Device Log Result`,
tableHTML
// ,
// ['ips@ipsupply.com.au', 'kay@ipsupply.com.au', 'joseph@apactech.io']
`[ATC] - [${this.config.stationName} - Line: ${this.config.lineNumber}] - Raw log issue`,
tableHTML +
`${`
<hr />
<p>Logs:</p>
<div style="white-space: break-spaces; background-color: #f5f5f5; color: black; padding: 8px; max-height: 500px; overflow-y: scroll; border: 1px #ccc solid;"><span style="color: black;">
${this.bufferLog.allBuffer}</span></div>`}`,
['ips@ipsupply.com.au', 'kay@ipsupply.com.au', 'joseph@apactech.io']
)
this.session.clear()
this.bufferLog.clear()
@ -847,8 +847,8 @@ export default class LineConnection {
const header = `
<tr>
<th style="padding:6px;">Level</th>
<th style="padding:6px;">Rule</th>
<th style="padding:6px; text-align:center;">Level</th>
<th style="padding:6px; text-align:center;">Rule</th>
<th style="padding:6px; text-align:center;">Message</th>
<th style="padding:6px; width:1000px;">Log Evidence</th>
</tr>
@ -881,7 +881,7 @@ export default class LineConnection {
renderAIDetectTable(row: any): string {
return `
<table border="1" cellpadding="6" style="border-collapse: collapse; width:100%;">
<table border="1" cellpadding="6" style="border-collapse: collapse; width:100%; margin-bottom: 15px;">
<tr>
<th style="padding:6px;">Summary</th>
<th style="padding:6px;">Issues</th>
@ -902,7 +902,6 @@ export default class LineConnection {
return `
<h3>Cisco Device Log Result</h3>
<p>Line: <b>${this.config.lineNumber}</b> - Station: <b>${this.config.stationName}</b></p>
<p><b>Status:</b> ${result.status}</p>
<p><b>Summary:</b> ${result.summary}</p>
<hr />
${table}

View File

@ -410,20 +410,20 @@ export const RULES: LogRule[] = [
message: 'Critical features disabled by license',
},
// INTERFACE
{
id: 'INTERFACE_UP',
category: 'INTERFACE',
match: /LINK-3-UPDOWN: Interface .* up/i,
level: 'PASS',
message: 'Interface up',
},
{
id: 'INTERFACE_FLAP',
category: 'INTERFACE',
match: /LINK-3-UPDOWN: Interface .* down/i,
level: 'WARN',
message: 'Interface flapping detected',
},
// {
// id: 'INTERFACE_UP',
// category: 'INTERFACE',
// match: /LINK-3-UPDOWN: Interface .* up/i,
// level: 'PASS',
// message: 'Interface up',
// },
// {
// id: 'INTERFACE_FLAP',
// category: 'INTERFACE',
// match: /LINK-3-UPDOWN: Interface .* down/i,
// level: 'WARN',
// message: 'Interface flapping detected',
// },
{
id: 'INTERFACE_ERROR',
category: 'INTERFACE',
@ -576,15 +576,17 @@ export class TestSession {
}
export class LogStreamBuffer {
public buffer = ''
public allBuffer = ''
private buffer = ''
public push(chunk: Buffer): string[] {
this.buffer += chunk.toString('utf8').replace('--More--', '').trim()
this.allBuffer += cleanData(chunk.toString())
const lines = this.buffer.split(/\r?\n/)
this.buffer = lines.pop() || ''
return lines.map((l) => l.trim()).filter(Boolean)
return lines.map((l) => l.replaceAll('--More--', '').trim()).filter(Boolean)
}
public flush(): string | null {
@ -595,7 +597,7 @@ export class LogStreamBuffer {
}
public clear() {
this.buffer = ''
this.allBuffer = ''
}
}