Aug 172011
 

This is a small utility I wrote in the process of my work. It is a bit ugly and nonetheless non-elegant and non-reliable, because the actual CPU utilization it measures for the switch also has the CPU needed to execute the “show system cpu” command from switch side.

Will be much more elegant if the switch reports the data to SNMP trap collector, but anyway. It will help you help yourself write your own script for this. This one is made to monitor the CPU while you work on other tasks.

The utility uses Tcl:Expect, Tcl:Math and Tcl:Tk. All of them are included in TclLibs package of your chosen distribution. The code is set to use SSH to the switch, but easily can be modified to work also with Telnet. Just un-comment the corresponding lines and set the “proto” value to be “telnet”. The other important values are ip, user, pass, enable, enablepass and the actual command the switch needs to show the CPU utilization. You can also tweak values of Delay and Width. I use 1000 and  600, because those are 10 minutes in one band-graph 100 pixels high.

CPU Utilization

CPU Utilization

Few disclaimers.

  • The code is not very good and will raise exceptions if the switch does not respond in time.
  • The code assumes that the result line will end in 1 to 3 digits followed by %-sign. (e.g. the result must say “current cpu is 100%” or “CPU 33%”)
  • There is virtually no guarantee the code will show you accurate values. It is for monitoring only.
  • When 10 minutes pass, the bar graph will start to scroll from right to left. No data is kept anywhere.
  • Part of this code is copy&pasted verbatim from the Tcl forums and discussions
#!/usr/bin/wish

package require Expect

set proto ssh
set ip 10.3.155.50
set user "admin"
set pass "admin"
set enable "enable"
set enablepass "enableadmin"
set cpu_command "show system cpu"

# Uncomment next line in case of simple Telnet connection
# spawn $proto $ip
# Uncomment next line in case of SSH connection
spawn $proto $user@$ip

expect -re "name:"
exp_send "$user\r"
expect -re "word:"
exp_send "$pass\r"

# Uncomment in case you need "enable" and password
#expect -re ">"
#exp_send "$enable\r"
#expect -re "word:"
#exp_send "$enablepass\r"

	proc cpu {} {
		global cpu_command
		expect -re "#"
		exp_send "$cpu_command\r"
		expect -re "(\\d*)\\s*%"
		return $expect_out(1,string)
	}

  array set params \
  {
    title     {CPU utilization}
    width     600
    height    100
    delay     1000
    plot      [cpu]
    t0        0
    t1        -1
  }

  # compute heights
  set h $params(height)
  set h1 [expr {int($h * 0.5)}]  ;# grey line @ 50%
  set h2 [expr {$h1 + 1}]
  # create canvas & bindings
  canvas .c -width $params(width) -height $h \
            -xscrollincrement 1 -bg beige
  pack .c
  bind .  { exit }
  # plotting itself
  wm title . $params(title)
  .c xview scroll $params(t0) unit
  set t $params(t0)
  while {$t != $params(t1)} \
  {
    update
    after $params(delay)
    set v [expr ($h - $params(plot))]
    .c create line $t $h1 $t $h2 -fill gray
    .c create rectangle $t $v $t $h
    incr t
    if {$t > $params(width)} { .c xview scroll 1 unit }
  }
 Posted by at 3:25 pm

Sorry, the comment form is closed at this time.