Test::Parser::Mpstat - Perl module to parse output from mpstat.


Test-Parser documentation Contained in the Test-Parser distribution.

Index


Code Index:

NAME

Top

Test::Parser::Mpstat - Perl module to parse output from mpstat.

SYNOPSIS

Top

 use Test::Parser::Mpstat;

 my $parser = new Test::Parser::Mpstat;
 $parser->parse($text);

DESCRIPTION

Top

This module transforms mpstat output into a hash that can be used to generate XML.

FUNCTIONS

Top

Also see Test::Parser for functions available from the base class.

new()

Creates a new Test::Parser::Mpstat instance. Also calls the Test::Parser base class' new() routine. Takes no arguments.

data()

Returns a hash representation of the mpstat data.

Override of Test::Parser's default parse_line() routine to make it able to parse mpstat output.

to_xml()

Returns mpstat data transformed into XML.

AUTHOR

Top

Mark Wong <markwkm@gmail.com>

COPYRIGHT

Top

SEE ALSO

Top

Test::Parser


Test-Parser documentation Contained in the Test-Parser distribution.
package Test::Parser::Mpstat;

use strict;
use warnings;
use Test::Parser;
use XML::Simple;

@Test::Parser::Mpstat::ISA = qw(Test::Parser);
use base 'Test::Parser';

use fields qw(
              data
              time_units
              );

use vars qw( %FIELDS $AUTOLOAD $VERSION );
our $VERSION = '1.7';

sub new {
    my $class = shift;
    my Test::Parser::Mpstat $self = fields::new($class);
    $self->SUPER::new();

    $self->name('mpstat');
    $self->type('standards');

    #
    # Mpstat data in an array.
    #
    $self->{data} = [];

    #
    # Used for plotting.
    #
    $self->{format} = 'png';
    $self->{outdir} = '.';
    $self->{time_units} = 'Minutes';

    return $self;
}

sub data {
    my $self = shift;
    if (@_) {
        $self->{data} = @_;
    }
    return {mpstat => {data => $self->{data}}};
}

sub parse_line {
    my $self = shift;
    my $line = shift;

    #
    # Trim any leading and trailing whitespaces.
    #
    $line =~ s/^\s+//;
    chomp($line);

    my @i = split / +/, $line;
    #
    # These should ignore the first header line.
    #
    return 1 if (scalar @i != 11);
    return 1 if ($i[1] eq 'CPU');
    #
    # The first set of data doesn't appear to be garbage.
    #
    my $count = scalar @{$self->{data}};
    push @{$self->{data}}, {cpu => $i[1], user => $i[2], nice => $i[3],
            sys => $i[4], iowait => $i[5], irq => $i[6], soft => $i[6],
            steal => $i[7], idle => $i[8], intrs => $i[9],
            elapsed_time => $count};

    return 1;
}

sub to_xml {
    my $self = shift;
    my $outfile = shift;
    return XMLout({data => $self->{data}}, RootName => 'mpstat');
}

1;
__END__