blib/lib/App/OpenVZ/BCWatch.pm

filter: Statementstmt 89.0% 163 / 183 Branchbran 71.7% 33 / 46 Conditioncond 43.7% 7 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 91.6% 22 / 24 Podpod 100% 2 / 2 totaltotal 81.0% 227 / 280 CC 37 SCAR 17.1
CC
37
Cov
83%
new
11.0
_prepare_report
9.0
_get_data_running
8.1
CRAP 5.5
↑ Summary
1 package App::OpenVZ::BCWatch;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Mail::Sendmail qw(sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Storable qw(store retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.07';
16  
17 sub new
18 1 ▶{
Subroutine new: called CC=8 SCAR=24.0
Pod documented
19 1 my $class = shift;
20 1 my %args = @_;
21  
22 1 ▶ my $defined_or = sub { defined $_[0] ? $_[0] : $_[1] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 2 3
23  
24 my $self = bless {
25 Config => {
26 input_file => $args{input_file} || '/proc/user_beancounters',
27 data_file => $args{data_file} || File::Spec->catfile(File::HomeDir->my_home, 'vzwatchd.dat'),
28 _field_names => [ qw(uid resource held maxheld barrier limit failcnt) ],
29 _exclude_fields => [ qw(uid resource) ],
30 monitor_fields => $args{monitor_fields} || [ qw(failcnt) ],
31 mail => {
32 from => $args{mail}->{from} || 'root@localhost',
33 to => $args{mail}->{to} || 'root@localhost',
34 subject => $args{mail}->{subject} || 'vzwatchd: NOTICE',
35 },
36 sleep => $defined_or->($args{sleep}, 60),
37 verbose => $defined_or->($args{verbose}, false),
38 1 ▶ _tests => $defined_or->($args{_tests}, false),
Condition43%7/16
$args{'input_file'} || '/proc/user_beancounters'
l!l
10
$args{'data_file'} || 'File::Spec'->catfile('File::HomeDir'->my_home, 'vzwatchd.dat')
l!l&&r!l&&!r
100
$args{'monitor_fields'} || ['failcnt']
l!l
01
$args{'mail'}{'from'} || 'root@localhost'
l!l
01
$args{'mail'}{'to'} || 'root@localhost'
l!l
01
$args{'mail'}{'subject'} || 'vzwatchd: NOTICE'
l!l
01
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/1
$args{'input_file'} || '/proc/user_beancounters'
$args{'input_file'}
MC/DC0%0/2
$args{'data_file'} || 'File::Spec'->catfile('File::HomeDir'->my_home, 'vzwatchd.dat')
$args{'data_file'} 'File::Spec'->catfile('File::HomeDir'->my_home, 'vzwatchd.dat')
MC/DC0%0/1
$args{'monitor_fields'} || ['failcnt']
$args{'monitor_fields'}
MC/DC0%0/1
$args{'mail'}{'from'} || 'root@localhost'
$args{'mail'}{'from'}
MC/DC0%0/1
$args{'mail'}{'to'} || 'root@localhost'
$args{'mail'}{'to'}
MC/DC0%0/1
$args{'mail'}{'subject'} || 'vzwatchd: NOTICE'
$args{'mail'}{'subject'}
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table50%1/2
A
A= $args{'input_file'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args{'data_file'}B= 'File::Spec'->catfile('File::HomeDir'->my_home, 'vzwatchd.dat')
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'monitor_fields'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'mail'}{'from'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'mail'}{'to'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'mail'}{'subject'}
Aresult
11
00
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
39 }
40 }, ref($class) || $class;
41  
42 1 $self->_init;
43  
44 1 return $self;
45 }
46  
47 sub process
48 0 ▶{
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
49 0 my $self = shift;
50  
51 0 delete @$self{qw(data stored)};
52  
53 0 $self->_get_data_running;
54 0 $self->_get_data_file;
55 0 $self->_compare_data;
56 0 $self->_put_data_file;
57 }
58  
59 sub _init
60 1 ▶{
Subroutine _init: called CC=4 SCAR=13.9
61 1 my $self = shift;
62  
63 1 ▶ eval { store({}, $self->{Config}->{data_file}) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { store(scalar {}, $self->{"Config"}{"data_file"}) } } or croak(qq[Cannot store to $self->{"Config"}{"data_file"}: $!]) 1 0 1
64 or croak "Cannot store to $self->{Config}->{data_file}: $!";
65  
66 1 my $pkg_tmpl = join '::', (__PACKAGE__, '_template');
67 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 ▶ if (defined ${$pkg_tmpl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$pkg_tmpl;}) { } 0 1 1
70 0 $self->{template} = ${$pkg_tmpl};
71 }
72 else {
73 1 ${$pkg_tmpl} = $self->{template} = do {
74 1 local $/ = '__END__';
75 1 local $_ = <DATA>;
76 1 chomp;
77 1 s/^\s+//;
78 1 s/\s+\z//;
79 1 $_
80 };
81 }
82  
83 $self->{excluded} = {
84 map {
85 7 my $field = $_;
86 7 ▶ (scalar grep $_ eq $field, @{$self->{Config}->{_exclude_fields}})
Branch: 1 branch
BranchTrueFalseTotal
scalar grep(($_ eq $field), @{$$self{'Config'}{'_exclude_fields'};}) ? : 2 5 7
87 ? ($field => true)
88 : ($field => false)
89 1 } @{$self->{Config}->{_field_names}}
90 };
91  
92 1 my $i;
93 1 $self->{index} = { map { $_ => $i++ } @{$self->{Config}->{_field_names}} };
94 }
95  
96 sub _get_data_running
97 1 ▶{
Subroutine _get_data_running: called CC=8 SCAR=20.9
98 1 my $self = shift;
99  
100 open(my $fh, '<', $self->{Config}->{input_file})
101 1 ▶ or croak "Cannot read $self->{Config}->{input_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"Config"}{"input_file"} or croak(qq[Cannot read $self->{"Config"}{"input_file"}: $!]) 1 0 1
102 1 my $output = do { local $/; <$fh> };
103 1 close($fh);
104  
105 1 my $valid_format = join '\s+', @{$self->{Config}->{_field_names}};
106  
107 1 ▶ unless ($output =~ /$valid_format/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output =~ /$valid_format/) 0 1 1
108 0 croak "Format of $self->{Config}->{input_file} not recognized";
109 }
110  
111 1 my $re = qr{
112 \s*?
113 (?:\d+?\:)? \s+?
114 (?:\w+?) \s+?
115 (?:(?:\d+?) \s*?){5}
116 }x;
117  
118 1 my $uid;
119  
120 my @names = grep {
121 7 ▶ !$self->{excluded}->{$_}
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'excluded'}{$_} ? : 5 2 7
122 ? $_ : ()
123 1 } @{$self->{Config}->{_field_names}};
124  
125 1 local $1;
126 1 ▶ while ($output =~ /^($re)$/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /^($re)$/gm) 144 1 145
127 144 my $line = $1;
128 144 ▶ if ($line =~ /^ \s+? (\d+?)\:/gx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ \s+? (\d+?)\:/gx) 6 138 144
129 6 $uid = $1;
130 }
131 144 my $res;
132 144 ▶ if ($line =~ /\G \s+? (\w+)/gx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\G \s+? (\w+)/gx) 144 0 144
133 144 $res = $1;
134 }
135 144 my @fields;
136 144 ▶ if ($line =~ /\G \s+ (.*) $/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\G \s+ (.*) $/x) 144 0 144
137 144 @fields = split /\s+/, $1;
138 }
139 144 push @{$self->{data}{$uid}{$res}},
140 144 { map { $names[$_] => $fields[$_] } (0 .. $#fields) };
141 }
142 }
143  
144 sub _get_data_file
145 1 ▶{
Subroutine _get_data_file: called CC=2 SCAR=7.1
146 1 my $self = shift;
147  
148 1 ▶ eval { $self->{stored} = retrieve($self->{Config}->{data_file}) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"stored"} = retrieve($self->{"Config"}{"data_file"}) } } or croak(qq[Cannot retrieve from $self->{"Config"}{"data_file"}: $!]) 1 0 1
149 or croak "Cannot retrieve from $self->{Config}->{data_file}: $!";
150 }
151  
152 sub _compare_data
153 1 ▶{
Subroutine _compare_data: called CC=5 SCAR=16.1
154 1 my $self = shift;
155  
156 my $has_changed = sub
157 144 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 144 my ($uid, $res, $i) = @_;
159  
160 my ($data, $stored) = map {
161 144 ▶ my $type = $_; sub { $self->{$type}{$uid}{$res}->[$i]->{$_[0]} || 0 }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{$type}{$uid}{$res}[$i]{$_[0]} or 0 3 285 288
162 288 } qw(data stored);
163  
164 144 return scalar grep { $data->($_) > $stored->($_) } @{$self->{Config}->{monitor_fields}};
165 1 };
166  
167 1 foreach my $uid (sort {$a <=> $b} keys %{$self->{stored}}) {
168 6 foreach my $res (sort {$a cmp $b} keys %{$self->{stored}{$uid}}) {
169 126 foreach my $index (0 .. $#{$self->{stored}{$uid}{$res}}) {
170 144 ▶ if ($has_changed->($uid, $res, $index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$has_changed($uid, $res, $index)) 2 142 144
171 2 $self->_create_report($uid, $res, $index);
172 }
173 }
174 }
175 }
176 }
177  
178 sub _create_report
179 2 ▶{
Subroutine _create_report: called CC=3 SCAR=14.8
180 2 my $self = shift;
181 2 my ($uid, $res, $index) = @_;
182  
183 2 ▶ if ($self->{Config}->{_tests}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Config'}{'_tests'}) { } 2 0 2
184 2 push @{$self->{tests}->{report}}, $self->_prepare_report($uid, $res, $index);
185 }
186 else {
187 0 my $report = $self->_prepare_report($uid, $res, $index);
188 0 $self->_send_mail($report);
189  
190 0 ▶ if ($self->{Config}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Config"}{"verbose"}) 0 0 0
191 0 print "Report for \"$uid: $res\" sent to '$self->{Config}->{mail}->{to}'\n";
192 }
193 }
194 }
195  
196 sub _put_data_file
197 1 ▶{
Subroutine _put_data_file: called CC=2 SCAR=7.1
198 1 my $self = shift;
199  
200 1 ▶ eval { store($self->{data}, $self->{Config}->{data_file}) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { store($self->{"data"}, $self->{"Config"}{"data_file"}) } } or croak(qq[Cannot store to $self->{"Config"}{"data_file"}: $!]) 1 0 1
201 or croak "Cannot store to $self->{Config}->{data_file}: $!";
202 }
203  
204 sub _prepare_report
205 2 ▶{
Subroutine _prepare_report: called CC=9 SCAR=22.0
206 2 my $self = shift;
207 2 my ($uid, $res, $index) = @_;
208  
209 2 my @fixed_fields = ($uid, $res) x 2;
210 my @mapping = (
211 [
212 { map {
213 4 $_ => shift @fixed_fields,
214 2 } @{$self->{Config}->{_exclude_fields}} },
215 { map {
216 10 $_ => $self->{stored}{$uid}{$res}->[$index]->{$_},
217 2 } grep !$self->{excluded}->{$_}, @{$self->{Config}->{_field_names}} }
218 ],
219 [
220 { map {
221 4 $_ => shift @fixed_fields,
222 2 } @{$self->{Config}->{_exclude_fields}} },
223 { map {
224 10 $_ => $self->{data}{$uid}{$res}->[$index]->{$_},
225 2 } grep !$self->{excluded}->{$_}, @{$self->{Config}->{_field_names}} }
226 ],
227 );
228  
229 2 my @values;
230 2 foreach my $map (@mapping) {
231 4 my @v;
232 4 foreach my $entry (@$map) {
233 push @v, map $entry->{$_}, sort {
234 8 $self->{index}->{$a} <=> $self->{index}->{$b}
235 } keys %$entry;
236 }
237 4 push @values, [ @v ];
238 }
239  
240 2 my %changed;
241 2 foreach my $field (keys %{$mapping[0]->[1]}) {
242 10 ▶ if ($mapping[0]->[1]->{$field} != $mapping[1]->[1]->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapping[0][1]{$field} != $mapping[1][1]{$field}) 2 8 10
243 2 $changed{$self->{index}->{$field}} = true;
244 }
245 }
246  
247 2 my $tmpl = \@values;
248 2 my $report = $self->{template};
249  
250 2 local $1;
251  
252 2 ▶ while (my ($var) = $report =~ /(\$\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($var) = $report =~ /(\$\S+)/) 28 2 30
253 28 ▶ unless ($report =~ /\Q$var\E$/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report =~ /\Q$var\E$/m) 14 14 28
254 14 my $len = length($var) - length do { eval $var };
255 14 $report =~ s/(?<=\Q$var\E)/' ' x $len/e;
256 }
257 28 $report =~ s/(\Q$var\E)/$1/ee;
258 }
259  
260 2 ▶ while (my ($pos) = $report =~ /\((\d+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($pos) = $report =~ /\((\d+)\)/) 14 2 16
261 14 ▶ my $marked = $changed{$pos} ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$changed{$pos} ? : 2 12 14
262 14 $report =~ s/\($pos\)/ $marked/;
263 }
264  
265 2 return $report;
266 }
267  
268 sub _send_mail
269 0 ▶{
Subroutine _send_mail: not called CC=2 SCAR=17.9
270 0 my $self = shift;
271 0 my ($report) = @_;
272  
273 my %mail = (
274 From => $self->{Config}->{mail}->{from},
275 To => $self->{Config}->{mail}->{to},
276 Subject => $self->{Config}->{mail}->{subject},
277 0 Message => <<"EOT",
278 0 ${\hostname}
279  
280 $report
281  
282 --
283 0 ${\basename($0)} v$VERSION - ${\scalar localtime}
284 EOT
285 );
286 0 ▶ sendmail(%mail)
Branch: 1 branch
BranchTrueFalseTotal
sendmail(%mail) or croak("Cannot send mail: $Mail::Sendmail::error") 0 0 0
287 or croak "Cannot send mail: $Mail::Sendmail::error";
288 }
289  
290 2 1;
291 __DATA__
↑ Summary