parsingcommand-line-interfacepython-textfsm

How to parse CLI output with cascading elements using textfsm


I am trying to parse CLI output that has cascading elements using textfsm & python. Here is an example: Ref - https://github.com/google/textfsm/wiki/TextFSM

Using this example. How can I get the value of 'CPU utilization' for each Slot ?

Routing Engine status:
  Slot 0:
    Current state                  Master
    Election priority              Master (default)
    Temperature                 39 degrees C / 102 degrees F
    CPU temperature             55 degrees C / 131 degrees F
    DRAM                      2048 MB
    Memory utilization          76 percent
    CPU utilization:
      User                      95 percent
      Background                 0 percent
      Kernel                     4 percent
      Interrupt                  1 percent
      Idle                       0 percent
    Model                          RE-4.0
    Serial ID                      xxxxxxxxxxxx
    Start time                     2008-04-10 20:32:25 PDT
    Uptime                         180 days, 22 hours, 45 minutes, 20 seconds
    Load averages:                 1 minute   5 minute  15 minute
                                       0.96       1.03       1.03
Routing Engine status:
  Slot 1:
    Current state                  Backup
    Election priority              Backup
    Temperature                 30 degrees C / 86 degrees F
    CPU temperature             31 degrees C / 87 degrees F
    DRAM                      2048 MB
    Memory utilization          14 percent
    CPU utilization:
      User                       0 percent
      Background                 0 percent
      Kernel                     0 percent
      Interrupt                  1 percent
      Idle                      99 percent
    Model                          RE-4.0
    Serial ID                      xxxxxxxxxxxx
    Start time                     2008-01-22 07:32:10 PST
    Uptime                         260 days, 10 hours, 45 minutes, 39 seconds

Template

Value Required Slot (\d+)
Value State (\w+)
Value Temp (\d+)
Value CPUTemp (\d+)
Value DRAM (\d+)
Value User (\d+)
Value Background (\d+)
Value Kernel (\d+)
Value Interrupt (\d+)
Value Idle (\d+)
Value Model (\S+)


Start
  ^Routing Engine status: -> Record RESlot
  ^\s+CPU utilization: -> Record SUBRESlot

RESlot
  ^\s+Slot\s+${Slot}
  ^\s+Current state\s+${State}
  ^\s+Temperature\s+${Temp} degrees
  ^\s+CPU temperature\s+${CPUTemp} degrees
  ^\s+DRAM\s+${DRAM} MB
  ^\s+Model\s+${Model} -> Start

SUBRESlot
  ^\s+User\s+${User}\s+percent
  ^\s+backgroud\s+${Background}\s+percent
  ^\s+Kernel\s+${Kernel}\s+percent
  ^\s+Interrupt\s+${Interrupt}\s+percent
  ^\s+Idle\s+${Idle}\s+percent -> Start

Output:

Slot, State, Temp, CPUTemp, DRAM, User, Background, Kernel, Interrupt, Idle, Model
0, Master, 39, 55, 2048, , , , , , RE-4.0
1, Backup, 30, 31, 2048, , , , , , RE-4.0

As you can see the CPU utilization elements are not getting populated.

I would really appreciate any pointer


Solution

  • I consider there are two mistakes in your template:

    1. You should Record when you have all data.
    2. The Model value you are trying to match in the last line of RESlot state can be matched only after you passed the CPU utilization section in the input text. Please note that textfsm parses the file line by line.

    You can use below template to get your data:

    Value Required Slot (\d+)
    Value State (\w+)
    Value Temp (\d+)
    Value CPUTemp (\d+)
    Value DRAM (\d+)
    Value User (\d+)
    Value Background (\d+)
    Value Kernel (\d+)
    Value Interrupt (\d+)
    Value Idle (\d+)
    Value Model (\S+)
    
    
    Start
      ^Routing Engine status: -> RESlot
      ^\s+CPU utilization: -> SUBRESlot
    
    
    RESlot
      ^\s+Slot\s+${Slot}
      ^\s+Current state\s+${State}
      ^\s+Temperature\s+${Temp} degrees
      ^\s+CPU temperature\s+${CPUTemp} degrees
      ^\s+DRAM\s+${DRAM} MB -> Start
      
    
    SUBRESlot
      ^\s+User\s+${User}\s+percent
      ^\s+Background\s+${Background}\s+percent
      ^\s+Kernel\s+${Kernel}\s+percent
      ^\s+Interrupt\s+${Interrupt}\s+percent
      ^\s+Idle\s+${Idle}\s+percent -> SUBModel
      
    SUBModel
      ^\s+Model\s+${Model} -> Record Start
    

    Result:

    Slot, State, Temp, CPUTemp, DRAM, User, Background, Kernel, Interrupt, Idle, Model
    0, Master, 39, 55, 2048, 95, 0, 4, 1, 0, RE-4.0
    1, Backup, 30, 31, 2048, 0, 0, 0, 1, 99, RE-4.0