| 1 | ################################################################################ | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use POSIX qw(mktime); | ||
| 5 | use StatsView::Graph; | ||
| 6 | package StatsView::Graph::Vmstat; | ||
| 7 | @StatsView::Graph::Vmstat::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*procs\s+memory\s+page\s+disk\s+faults\s+cpu\s*$/i | ||
| 34 | || return(undef); | ||
| 35 | |||
| 36 | my $self = $class->SUPER::init($file); | ||
| 37 | return($self); | ||
| 38 | } | ||
| 39 | |||
| 40 | ################################################################################ | ||
| 41 | |||
| 42 | sub read($) | ||
| 43 | { | ||
| 44 | my ($self) = @_; | ||
| 45 | $self->SUPER::read(); | ||
| 46 | |||
| 47 | # Open the file | ||
| 48 | my $vmstat = IO::File->new($self->{file}, "r") | ||
| 49 | || die("Can't open $self->{file}: $!\n"); | ||
| 50 | $self->{title} = "Vmstat Statistics"; | ||
| 51 | my ($line1, $line2); | ||
| 52 | |||
| 53 | # Look for the start/interval line | ||
| 54 | while (defined($line1 = $self->getline($vmstat)) && $line1 =~ /^\s*$/) { } | ||
| 55 | $line1 =~ /start:/i && $line1 =~ /interval:/i | ||
| 56 | || die("$self->{file} is not an vmstat file (1)\n"); | ||
| 57 | $line1 =~ m!interval:\s+(\d+)!i; | ||
| 58 | $self->{interval} = $1; | ||
| 59 | $line1 =~ m!start:\s+(\d\d/\d\d/\d\d(?:\d\d)?)\s+(\d\d:\d\d:\d\d)!i; | ||
| 60 | my ($D, $M, $Y) = split(/\//, $1); | ||
| 61 | my ($h, $m, $s) = split(/:/, $2); | ||
| 62 | $M--; | ||
| 63 | if ($Y >= 100) { $Y -= 1900; } | ||
| 64 | elsif ($Y <= 50) { $Y += 100; } | ||
| 65 | $self->{start} = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1) | ||
| 66 | + $self->{interval}; | ||
| 67 | |||
| 68 | # Look for the first header lines | ||
| 69 | while (defined ($line1 = $self->getline($vmstat)) | ||
| 70 | && $line1 !~ /^\s*procs\s+memory\s+page\s+disk\s+faults\s+cpu\s*$/i) { } | ||
| 71 | die("$self->{file} is not a vmstat file (2)\n") if (! $line1); | ||
| 72 | $line2 = $self->getline($vmstat); | ||
| 73 | die("$self->{file} is not a vmstat file (3)\n") if (! $line2); | ||
| 74 | |||
| 75 | # How many headers on line2 share a header from line1 | ||
| 76 | my (@one2two) = (3, 2, 7, 4, 3, 3); | ||
| 77 | |||
| 78 | # Register the column types | ||
| 79 | my (@colname, @coltype); | ||
| 80 | my @h2 = split(' ', $line2); | ||
| 81 | foreach my $h1 (split(' ', $line1)) | ||
| 82 | { | ||
| 83 | foreach my $h (splice(@h2, 0, shift(@one2two))) | ||
| 84 | { | ||
| 85 | push(@colname, "$h1:$h"); | ||
| 86 | push(@coltype, $h1 eq 'cpu' ? '%' : 'N'); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | $self->define_cols(\@colname, \@coltype); | ||
| 90 | |||
| 91 | # Skip the first data line, which is the values since the last reboot | ||
| 92 | $self->getline($vmstat); | ||
| 93 | |||
| 94 | # Work out the timestamp initial values | ||
| 95 | my $tstamp = $self->{start}; | ||
| 96 | my $interval = $self->{interval}; | ||
| 97 | |||
| 98 | # Read the data | ||
| 99 | while (defined($line1 = $self->getline($vmstat))) | ||
| 100 | { | ||
| 101 | # Skip header lines | ||
| 102 | next if ($line1 =~ /^\s*procs/ || $line1 =~ /^\s*r b w/); | ||
| 103 | |||
| 104 | # Save the data | ||
| 105 | my (@value) = split(' ', $line1); | ||
| 106 | push(@{$self->{data}}, { tstamp => $tstamp, value => [ @value ] }); | ||
| 107 | $tstamp += $interval; | ||
| 108 | } | ||
| 109 | $self->{finish} = $tstamp; | ||
| 110 | $vmstat->close(); | ||
| 111 | } | ||
| 112 | |||
| 113 | ################################################################################ | ||
| 114 | |||
| 115 | sub get_data_type($;$) | ||
| 116 | { | ||
| 117 | return("2d"); | ||
| 118 | } | ||
| 119 | |||
| 120 | ################################################################################ | ||
| 121 | 1; |