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

91 lines
2.8 KiB
TypeScript

import XRegExp from 'xregexp'
// Parser function
const parseLog = (data: string) => {
const patterns = [
// 1. Software Image & Version
XRegExp(
'^.*Software.*\\((?<SOFTWARE_IMAGE>\\S+)\\),\\s+Version\\s+(?<VERSION>.+?),\\s+RELEASE.*\\((?<RELEASE>\\S+)\\)'
),
XRegExp('Active-image:\\s+(?<SOFTWARE_IMAGE>\\S+)'),
XRegExp('Version:\\s+(?<VERSION>\\S+)'),
XRegExp('^ROM:\\s+(?<ROMMON>\\S+)'),
// 2. Uptime
XRegExp('^(?<HOSTNAME>\\S+)\\s+uptime\\s+is\\s+(?<UPTIME>.+)'),
XRegExp('Date:\\s+(?<UPTIME>\\S+)'),
XRegExp('uptime\\s+is.*\\s+(?<UPTIME_YEARS>\\d+)\\syear'),
XRegExp('uptime\\s+is.*\\s+(?<UPTIME_WEEKS>\\d+)\\sweek'),
XRegExp('uptime\\s+is.*\\s+(?<UPTIME_DAYS>\\d+)\\sday'),
XRegExp('uptime\\s+is.*\\s+(?<UPTIME_HOURS>\\d+)\\shour'),
XRegExp('uptime\\s+is.*\\s+(?<UPTIME_MINUTES>\\d+)\\sminute'),
// 3. Reload Reason & Serial
XRegExp('System\\s+image\\s+file\\s+is\\s+"(?:.*?):(?<RUNNING_IMAGE>\\S+)"'),
XRegExp('(?:Last reload reason:|System returned to ROM by)\\s+(?<RELOAD_REASON>.+)'),
XRegExp('[Pp]rocessor\\s+board\\s+ID\\s+(?<SERIAL>\\w+)'),
// 4. MEMORY & HARDWARE
XRegExp('^(?<MEMORY>\\d+[KMG])\\s+bytes\\s+of\\s+physical\\s+memory', 'i'),
XRegExp(
'[Cc]isco\\s+(?<HARDWARE>\\S+).+?with\\s+(?<MEMORY>\\d+[KMG]\\/\\d+[KMG])\\s+bytes\\s+of\\s+memory',
'i'
),
// 5. Flash / Storage
XRegExp(
'^(?<USB_FLASH>\\d+[KMG])\\s+bytes\\s+of\\s+(?:' +
'[Uu][Ss][Bb]+\\s+[Ff]lash' +
'|ATA\\s+System\\s+CompactFlash.*' +
'|flash\\s+memory\\s+at\\s+bootflash:' +
')',
'i'
),
// 6. Config & MAC
XRegExp(
'Base\\s+[Ee]thernet\\s+MAC\\s+[Aa]ddress\\s+:\\s+(?<MAC_ADDRESS>[0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5})'
),
XRegExp('Configuration\\s+register\\s+is\\s+(?<CONFIG_REGISTER>\\S+)'),
]
const lines = data.split('\n')
const records: any = {
SOFTWARE_IMAGE: '',
VERSION: '',
RELEASE: '',
ROMMON: '',
HOSTNAME: '',
UPTIME: '',
UPTIME_YEARS: '',
UPTIME_WEEKS: '',
UPTIME_DAYS: '',
UPTIME_HOURS: '',
UPTIME_MINUTES: '',
RELOAD_REASON: '',
RUNNING_IMAGE: '',
HARDWARE: '',
SERIAL: '',
CONFIG_REGISTER: '',
MAC_ADDRESS: '',
MEMORY: '',
USB_FLASH: '',
}
for (const line of lines) {
for (const pattern of patterns) {
const match = XRegExp.exec(line, pattern)
if (match) {
const item = match?.groups || {}
Object.keys(item).forEach((key) => {
// Chỉ update nếu giá trị tìm thấy không undefined
if (item[key] !== undefined) {
records[key] = item[key]
}
})
// Break inner loop (patterns) để sang dòng tiếp theo
break
}
}
}
return [records]
}
export default parseLog