#!/bin/sh
# the next line restarts using wish and correctly pass the arguments to this\
exec wish "$0" -- "$@"


###############################################################################
#
# Copyright (c) 2000, 2001 David Doubrava (linux_monitor@volny.cz)
#
# Primary-site:	http://www.volny.cz:/linux_monitor/isag
#
# This program/module is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by Free Software Foundation; either version 2 of the
# License, or (at your option) any later version
#  
# This program is distributed in the hope that it will be usefull,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
###############################################################################
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
#
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.17  2001/07/30 18:05:41  sarrep
# fix: bug2001060501; set LC_ALL to sane value; see Debian bug #101545
#
# Revision 1.16  2001/04/22 14:19:34  sarrep
# Added GPL related text
# remove notes about solving R switch problems
#
# Revision 1.15  2001/03/17 17:30:15  sarrep
# CVSizing, unified headers of each element file
#
# Revision 1.14  2001/03/17 16:55:26  sarrep
# LINUX RESTART zeroes all counters.
#
# Revision 1.13  2001/03/11 20:15:19  sarrep
# First step after CVSizing
#
# Revision 1.12  2001/02/20 19:06:10  sarrep
# added q switch and actialize B switch to version 3.3.6
#
# Revision 1.11  2001/02/10 10:42:03  sarrep
# finally renamed from sarrep to isag
# added additional commnts in header
# added patching
#
# Revision 1.10  2001/02/03 11:02:17  sarrep
# removed many same (for family) parts to different files.
# improved generationg of cmd array.
#
# Revision 1.9  2001/01/27 19:28:43  root
# added secondary Y axes functionality
# added -c cfg_file cmdln option
# writting of selected config data to cfg_file
# renamed chart lines to names used in sar
# parametrized creating of cmd() array
# added generic var __M4_TIMELEN__ for proc_line_generic
#
# Revision 1.8  2001/01/20 19:02:53  root
# added version check due to bug.2312200001
# added secondary Y label
#
# Revision 1.7  2000/12/30 18:35:06  root
# split into few parts. main idea next extensibility for other platforms
#
# Revision 1.6  2000/12/30 11:30:20  root
# command line argument(s) functionality was added
# preparation for config file was done
# and few small changes
#
# Revision 1.5  2000/12/23 21:22:10  root
# fixed situation if LINUX RESTART is in data file
# fixed if no more than one line in data file.
# try to fix wrong graph size after resizing appl. window
#
# Revision 1.4  2000/12/23 20:48:31  root
# function rescale_graph was renamed to redraw_graph
# and one menu item was added.
#
# Revision 1.3  2000/12/16 13:37:12  root
# resize window related functions
#
# Revision 1.2  2000/12/11 18:54:17  root
# behavior will be parametrized
#
# Revision 1.1  2000/12/11 15:01:07  root
# Initial revision
#
#
###############################################################################

# specific for sebastien's sar

#
###############################################################################
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.3  2001/03/17 17:43:07  sarrep
# About dialog modify
#
# Revision 1.2  2001/03/17 17:30:26  sarrep
# CVSizing, unified headers of each element file
#
#
###############################################################################

set ident "/usr/bin/ident"
set version ""
# This can be set only if small interactive change in result code was occured
set patchlevel ""
if {[catch {set fp [open "| $ident $argv0" "r"]}] == 0} {
	set n 0
	while {[gets $fp line] != -1} {
		if {$n != 0 && -1 != [string first "Id:" $line]} {
			set version "$version\n[string trim $line]"
		} else {
			set n 1
		}
	}
} else {
	set version "Unknown! $ident is missing"
}
if {"" != $patchlevel} {
	set version "$version\nPatch level: $patchlevel"
}
# eof: version.m4
###############################################################################
# Message box functionality
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.4  2001/06/03 19:58:04  sarrep
# improved behaviour, added grab capability
# removed (softly) Cancel button
#
# Revision 1.3  2001/03/17 17:30:22  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################

proc msg_box_btn {w} {
	destroy $w
}

proc msg_box {title text} {
	toplevel .msg_box
	
	wm title .msg_box $title
	label .msg_box.l -text $text -justify left
	pack .msg_box.l -side top
	frame .msg_box.b
	pack .msg_box.b -side top

	button .msg_box.b.ok -text OK -command {msg_box_btn .msg_box}
	pack .msg_box.b.ok -side left

#	button .msg_box.b.cancel -text cancel -command {msg_box_btn .msg_box}
#	pack .msg_box.b.cancel -side left

	grab .msg_box
	tkwait window .msg_box
}

#eof: msg_box.m4
##############################################################################
# Tcl/Tk: version check
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.3  2001/03/17 17:30:25  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################
set needed_version 8.0
if {$tcl_version < $needed_version} {
	msg_box "Warning Version check" "Sarrep needs $needed_version, but this is version $tcl_version of Tcl.\nSome features can fail"
#	exit
}

#eof: ver_check.m4
###############################################################################
# default paths, programs and other variables
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.3  2001/03/17 17:30:24  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################

set gnuplot	"/usr/bin/gnuplot"
set grep	"/bin/grep"
set sh		"/bin/sh"
set gunzip	"/usr/bin/gunzip"
set sed		"/bin/sed"

# default placement of config file
set cfg_file	"$env(HOME)/.isag.cfg"
set graph_wd	720
set graph_ht	400

#eof: variables.m4

# specific paths and programs
set sar_data_path "/var/log/sa"
set sar_data_mask "sa\[0-9\]\[0-9\]"
set sar "/usr/bin/sar"
#set sar "/home/sarrep/source/sysstat-3.3.6/sar"

#fix: bug2001060501; set LC_ALL to sane value; see Debian bug #101545 
set env(LC_NUMERIC) "C"


###############################################################################
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.3  2001/03/17 17:30:20  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################
proc Usage {} {
	global sar_data_path sar_data_mask cfg_file graph_wd graph_ht

	msg_box "Usage" \
"isag \[options\]

Options:
\t-p data_path (default: $sar_data_path)
\t-m data_file_mask (default: $sar_data_mask)
\t-c cfg_file (default: $cfg_file) 
\t-gwd graph_width (default: $graph_wd)
\t-ght graph_height (default: $graph_ht) "
        exit
}

proc getopt {argv opt} {
	set rv ""
	if {-1 != [set t [lsearch -exact $argv "-$opt"]]} {
        	set rv [lindex $argv [expr $t + 1]]
	}
	return $rv
}

if {-1 != [set t [lsearch -exact $argv "-h"]]} {
	        Usage
}
if {-1 != [set t [lsearch -exact $argv "-?"]]} {
	        Usage
}
if {[set t [getopt $argv c]] != ""} {
	# check for path
	if {1 == ([file exist $t] && [file isfile $t])} {
		set cfg_file $t
	} else {
		msg_box "Error" "<$t> is not regular file using default <$cfg_file>"
	}
}
if {[set t [getopt $argv p]] != ""} {
	# check for path
	if {1 == ([file exist $t] && [file isdirectory $t])} {
		set sar_data_path $t
	} else {
		msg_box "Error" "<$t> is not directory using default <$sar_data_path>"
	}
}
if {[set t [getopt $argv m]] != ""} {
		set sar_data_mask $t
}
if {[set t [getopt $argv ght]] != ""} {
	if {1 == [string is digit $t]} {
		set graph_ht $t
	} else {
		msg_box "Error" "<$t> is not decimal number default <$graph_ht>"
	}
}
if {[set t [getopt $argv gwd]] != ""} {
	if {1 == [string is digit $t]} {
		set graph_wd $t
	} else {
		msg_box "Error" "<$t> is not decimal number default <$graph_wd>"
	}
}

unset t
# eof: isag_common_options.m4

###############################################################################
#
# Variables connected to graphs and their default values
#
# selected archive file for sa data, typically saNN
# this variable is set thru: data Chooser (see: Concept Guide)
set archive ""

# last_graph contains last drawing function as letter
set last_graph ""

# sar switches as names for object-instances (final and curent)
#set prog_swtch [list b B c n q r R u v w W y]
set  prog_swtch [list b B c   q r R u v w W]

# maximal and current values for each graph indexed by sar switch i.e. for cpu 
# is index u not -u

foreach l $prog_swtch {
	set val_max($l) 0
	set val_cur($l) 0
	set has_negative($l) 0
}
unset l
# exceptions
set val_max(u) 100
set has_negative(R) 1

set val_max(W) 1
set val_cur(W) 1

# graph/view names this is shoved in menu-view and as name of the graph
# and other functional parameters
#	cmd - command line which is used to generate textual source
#	xlabel - label for x-axis
#	ylabel - label for y-axis
#	y2label - label for secondary y-axis
#	plotstr - string plot for gnuplot, describes a graph
#
# view_n(ame) array
set view_n(b) "I/O transfer rate"
set view_n(B) "Paging statistics"
set view_n(c) "Process creation"
set view_n(n) "Network statistics"
set view_n(q) "Run Queue"
set view_n(r) "Memory and Swap"
set view_n(R) "Memory statistics"
set view_n(R) "Memory Activities"
set view_n(u) "CPU Utilization"
set view_n(v) "inode status"
set view_n(w) "System switching"
set view_n(W) "System swapping"

# xlabel is not an array - all values are same
set xlabel "Time"

# ylabel array
set ylabel(b) "ops/sec"
set ylabel(B) "pages/sec"
set ylabel(c) "procs/sec"
set ylabel(n) ""
set ylabel(q) ""
set ylabel(r) "KB"
set ylabel(R) "ops/sec"
set ylabel(u) "Percent"
set ylabel(v) ""
set ylabel(w) "cswch/sec"
set ylabel(W) "swp/sec"

set y2label(b) ""
set y2label(B) ""
set y2label(c) ""
set y2label(n) ""
set y2label(q) ""
set y2label(r) ""
set y2label(R) ""
set y2label(u) ""
set y2label(v) ""
set y2label(w) ""
set y2label(W) ""

# out_tokens describes how is input file covered
# if no list specified the proc_line_$l() is called
# Note: first element has index 0 (zero)!
set out_tokens(b) [list 1 2 3 4]
set out_tokens2(b) ""
set out_tokens(B) [list 0 1 3 4 5 6]
set out_tokens2(B) ""
set out_tokens(c) [list 0]
set out_tokens2(c) ""
set out_tokens(q) [list 0 1 2 3]
set out_tokens2(q) ""
set out_tokens(r) [list 0 1 3 4 5 6 7]
set out_tokens2(r) ""
set out_tokens(R) [list 0 1 2 3]
set out_tokens2(R) ""
# CPU needs special computation
set out_tokens(u) ""
set out_tokens2(u) ""
set out_tokens(v) [list 1 3 5 7 9]
set out_tokens2(v) ""
set out_tokens(w) [list 0]
set out_tokens2(w) ""
set out_tokens(W) [list 0 1]
set out_tokens2(W) ""

# cmd(line) array - this must be refreshed every time if source file is changing
# beacuse strings are containing actual data file
set cmd(0) ""
proc reget_cmd {} {
 global cmd sar archive grep sar_data_path

 # this can avoid some errors, WARNING on pipe symbol on beggining
 set commstr "| $grep '^\[0-9\]' | $grep -v '^$'"

 set cmd(b) "$sar -b -f $sar_data_path/$archive | $grep -v 'tps' $commstr"
 set cmd(B) "$sar -B -f $sar_data_path/$archive |  $grep -v 'pgpgin' $commstr"
 set cmd(c) "$sar -c -f $sar_data_path/$archive | $grep -v 'proc' $commstr"
 set cmd(n) ""
 set cmd(q) "$sar -q -f $sar_data_path/$archive | $grep -v 'runq-sz' $commstr"
 set cmd(r) "$sar -r -f $sar_data_path/$archive | $grep -v 'kbmemfree' $commstr"
 set cmd(R) "$sar -R -f $sar_data_path/$archive | $grep -v 'campg' $commstr"
 set cmd(u) "$sar -u -f $sar_data_path/$archive | $grep -v 'user' $commstr"
 set cmd(v) "$sar -v -f $sar_data_path/$archive | $grep -v 'dentunusd' $commstr"
 set cmd(w) "$sar -w -f $sar_data_path/$archive | $grep -v 'cswch' $commstr"
 set cmd(W) "$sar -W -f $sar_data_path/$archive | $grep -v 'pswpin' $commstr"
}
reget_cmd

# Functions which are responsible for data source selection and processing
# funcs are referred from GUI
proc fill_file_menu {} {
	global sar_data_path sar_data_mask

		set fp [open "| sh -c \"ls $sar_data_path/$sar_data_mask\""]
	menu .file.menu.m -tearoff false
	while {[gets $fp line] != -1} {
		set l [string last "/" $line]
		set line [string range $line [expr $l + 1] end]
				.file.menu.m add command -label "$line" -command "set_file $line"
		
	}
	catch {close $fp}
}

proc set_file {name} {
	global archive last_graph

	set archive $name
	.file.lbl configure -text "data source: $name"

	reget_cmd
	if {"" != $last_graph} {
		new_chart $last_graph
	}
}

###############################################################################
# GUI
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.7  2001/03/17 17:43:06  sarrep
# About dialog modify
#
# Revision 1.6  2001/03/17 17:30:11  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################
set title "System Activity Grapher"
wm title . $title

frame .menu -relief raised -borderwidth 2
pack .menu -side top -fill x
menubutton .menu.file -text "Program" -menu .menu.file.m -underline 0
menu .menu.file.m -tearoff false
.menu.file.m add command -label "Redraw" -underline 0 \
        -command "set gr_scale_old 0;redraw_graph" -accelerator "F5"
.menu.file.m add separator
.menu.file.m add command -label "Exit program" -underline 0 \
        -command "exit_program" -accelerator "Alt-X"

menubutton .menu.chart -text "Chart" -menu .menu.chart.m -underline 0
menu .menu.chart.m -tearoff true

# creates for each sar-switch menu item with apropriate command
foreach l $prog_swtch {
	.menu.chart.m add command -label $view_n($l) \
		-command "new_chart $l" -accelerator $l
	bind all $l "new_chart $l"
}

menubutton .menu.help -text "Help" -menu .menu.help.m -underline 0
menu .menu.help.m -tearoff false
.menu.help.m add command -label "About" -underline 0 \
        -command {About}

pack .menu.file .menu.chart -side left
pack .menu.help -side right

frame .file -relief raised
pack .file -side top -fill x
label .file.lbl -text "(no file)" -borderwidth 2
menubutton .file.menu -relief raised -borderwidth 2 -text "-" -menu .file.menu.m
fill_file_menu
pack .file.lbl .file.menu -side left

frame .graph
pack .graph -side top -expand true -fill both

# this var contains a current value
set gr_scale 0
scale .graph.scale -orient vertical -length $graph_ht -to 0 -from 1 -variable gr_scale

bind .graph.scale <Leave> "redraw_graph"

canvas .graph.canv -width $graph_wd -height $graph_ht
pack .graph.scale -side left -fill y
pack .graph.canv -side right -expand true -fill both


#bind all <Enter> "%W configure -highlightbackground blue"
#bind all <Leave> "%W configure -highlightbackground white"

bind all <Alt-x> "exit_program"
bind all <F5> "set gr_scale_old 0;redraw_graph"

###############################################################################
#
# this function normalizes max val and sets scale and so on.
# this part is most common for draw_? functions
proc set_cur_max {l} {
	global val_max gr_scale val_cur

	set norm [num_norm $val_max($l)]
	set val_max($l) [lindex $norm 0]
	.graph.scale configure -from $val_max($l) -resolution [lindex $norm 1]
	unset norm
	
	set val_cur($l) $gr_scale
	if {$val_cur($l) == 0} {
		set val_cur($l) $val_max($l)
		set gr_scale $val_cur($l)
	}
}

###############################################################################
#
# this function is called when mouse leaves a graph-scale widget
#	and here is local variable gr_scale_old
#
# this can be also as flag, is set to 0 after resizing
set gr_scale_old 0
proc redraw_graph {} {
	global last_graph gr_scale gr_scale_old archive

	if {($last_graph == "") || ($archive == "")} {
		return
	}

	if {$gr_scale_old != $gr_scale} {
		set gr_scale_old $gr_scale
		draw $last_graph
	}
}
###############################################################################
#
# this function is called from menu-view and decides which graph will be drawn
#
proc new_chart {l} {
	global last_graph val_cur gr_scale

	if {$last_graph != "$l"} {
		set m [.graph.scale configure -from]
		if {$m < $val_cur($l)} {
			.graph.scale configure -from $val_cur($l)
		}
		set gr_scale $val_cur($l)
		set last_graph "$l"
		unset m
	}
	draw $l
}
###############################################################################
#
# About message box
#
proc About {} {
	global version

	msg_box About "Sar Grapher\n\nBuild info:$version\n\nhttp://www.volny.cz/linux_monitor/isag/index.html\ne-mail: linux_monitor@volny.cz"
}

# eof: sarrep.GUI.m4
###############################################################################
# Resizing graph window
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log:#
#
#
###############################################################################

bind . <Configure> {ResizeAll}
set enResizing 0
# first time this function is empty
proc gnuplot {w} {}

proc ResizeAll {} {
	global enResizing gr_scale_old
	
	set width [winfo width .]
	set height [winfo height .]
	
	set w [expr [winfo width .graph.scale] + [winfo width .graph.canv]]
	set h [expr [winfo height .menu] + [winfo height .file] + [winfo height .graph.scale]]
	
	if {($w == $width) && ($h == $height)} {
		incr enResizing
		return
	}
	
	if {0 == $enResizing} {
		return
	}

		set enResizing 0
	if {$w != $width} {
# number two look as magic number
		.graph.canv configure -width [expr $width - [winfo width .graph.scale] - 2]
	} 

	if {$h != $height} {
		set h1 [expr [winfo height .menu] + [.file cget -height]]
		.graph.canv configure -height [expr $height - $h1]
		.graph.scale configure -length [expr $height - $h1]
	}
#	set gr_scale_old 0
#	redraw_graph
## not reliable	WHY?
	gnuplot .graph.canv 
	#set enResizing 1
}

# eof: sarrep.ResizeAll.m4
###############################################################################
# Other functions
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State:#
# $Log:#
#
###############################################################################

##############################################################################
#
#

proc strrep {str old new} {
	set l [string length $old]

	for {set n [string first $old $str]} {$n != -1} {set n [string first $old $str]} {
		set str "[string range $str 0 [expr $n - 1]]$new[string range $str [expr $n + $l] end]"
	}
	return $str
}

# eof: strrep.m4


# normalizes time if HH:MM:SS PM to 00-24 `format' for gnuplot
proc time_normalize {time} {
	set n [string first "AM" $time]
	if {$n > 0} {
		set time [string trim [string range $time 0 [expr $n - 1]]]
		set hours [string range $time 0 1]
# if hour is 12 then time is after midnight
		if {$hours == 12} {
			set time "[format %02d [expr $hours - 12]][string range $time 2 end]"
		} else {
			return [string trim [string range $time 0 [expr $n - 1]]]
		}
		unset hours
	}
	set n [string first "PM" $time]
	if {$n > 0} {
		set time [string trim [string range $time 0 [expr $n - 1]]]
		set hours [string range $time 0 1]
	# if hours string looks like 07, then is evaluated as octal number
	# in case of 08 and 09 generates an error
		if {"0" == [string range $hours 0 0]} {
			set hours [string range $hours 1 1]
		}
		if {$hours < 12} {
			set time "[format %02d [expr $hours + 12]][string range $time 2 end]"
		}
		unset hours
	}
	unset n
	return $time
}

# tokens are divided by one or more space(s)
proc parse_line {ln} {
	global needed_version tcl_version

		set res ""
	if {$tcl_version > $needed_version} {
	# this is a more effective
		set rest [string map { \t " " } [string trim $ln]]
	} else {
		set rest [strrep [string trim $ln] \t " "]
	}

	for {} {[string length $rest] > 0} {} {
		set a [string first " " $rest]
		if {$a == -1} {
			lappend res "$rest"
			set rest ""
		} else {
			set token [string trim [string range $rest 0 [expr $a - 1]]]
			set rest [string trim [string range $rest [expr $a + 1] end]]
			lappend res "$token"
		}
	}
	unset rest
		return $res
}


# normalizes number to 2valid digits representation
# and returns lis of norm number and step for list
proc num_norm {num} {
	if {$num < 10.0} {
		return [list 10 1]
	}
	set l [expr int ([expr log10 ($num)])]
	set n [expr int ([expr pow (10, [expr $l - 1])])]
	set m [expr int ([expr ($num - 1)  / $n]) + 1]

	set max [expr $m * $n]
	set step [expr int ([expr pow (10, [expr $l - 2])])]
	if {$step < 1} {
		set step 1
	}
	unset l n m
	return [list $max $step]	
}

proc create_gnuplot_file {gp_term fn title xlabel ylabel y2label yrange plot_str} {
# Y2 percentage
	set fp [open $fn.gp "w"]
	puts $fp "set term $gp_term"
	puts $fp "set title \"$title\""
	puts $fp "set xlabel \"$xlabel\""
	puts $fp "set ylabel \"$ylabel\""
	if {$y2label != ""} {
		puts $fp "set y2label \"$y2label\""
		puts $fp "set y2range \[0:100\]"
	}
	puts $fp "set timefmt \"%H:%M:%S\""
	puts $fp "set xdata time"
	puts $fp "set format x \"%H:%M\""
	puts $fp "set yrange $yrange"
	puts $fp "set output \"$fn.tk\""
	puts $fp "plot $plot_str"
	close $fp

}

proc fngr {l} {
	return "/tmp/gp_$l[pid]"
}

# eof: sarrep.Other.Funcs.m4

###############################################################################
#
# Remaining graph paramaters, function fngr() must be defined before this
#

# plotstr array
# in future - this string will not be fixed but functions will coming

set plotstr(b) "\"[fngr b]\" using 1:2 t \"rtps\" with line, \"[fngr b]\" using 1:3 t \"wtps\" with line, \"[fngr b]\" using 1:4 t \"bread/s\" with line,  \"[fngr b]\" using 1:5 t \"bwrtn/s\" with line"
set plotstr(B) "\"[fngr B]\" using 1:2 t \"pgpgin/s\" with line, \"[fngr B]\" using 1:3 t \"pgpgout/s\" with line, \"[fngr B]\" using 1:4 t \"activepg\" with line, \"[fngr B]\" using 1:5 t \"inadtypg\" with line,  \"[fngr B]\" using 1:6 t \"inaclnpg\" with line, \"[fngr B]\" using 1:7 t \"inatarpg\" with line"
set plotstr(c) "\"[fngr c]\" using 1:2 t \"proc/s\" with line"
set plotstr(n) ""
set plotstr(q) "\"[fngr q]\" using 1:2 t \"runq-sz\" with line, \"[fngr q]\" using 1:3 t \"plist-sz\" with line, \"[fngr q]\" using 1:4 t \"ldavg-1\" with line, \"[fngr q]\" using 1:5 t \"ldavg-5\" with line"
set plotstr(r) "\"[fngr r]\" using 1:2 t \"kbmemfree\" with line, \"[fngr r]\" using 1:3 t \"kbmemused\" with line, \"[fngr r]\" using 1:4 t \"kbmemshrd\" with line, \"[fngr r]\" using 1:5 t \"kbbuffers\" with line, \"[fngr r]\" using 1:6 t \"kbcached\" with line, \"[fngr r]\" using 1:7 t \"kbswpfree\" with line, \"[fngr r]\" using 1:8 t \"kbswpused\" with line"
set plotstr(R) "\"[fngr R]\" using 1:2 t \"frmpg/s\" with line, \"[fngr R]\" using 1:3 t \"shmpg/s\" with line, \"[fngr R]\" using 1:4 t \"bufpg/s\" with line, \"[fngr R]\" using 1:5 t \"campg/s\" with line"
set plotstr(u) "\"[fngr u]\" using 1:2 t \"%user\" with line, \"[fngr u]\" using 1:3 t \"%nice\" with line, \"[fngr u]\" using 1:4 t \"%system\" with line"
set plotstr(v) "\"[fngr v]\" using 1:2 t \"file-sz\" with line, \"[fngr v]\" using 1:3 t \"inode-sz\" with line, \"[fngr v]\" using 1:4 t \"super-sz\" with line, \"[fngr v]\" using 1:5 t \"dquot-sz\" with line, \"[fngr v]\" using 1:6 t \"rtsig-sz\" with line"
set plotstr(w) "\"[fngr w]\" using 1:2 t \"cswch/s\" with line"
set plotstr(W) "\"[fngr W]\" using 1:2 t \"pswpin/s\" with line, \"[fngr W]\" using 1:3 t \"pswpout/s\" with line"

###############################################################################
# Performance data processing
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.8  2001/03/30 16:42:26  sarrep
# Fixed bug about _last_time, (mangled graph)
#
# Revision 1.7  2001/03/17 17:30:16  sarrep
# CVSizing, unified headers of each element file
#
# Revision 1.6  2001/03/17 16:55:27  sarrep
# LINUX RESTART zeroes all counters.
#
###############################################################################

# CPU measurement specific line processing
proc proc_line_u {fp line} {
	global val_cur val_max _last_time _reboot_flag

	set rval 0
		set a [string first "all" $line]
	if {$a != -1} {
				set time [string trim [string range $line 0 [expr $a - 1]]]
		set time [time_normalize "$time"]
		set rest [string trim [string range $line [expr $a + 3] end]]
		if {1 == $_reboot_flag} {
# after reboot, set to zero also, all charts are with lines
						set _reboot_flag 0
# NOTE: The func __output_zero_metrics() cannot be used, because 'u' don't have out_tokens
			puts $fp "$time 0 0 0"
		}

				set nums [parse_line $rest]
			# user nice system (idle)
		set user [lindex $nums 0]
		set nice [lindex $nums 1]
		set syst [lindex $nums 2]

		set total [expr $user + $nice + $syst]
		if {$total > $val_max(u)} {set val_max(u) $total}
				puts $fp "$time $total [expr $nice + $syst] $syst"
		set rval 1
		set _last_time $time
	} else {if {-1 != [string first "RESTART" $line]} {
				set _reboot_flag 1
# see previous NOTE
		if {$_last_time != ""} {
			puts $fp "$_last_time 0 0 0"
			set rval 1
		}
	}}
	return $rval
}
# eof: sarrep.perf_data_procs.m4
###############################################################################
# Performance Data Processing and Drawing functions
#	proces line functions
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.7  2001/07/30 18:06:25  sarrep
# Fixed one debug print. It was on wrong place.
#
# Revision 1.6  2001/03/30 16:42:26  sarrep
# Fixed bug about _last_time, (mangled graph)
#
# Revision 1.5  2001/03/17 17:30:23  sarrep
# CVSizing, unified headers of each element file
#
# Revision 1.4  2001/03/17 16:55:28  sarrep
# LINUX RESTART zeroes all counters.
#
#
###############################################################################

proc __output_zero_metrics {time  l} {
	global out_tokens out_tokens2

	set out "$time "
	foreach N $out_tokens($l) {
		set out "$out 0"
	}
	foreach N $out_tokens2($l) {
		set out "$out 0"
	}
	return "$out"
}

set _last_time ""
set _reboot_flag 0

# generic line processing
proc proc_line_generic {l fp line} {
	global val_cur val_max out_tokens out_tokens2 _last_time _reboot_flag

		set time [string trim [string range $line 0 12]]
	# Here was a problem, time string was badly cutten
		set time [time_normalize "$time"]
	set rest [string trim [string range $line 12 end]]
	#
# Sebastien's isag specific
# check for line: "time LINUX RESTART" and reset counter to next
# valid time stamp.
# NOTE: Values must be zeroed also for first valid time after reboot. For
# explanation you can imagin the chart
#
	if {-1 == [string first "RESTART" $rest]} {
		if {1 == $_reboot_flag} {
# after reboot, set to zero also, all charts are with lines
			set _reboot_flag 0
			puts $fp [__output_zero_metrics $time $l]
		}
		set nums [parse_line $rest]
		set out "$time "
		foreach N $out_tokens($l) {
			set n [lindex $nums $N]
			set out "$out $n"
			if {$n > $val_max($l)} {set val_max($l) $n}
		}

		foreach N $out_tokens2($l) {
			set n [lindex $nums $N]
			set out "$out $n"
		}
		set _last_time $time
		unset nums n N
	} else {
# reboot detected, drop all metrics to zero
		set _reboot_flag 1
		if {$_last_time != ""} {
			set out [__output_zero_metrics $_last_time $l]
		}
	}

	catch {
				puts $fp "$out"
		unset out
	}
	unset rest time
	return 1
}

# eof: proc_line_generic.m4
###############################################################################
# Performance Data Processing and Drawing functions
#	generic draw function
#	parametrical draw func for each kind of graph
#
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.7  2001/03/30 16:42:26  sarrep
# Fixed bug about _last_time, (mangled graph)
#
# Revision 1.6  2001/03/17 17:30:10  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################

#
# generic draw function
# Parameter:
#	l - graph letter other values are parameters
#
proc draw {l} {
	global archive title sh sar grep sar_data_path gnuplot \
		gr_scale val_cur val_max view_n cmd xlabel out_tokens \
		ylabel y2label plotstr enResizing has_negative \
		_last_time

	
	if {$archive == ""} {
		msg_box "Error" "No data source selected for: $view_n($l)"
		return
	}

	wm title . "$title: $view_n($l)"

	set _last_time ""

	set fn "/tmp/sag_$l[pid]"
	set fp [open $fn w]
	puts $fp "#!$sh"
	puts $fp " "
		puts $fp "$cmd($l)"
	close $fp
	exec chmod u+x $fn

	set fp [open "| $fn" "r"]
	set fpgr [open [fngr "$l"] w]

	set nlines 0
	while {[gets $fp line] != -1} {
				if {"" != $out_tokens($l)} {
			set n [proc_line_generic $l $fpgr "$line"]
		} else {
			set n [proc_line_$l $fpgr "$line"]
		}
		incr nlines $n
	}
	catch {close $fp}
	catch {close $fpgr}
	file delete $fn

# at least two lines of data
	if {$nlines > 1} {
		set_cur_max $l

		if {1 == $has_negative($l)} {
			set yrange "\[-$val_cur($l):$val_cur($l)\]"
		} else {
			set yrange "\[0:$val_cur($l)\]"
		}
		create_gnuplot_file tkcanvas "[fngr $l]" "$view_n($l)" \
			"$xlabel" \
			"$ylabel($l)" $y2label($l) \
			$yrange \
			"$plotstr($l)"

		exec $gnuplot [fngr $l].gp

		source [fngr $l].tk
		gnuplot .graph.canv

		file delete [fngr $l].gp
		file delete [fngr $l].tk
	} else {
		.graph.canv delete all
		msg_box "Datafile: archive" "No data to show"
	}
	file delete [fngr $l]

	unset fp fpgr fn line nlines 
}

# eof: draw.m4
###############################################################################
# $Id: isag,v 1.1.1.1 2004/07/01 15:59:16 dsmith Exp $
# $State: Exp $
# $Log: isag,v $
# Revision 1.1.1.1  2004/07/01 15:59:16  dsmith
# Importing sysstat-4.0.7-4.EL3.2.
#
# Revision 1.3  2001/03/17 17:30:19  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################
if {[file isfile $cfg_file] == 1} {
	source $cfg_file

	if {"" != $last_graph} {
				if {$val_max($last_graph) > 0} {
			.graph.scale configure -to 0 \
				-from $val_max($last_graph)
			set gr_scale $val_cur($last_graph)
					}
	}
}

#
# Finishing a program and writting of config file
#
proc exit_program {} {
	global cfg_file prog_swtch val_max val_cur last_graph


	set fp [open $cfg_file w]
	foreach l $prog_swtch {
		puts $fp "set val_max($l) $val_max($l)"
		puts $fp "set val_cur($l) $val_cur($l)"
	}
	if {$last_graph == ""} {
		puts $fp "set last_graph \"\""
	} else {
		puts $fp "set last_graph $last_graph"
	}
	close $fp

	destroy .
	exit
}

#eof: isag_cfg_file.m4

# eof: isag.m4
