Friday, October 14, 2005

[Tcl] Remote Monitoring (rstat)

#! /usr/local/bin/tclsh
#
# On Solaris, rstat requires /etc/rc2.d/S71rpc to start and rstat in /etc/inetd.conf
# to be enabled
#
#




if { $argc < 1 || $argc > 3 } {
 puts "Usage: $argv0  \[interval\] \[iteration\]\n"
 puts {       interval [60]}
 puts {       iteration [10]}
 exit
}
set host [lindex $argv 0]
set interval [lindex $argv 1]
set iteration [lindex $argv 2]
if { [string length $interval] == 0 } {
 set interval 60
}
if { [string length $iteration] == 0 } {
 set iteration 10
}


#
# get pagesize
#set pagesize [exec pagesize]
set pagesize 8192


package require Tnm
namespace import Tnm::*


proc Sunrpc { host } {
 global old new
 global pagesize

 set new [join [sunrpc stat $host]]
 foreach { n t v } $old { set s1($n) $v }
 foreach { n t v } $new { set s2($n) $v }
 set delta [expr $s2(curtime) - $s1(curtime)] 
 foreach { n1 t1 v1 } $old { n2 t2 v2 } $new {
  switch $t1 {
   Counter {
    set r($n1) [expr $delta > 0 ?      int(double($v2-$v1)/$delta) : 0]
   }
   Gauge {
    set r($n1) [expr $v2/256.0]
   }
   default {
    set r($n1) $v2
   }
  }
 }

 set f [expr $pagesize/1024]
 puts [format {%s  %3d %3d %3d %3d  %4d %4d %4d %4d  %4d %4d  %6.2f %6.2f %6.2f}   [clock format $r(curtime) -format {%H:%M:%S}]   $r(cp_user) $r(cp_system) $r(cp_nice) $r(cp_idle)   [expr $r(v_pgpgin)*$f] [expr $r(v_pgpgout)*$f]   [expr $r(v_pswpin)*$f] [expr $r(v_pswpout)*$f]   $r(v_intr) $r(v_swtch)   $r(avenrun_0) $r(avenrun_1) $r(avenrun_2)]


 set old $new
}



#
# calculate time, uptime
#
set old [join [sunrpc stat $host]]
foreach { n t v } $old {
 set initial($n) $v
}
set curtime [clock format $initial(curtime) -format {%H:%M%p}]
set delta [expr $initial(curtime) - $initial(boottime)]
set day [expr $delta/86400]
set hr  [expr ($delta-$day*86400)/3600]
set min [expr ($delta-$day*86400-$hr*3600)/60]
puts "uptime:"
puts " $curtime  up  $day day(s), $hr:$min\n\n"


#     12345678901234567890123456789012345678901234567890123456789012345678901234567890
#     HH:MM:SS
puts "                cpu          page(kB)  swap(kB)                 run queue"
puts "          %us %sy %wt %id    pi   po   si   so  intr  csw   1-min  5-min 15-min"


after [expr $interval*1000]
job create -interval [expr $interval*1000]  -command [list Sunrpc $host]  -iterations $iteration  -exit { set forever {} }


vwait forever



# sample output
#
# ./rstat.tcl 192.168.0.1 5 10
# uptime:
#  14:12PM  up  36 day(s), 2:36
#  
#  
#                 cpu          page(kB)  swap(kB)                 run queue
#           %us %sy %wt %id    pi   po   si   so  intr  csw   1-min  5-min 15-min
# 14:12:26    0   1   0 399     0    0    0    0   270  248    0.02   0.02   0.02
# 14:12:31    0   1   0 398     0    0    0    0   269  241    0.02   0.02   0.02
# 14:12:36    0   5   0 394     0    0    0    0   275  256    0.02   0.02   0.02
# 14:12:41    0   1   0 398     0    0    0    0   276  263    0.02   0.02   0.02
# 14:12:46    0   0   0 398     0    0    0    0   281  253    0.02   0.02   0.02
# 14:12:51    0   1   0 398     0    0    0    0   274  253    0.02   0.02   0.02
# 14:12:56    0   0   0 398     0    0    0    0   274  245    0.02   0.02   0.02
# 14:13:01    0   1   0 397     0    0    0    0   270  246    0.02   0.02   0.02
# 14:13:06    0   4   0 395     0    0    0    0   272  241    0.02   0.02   0.02
# 14:13:11    0   1   0 398     0    0    0    0   268  253    0.02   0.02   0.02

0 Comments:

Post a Comment

<< Home