Watch Command

Problem this snippet solves:

The basic idea is to re-create the *nix "watch" command but to allow a user to run a show command instead. The command takes three arguments, the show command to repeat (likely encapsulated along with its arguments in quotes), the number of times to iterate the command, and the delay between each iteration.

With an alias to this command you could effectively add a new command to the shell for you to use at will.

Original Version

proc script::run {} {
  if {$tmsh::argc < 4} {
      puts "This command requires a minimum of 4 arguments, number of times to iterate, \
the delay between iterations, followed by arguments that are accepted by the show command. $::example"
      exit
  }
  set count [lindex $tmsh::argv 1]
  set delay [lindex $tmsh::argv 2]
  set command [lrange $tmsh::argv 3 end]
  set x 0
  tmsh::clear_screen
  while {($x < $count) || ($count == 0)} {
    puts "Iteration #[expr $x + 1]"
    puts "[tmsh::show $command]\n\n\n\n\n"
    incr x
    if {($x < $count) || ($count == 0)} {
      after [expr {int($delay * 1000)}]
    }
  }
}

proc script::help {} {
    if {$tmsh::argc < 3} {
      tmsh::add_help "Enter a tmsh show command to watch, how many times to run it, 
and how long to delay between each execution.\n\n$::example\n\nA value of 0 may be used to watch a show command until cancelled."
    }
    else {
        tmsh::builtin_help "show" [lrange $tmsh::argv 3 end]
    }
}

proc script::tabc {} {
    if {$tmsh::argc < 3} {
        tmsh::add_tabc "Enter   "
    }
    else {
        tmsh::builtin_tabc "show" [lrange $tmsh::argv 3 end]
    }
}

proc script::init {} {
  set ::example "I.E.: watchproc.tcl 5 10 /sys cpu"
}

Alternative implementation

Attempts to mimic the CLI watch behavior by displaying the command and time as the heading and interval is specified using the "-n" switch". This versions also supports any action (e.g., show, list).

Recommend to call the script via alias (e.g., create /cli alias watch command "run /cli script tmsh_watch").

Examples: watch -n 1 show /ltm pool watch -n 200ms show /ltm virtual

Code :

proc script::init {} {
    set ::Usage "Usage: [lindex $tmsh::argv 0] \[-n interval\] "
    # defaults
    set ::interval 2
}
proc script::run {} {
    # argument handling
    set continue 1
    for { set i 1 } { $i < [llength $tmsh::argv] } { incr i } {
        set arg [lindex $tmsh::argv $i]
        if { $continue && [string match {-*} $arg] } {
            switch -- [string range $arg 1 end] {
                n {
                    incr i
                    set ::interval [lindex $tmsh::argv $i]
                }
                - {
                    set continue 0
                }
            }
            continue
        }
        lappend argv $arg
    }
    set action [lindex $argv 0]
    set argv [lrange $argv 1 end]
    if { [llength $argv] <= 1 } {
        puts $::Usage
        return
    }
    # time interval parsing
    if { ! [string match {*ms} $::interval] } {
        set interval_str ${::interval}s
        set ::interval [expr { $::interval * 1000 }]
    } else {
        set ::interval [string range $::interval 0 end-2]
        set interval_str ${::interval}ms
    }
    while { true } {
        tmsh::clear_screen
        set time [clock format [clock seconds] -format {%a %b %m %T %Y}]
        puts [format "Every %s: %-40s %s\n" $interval_str [join [list $action $argv]] $time]
        puts [eval {tmsh::$action $argv}]
        after $::interval
    }
}
proc script::help {} {
    tmsh::add_help $::Usage
}
Published Mar 10, 2015
Version 1.0
No CommentsBe the first to comment