ATC_SIMPLE/BACKEND/app/ultils/templates/index.ts

110 lines
3.2 KiB
TypeScript

import showInventory from './show_inventory.js'
import showVersion from './show_version.js'
import showLicense from './show_license.js'
import showLogging from './show_logging.js'
import showEnv from './show_env.js'
// Function to parse logs
function getStructuredDataTextfsm(output: string, command: string) {
switch (command) {
case 'show inventory':
case 'show inv':
case 'sh inventory':
case 'sh inv':
return showInventory(output)
case 'show version':
case 'show ver':
case 'sh version':
case 'sh ver':
return showVersion(output)
case 'show license':
case 'show lic':
case 'sh license':
case 'sh lic':
return showLicense(output)
case 'show logging':
case 'show log':
case 'sh logging':
case 'sh log':
return showLogging(output)
case 'show environment':
case 'show env':
case 'sh environment':
case 'sh env':
case 'show environment all':
case 'show env all':
case 'sh environment all':
case 'sh env all':
return showEnv(output)
default:
return ''
}
// Call the parseLog method with log data and patterns
}
export const textfsmResults = (logContent: string, cmd: string) => {
let results = []
if (cmd) {
let structuredOutput = getStructuredDataTextfsm(logContent, cmd)
if (typeof structuredOutput === 'string') {
structuredOutput = {} // Convert to an empty object if it's a string
}
results = [
{
command: cmd,
output: logContent,
textfsm: JSON.stringify(structuredOutput).replace(/[\x00-\x1f\x7f-\x9f]/g, ''),
},
]
} else {
// Regular expression to parse commands and outputs inside the scoped content
const regexPattern =
/---send-command---"(?<command>.+?)"---\d+---(?<output>[\s\S]*?)(?=(---send-command---|---split-point---|---end-testing---|---end-scenarios---))/gms
// Parse commands and outputs
const matches = [...logContent.matchAll(regexPattern)]
const mergedMatches = []
for (let match of matches) {
const m = match
const command = m.groups?.command?.trim() || ''
if (command?.toLowerCase() === 'show version | include license') {
// Gộp vào phần tử trước
if (mergedMatches.length > 1) {
const lastMatch = mergedMatches[mergedMatches.length - 1]
if (lastMatch?.groups?.output) lastMatch.groups.output += '\n' + m?.groups?.output
}
continue
}
mergedMatches.push(m)
}
// Process matches
results = mergedMatches
.map((match) => {
const command = match.groups?.command.trim() || ''
const output = match.groups?.output.trim() || ''
// Get structured output using the parser
let structuredOutput = getStructuredDataTextfsm(output, command)
if (typeof structuredOutput === 'string') {
structuredOutput = {} // Convert to an empty object if it's a string
}
return {
command,
output,
textfsm: JSON.stringify(structuredOutput).replace(/[\x00-\x1f\x7f-\x9f]/g, ''), // Clean special characters
}
})
.filter((el) => el.command)
}
return results
}