#!/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: Fri, May 8, 1992
# Author: Gustaf Neumann
# Version: 0.91
#
{
$options = "u:";
$WafeLib = $ENV{'WAFELIB'} || "/usr/lib/X11/wafe";
require "$WafeLib/perl/wafe.pl";

$opt_u = $opt_u ||  5; # each tive seconds per default
$iostat = "iostat";
$mount = "/etc/mount";
#ifdef __alpha
$mount = "/usr/sbin/mount";
#else
$mount = "/etc/mount";
#endif

#if defined(_IBMR2) || defined(sun)
$iostat = "iostat -d";
#endif


#ifdef hpux
open(STAT,"$iostat |") && ($read=<STAT>) && close(STAT);
$ndisks=@disks=grep(m|/([a-z0-9]+)[^/]*$| &&($_=$1),split(/\s+/,$read));
#endif
#if defined(ultrix) || defined(__alpha)
open(STAT,"$iostat |") && ($read=<STAT>) && close(STAT);
$ndisks=@disks=grep(m|(rz[0-9]+)| &&($_=$1),split(/\s+/,$read));
#endif
#ifdef _IBMR2
$/=""; open(STAT,"$iostat |") && ($read=<STAT>) && close(STAT); $/="\n";
$*=1; $ndisks=@disks=grep(m|^([a-z]+[0-9])| &&($_=$1),split(/\s+/,$read)); $*=0;
#endif
#ifdef sun
open(STAT,"$iostat |") && ($read=<STAT>) && close(STAT);
$ndisks=@disks=grep(m|(sd[0-9]+)| &&($_=$1),split(/\s+/,$read));
#endif

### offset in iostat columns
#ifdef __alpha
$offset = 2;
#else
$offset = 0;
#endif

$*=1; 
#ifdef hpux
$/=""; open(STAT,"$mount |") && ($read=<STAT>) && close(STAT); $/="\n";
grep($read=~m|^(\S+)\s+on\s+\S*$_| && ($disklabel{$_}= $1),@disks); 
#endif
#if defined(ultrix) || defined(sun) || defined(__alpha)
$/=""; open(STAT,"$mount |") && ($read=<STAT>) && close(STAT); $/="\n";
grep($read=~m|^/dev/$_[a-z]\s+on\s+(\S+)| && ($disklabel{$_}= $1),@disks); 
#endif
#ifdef _IBMR2
grep(($disklabel{$_}= $_),@disks); 
#endif
$*=0;


#ifdef hpux
@charts = ('bps', 'sps', 'msps');
%label = ('bps', 'KB/sec', 'sps', 'Seek/sec', 'msps','ms/seek');
%scale  = ('bps', 100, 'sps', 10, 'msps', 30);
#endif

#if defined(ultrix) || defined(__alpha)
@charts = ('bps', 'tps');
%label = ('bps', 'Blocks/sec', 'tps', 'Transfers/sec');
%scale  = ('bps', 100, 'tps', 10);
#endif

#ifdef _IBMR2
@charts = ('tm_act', 'kbps','tps');
%label = ('tm_act', 'Time active', 'kbps', 'KB/sec','tps','Transfers/sec');
%scale = ('tm_act', 100, 'kbps', 100, 'tps', 10);
#endif

#ifdef sun
@charts = ('bps', 'tps','msps');
%label = ('bps', 'Blocks/sec', 'tps', 'Transfers/sec','msps','ms/seek');
%scale  = ('bps', 100, 'tps', 10,'msps',30);
#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 \\
      *font -adobe-*-medium-r-*-*-12-*-*-*-*-*-*-*

  form top topLevel \\
      defaultDistance 2 $backGround translations "<Key>q: exec(quit)"
__

foreach $d (@disks) {
    &Xui(<<"__");
    label disk$d top label {Disk: $disklabel{$d}} $backGround \\
	right chainLeft $vert vertDistance 10 \\
        font -adobe-*-bold-r-*-*-14-*-*-*-*-*-*-*
__
    $horiz ="";
    foreach (@charts) {
        &Xui(<<"__");
	label name_$d$_ top {
	    label {$label{$_}: $scale{$_}} $backGround 
	    right chainLeft $horiz fromVert disk$d
	}
	stripChart $d$_ top {
	    jumpScroll 1 update $opt_u width 160 height 40
	    $horiz fromVert name_$d$_ $roColors $ruleColor
	}
__
	$stats{"$d$_"} = 0;
	$horiz = "fromHoriz $d$_";
    }
    $vert = "fromVert $d$charts[$#charts]";
}
&Xui("realize;deleteWindowProtocol quit");

open(STAT,"$iostat $opt_u|");
while(<STAT>) {
#ifndef _IBMR2
    next if !/^\s*\d+/;
    $_ = $1 if /^\s+(\S.*)$/;
    split;
    $i=$[ + $offset;
    foreach $d (@disks) {
	foreach $c (@charts) {
#	    print "$d$c $i: $_[$i] $scale{$c}\n";
	    $val = $_[$i]/$scale{$c};
	    &Xui("scSet $d$c $val");
	    $i++;
	}
    }
#else
    next if !/^([a-z]+\d)/;
    split;
    $i=1; $disk=$1;
    foreach $c (@charts) {
	$val = $_[$i]/$scale{$c};
	&Xui("scSet $disk$c $val");
	$i++;
    }
#endif
}
}

