I'm fairly new to TCL and need some assistance grouping log messages together based on date stamps from a Cisco router.
UPDATED: change in sample log. have discovered that there is an extra space when the DD is a single digit. e.g " 1"
Samplelog : show logging
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: OFFLINE
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 3 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Feb 3 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Feb 4 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 4 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 15 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Mar 15 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 15 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 16 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Mar 16 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 16 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
Desired output
Feb 01 || 3 down
Feb 04 || 2 down
Mar 15 || 2 down
Mar 16 || 2 down
Updated with new code with new logic to try and address extra space, although this is not working as expected.
set y [read -nonewline [set f [open "tmp.txt" r]]];
array set counts [list];
foreach message [split $y "\n"] {
# This gets the status, ie: DOWN/UP/OFFLINE.
set status [lindex [split $message] end]; # will assign last field
set mon [lindex [split $message] 1]; # number represents the filed number
set day [lindex [split $message] 2];
if {$day ==""} {
set day [lindex [split $message] 3];
} else {
set day [lindex [split $message] 2]
}
# +number represents the counter
if {[info exists counts($mon,$day,$status)]} {
set counts($mon,$day,$status) [expr {$counts($mon,$day,$status)+1}]
} else {
set counts($mon,$day,$status) 1
}
}
# sort based for down type events
puts "\n\n MMM DD || Cnt Status"
puts " ====================="
foreach count [lsort -increasing -unique [array names counts]] {
foreach {mon day status} [split $count ","] { break; }
if {$status =="down"} {
puts " $mon $day || [set counts($count)] \t $status"
}
}
if {[info exists f]} { close $f }