81 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import XRegExp from 'xregexp'
 | 
						|
 | 
						|
// Parser function
 | 
						|
const parseLog = (data: string) => {
 | 
						|
  const patterns = [
 | 
						|
    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+)'),
 | 
						|
    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'),
 | 
						|
    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+)'),
 | 
						|
    XRegExp(
 | 
						|
      '[Cc]isco\\s+(?<HARDWARE>\\S+|\\S+\\d\\S+)\\s+\\(.+\\)\\s+with\\s+(?<MEMORY>.+)\\s+bytes'
 | 
						|
    ),
 | 
						|
    // XRegExp("^(?<USB_FLASH>.+)\\s+bytes\\s+of\\s+[Uu][Ss][Bb]+\\s+[Ff]lash"),
 | 
						|
    XRegExp(
 | 
						|
      '^(?<USB_FLASH>.+?)\\s+bytes\\s+of\\s+(?:' +
 | 
						|
        '[Uu][Ss][Bb]+\\s+[Ff]lash' + // USB Flash
 | 
						|
        '|ATA\\s+System\\s+CompactFlash.*' + // ATA System CompactFlash
 | 
						|
        '|flash\\s+memory\\s+at\\s+bootflash:' + // flash memory at bootflash
 | 
						|
        ')'
 | 
						|
    ),
 | 
						|
    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) => {
 | 
						|
          if (item[key] !== undefined) {
 | 
						|
            records[key] = item[key]
 | 
						|
          }
 | 
						|
        })
 | 
						|
        break
 | 
						|
      }
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  return [records]
 | 
						|
}
 | 
						|
 | 
						|
export default parseLog
 |