Simple/UserAgent.pm

filter: Statementstmt 17.7% 17 / 96 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 4 totaltotal 14.2% 22 / 154 CC 25 SCAR 39.9
CC
25
Cov
12%
process_response
132.0
head_or_exit
30.0
post_or_exit
30.0
CRAP 53.9
↑ Summary Simple/Utils.pm »
1 #-----------------------------------------------------------------
2 # Monitor::Simple::UserAgent
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # ABSTRACT: See documentation in Monitor::Simple
7 # PODNAME: Monitor::Simple::UserAgent
8 #-----------------------------------------------------------------
9  
10 package Monitor::Simple::UserAgent;
11 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Monitor::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 our $VERSION = '0.2.8'; # VERSION
18  
19 #-----------------------------------------------------------------
20 # Create and return a user agent HTTP header.
21 #-----------------------------------------------------------------
22 0 ▶sub _get_agent_name {
Subroutine _get_agent_name: not called CC=2 SCAR=17.9
23 0 my ($self, $plugin_name) = @_;
24 0 ▶ my $name =
Branch: 1 branch
BranchTrueFalseTotal
$plugin_name ? : 0 0 0
25 'User-Agent: Monitor_Simple' . ($plugin_name ? "_$plugin_name" : '') . ' (nagios-plugin)';
26 0 return $name;
27 }
28  
29 #-----------------------------------------------------------------
30 # Create and return an LWP::UserAgent instance, potentially filled by
31 # properties from the given $service_config for the given test (whose
32 # properties are defined in $test_config).
33 # -----------------------------------------------------------------
34 0 ▶sub _get_agent {
Subroutine _get_agent: not called CC=3 SCAR=24.8
35 0 my ($self, $service_id, $service_config, $test_config) = @_;
36  
37 0 my $ua = LWP::UserAgent->new;
38 0 $ua->agent ($self->_get_agent_name ($service_config->{plugin}->{command}));
39 0 ▶ if (defined $test_config->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $test_config->{"timeout"}) 0 0 0
40 0 ▶ if (! Monitor::Simple::Utils->is_integer ($test_config->{timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Monitor::Simple::Utils"->is_integer($test_config->{"timeout"})) 0 0 0
41 0 Monitor::Simple::Utils->report_and_exit ($service_id, undef,
42 Monitor::Simple::RETURN_WARNING,
43 'Test cannot be executed (a non-integer value in timeout)');
44 }
45 0 $ua->timeout ($test_config->{timeout});
46 }
47 # add here more properties from $config to $ua - as we need them
48  
49 0 return $ua;
50 }
51  
52 #-----------------------------------------------------------------
53 #
54 #-----------------------------------------------------------------
55 0 ▶sub head_or_exit {
Subroutine head_or_exit: not called CC=5 SCAR=34.0
Pod undocumented
56 0 my ($self, $service_id, $full_config) = @_;
57 0 my $config = Monitor::Simple::Utils->service_config_or_exit ($service_id, $full_config);
58  
59 # warn (and exit) if the test cannot be executed
60 0 ▶ unless (exists $config->{plugin}->{'head-test'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"plugin"}{"head-test"}) 0 0 0
61 # a warning: test ignored
62 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
63 Monitor::Simple::RETURN_WARNING,
64 'HEAD test(s) ignored');
65 }
66 0 foreach my $headtest (@{ $config->{plugin}->{'head-test'} }) {
67 0 ▶ unless ($headtest->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headtest->{"url"}) 0 0 0
68 # a warning: test cannot be executed
69 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
70 Monitor::Simple::RETURN_WARNING,
71 'HEAD test cannot be executed (missing URL parameter)');
72 }
73  
74 # make the test
75 0 my $ua = $self->_get_agent ($service_id, $config, $headtest);
76 0 DEBUG ("Invoking HTTP HEAD: " . $headtest->{url});
77 0 my $response = $ua->head ($headtest->{url});
78  
79 # an error: report it and exit accordingly
80 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
81 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
82 Monitor::Simple::RETURN_CRITICAL,
83 0 $response->status_line() . ' - ' . $headtest->{url});
84 }
85 }
86  
87 # everything is okay
88 0 return;
89 }
90  
91 #-----------------------------------------------------------------
92 #
93 #-----------------------------------------------------------------
94 0 ▶sub post_or_exit {
Subroutine post_or_exit: not called CC=5 SCAR=34.0
Pod undocumented
95 0 my ($self, $service_id, $full_config) = @_;
96 0 my $config = Monitor::Simple::Utils->service_config_or_exit ($service_id, $full_config);
97  
98 # warn (and exit) if the test cannot be executed
99 0 ▶ unless (exists $config->{plugin}->{'post-test'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"plugin"}{"post-test"}) 0 0 0
100 # a warning: test ignored
101 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
102 Monitor::Simple::RETURN_WARNING,
103 'POST test(s) ignored');
104 }
105 0 foreach my $posttest (@{ $config->{plugin}->{'post-test'} }) {
106 0 ▶ unless ($posttest->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($posttest->{"url"}) 0 0 0
107 # a warning: test cannot be executed
108 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
109 Monitor::Simple::RETURN_WARNING,
110 'POST test cannot be executed (missing URL parameter)');
111 }
112 0 my $content = $posttest->{data};
113 0 ▶ unless (defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
114 # a warning: test cannot be executed - no input data
115 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
116 Monitor::Simple::RETURN_WARNING,
117 'POST test cannot be executed (missing input data)');
118 }
119  
120 # make the test
121 0 my $ua = $self->_get_agent ($service_id, $config, $posttest);
122 0 DEBUG ("Invoking HTTP POST: " . $posttest->{url});
123 0 my $response = $ua->post ($posttest->{url}, Content => $content);
124  
125 0 $self->process_response ($response, { service_id => $service_id,
126 config => $full_config,
127 test => $posttest });
128 }
129  
130 # everything is okay
131 0 return;
132 }
133  
134 #-----------------------------------------------------------------
135 #
136 #-----------------------------------------------------------------
137 0 ▶sub get_or_exit {
Subroutine get_or_exit: not called CC=4 SCAR=30.0
Pod undocumented
138 0 my ($self, $service_id, $full_config) = @_;
139 0 my $config = Monitor::Simple::Utils->service_config_or_exit ($service_id, $full_config);
140  
141 # warn (and exit) if the test cannot be executed
142 0 ▶ unless (exists $config->{plugin}->{'get-test'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"plugin"}{"get-test"}) 0 0 0
143 # a warning: test ignored
144 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
145 Monitor::Simple::RETURN_WARNING,
146 'GET test(s) ignored');
147 }
148 0 foreach my $gettest (@{ $config->{plugin}->{'get-test'} }) {
149 0 ▶ unless ($gettest->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gettest->{"url"}) 0 0 0
150 # a warning: test cannot be executed
151 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
152 Monitor::Simple::RETURN_WARNING,
153 'GET test cannot be executed (missing URL parameter)');
154 }
155  
156 # make the test
157 0 my $ua = $self->_get_agent ($service_id, $config, $gettest);
158 0 DEBUG ("Invoking HTTP GET: " . $gettest->{url});
159 0 my $response = $ua->get ($gettest->{url});
160  
161 0 $self->process_response ($response, { service_id => $service_id,
162 config => $full_config,
163 test => $gettest });
164 }
165  
166 # everything is okay
167 0 return;
168 }
169  
170 #-----------------------------------------------------------------
171 # $args: { service_id => $service_id,
172 # config => $config,
173 # test => $test }
174 #-----------------------------------------------------------------
175 0 ▶sub process_response {
Subroutine process_response: not called CC=11 SCAR=48.8
Pod undocumented
176 0 my ($self, $response, $args) = @_;
177 0 my $full_config = $args->{config};
178 0 my $test = $args->{test};
179 0 my $service_id = $args->{service_id};
180  
181 # error: report it and exit accordingly
182 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
183 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
184 Monitor::Simple::RETURN_CRITICAL,
185 0 $response->status_line() . ' - ' . $test->{url});
186 }
187  
188 # optionally make more tests on the returned data
189 0 ▶ if (exists $test->{response}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test->{"response"}) 0 0 0
190 # have we got an expected Content-Type?
191 0 my $expected_content_type = $test->{response}->{'content-type'};
192 0 ▶ if ($expected_content_type and $expected_content_type ne $response->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_content_type and $expected_content_type ne $response->content_type) 0 0 0
Condition0%0/3
$expected_content_type and $expected_content_type ne $response->content_type
!ll&&!rl&&r
000
MC/DC0%0/2
$expected_content_type and $expected_content_type ne $response->content_type
$expected_content_type $expected_content_type ne $response->content_type
Truth table0%0/3
A and B
A= $expected_content_typeB= $expected_content_type ne $response->content_type
ABresult
0X0
100
111
193 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
194 Monitor::Simple::RETURN_WARNING,
195 'Unexpected Content-Type returned: ' . $response->content_type);
196 }
197 # have we got an expected content?
198 0 my $expected = $test->{response}->{contains};
199 0 ▶ if (defined $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected) 0 0 0
200 0 foreach my $content (@$expected) {
201 0 my $quoted = quotemeta ($content);
202 0 ▶ if ($response->content !~ m{$quoted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->content =~ /$quoted/) 0 0 0
203 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
204 Monitor::Simple::RETURN_WARNING,
205 "Returned content does not contain: $content");
206 }
207 }
208 }
209 # have we got an exact content?
210 0 $expected = $test->{response}->{equal};
211 0 ▶ if (defined $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected) 0 0 0
212 0 $expected =~ s{^\s*|\s*$}{}g;
213 0 ▶ if (defined $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected) 0 0 0
214 0 my $obtained_value = $response->content;
215 0 $obtained_value =~ s{^\s*|\s*$}{}g;
216 0 ▶ if ($obtained_value ne $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obtained_value ne $expected) 0 0 0
217 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
218 Monitor::Simple::RETURN_WARNING,
219 "Returned content is not equal to expected value: $obtained_value <=> $expected");
220 }
221 }
222 }
223 }
224 0 return;
225 }
226  
227  
228 40 1;
229  
230  
231 =pod
232  
233 =head1 NAME
234  
235 Monitor::Simple::UserAgent - See documentation in Monitor::Simple
236  
237 =head1 VERSION
238  
239 version 0.2.8
240  
241 =head1 AUTHOR
242  
243 Martin Senger <martin.senger@gmail.com>
244  
245 =head1 COPYRIGHT AND LICENSE
246  
247 This software is copyright (c) 2013 by Martin Senger, CBRC-KAUST (Computational Biology Research Center - King Abdullah University of Science and Technology) All Rights Reserved.
248  
249 This is free software; you can redistribute it and/or modify it under
250 the same terms as the Perl 5 programming language system itself.
251  
252 =cut
253  
254  
255 __END__
↑ Summary Simple/Utils.pm »