#!/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.m4,v 1.22 2003/02/26 18:19:09 david Exp $
# $State: Exp $
#
# $Log: isag.m4,v $
# Revision 1.22  2003/02/26 18:19:09  david
# realized new layout of files
#
# Revision 1.21  2003/02/23 21:07:35  david
# first prototype of sag
#
# Revision 1.20  2003/01/26 20:41:48  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.19  2002/03/04 20:23:26  david
# fixed whole day functionality
#
# 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: version.m4,v 1.4 2002/03/04 20:20:27 david Exp $
# $State: Exp $
# $Log: version.m4,v $
# Revision 1.4  2002/03/04 20:20:27  david
# make new end line
#
# 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"
}
# $Source: /mnt/CVSROOT/cvs/i-sag/common/version.m4,v $
##############################################################################
# Tcl/Tk: version check
#
# $Id: ver_check.m4,v 1.6 2003/03/03 20:11:16 david Exp $
# $State: Exp $
# $Log: ver_check.m4,v $
# Revision 1.6  2003/03/03 20:11:16  david
# fixed tools name
#
# Revision 1.5  2002/03/24 15:18:26  david
# added
#
# Revision 1.4  2002/03/04 20:20:27  david
# make new end line
#
# 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} {
	out_msg "Warning Version check" "sag needs $needed_version, but this is version $tcl_version of Tcl.\nSome features can fail"
#	exit
}

# $Source: /mnt/CVSROOT/cvs/i-sag/common/ver_check.m4,v $
###############################################################################
# default paths, programs and other variables
#
# $Id: variables.m4,v 1.5 2003/01/26 20:41:49 david Exp $
# $State: Exp $
# $Log: variables.m4,v $
# Revision 1.5  2003/01/26 20:41:49  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.4  2002/03/04 20:20:27  david
# make new end line
#
# 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 isag_cfg_file	"$env(HOME)/.isag.cfg"
set sag_graph_wd	720
set sag_graph_ht	400

# $Source: /mnt/CVSROOT/cvs/i-sag/common/variables.m4,v $

###############################################################################
# Message box functionality
#
# $Id: msg_box.m4,v 1.6 2002/03/24 15:18:26 david Exp $
# $State: Exp $
# $Log: msg_box.m4,v $
# Revision 1.6  2002/03/24 15:18:26  david
# added
#
# Revision 1.5  2002/03/04 20:21:08  david
# make new end line
#
# 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 out_msg {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
}

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/msg_box.m4,v $

# 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_ALL) "C"


###############################################################################
# $Id: isag_common_options.m4,v 1.6 2003/01/26 20:41:48 david Exp $
# $State: Exp $
# $Log: isag_common_options.m4,v $
# Revision 1.6  2003/01/26 20:41:48  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.5  2002/03/24 15:18:26  david
# added
#
# Revision 1.4  2002/03/04 20:21:51  david
# make new end line
#
# 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 isag_cfg_file sag_graph_wd sag_graph_ht

	out_msg "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: $isag_cfg_file) 
\t-gwd graph_width (default: $sag_graph_wd)
\t-ght graph_height (default: $sag_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 isag_cfg_file $t
	} else {
		out_msg "Error" "<$t> is not regular file, using default <$isag_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 {
		out_msg "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 sag_graph_ht $t
	} else {
		out_msg "Error" "<$t> is not decimal number, using default <$sag_graph_ht>"
	}
}
if {[set t [getopt $argv gwd]] != ""} {
	if {1 == [string is digit $t]} {
		set sag_graph_wd $t
	} else {
		out_msg "Error" "<$t> is not decimal number, using default <$sag_graph_wd>"
	}
}

unset t
# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag_common_options.m4,v $

###############################################################################
#
# 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 sag_if_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
#	sag_if_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) ""

# sag_if_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 sag_if_out_tokens(b) [list 1 2 3 4]
set sag_if_out_tokens2(b) ""
set sag_if_out_tokens(B) [list 0 1 3 4 5 6]
set sag_if_out_tokens2(B) ""
set sag_if_out_tokens(c) [list 0]
set sag_if_out_tokens2(c) ""
set sag_if_out_tokens(q) [list 0 1 2 3]
set sag_if_out_tokens2(q) ""
set sag_if_out_tokens(r) [list 0 1 3 4 5 6 7]
set sag_if_out_tokens2(r) ""
set sag_if_out_tokens(R) [list 0 1 2 3]
set sag_if_out_tokens2(R) ""
# CPU needs special computation, following list must be empty
# if no list is entered then following functions must be defined
# __output_zero_metrics_$l & proc_line_$l (see: proc_line_u())
set sag_if_out_tokens(u) ""
set sag_if_out_tokens2(u) ""
set sag_if_out_tokens(v) [list 1 3 5 7 9]
set sag_if_out_tokens2(v) ""
set sag_if_out_tokens(w) [list 0]
set sag_if_out_tokens2(w) ""
set sag_if_out_tokens(W) [list 0 1]
set sag_if_out_tokens2(W) ""

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

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

 set sag_if_cmd(b) "$sar -b -f $sar_data_path/$sag_if_archive | $grep -v 'tps' $commstr"
 set sag_if_cmd(B) "$sar -B -f $sar_data_path/$sag_if_archive | $grep -v 'pgpgin' $commstr"
 set sag_if_cmd(c) "$sar -c -f $sar_data_path/$sag_if_archive | $grep -v 'proc' $commstr"
 set sag_if_cmd(n) ""
 set sag_if_cmd(q) "$sar -q -f $sar_data_path/$sag_if_archive | $grep -v 'runq-sz' $commstr"
 set sag_if_cmd(r) "$sar -r -f $sar_data_path/$sag_if_archive | $grep -v 'kbmemfree' $commstr"
 set sag_if_cmd(R) "$sar -R -f $sar_data_path/$sag_if_archive | $grep -v 'campg' $commstr"
 set sag_if_cmd(u) "$sar -u -f $sar_data_path/$sag_if_archive | $grep -v 'user' $commstr"
 set sag_if_cmd(v) "$sar -v -f $sar_data_path/$sag_if_archive | $grep -v 'dentunusd' $commstr"
 set sag_if_cmd(w) "$sar -w -f $sar_data_path/$sag_if_archive | $grep -v 'cswch' $commstr"
 set sag_if_cmd(W) "$sar -W -f $sar_data_path/$sag_if_archive | $grep -v 'pswpin' $commstr"
}
sag_if_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 sag_if_archive last_graph

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

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

###############################################################################
# GUI
#
# $Id: isag.GUI.m4,v 1.12 2003/01/26 20:41:48 david Exp $
# $State: Exp $
# $Log: isag.GUI.m4,v $
# Revision 1.12  2003/01/26 20:41:48  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.11  2002/10/31 15:07:21  david
# fixed bad behaviour with OpenMotif WM
#
# Revision 1.10  2002/03/24 15:18:26  david
# added
#
# Revision 1.9  2002/03/04 20:23:26  david
# fixed whole day functionality
#
# Revision 1.8  2002/03/04 18:36:37  david
# grid feature implemented
#
# 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 isag_title "System Activity Grapher"
wm title . $isag_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 isag_gr_scale_old 0;redraw_graph" -accelerator "F5"
.menu.file.m add command -label "Save Picture" -underline 0 \
		-command "sag_save_graph"
.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.opt -text "Options" -menu .menu.opt.m -underline 0
menu .menu.opt.m -tearoff false
.menu.opt.m add checkbutton -label "Whole day (00:00-23:59)" -underline 0 \
        -variable sag_opt_whole_day \
		-command "set isag_gr_scale_old 0;redraw_graph"
.menu.opt.m add checkbutton -label "Show grid" -underline 0 \
        -variable sag_opt_grid -command "set isag_gr_scale_old 0;redraw_graph"
		
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 .menu.opt -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 sag_gr_scale 0
scale .graph.scale -orient vertical -length $sag_graph_ht -to 0 -from 1 -variable sag_gr_scale

bind .graph.scale <Leave> "redraw_graph"

canvas .graph.canv -width $sag_graph_wd -height $sag_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 isag_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 sag_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) $sag_gr_scale
	if {$val_cur($l) == 0} {
		set val_cur($l) $val_max($l)
		set sag_gr_scale $val_cur($l)
	}
}

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

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

	if {$isag_gr_scale_old != $sag_gr_scale} {
		set isag_gr_scale_old $sag_gr_scale
		sag_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 sag_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 sag_gr_scale $val_cur($l)
		set last_graph "$l"
		unset m
	}
	sag_draw $l
}
###############################################################################
#
# About message box
#
proc About {} {
	global version

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

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag.GUI.m4,v $
###############################################################################
# Resizing graph window
#
# $Id: isag.ResizeAll.m4,v 1.7 2003/01/26 20:41:48 david Exp $
# $State: Exp $
# $Log:#
#
#
###############################################################################

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

proc ResizeAll {} {
	global isag_enResizing isag_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 isag_enResizing
		return
	}
	
	if {0 == $isag_enResizing} {
		return
	}

		set isag_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 isag_gr_scale_old 0
#	redraw_graph
## not reliable	WHY?
	gnuplot .graph.canv 
	#set isag_enResizing 1
}

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag.ResizeAll.m4,v $
###############################################################################
# Other functions
#
# $Id: isag.Other.Funcs.m4,v 1.16 2003/02/23 21:07:35 david 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]	
}

##
## Parameters
##	gp_term  - gnuplot terminal (currently used: tkcanvas & jpeg)
##	ext      - out filename extension (related to gp_term)
##	fn       - file name base for resulting file
##	title	 - graph title/name
##	xlabel	 - xlabel text
##	ylabel   - primary ylabel text
##	y2label  - secondary ylabel text (scale 0-100%)
##	yrange   - yrange (y-maximum)
##	plot_str - gplot command which produces desired output graph
##	f_grid   - flag if grid should be drawn
##
proc create_gnuplot_file {gp_term ext fn title xlabel ylabel y2label yrange plot_str f_grid} {
	global sag_graph_wd sag_graph_ht

	
# Y2 percentage
	set fp [open $fn.gp "w"]
	
# not consistent workaround	
	if { $gp_term == "jpeg" } {
		puts $fp "set term $gp_term transparent interlace size $sag_graph_wd,$sag_graph_ht"
	} else {
		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\""
	if {$yrange != ""} {
		puts $fp "set yrange $yrange"
	}
	puts $fp "set output \"$fn.$ext\""
	if { 0 != $f_grid } {
		puts $fp "set grid"
	}
	puts $fp "plot $plot_str"
	close $fp
}

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

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag.Other.Funcs.m4,v $

###############################################################################
#
# 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.perf_data_procs.m4,v 1.11 2003/01/26 20:41:48 david Exp $
# $State: Exp $
# $Log: isag.perf_data_procs.m4,v $
# Revision 1.11  2003/01/26 20:41:48  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.10  2002/03/24 15:18:26  david
# added
#
# Revision 1.9  2002/03/04 20:23:26  david
# fixed whole day functionality
#
# 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 sag_if_proc_line_u {fp line} {
	global val_cur val_max sag_if_last_time sag_if_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 == $sag_if_reboot_flag} {
# after reboot, set to zero also, all charts are with lines
						set sag_if_reboot_flag 0
# NOTE: The func __output_zero_metrics() cannot be used, because 'u' don't have sag_if_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 sag_if_last_time $time
	} else {if {-1 != [string first "RESTART" $line]} {
				set sag_if_reboot_flag 1
# see previous NOTE
		if {$sag_if_last_time != ""} {
			puts $fp "$sag_if_last_time 0 0 0"
			set rval 1
		}
	}}
	return $rval
}

proc sag_if__output_zero_metrics_u {time} {
	return 	"$time 0 0 0"
}

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag.perf_data_procs.m4,v $
###############################################################################
# Performance Data Processing and Drawing functions
#	proces line functions
#
# $Id: proc_line_generic.m4,v 1.10 2003/01/26 20:41:49 david Exp $
# $State: Exp $
# $Log: proc_line_generic.m4,v $
# Revision 1.10  2003/01/26 20:41:49  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.9  2002/03/24 15:18:26  david
# added
#
# Revision 1.8  2002/03/04 20:23:26  david
# fixed whole day functionality
#
# 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.
#
#
###############################################################################

#
# NOTE: following function is used when
# graph of whole day is required (see: draw() function)
#
proc sag_if__output_zero_metrics {time  l} {
	global sag_if_out_tokens sag_if_out_tokens2

	set out "$time "
	foreach N $sag_if_out_tokens($l) {
		set out "$out 0"
	}
	foreach N $sag_if_out_tokens2($l) {
		set out "$out 0"
	}
	return "$out"
}

#
# NOTE: `_last_time' related functionality is used when
# graph of whole day is required (see: draw() function)
#
set sag_if_last_time ""
set sag_if_reboot_flag 0

# generic line processing
proc sag_if_proc_line_generic {l fp line} {
	global val_cur val_max sag_if_out_tokens sag_if_out_tokens2 sag_if_last_time sag_if_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 == $sag_if_reboot_flag} {
# after reboot, set to zero also, all charts are with lines
			set sag_if_reboot_flag 0
			puts $fp [sag_if__output_zero_metrics $time $l]
		}
		set nums [parse_line $rest]
		set out "$time "
		foreach N $sag_if_out_tokens($l) {
			set n [lindex $nums $N]
			set out "$out $n"
			if {$n > $val_max($l)} {set val_max($l) $n}
		}

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

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

# $Source: /mnt/CVSROOT/cvs/i-sag/common/proc_line_generic.m4,v $
###############################################################################
# Performance Data Processing and Drawing functions
#	generic draw function
#	parametrical draw func for each kind of graph
#
# $Id: draw.m4,v 1.13 2003/02/23 19:48:16 david Exp $
# $State: Exp $
# $Log: draw.m4,v $
# Revision 1.13  2003/02/23 19:48:16  david
# unified redundant code which generates data file
#
# Revision 1.12  2003/01/26 20:41:48  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.11  2002/03/24 15:18:26  david
# added
#
# Revision 1.10  2002/03/04 20:23:43  david
# fixed whole day functionality
#
# Revision 1.9  2002/03/04 18:36:37  david
# grid feature implemented
#
# Revision 1.8  2002/03/03 19:45:19  david
# added parameters to create_gnuplot_file()
#
# 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
#
#
#
###############################################################################

proc sag_prepare_datafile {l} {
	global sh sar_data_path sag_if_cmd sag_if_out_tokens \
		sag_opt_whole_day sag_if_last_time

		
	set sag_if_last_time ""

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

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

	set nlines 0
	
# if needed set starting time
	if { 0 != $sag_opt_whole_day } {
		set sag_if_last_time "00:00"
	}
	
	while {[gets $fp line] != -1} {
				if {"" != $sag_if_out_tokens($l)} {
			set n [sag_if_proc_line_generic $l $fpgr "$line"]
		} else {
			set n [sag_if_proc_line_$l $fpgr "$line"]
		}
		incr nlines $n
	}
	
## make closing remark if necessary
# finishing whole day	
	if { 0 != $sag_opt_whole_day } {
		if {"" != $sag_if_out_tokens($l)} {
			puts $fpgr [sag_if__output_zero_metrics $sag_if_last_time $l]
		} else {
			puts $fpgr [sag_if__output_zero_metrics_$l $sag_if_last_time]
		}
		if {"" != $sag_if_out_tokens($l)} {
			puts $fpgr [sag_if__output_zero_metrics "23:59" $l]
		} else {
			puts $fpgr [sag_if__output_zero_metrics_$l "23:59"]
		}
	}
	
	catch {close $fp}
	catch {close $fpgr}
	file delete $fn

	unset fp fpgr fn line


	return nlines
}

#
# generic draw function
# Parameter:
#	l - graph letter other values are parameters
#
proc sag_draw {l} {
	global sag_if_archive isag_title sh sar grep sar_data_path gnuplot \
		sag_gr_scale val_cur val_max view_n sag_if_cmd xlabel sag_if_out_tokens \
		ylabel y2label plotstr has_negative \
		sag_if_last_time sag_opt_grid sag_opt_whole_day

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

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

	set nlines [sag_prepare_datafile $l]

# 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 tk "[fngr $l]" "$view_n($l)" \
			"$xlabel" \
			"$ylabel($l)" $y2label($l) \
			$yrange \
			"$plotstr($l)" $sag_opt_grid

		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
		out_msg "Datafile: sag_if_archive" "No data to show"
	}
	file delete [fngr $l]

	unset nlines 
}

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/draw.m4,v $
###############################################################################
# Performance Data Processing and Drawing functions
#	proces line functions
#
# $Id: sag.save_graph.m4,v 1.3 2003/02/23 21:07:41 david Exp $
# $State: Exp $
# $Log: sag.save_graph.m4,v $
# Revision 1.3  2003/02/23 21:07:41  david
# first prototype of sag
#
# Revision 1.2  2003/02/23 19:48:16  david
# unified redundant code which generates data file
#
# Revision 1.1  2003/01/26 20:41:49  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.9  2002/03/24 15:18:26  david
# added
#
# Revision 1.8  2002/03/04 20:23:26  david
# fixed whole day functionality
#
# 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 sag_save_graph {} {
	global sag_if_archive isag_title sh sar grep sar_data_path gnuplot \
		sag_gr_scale val_cur val_max view_n sag_if_cmd xlabel sag_if_out_tokens \
		ylabel y2label plotstr has_negative \
		sag_if_last_time sag_opt_grid sag_opt_whole_day last_graph

		
	set l $last_graph

	if {$sag_if_archive == ""} {
#		out_msg "Error" "No data source selected for: $view_n($l)"
		return
	}

	set nlines [sag_prepare_datafile $l]

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

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

		exec $gnuplot [fngr $l].gp

		file delete [fngr $l].gp
		file rename -force [fngr $l].jpg $sag_if_archive.$l.jpg

	} else {
		.graph.canv delete all
		out_msg "Datafile: sag_if_archive" "No data to show"
	}
	file delete [fngr $l]

	unset nlines 
}

# $Source: /mnt/CVSROOT/cvs/i-sag/common/sag.save_graph.m4,v $
###############################################################################
# $Id: isag_cfg_file.m4,v 1.6 2003/01/26 20:41:48 david Exp $
# $State: Exp $
# $Log: isag_cfg_file.m4,v $
# Revision 1.6  2003/01/26 20:41:48  david
# partially renamed variables, 1st implementation of "Save Graph"
#
# Revision 1.5  2002/03/04 20:21:51  david
# make new end line
#
# Revision 1.4  2002/03/04 18:36:37  david
# grid feature implemented
#
# Revision 1.3  2001/03/17 17:30:19  sarrep
# CVSizing, unified headers of each element file
#
#
#
###############################################################################
if {[file isfile $isag_cfg_file] == 1} {
	source $isag_cfg_file

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

#
# Finishing a program and writting of config file
#
proc exit_program {} {
	global isag_cfg_file prog_swtch val_max val_cur last_graph \
		sag_opt_whole_day sag_opt_grid

	set fp [open $isag_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"
	}
	
	puts $fp "set sag_opt_whole_day $sag_opt_whole_day"
	puts $fp "set sag_opt_grid $sag_opt_grid"
	
	close $fp

	destroy .
	exit
}

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag_cfg_file.m4,v $

# $Source: /mnt/CVSROOT/cvs/i-sag/isag/isag.m4,v $
