File Coverage

blib/lib/Metabrik/System/Freebsd/Iostat.pm
Criterion Covered Total %
statement 9 76 11.8
branch 0 20 0.0
condition n/a
subroutine 3 6 50.0
pod 1 3 33.3
total 13 105 12.3


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # system::freebsd::iostat Brik
5             #
6             package Metabrik::System::Freebsd::Iostat;
7 1     1   863 use strict;
  1         3  
  1         32  
8 1     1   5 use warnings;
  1         3  
  1         28  
9              
10 1     1   5 use base qw(Metabrik::Shell::Command);
  1         2  
  1         829  
11              
12             sub brik_properties {
13             return {
14 0     0 1   revision => '$Revision$',
15             tags => [ qw(unstable) ],
16             author => 'GomoR ',
17             license => 'http://opensource.org/licenses/BSD-3-Clause',
18             commands => {
19             standard => [ ],
20             extended => [ ],
21             },
22             require_binaries => {
23             iostat => [ ],
24             },
25             };
26             }
27              
28             sub standard {
29 0     0 0   my $self = shift;
30              
31             # Display two times with -c 2. The first result row is always the same (?)
32             # -C is used to add CPU stats too.
33 0           my $cmd = 'iostat -C -c 2 -w0.1';
34              
35             #
36             # FreeBSD 10.2-RELEASE
37             #
38             # 0: " tty ada0 ada1 pass0 cpu",
39             # 1: " tin tout KB/t tps MB/s KB/t tps MB/s KB/t tps MB/s us ni sy in id",
40             # 2: " 0 1151 36.26 27 0.96 35.96 27 0.96 0.38 0 0.00 3 0 2 0 95",
41             # 3: " 0 0 29.14 7 0.20 29.14 7 0.20 0.00 0 0.00 2 0 0 0 98",
42              
43 0 0         my $lines = $self->capture($cmd) or return;
44              
45 0           my $dev = [];
46 0           my $info = {
47             raw => $lines,
48             };
49 0           my $row = 0;
50 0           for my $line (@$lines) {
51 0           $line =~ s{^\s*}{};
52 0           $line =~ s{\s*$}{};
53              
54 0           my @t = split(/\s+/, $line);
55              
56             # Device line
57 0 0         if ($row == 0) {
    0          
58 0           $dev = \@t;
59             }
60             # Stats line
61             elsif ($row == 3) {
62 0           my $offset = 0;
63 0           for my $this (@$dev) {
64 0 0         if ($this eq 'tty') {
    0          
65 0           $info->{$this}{characters_read} = $t[$offset++];
66 0           $info->{$this}{characters_written} = $t[$offset++];
67             }
68             elsif ($this eq 'cpu') {
69 0           $info->{$this}{user} = $t[-5];
70 0           $info->{$this}{nice} = $t[-4];
71 0           $info->{$this}{system} = $t[-3];
72 0           $info->{$this}{interrupt} = $t[-2];
73 0           $info->{$this}{idle} = $t[-1];
74             }
75             else {
76 0           $info->{$this}{kilobytes_per_transfer} = $t[$offset++];
77 0           $info->{$this}{transfers_per_second} = $t[$offset++];
78 0           $info->{$this}{megabytes_per_second} = $t[$offset++];
79             }
80             }
81             }
82              
83 0           $row++;
84             }
85              
86 0           return $info;
87             }
88              
89             sub extended {
90 0     0 0   my $self = shift;
91              
92 0           my $cmd = 'iostat -C -c 2 -w0.1 -x';
93              
94             # " extended device statistics cpu ",
95             # "device r/s w/s kr/s kw/s qlen svc_t %b us ni sy in id ",
96             # "ada0 3.3 19.3 237.2 749.4 0 4.2 7 3 0 2 0 95",
97             # "ada1 3.2 19.5 236.2 749.5 0 4.2 7 ",
98             # "pass0 0.0 0.0 0.0 0.0 0 244.0 0 ",
99             # "pass1 0.0 0.0 0.0 0.0 0 248.0 0 ",
100             # "pass2 0.0 0.0 0.0 0.0 0 0.0 0 ",
101             # " extended device statistics cpu ",
102             # "device r/s w/s kr/s kw/s qlen svc_t %b us ni sy in id ",
103             # "ada0 0.0 0.0 0.0 0.0 0 0.0 0 3 0 1 0 96",
104             # "ada1 0.0 0.0 0.0 0.0 0 0.0 0 ",
105             # "pass0 0.0 0.0 0.0 0.0 0 0.0 0 ",
106             # "pass1 0.0 0.0 0.0 0.0 0 0.0 0 ",
107             # "pass2 0.0 0.0 0.0 0.0 0 0.0 0 ",
108              
109 0 0         my $lines = $self->capture($cmd) or return;
110              
111 0           my $info = {
112             raw => $lines,
113             };
114 0           my $count = 0;
115 0           for my $line (@$lines) {
116 0           $line =~ s{^\s*}{};
117 0           $line =~ s{\s*$}{};
118              
119             # Skip until second chunks of results
120 0 0         if ($line =~ m{extended device statistics}) {
121 0           $count++;
122 0           next;
123             }
124 0 0         if ($count < 2) {
125 0           next;
126             }
127              
128 0           my @t = split(/\s+/, $line);
129              
130 0           my $offset = 0;
131 0           my $dev = $t[$offset++];
132 0           my $r_s = $t[$offset++];
133 0           my $w_s = $t[$offset++];
134 0           my $kr_s = $t[$offset++];
135 0           my $kw_s = $t[$offset++];
136 0           my $qlen = $t[$offset++];
137 0           my $svc_t = $t[$offset++];
138 0           my $b_percentage = $t[$offset++];
139              
140             # device line is a header
141 0 0         if ($dev eq 'device') {
142 0           next;
143             }
144              
145 0           $info->{$dev}{read_op_per_second} = $r_s;
146 0           $info->{$dev}{write_op_per_second} = $w_s;
147 0           $info->{$dev}{kilobytes_read_per_second} = $kr_s;
148 0           $info->{$dev}{kilobytes_write_per_second} = $kw_s;
149 0           $info->{$dev}{transaction_queue_length} = $qlen;
150 0           $info->{$dev}{average_transaction_duration_in_ms} = $svc_t;
151 0           $info->{$dev}{outstanding_transaction_time_percentage} = $b_percentage;
152              
153             # Only one line contains CPU info
154 0 0         if (! exists($info->{cpu})) {
155 0           $info->{cpu}{user} = $t[-5];
156 0           $info->{cpu}{nice} = $t[-4];
157 0           $info->{cpu}{system} = $t[-3];
158 0           $info->{cpu}{interrupt} = $t[-2];
159 0           $info->{cpu}{idle} = $t[-1];
160             }
161             }
162              
163 0           return $info;
164             }
165              
166             1;
167              
168             __END__