如何使用 textfsm 解析带有级联元素的 CLI 输出
How to parse CLI output with cascading elements using textfsm
我正在尝试使用 textfsm & python 解析具有级联元素的 CLI 输出。这是一个例子:参考 - https://github.com/google/textfsm/wiki/TextFSM
使用这个例子。如何获得每个插槽的 'CPU utilization' 的值?
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
模板
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
输出:
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
如您所见,CPU 利用率元素未填充。
非常感谢任何指点
我认为你的模板有两个错误:
- 你应该记录当你有所有数据。
- 您在RESlot状态的最后一行试图匹配的Model值只有在您通过输入文本中的CPU利用部分后才能匹配。请注意,textfsm 逐行解析文件。
您可以使用以下模板获取数据:
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
结果:
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
我正在尝试使用 textfsm & python 解析具有级联元素的 CLI 输出。这是一个例子:参考 - https://github.com/google/textfsm/wiki/TextFSM
使用这个例子。如何获得每个插槽的 'CPU utilization' 的值?
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
模板
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
输出:
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
如您所见,CPU 利用率元素未填充。
非常感谢任何指点
我认为你的模板有两个错误:
- 你应该记录当你有所有数据。
- 您在RESlot状态的最后一行试图匹配的Model值只有在您通过输入文本中的CPU利用部分后才能匹配。请注意,textfsm 逐行解析文件。
您可以使用以下模板获取数据:
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
结果:
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