Wednesday, March 07, 2007

Solaris + Tcl + Apache + RRDtool = ??

It is S.T.A.R and it shines!

My colleague wants to visualise the trend of some sampling data. Back in my mind there is this voice keep telling me to crank out a demo to prove how all these wonderful opensource can do. So, I came up with a web-form to take in the raw sampling data and plot them out dynamically. Here are the stuff that I used for this little toy:

  • Solaris 10 and we run everything from a spare root zone
  • Tcl (Tool Command Language), my favourite scripting langauge. My friends used to call that "Talk Cock Language".
  • Apache 1.3.x (comes with Solaris 10)
  • RRDtool, a very powerful data logging and graphing toolkit. Blindings for Perl, Python, Ruby and Tcl are built-in in the source tarball.

Enclosed some screen dump:

The Tcl cgi script:

package require cgi package require Rrd cgi_input set ds [cgi_import ds] set step [cgi_import step] set type [cgi_import type] set title [cgi_import title] set ylabel [cgi_import ylabel] set data [string trim [cgi_import data]] # find out the start time (t0) and end time (t2) set t1 [lindex [split [lindex $data 0] :] 0] set t2 [lindex [split [lindex $data end] :] 0] set ndata [llength [split $data "\n"]] # set start [expr $t1 - 10] set end [expr $t2 + 10] set rrdfile "/tmp/[file tail $argv0]-[pid].rrd" set heartbeat [expr $step * 2] set DS {} foreach i [split $ds { ,}] { set var [string trim $i] append DS "DS:$var:$type:$heartbeat:U:U " } eval Rrd::create $rrdfile --step $step --start $start \ $DS RRA:AVERAGE:0.5:1:$ndata foreach d [split $data "\n"] { Rrd::update $rrdfile $d } set DEFS {} foreach i [split $ds { ,}] { set var [string trim $i] append DEFS "DEF:$i=$rrdfile:$i:AVERAGE " } set LINES {} set color { ff0000 00ff00 0000ff ffff00 ff00ff 00ffff 880000 008800 000088 888800 880088 008888 } set ncolor [llength $color] set cnt 0 foreach i [split $ds { ,}] { set c [lindex $color [expr $cnt % $ncolor]] set var [string trim $i] append LINES "LINE:$i#$c:$i " incr cnt } puts "Content-type: image/png\n" eval Rrd::graph - --imgformat PNG --star $t1 --end $t2 \ --title \"$title\" --vertical-label \"$ylabel\" \ $DEFS $LINES file delete -force $rrdfile

Labels: , , ,

0 Comments:

Post a Comment

<< Home