| 1 | ################################################################################ | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use POSIX qw(mktime); | ||
| 5 | use StatsView::Graph; | ||
| 6 | package StatsView::Graph::Iostat; | ||
| 7 | @StatsView::Graph::Iostat::ISA = qw(StatsView::Graph); | ||
| 8 | |||
| 9 | ################################################################################ | ||
| 10 | |||
| 11 | sub getline($$) | ||
| 12 | { | ||
| 13 | my ($self, $fh) = @_; | ||
| 14 | my $line; | ||
| 15 | while (defined($line = $fh->getline()) && $line =~ /<<State change>>/i) { } | ||
| 16 | return($line); | ||
| 17 | } | ||
| 18 | |||
| 19 | ################################################################################ | ||
| 20 | |||
| 21 | sub new($$$) | ||
| 22 | { | ||
| 23 | my ($class, $file, $fh) = @_; | ||
| 24 | $class = ref($class) || $class; | ||
| 25 | |||
| 26 | # Look for the start/interval line | ||
| 27 | my $line; | ||
| 28 | while (defined($line = $class->getline($fh)) && $line =~ /^\s*$/) { } | ||
| 29 | $line =~ /start:/i && $line =~ /interval:/i || return(undef); | ||
| 30 | |||
| 31 | # Look for the first header line | ||
| 32 | while (defined($line = $class->getline($fh)) && $line =~ /^\s*$/) { } | ||
| 33 | $line =~ /^\s*extended\s+(device|disk)\s+statistics\s*$/i || return(undef); | ||
| 34 | |||
| 35 | my $self = $class->SUPER::init($file); | ||
| 36 | return($self); | ||
| 37 | } | ||
| 38 | |||
| 39 | ################################################################################ | ||
| 40 | |||
| 41 | sub read($) | ||
| 42 | { | ||
| 43 | my ($self) = @_; | ||
| 44 | $self->SUPER::read(); | ||
| 45 | |||
| 46 | # Open the file | ||
| 47 | my $iostat = IO::File->new($self->{file}, "r") | ||
| 48 | || die("Can't open $self->{file}: $!\n"); | ||
| 49 | $self->{title} = "Iostat Statistics"; | ||
| 50 | |||
| 51 | # Look for the start/interval line | ||
| 52 | my $line; | ||
| 53 | while (defined($line = $self->getline($iostat)) && $line =~ /^\s*$/) { } | ||
| 54 | $line =~ /start:/i && $line =~ /interval:/i | ||
| 55 | || die("$self->{file} is not an iostat file (1)\n"); | ||
| 56 | $line =~ m!interval:\s+(\d+)!i; | ||
| 57 | $self->{interval} = $1; | ||
| 58 | $line =~ m!start:\s+(\d\d/\d\d/\d\d(?:\d\d)?)\s+(\d\d:\d\d:\d\d)!i; | ||
| 59 | my ($D, $M, $Y) = split(/\//, $1); | ||
| 60 | my ($h, $m, $s) = split(/:/, $2); | ||
| 61 | $M--; | ||
| 62 | if ($Y >= 100) { $Y -= 1900; } | ||
| 63 | elsif ($Y <= 50) { $Y += 100; } | ||
| 64 | $self->{start} = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1) | ||
| 65 | + $self->{interval}; | ||
| 66 | |||
| 67 | # Look for the first header line | ||
| 68 | while (defined($line = $self->getline($iostat)) && $line =~ /^\s*$/) { } | ||
| 69 | $line =~ /^\s*extended\s+(device|disk)\s+statistics\s*$/i | ||
| 70 | || die("$self->{file} is not an iostat file (2)\n"); | ||
| 71 | $line = $self->getline($iostat); | ||
| 72 | die("$self->{file} is not a iostat file (3)\n") if (! $line); | ||
| 73 | |||
| 74 | # Figure out where the device name is & get rid of it | ||
| 75 | my @colname = split(' ', $line); | ||
| 76 | my $dev_pos; | ||
| 77 | if ($colname[0] eq 'device' || $colname[0] eq 'disk') | ||
| 78 | { | ||
| 79 | shift(@colname); | ||
| 80 | $dev_pos = "first"; | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | pop(@colname); | ||
| 85 | $dev_pos = "last"; | ||
| 86 | } | ||
| 87 | |||
| 88 | # Figure out the column types - N = numeric, % = percentage | ||
| 89 | my @coltype; | ||
| 90 | foreach my $c (@colname) | ||
| 91 | { push(@coltype, $c =~ /%/ ? '%' : 'N'); } | ||
| 92 | $self->define_cols(\@colname, \@coltype); | ||
| 93 | |||
| 94 | # Skip to the start of the second header - | ||
| 95 | # the data after the first is info from the last reboot to the present | ||
| 96 | while (defined ($line = $self->getline($iostat)) | ||
| 97 | && $line !~ /extended\s+(device|disk)\s+statistics/i) { } | ||
| 98 | die("$self->{file} is not a iostat file (3)\n") if (! $line); | ||
| 99 | |||
| 100 | # Work out the timestamp initial values | ||
| 101 | my $tstamp = $self->{start}; | ||
| 102 | my $interval = $self->{interval}; | ||
| 103 | while (defined($line = $self->getline($iostat))) | ||
| 104 | { | ||
| 105 | # Read the data | ||
| 106 | while (defined($line = $self->getline($iostat)) | ||
| 107 | && $line !~ /extended/ && $line !~ /^\s*$/) | ||
| 108 | { | ||
| 109 | my (@value) = split(' ', $line); | ||
| 110 | my $inst = $dev_pos eq "first" ? shift(@value) : pop(@value); | ||
| 111 | |||
| 112 | # Ignore slice and NFS data | ||
| 113 | if ($inst !~ /,\w$|s\d$|:|^nfs/) | ||
| 114 | { | ||
| 115 | $self->define_inst($inst); | ||
| 116 | push(@{$self->{data}{$inst}}, { tstamp => $tstamp, | ||
| 117 | value => [ @value ] }); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | $tstamp += $interval; | ||
| 121 | } | ||
| 122 | $self->{finish} = $tstamp - $interval; | ||
| 123 | $iostat->close(); | ||
| 124 | } | ||
| 125 | |||
| 126 | ################################################################################ | ||
| 127 | |||
| 128 | sub get_data_type($;$) | ||
| 129 | { | ||
| 130 | return("3d"); | ||
| 131 | } | ||
| 132 | |||
| 133 | ################################################################################ | ||
| 134 | 1; |