#!/usr/perl5/bin/perl -w
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)kstat.pl	1.1	99/08/16 SMI"

require 5.005;
use strict;
use locale;
use Getopt::Std;
use POSIX qw(locale_h ctime);
use File::Basename;
use Sun::Solaris::Utils qw(textdomain gettext gmatch);
use Sun::Solaris::Kstat;

#
# Print an usage message and exit
#

sub usage(@)
{
	my (@msg) = @_;
	print STDERR basename($0), ": @msg\n" if (@msg);
	print STDERR gettext(
	"Usage:\n" .
			     "kstat [interfaces ] [ interval [ count ] ]\n");
	exit(2);
}

#
# Print a fatal error message and exit
#

sub error(@)
{
	my (@msg) = @_;
	print STDERR basename($0), ": @msg\n" if (@msg);
	exit(1);
}

#
# Main routine of the script
#

# Set message locale
setlocale(LC_ALL, "");
textdomain("SUNW_OST_OSCMD");

# Process command options
my (%opt);
getopts('?qlpT:m:i:n:s:c:', \%opt) || usage();
usage() if exists($opt{'?'});

# Get interval & count if specified
my ($interval, $count) = (5, 1);
if (@ARGV >= 2 && $ARGV[-2] =~ /^\d+$/ && $ARGV[-1] =~ /^\d+$/) {
	$count = pop(@ARGV);
	$interval = pop(@ARGV);
	usage(gettext("Interval must be an integer >= 1")) if ($interval < 1);
	usage(gettext("Count must be an integer >= 1")) if ($count < 1);
} elsif (@ARGV >= 1 && $ARGV[-1] =~ /^\d+$/) {
	$interval = pop(@ARGV);
	$count = -1;
	usage(gettext("Interval must be an integer >= 1")) if ($interval < 1);
}

#
# enumerate network indices
#
my @iflist;
if (! @ARGV) {
    my $ifcount=-1;
    while (</etc/hostname.*>) {
	$ifcount++;
	my $ifname = $_;
	$ifname =~ s|/etc/hostname\.||;
	push (@iflist,$ifname);
    }
    if ($ifcount == -1) {
	print "No interfaces found.\n";
	exit 1;
    }
} else {
    foreach my $p (@ARGV) {
	push (@iflist,$p);
    }
}

# Loop, printing the selected kstats as many times and as often as required
my $ks = Sun::Solaris::Kstat->new();

my (@inbytes,@outbytes);

my $i = 0;
foreach my $m (@iflist) {
#    print "$m\n";
    my $dname = $m;
#    $dname =~ s/[0-9]//g;
    $dname =~ s/[0-9]*$//g;
    my $dinst = $m;
#    $dinst =~ s/[a-z]//g;
    $dinst =~ s/$dname//;
#    print "$dname:$dinst:$m:obytes64\n";
    $outbytes[$i] = $ks->{$dname}{$dinst}{$m}{obytes64};
#    print "$outbytes[$i]\n";
    $inbytes[$i] = $ks->{$dname}{$dinst}{$m}{rbytes64};
#    print "$inbytes[$i]\n";
    $i++;
}

$|=1;

my ($obytes, $ibytes);

while ($count == -1 || $count-- > 0) {
    sleep $interval;
    $ks->update();
    my $i = 0;
    foreach my $m (@iflist) {
	my $dname = $m;
#	$dname =~ s/[0-9]//g;
	$dname =~ s/[0-9]*$//g;
	my $dinst = $m;
#	$dinst =~ s/[a-z]//g;
	$dinst =~ s/$dname//g;
#	print "$dname:$dinst:$m:obytes64\n";
	$obytes = $ks->{$dname}{$dinst}{$m}{obytes64};
	print "$m: ";
	printf("%8d k/s out,  ",($obytes-$outbytes[$i])/($interval*1024));
	$outbytes[$i] = $obytes;
#	print "$obytes $outbytes[$i]\n";
	$ibytes= $ks->{$dname}{$dinst}{$m}{rbytes64};
	printf("%8d k/s in",($ibytes-$inbytes[$i])/($interval*1024));
	$inbytes[$i] = $ibytes;
#	print "$inbytes[$i]\n";
	print "\n";
	$i++;
    }
}

exit;
