File Coverage

blib/lib/App/MonM/Checkit.pm
Criterion Covered Total %
statement 27 141 19.1
branch 0 60 0.0
condition 0 43 0.0
subroutine 9 23 39.1
pod 11 11 100.0
total 47 278 16.9


line stmt bran cond sub pod time code
1             package App::MonM::Checkit; # $Id: Checkit.pm 80 2019-07-08 10:41:47Z abalama $
2 1     1   5 use warnings;
  1         3  
  1         29  
3 1     1   4 use strict;
  1         2  
  1         33  
4 1     1   5 use utf8;
  1         1  
  1         5  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             App::MonM::Checkit - App::MonM checkit class
11              
12             =head1 VIRSION
13              
14             Version 1.02
15              
16             =head1 SYNOPSIS
17              
18             use App::MonM::Checkit;
19              
20             =head1 DESCRIPTION
21              
22             App::MonM checkit class
23              
24             =head2 new
25              
26             my $checker = new App::MonM::Checkit;
27              
28             Returns checker object
29              
30             =head2 check
31              
32             my $ostat = $checker->check({ ... });
33              
34             Performs checking of checkit-sources by checkit rules (checkit config sections)
35              
36             Returns status: 0 - PASS; 1 - FAIL
37              
38             =head2 cleanup
39              
40             my $self = $checker->cleanup;
41              
42             Flushes all working variables to defaults
43              
44             =head2 code
45              
46             my $code = $checker->code;
47             my $newcode = $checker->code(200);
48              
49             Sets and returns response code (rc)
50              
51             =head2 config
52              
53             my $conf = $checker->config;
54              
55             Returns Checkit config structure
56              
57             =head2 content
58              
59             my $content = $checker->content;
60             my $newcontent = $checker->content("Foo Bar Baz");
61              
62             Sets and returns the content value
63              
64             =head2 error
65              
66             my $error = $checker->error;
67             my $newerror = $checker->error("Blah-Blah-Blah");
68              
69             Sets and returns the error value
70              
71             =head2 message
72              
73             my $message = $checker->message;
74             my $newmessage = $checker->message("Foo Bar Baz");
75              
76             Sets and returns the message value
77              
78             =head2 source
79              
80             my $source = $checker->source;
81             my $newsource = $checker->source("http://foo.example.com");
82              
83             Sets and returns the source value
84              
85             =head2 status
86              
87             my $status = $checker->status;
88             my $newstatus = $checker->status(1);
89              
90             Sets and returns the status value
91              
92             =head2 type
93              
94             my $type = $checker->type;
95             my $newtype = $checker->type(1);
96              
97             Sets and returns the type value
98              
99             =head1 HISTORY
100              
101             See C file
102              
103             =head1 TO DO
104              
105             See C file
106              
107             =head1 BUGS
108              
109             * none noted
110              
111             =head1 SEE ALSO
112              
113             L
114              
115             =head1 AUTHOR
116              
117             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
118              
119             =head1 COPYRIGHT
120              
121             Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
122              
123             =head1 LICENSE
124              
125             This program is free software; you can redistribute it and/or
126             modify it under the same terms as Perl itself.
127              
128             See C file and L
129              
130             =cut
131              
132 1     1   36 use vars qw/$VERSION/;
  1         1  
  1         48  
133             $VERSION = '1.02';
134              
135 1     1   6 use Class::C3::Adopt::NEXT;
  1         2  
  1         11  
136 1     1   24 use CTK::ConfGenUtil;
  1         16  
  1         65  
137 1     1   5 use CTK::TFVals qw/ :ALL /;
  1         2  
  1         165  
138              
139 1         359 use base qw/
140             App::MonM::Checkit::HTTP
141             App::MonM::Checkit::Command
142             App::MonM::Checkit::DBI
143 1     1   5 /;
  1         2  
144              
145             use constant {
146             TRUERX => qr/^\s*(1|ok|pass|yes|true)/i, # True regexp
147             FALSERX => qr/^\s*(0|error|fail|no|false)/i, # False regext
148             ORDERBY => "true,false",
149             TARGET => "status",
150             FAIL => 0,
151             PASS => 1,
152             QRTYPES => {
153 0           '' => sub { qr{$_[0]} },
154 0           x => sub { qr{$_[0]}x },
155 0           i => sub { qr{$_[0]}i },
156 0           s => sub { qr{$_[0]}s },
157 0           m => sub { qr{$_[0]}m },
158 0           ix => sub { qr{$_[0]}ix },
159 0           sx => sub { qr{$_[0]}sx },
160 0           mx => sub { qr{$_[0]}mx },
161 0           si => sub { qr{$_[0]}si },
162 0           mi => sub { qr{$_[0]}mi },
163 0           ms => sub { qr{$_[0]}sm },
164 0           six => sub { qr{$_[0]}six },
165 0           mix => sub { qr{$_[0]}mix },
166 0           msx => sub { qr{$_[0]}msx },
167 0           msi => sub { qr{$_[0]}msi },
168 0           msix => sub { qr{$_[0]}msix },
169             },
170 1     1   6 };
  1         2  
  1         1110  
171              
172             sub new {
173 0     0 1   my $class = shift;
174 0           my %args = @_;
175 0           my $self = bless {%args}, $class;
176 0           return $self->cleanup;
177             }
178             sub cleanup {
179 0     0 1   my $self = shift;
180 0           $self->{config} = {}; # Config
181 0           $self->{status} = undef; # 1 - Ok; 0 - Error
182 0           $self->{error} = ''; # Error message
183 0           $self->{code} = undef; # 200
184 0           $self->{type} = undef; # http/dbi/command
185 0           $self->{source} = ''; # URL/DSN/Command
186 0           $self->{message} = ''; # Message string or error
187 0           $self->{content} = ''; # Content data or STDOUT data
188 0           return $self;
189             }
190             sub config {
191 0     0 1   my $self = shift;
192 0           return $self->{config};
193             }
194             sub status {
195 0     0 1   my $self = shift;
196 0           my $v = shift;
197 0 0         $self->{status} = $v if defined $v;
198 0           return $self->{status};
199             }
200             sub error {
201 0     0 1   my $self = shift;
202 0           my $v = shift;
203 0 0         $self->{error} = $v if defined $v;
204 0           return $self->{error};
205             }
206             sub code {
207 0     0 1   my $self = shift;
208 0           my $v = shift;
209 0 0         $self->{code} = $v if defined $v;
210 0           return $self->{code};
211             }
212             sub type {
213 0     0 1   my $self = shift;
214 0           my $v = shift;
215 0 0         $self->{type} = $v if defined $v;
216 0           return $self->{type};
217             }
218             sub source {
219 0     0 1   my $self = shift;
220 0           my $v = shift;
221 0 0         $self->{source} = $v if defined $v;
222 0           return $self->{source};
223             }
224             sub message {
225 0     0 1   my $self = shift;
226 0           my $v = shift;
227 0 0         $self->{message} = $v if defined $v;
228 0           return $self->{message};
229             }
230             sub content {
231 0     0 1   my $self = shift;
232 0           my $v = shift;
233 0 0         $self->{content} = $v if defined $v;
234 0           return $self->{content};
235             }
236             sub check {
237 0     0 1   my $self = shift;
238 0           my $conf = shift;
239 0           my $status = FAIL;
240 0           $self->cleanup;
241 0 0         $self->{config} = $conf if ref($conf) eq 'HASH';
242 0   0       $self->type(lc(value($conf, 'type') || 'http'));
243 0           $self->maybe::next::method();
244              
245             # Check response
246 0           my $true_regexp = _qrreconstruct(value($conf, 'istrue'));
247 0           my $false_regexp= _qrreconstruct(value($conf, 'isfalse'));
248 0   0       my $orderby = value($conf, 'orderby') || ORDERBY;
249 0   0       my $target = lc(value($conf, 'target') || TARGET);
250 0           my $result;
251 0 0         if ($target eq 'code') { $result = $self->code } # code
  0 0          
    0          
252 0           elsif ($target eq 'message') { $result = $self->message } # message
253 0           elsif ($target eq 'content') { $result = $self->content } # content
254 0           else { $result = $self->status } # status
255 0   0       $result //= '';
256              
257             # Check result
258 0 0 0       my $rtt = (defined($true_regexp) && ref($true_regexp)) ? ref($true_regexp) : 'String';
259 0 0 0       my $rtf = (defined($false_regexp) && ref($false_regexp)) ? ref($false_regexp) : 'String';
260 0 0 0       if (($orderby =~ /false\s*\,\s*true/i) || ($orderby =~ /desc/i)) { # DESC
261 0 0         if (defined $false_regexp) {
    0          
262 0           $status = _cmp($result, $false_regexp, [FAIL, PASS]);
263 0 0 0       $self->error("RESULT == FALSE (DEC ORDERED) [AS $rtf]") if !$status && !$self->error;
264             } elsif (defined $true_regexp) {
265 0           $status = _cmp($result, $true_regexp, [PASS, FAIL]);
266 0 0 0       $self->error("RESULT != TRUE (DEC ORDERED) [AS $rtt]") if !$status && !$self->error;
267             } else {
268 0           $status = _cmp($result, FALSERX, [FAIL, PASS]);
269 0 0 0       $self->error("RESULT == FALSE-DEFAULT (DEC ORDERED) [AS Regexp (DEFAULT)]") if !$status && !$self->error;
270             }
271             } else { # ASC
272 0 0         if (defined $true_regexp) {
    0          
273 0           $status = _cmp($result, $true_regexp, [PASS, FAIL]);
274 0 0 0       $self->error("RESULT != TRUE (ASC ORDERED) [AS $rtt]") if !$status && !$self->error;
275             } elsif (defined $false_regexp) {
276 0           $status = _cmp($result, $false_regexp, [FAIL, PASS]);
277 0 0 0       $self->error("RESULT == FALSE (ASC ORDERED) [AS $rtf]") if !$status && !$self->error;
278             } else {
279 0           $status = _cmp($result, TRUERX, [PASS, FAIL]);
280 0 0 0       $self->error("RESULT != TRUE-DEFAULT (ASC ORDERED) [AS Regexp (DEFAULT)]") if !$status && !$self->error;
281             }
282             }
283 0           return $status;
284             }
285              
286             sub _qrreconstruct {
287             # Returns regular expression (QR)
288             # Gets from YAML::Type::regexp of YAML::Types
289             # To input:
290             # !!perl/regexp (?i-xsm:^\s*(error|fault|no))
291             # Translate to:
292             # qr/^\s*(error|fault|no)/i
293 0     0     my $v = shift;
294 0 0         return undef unless defined $v;
295 0 0         return $v unless $v =~ /^\s*\!\!perl\/regexp\s*/i;
296 0           $v =~ s/\s*\!\!perl\/regexp\s*//i;
297 0 0         return qr{$v} unless $v =~ /^\(\?([\^\-xism]*):(.*)\)\z/s;
298 0           my ($flags, $re) = ($1, $2);
299 0           $flags =~ s/-.*//;
300 0           $flags =~ s/^\^//;
301 0   0 0     my $sub = QRTYPES->{$flags} || sub { qr{$_[0]} };
  0            
302 0           return $sub->($re);
303             }
304             sub _cmp {
305 0   0 0     my $s = shift || ''; # Text
306 0   0       my $x = shift || ''; # Regext
307 0   0       my $r = shift || [PASS, FAIL]; # Select [OK, ERROR]
308 0 0         if (ref($x) eq 'Regexp') {
309 0 0         return $r->[0] if $s =~ $x;
310             } else {
311 0 0         return $r->[0] if $s eq $x;
312             }
313 0           return $r->[1];
314             }
315              
316             1;
317              
318             __END__