Note that there are some explanatory texts on larger screens.

plurals
  1. POTCL: Grouping tunnel message together by date day and tunnel
    text
    copied!<p>Hi I'm pretty new to TCL coding and wrote this script with the help of this site. I was wondering if there is a better/cleaner way to write this?</p> <pre><code>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 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 evetns puts "\n\nTabl: Down per day" 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" } } # process tunnels that are down and flapped 3 times array set countsT [list]; foreach message [split $y "\n"] { # This gets the status, ie: DOWN/UP/OFFLINE. set status [lindex [split $message] end]; set mon [lindex [split $message] 1]; set day [lindex [split $message] 2]; set Tunnx [lindex [split $message] 9]; if {$day ==""} { set day [lindex [split $message] 3]; set Tunnx [lindex [split $message] 10] } else { set day [lindex [split $message] 2] } if {[info exists countsT($mon,$day,$Tunnx,$status)]} { set countsT($mon,$day,$Tunnx,$status) [expr {$countsT($mon,$day,$Tunnx,$status)+1}] } else { set countsT($mon,$day,$Tunnx,$status) 1 } } puts "\n\nTabl: Tunnel = Flap &gt;3" puts "\n\n MMM DD || Tunnelx\tCnt\t Status" puts " =================================" foreach count [lsort -increasing -unique [array names countsT]] { if {[set countsT($count)] &lt; 2} { continue; } { foreach {mon day tun n status} [split $count ","] { break; } if {$status =="down"} { puts " $mon $day || $tun \t [set countsT($count)] \t$status" } } } if {[info exists f]} { close $f } </code></pre> <p>Sample log:</p> <pre><code>670555: Mar 9 23:39:18.214: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel1, changed state to down 670557: Mar 9 23:39:50.877: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670559: Mar 9 23:41:08.662: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel4, changed state to down 670561: Mar 9 23:41:18.309: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel3, changed state to down 670562: Mar 9 23:43:13.237: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670564: Mar 9 23:45:26.549: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670567: Mar 9 23:46:45.708: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel3, changed state to down 670570: Mar 9 23:49:31.222: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670574: Mar 9 23:53:14.295: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670576: Mar 9 23:55:49.217: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670577: Mar 9 23:56:16.180: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel1, changed state to down 670581: Mar 9 23:56:45.480: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel3, changed state to down 670583: Mar 9 23:59:54.080: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670585: Mar 10 00:00:33.224: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel1, changed state to down 670587: Mar 10 00:04:03.292: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670589: Mar 10 00:04:38.921: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel3, changed state to down 670590: Mar 10 00:05:00.505: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel1, changed state to down 670593: Mar 10 00:06:22.473: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670596: Mar 10 00:09:07.262: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel1, changed state to down 670598: Mar 10 00:11:11.294: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down 670602: Mar 10 00:14:23.649: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel1, changed state to down 670604: Mar 10 00:14:59.296: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel2, changed state to down Results: Tabl: Down per day MMM DD || Cnt Status ===================== Mar 10 || 9 down Mar 9 || 13 down Tabl: Tunnel = Flap &gt;3 MMM DD || Tunnelx Cnt Status ================================= Mar 10 || Tunnel1 4 down Mar 10 || Tunnel2 4 down Mar 9 || Tunnel1 2 down Mar 9 || Tunnel2 7 down Mar 9 || Tunnel3 3 down </code></pre> <p><b>UPDATE:</b></p> <p>Required to record the amount of tunnels that flapped over the day. </p> <p>Using the table below for a data reference the new table would be Mar 10 2 tunnels that flapped and Mar 9th 3 tunnels. </p> <p>I can't see how to work out the additional grouping of tunnelx field to count number flapped tunnels.</p> <pre><code> MMM DD || Tunnelx Cnt Status ================================= Mar 10 || Tunnel1 4 down Mar 10 || Tunnel2 4 down Mar 9 || Tunnel1 2 down Mar 9 || Tunnel2 7 down Mar 9 || Tunnel3 3 down </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload