package Requester::sysvsar;

use strict;

our( @ISA );
@ISA = qw( Requester::CommandWatchRequester );

use Requester::CommandWatchRequester;

my $requester = new Requester::sysvsar();

sub new {
    my ($this, $args ) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);
    $self->set_command( "sar -A %d 1" );
    $self->register( "sysvsar" );
    return $self;
}


sub parse {
    my( $self, $lines, $duration ) = @_;
	my %sar;
	my $info = {};

    return undef unless( $lines && ($#$lines >= 3) );
	my @nametable = @$lines;
	(shift @nametable) while( @nametable && ! ($nametable[0] =~ /^\d+:/ ) );
	$nametable[0] =~ s/^[^\s\t]+//;

	my $n;
	for( $n=1; ($n<=$#nametable) && ! ($nametable[$n] =~ /^\d+:/); $n++ ) {;}
	$nametable[$n] =~ s/^[^\s\t]+//;

	for( my $i=0; ($i<=$#nametable) && $nametable[$i]; $i++ ) {
	    my @names = split( /[\s\t]+/, $nametable[$i] );
	    shift @names;
	    my @values = split( /[\s\t]+/, $nametable[$n++] );
		shift @values unless( $values[0] );
	    if( $names[0] eq "device" ) {
			my( $div, $dev );

			$div = 0;
			$n--;
			while( $nametable[$n] =~ /^[\s\t]*[a-z]+[a-z0-9]+/ ) {
				@values = split( /[\s\t]+/, $nametable[$n] );
				shift @values unless( $values[0] );
				last unless( $#values == $#names );
				$dev = $values[0];
				for( my $j=1; $j<=$#names; $j++ ) {
					$sar{$names[$j].".$dev"} = $values[$j];
					if( $sar{$names[$j]} ) {
						$sar{$names[$j]} += $values[$j];
					}
					else {
						$sar{$names[$j]} = $values[$j];
					}
				}
				$div++;
				$n++;
			}
			if( $div ) {
				for( my $j=1; $j<=$#names; $j++ ) {
					$sar{$names[$j]} /= $div;
				}
			}
	    }
		else {
			for( my $j=0; $j<=$#names; $j++ ) {
				$sar{$names[$j]} = $values[$j];
			}
		}
		while( ($nametable[$n] =~ /^[\s\t]*$/) && ($n <= $#nametable) ) { $n++; }
	}

	$info->{"0"} = \%sar;

    return $info;
}


sub available {
    my( $self ) = @_;

	return( $self->command_available( "sar", "0", "0" ) );
}

1;

