#!/usr/local/bin/perl -P
#
# Copyright (C) 1992 by Gustaf Neumann, Stefan Nusser
#
#      Wirtschaftsuniversitaet Wien,
#      Abteilung fuer Wirtschaftsinformatik
#      Augasse 2-6,
#      A-1090 Vienna, Austria
#      neumann@wu-wien.ac.at, nusser@wu-wien.ac.at
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appears in all copies and that both that
# copyright notice and this permission notice appear in all supporting
# documentation.  This software is provided "as is" without expressed or
# implied warranty.
#
# Date: Mon, Apr 13 1992
# Author: Gustaf Neumann
# Version: 0.9
#
{
$options = "u:";
$WafeLib = $ENV{'WAFELIB'} || "/usr/lib/X11/wafe";
require "$WafeLib/perl/wafe.pl";

$opt_u = $opt_u ||  5; # update each five seconds per default

@charts = (user, 'system', idle, 'page-in', 'page-out', interrupts,syscalls,'context-switches');
%scale  = (user, 100, 'system', 100, idle, 100, 'page-in', 10, 'page-out', 10,
	   interrupts,500, syscalls,300, 'context-switches',100);


#ifdef ultrix
$nrfields = 19;
%pos    = (user,   7, 'system',   8, idle,   9, 'page-in', 14, 'page-out', 15, 
	   interrupts,4, syscalls,5, 'context-switches',6);
#endif

#ifdef sun
$nrfields = 22;
%pos    = (user,20, 'system', 21, idle,22, 'page-in', 8, 'page-out', 9,
           interrupts,17, syscalls,18, 'context-switches',19);
#endif

# for a rs6000 use following definitions
#ifdef _IBMR2
$nrfields = 17;
%pos    = (user,14, 'system', 15, idle,16, 'page-in', 6, 'page-out', 7,
           interrupts,11, syscalls,12, 'context-switches',13);
#endif

#ifdef hpux
$nrfields = 18;
%pos    = (user,16, 'system', 17, idle,18, 'page-in', 8, 'page-out', 9,
           interrupts,13, syscalls,14, 'context-switches',15);
#endif

#ifdef __alpha
$nrfields = 18;
%pos    = (user,16, 'system', 17, idle,18, 'page-in', 11, 'page-out', 12,
           interrupts,13, syscalls,14, 'context-switches',15);
#endif

$ruleColor="highlight $1" if $sbColors =~ /background\s+(\S+)/; 
chop($hostname=`hostname`); $hostname =~ s/\..*$//;

&Xui(<<"__");
  sV topLevel iconName {$appName@$hostname} title {$appName@$hostname}
  mergeResources topLevel *borderWidth 0
  form top topLevel \\
      defaultDistance 2 $backGround translations "<Key>q: exec(quit)"
__
for (@charts) {
    &Xui(<<"__");
    label name_$_ top {
	label {$_: $scale{$_}} right chainLeft $backGround $vert
    }
    stripChart $_ top {
	jumpScroll 1 update $opt_u width 160
	height 40 fromVert name_$_ $roColors $ruleColor
    }
__
    $stats{$_} = 0;
    $vert = "fromVert $_";
}
&Xui("realize;deleteWindowProtocol quit");

open(STAT,"vmstat $opt_u|");
while(<STAT>) {
    next if !/^\s*\d+/;

    split;
    if ($#_ ge $nrfields) {
	foreach $c (keys %scale) {	
	    $val = $_[$pos{$c}]/$scale{$c};
	    &Xui("scSet $c $val");
	}  
    } else {
	print "there might be a field overflow in the vmstat output\n" 
	    if $#_-1 == $nrfields;
    }
# print;
}
}
