Graph/Sar.pm untested

filter: Statementstmt 0.0% 0 / 211 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 404 CC 45 SCAR 49.6
CC
45
Cov
0%
read_text_hhdd
306.0
scan_hdhd
182.0
store_sample
56.0
CRAP 143.0
1 ################################################################################
2  
3 use strict;
4 use POSIX qw(mktime);
5 use StatsView::Graph;
6 package StatsView::Graph::Sar;
7 @StatsView::Graph::Sar::ISA = qw(StatsView::Graph);
8
9 %StatsView::Graph::Sar::info =
10 (
11 'Filesystem activity' => { flag => '-a',
12 type => '2d',
13 pattern => qr/iget/, },
14 'Buffer activity' => { flag => '-b',
15 type => '2d',
16 pattern => qr/bread/, },
17 'System calls' => { flag => '-c',
18 type => '2d' ,
19 pattern => qr/scall/, },
20 'Disk IO' => { flag => '-d',
21 type => '3d' ,
22 pattern => qr/device/, },
23 'Paging (2)' => { flag => '-g',
24 type => '2d' ,
25 pattern => qr/pgout/, },
26 'Kernel memory' => { flag => '-k',
27 type => '2d' ,
28 pattern => qr/sml_mem/, },
29 'IPC' => { flag => '-m',
30 type => '2d' ,
31 pattern => qr/msg/, },
32 'Paging (1)' => { flag => '-p',
33 type => '2d' ,
34 pattern => qr/atch/, },
35 'Run queue' => { flag => '-q',
36 type => '2d' ,
37 pattern => qr/runq/, },
38 'Free memory' => { flag => '-r',
39 type => '2d' ,
40 pattern => qr/freemem/, },
41 'CPU usage' => { flag => '-u',
42 type => '2d' ,
43 pattern => qr/%usr/, },
44 'Kernel table sizes' => { flag => '-v',
45 type => '2d' ,
46 pattern => qr/proc-sz/, },
47 'Swapping/Switching' => { flag => '-w',
48 type => '2d' ,
49 pattern => qr/swpin/, },
50 'TTY activity' => { flag => '-y',
51 type => '2d' ,
52 pattern => qr/rawch/, },
53 );
54  
55  
56 ################################################################################
57 # Figure out what sort of data this line is a header for
58  
59 sub classify_header($$)
60 {
61 my ($self, $line) = @_;
62 my ($desc, $inf);
63 while (($desc, $inf) = each(%StatsView::Graph::Sar::info))
64 {
65 last if ($line =~ $inf->{pattern});
66 }
67 scalar(keys(%StatsView::Graph::Sar::info)); # reset the each() iterator
68 return($desc);
69 }
70  
71 ################################################################################
72 # Get the next line, ignoring state change lines
73  
74 sub getline($$)
75 {
76 my ($self, $fh) = @_;
77 my $line;
78 while (defined($line = $fh->getline()) &&
79 index($line, "<<State change>>") != -1)
80 { }
81 return($line);
82 }
83  
84 ################################################################################
85  
86 sub new($$$)
87 {
88 my ($class, $file, $fh) = @_;
89 $class = ref($class) || $class;
90  
91 # Assume binary files are binary sar output
92 if (-B $file)
93 {
94 my $self = $class->SUPER::init($file);
95 @{$self->{category}} = keys(%StatsView::Graph::Sar::info);
96 $self->{reader} = sub { shift(@_)->read_binary(@_); };
97 return($self);
98 }
99 # Otherwise, check for the two possible text formats
100 else
101 {
102 # Look for the first header line
103 my ($line, $type);
104 while (defined($line = $class->getline($fh)) && $line !~ /^\d\d:\d\d:\d\d/)
105 { }
106 return(undef) if (! $line);
107  
108 # Not a sar file if the line is not a header
109 $type = $class->classify_header($line) || return(undef);
110  
111 # Save the header type
112 my $self = $class->SUPER::init($file);
113 push(@{$self->{category}}, $type);
114
115 # Peek at the next line. If it too is a header, the format is hhdd
116 $line = $self->getline($fh);
117 if ($type = $self->classify_header($line))
118 {
119 push(@{$self->{category}}, $type);
120 $self->{reader} = sub { shift(@_)->read_text_hhdd(@_); };
121  
122 # All the headers will be between here and the next blank line
123 while (defined($line = $self->getline($fh)) && $line !~ /^\s*$/)
124 {
125 $type = $self->classify_header($line) || return(undef);
126 push(@{$self->{category}}, $type);
127 }
128 }
129 # Otherwise the format is hdhd
130 else
131 {
132 $self->{reader} = sub { shift(@_)->read_text_hdhd(@_); };
133  
134 # Headers will be scattered throughout the file
135 while (defined($line = $self->getline($fh)))
136 {
137 # Look for timestamp lines
138 next if ($line !~ /^\d\d:\d\d:\d\d/);
139 if ($type = $self->classify_header($line))
140 {
141 push(@{$self->{category}}, $type);
142 }
143 }
144 }
145 return($self);
146 }
147 }
148  
149 ################################################################################
150  
151 sub store_sample($$$$;$)
152 {
153 my ($self, $type, $tstamp, $line, $sar) = @_;
154  
155 # Remove any timestamp
156 $line =~ s/^\d\d:\d\d:\d\d//;
157 push(@{$self->{tstamps}}, $tstamp) if ($line !~ /unix restarts/);
158  
159 # 2d samples just live on one line
160 if ($type eq '2d')
161 {
162 # save no data for a restart line, otherwise save the fields
163 my @value = $line =~ /unix restarts/ ? () : split(' ', $line);
164 push(@{$self->{data}}, { tstamp => $tstamp, value => [ @value ] });
165 }
166  
167 # 3d samples live on multiple lines. We have to guess where they end :-(
168 else
169 {
170 # If the line is a restart line, push an empty sample onto each instance
171 if ($line =~ /unix restarts/)
172 {
173 foreach my $inst ($self->get_instances())
174 {
175 push(@{$self->{data}{$inst}}, { tstamp => $tstamp, value => [ ] });
176 }
177 return;
178 }
179  
180 # Otherwise, process the first line of the sample point
181 my ($inst, @value) = split(' ', $line);
182 # Ignore slice and NFS data
183 if ($inst !~ /,\w$|s\d$|^\w+:|^nfs/)
184 {
185 $self->define_inst($inst);
186 push(@{$self->{data}{$inst}}, { tstamp => $tstamp, value => [ @value ] });
187 }
188  
189 # Process all the subsequent lines of the sample point
190 while (defined($line = $self->getline($sar)) && $line =~ /^\s*[a-z]/i)
191 {
192 ($inst, @value) = split(' ', $line);
193 # Ignore slice and NFS data
194 if ($inst !~ /,\w$|s\d$|^\w+:|^nfs/)
195 {
196 $self->define_inst($inst);
197 push(@{$self->{data}{$inst}}, { tstamp => $tstamp,
198 value => [ @value ] });
199 }
200 }
201 }
202 }
203  
204 ################################################################################
205 # Run queue stats use blanks instead of zeros, so split won't work
206  
207 sub horrid_run_queue_hack($$$)
208 {
209 my ($self, $tstamp, $line) = @_;
210  
211 # Remove any timestamp
212 $line =~ s/^\d\d:\d\d:\d\d//;
213 push(@{$self->{tstamps}}, $tstamp) if ($line !~ /unix restarts/);
214  
215 my @value;
216 foreach my $v (unpack('A8A8A8A8', $line))
217 {
218 $v =~ s/\s+//g;
219 $v = 0 if ($v eq '');
220 push(@value, $v);
221 }
222 push(@{$self->{data}}, { tstamp => $tstamp, value => [ @value ] });
223 }
224  
225 ################################################################################
226  
227 sub scan_hdhd($$$)
228 {
229 my ($self, $sar, $category) = @_;
230 my ($type, $pattern) =
231 @{$StatsView::Graph::Sar::info{$category}}{qw(type pattern)};
232 my $line;
233  
234 # Look for the banner
235 while (defined ($line = $self->getline($sar)) && $line !~ /^SunOS/) { }
236 die("$self->{file} is not a sar file (1)\n") if (! $line);
237 my ($M, $D, $Y) = split(/\//, (split(' ', $line))[5]);
238 if ($Y >= 100) { $Y -= 1900; }
239 elsif ($Y <= 50) { $Y += 100; }
240 $M--;
241  
242 # Look for the header line & get a list of column names
243 while (defined($line = $self->getline($sar)))
244 {
245 last if ($line =~ /^\d\d:\d\d:\d\d/ && $line =~ $pattern);
246 }
247 die("$self->{file} is not a sar file (2)\n") if (! $line);
248 my @colname = split(' ', $line);
249 shift(@colname); # lose the timestamp
250 shift(@colname) if ($type eq '3d'); # and the instance for 3d data
251  
252 # Figure out their types - N = numeric, % = percentage
253 my @coltype;
254 foreach my $c (@colname)
255 { push(@coltype, $c =~ /%/ ? '%' : 'N'); }
256 $self->define_cols(\@colname, \@coltype);
257  
258 # Read the data block up to the Averages part
259 my $last_tstamp = POSIX::mktime(0, 0, 0, $D, $M, $Y, 0, 0, -1);
260 my $tstamp;
261 my $sample = 1;
262 while (defined($line = $self->getline($sar)) && $line !~ /^Average/)
263 {
264 # Look for the start of the next sample point (a timestamp)
265 next if ($line !~ /^(\d\d):(\d\d):(\d\d)/);
266 my ($h, $m, $s) = ($1, $2, $3);
267 $tstamp = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1);
268  
269 # Look for day rollover
270 if ($tstamp < $last_tstamp)
271 {
272 $D++;
273 $tstamp = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1);
274 }
275  
276 # If this is the first sample, store the start time
277 if ($sample == 1)
278 { $self->{start} = $tstamp; $sample++; }
279 # If this is the second sample, store the interval
280 elsif ($sample == 2)
281 { $self->{interval} = $tstamp - $last_tstamp; $sample++; }
282  
283 # Store the sample
284 if ($category eq 'Run queue')
285 { $self->horrid_run_queue_hack($tstamp, $line); }
286 else
287 { $self->store_sample($type, $tstamp, $line, $sar); }
288  
289 $last_tstamp = $tstamp;
290 }
291 $self->{finish} = $tstamp;
292 }
293  
294 ################################################################################
295  
296 sub read_binary($$)
297 {
298 my ($self, $category) = @_;
299  
300 $self->{title} = "Sar $category";
301 my $sar =
302 IO::File->new("sar $StatsView::Graph::Sar::info{$category}{flag} " .
303 "-f $self->{file} |")
304 || die("Can't run sar: $!\n");
305 $self->scan_hdhd($sar, $category);
306 $sar->close();
307 die("$self->{file} is not a sar file\n") if (! defined($self->{data}));
308 return(1);
309 }
310  
311 ################################################################################
312  
313 sub read_text_hdhd($$)
314 {
315 my ($self, $category) = @_;
316  
317 $self->{title} = "Sar $category";
318 my $sar = IO::File->new($self->{file}, "r")
319 || die("Can't open $self->{file}: $!\n");
320 $self->scan_hdhd($sar, $category);
321 $sar->close();
322 return(1);
323 }
324  
325 ################################################################################
326  
327 sub read_text_hhdd($$)
328 {
329 my ($self, $category) = @_;
330 my $type = $StatsView::Graph::Sar::info{$category}{type};
331  
332 $self->{title} = "Sar $category";
333 my $sar = IO::File->new($self->{file}, "r")
334 || die("Can't open $self->{file}: $!\n");
335 my $line;
336  
337 # Look for the banner
338 while (defined ($line = $self->getline($sar)) && $line !~ /^SunOS/) { }
339 die("$self->{file} is not a sar file (3)\n") if (! $line);
340 my ($M, $D, $Y) = split(/\//, (split(' ', $line))[5]);
341 if ($Y >= 100) { $Y -= 1900; }
342 elsif ($Y <= 50) { $Y += 100; }
343 $M--;
344  
345 # Look for the headers
346 while (defined($line = $self->getline($sar)) && $line !~ /^\d\d:\d\d:\d\d/) { }
347 die("$self->{file} is not a sar file (4)\n") if (! $line);
348  
349 # All the headers are in a block, terminated by a blank line.
350 # Find how far down the one we want is
351 my @skip;
352 while (defined($line) && $line !~ /^\s*$/)
353 {
354 # Classify the header & get it's type
355 my $type = $self->classify_header($line);
356 die("$self->{file} is not a sar file (5)\n") if (! $type);
357 last if ($type eq $category);
358 push(@skip, $StatsView::Graph::Sar::info{$type}{type});
359 $line = $self->getline($sar);
360 }
361  
362 # Get a list of column names
363 die("$self->{file} is not a sar file (6)\n") if (! $line);
364 $line =~ s/^\d\d:\d\d:\d\d//; # lose any timestamp
365 my @colname = split(' ', $line);
366 shift(@colname) if ($type eq '3d'); # lose the instance for 3d data
367  
368 # Figure out their types - N = numeric, % = percentage
369 my @coltype;
370 foreach my $c (@colname)
371 { push(@coltype, $c =~ /%/ ? '%' : 'N'); }
372 $self->define_cols(\@colname, \@coltype);
373  
374 # Scan the file, up to the Averages block
375 my $last_tstamp = POSIX::mktime(0, 0, 0, $D, $M, $Y, 0, 0, -1);
376 my $tstamp;
377 my $sample = 1;
378 while (defined($line = $self->getline($sar)) && $line !~ /^Average/)
379 {
380 # Look for the start of the next sample point (a timestamp)
381 next if ($line !~ /^(\d\d):(\d\d):(\d\d)/);
382 my ($h, $m, $s) = ($1, $2, $3);
383 $tstamp = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1);
384  
385 # Look for day rollover
386 if ($tstamp < $last_tstamp)
387 {
388 $D++;
389 $tstamp = POSIX::mktime($s, $m, $h, $D, $M, $Y, 0, 0, -1);
390 }
391  
392 # If this is the first sample, store the start time
393 if ($sample == 1) { $self->{start} = $tstamp; }
394 # If this is the second sample, store the interval
395 elsif ($sample == 2) { $self->{interval} = $tstamp - $last_tstamp; }
396  
397 # Skip lines up to the start of the info we want
398 foreach my $hdr (@skip)
399 {
400 if ($hdr eq '2d')
401 {
402 $line = $self->getline($sar);
403 }
404 else
405 {
406 # Read up to the end of the data block. Have to guess this :-(
407 while (defined($line = $self->getline($sar))
408 && $line =~ /^\s*[a-z]/i) { }
409 }
410 }
411 # Deal gracefully with truncated files
412 last if (! $line);
413  
414 # Store the sample
415 if ($category eq 'Run queue')
416 { $self->horrid_run_queue_hack($tstamp, $line); }
417 else
418 { $self->store_sample($type, $tstamp, $line, $sar); }
419  
420 $sample++;
421 $last_tstamp = $tstamp;
422 }
423 $self->{finish} = $tstamp;
424 $sar->close();
425 return(1);
426 }
427  
428 ################################################################################
429  
430 sub read($$)
431 {
432 my ($self, $category) = @_;
433 die("Illegal category type $category\n")
434 if (! exists($StatsView::Graph::Sar::info{$category}));
435 $self->SUPER::read($category);
436 return(&{$self->{reader}}($self, $category));
437 }
438  
439 ################################################################################
440  
441 sub get_data_type($;$)
442 {
443 my ($self, $category) = @_;
444 return($StatsView::Graph::Sar::info{$category}{type});
445 }
446  
447 ################################################################################
448  
449 1;