System/Freebsd/Iostat.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« System/Freebsd/Iocage.pm ↑ Summary System/Freebsd/Netstat.pm »
1 #
2 # $Id$
3 #
4 # system::freebsd::iostat Brik
5 #
6 package Metabrik::System::Freebsd::Iostat;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
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 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 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 my $lines = $self->capture($cmd) or return;
44  
45 my $dev = [];
46 my $info = {
47 raw => $lines,
48 };
49 my $row = 0;
50 for my $line (@$lines) {
51 $line =~ s{^\s*}{};
52 $line =~ s{\s*$}{};
53  
54 my @t = split(/\s+/, $line);
55  
56 # Device line
57 if ($row == 0) {
58 $dev = \@t;
59 }
60 # Stats line
61 elsif ($row == 3) {
62 my $offset = 0;
63 for my $this (@$dev) {
64 if ($this eq 'tty') {
65 $info->{$this}{characters_read} = $t[$offset++];
66 $info->{$this}{characters_written} = $t[$offset++];
67 }
68 elsif ($this eq 'cpu') {
69 $info->{$this}{user} = $t[-5];
70 $info->{$this}{nice} = $t[-4];
71 $info->{$this}{system} = $t[-3];
72 $info->{$this}{interrupt} = $t[-2];
73 $info->{$this}{idle} = $t[-1];
74 }
75 else {
76 $info->{$this}{kilobytes_per_transfer} = $t[$offset++];
77 $info->{$this}{transfers_per_second} = $t[$offset++];
78 $info->{$this}{megabytes_per_second} = $t[$offset++];
79 }
80 }
81 }
82  
83 $row++;
84 }
85  
86 return $info;
87 }
88  
89 sub extended {
90 my $self = shift;
91  
92 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 my $lines = $self->capture($cmd) or return;
110  
111 my $info = {
112 raw => $lines,
113 };
114 my $count = 0;
115 for my $line (@$lines) {
116 $line =~ s{^\s*}{};
117 $line =~ s{\s*$}{};
118  
119 # Skip until second chunks of results
120 if ($line =~ m{extended device statistics}) {
121 $count++;
122 next;
123 }
124 if ($count < 2) {
125 next;
126 }
127  
128 my @t = split(/\s+/, $line);
129  
130 my $offset = 0;
131 my $dev = $t[$offset++];
132 my $r_s = $t[$offset++];
133 my $w_s = $t[$offset++];
134 my $kr_s = $t[$offset++];
135 my $kw_s = $t[$offset++];
136 my $qlen = $t[$offset++];
137 my $svc_t = $t[$offset++];
138 my $b_percentage = $t[$offset++];
139  
140 # device line is a header
141 if ($dev eq 'device') {
142 next;
143 }
144  
145 $info->{$dev}{read_op_per_second} = $r_s;
146 $info->{$dev}{write_op_per_second} = $w_s;
147 $info->{$dev}{kilobytes_read_per_second} = $kr_s;
148 $info->{$dev}{kilobytes_write_per_second} = $kw_s;
149 $info->{$dev}{transaction_queue_length} = $qlen;
150 $info->{$dev}{average_transaction_duration_in_ms} = $svc_t;
151 $info->{$dev}{outstanding_transaction_time_percentage} = $b_percentage;
152  
153 # Only one line contains CPU info
154 if (! exists($info->{cpu})) {
155 $info->{cpu}{user} = $t[-5];
156 $info->{cpu}{nice} = $t[-4];
157 $info->{cpu}{system} = $t[-3];
158 $info->{cpu}{interrupt} = $t[-2];
159 $info->{cpu}{idle} = $t[-1];
160 }
161 }
162  
163 return $info;
164 }
165  
166 1;
167  
168 __END__
« System/Freebsd/Iocage.pm ↑ Summary System/Freebsd/Netstat.pm »