blib/lib/Nagios/Status/Service.pm

filter: Statementstmt 9.8% 8 / 81 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 15.3% 2 / 13 Podpod 100% 10 / 10 totaltotal 13.3% 20 / 150 CC 22 SCAR 38.6
CC
22
Cov
7%
_populate
132.0
set_attribute
12.0
get_attribute
12.0
CRAP 47.4
↑ Summary
1 package Nagios::Status::Service;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Nagios::Status::Service - Nagios 3.0 Container Class for Status Services.
9  
10 =head1 VERSION
11  
12 Version 0.02
13  
14 =cut
15  
16 2 our $VERSION = '0.02';
17  
18 =head1 SYNOPSIS
19  
20 # Import Module
21 use Nagios::Status::Service;
22  
23 # Instantiate the Service object.
24 my $serv = Nagios::Status::Service->new($nagios_status_log_path);
25 # OR
26 my $serv = Nagios::Status::Service->new($nagios_status_log_path, $host_name);
27  
28 # You can set single attributes.
29 $serv->set_attribute('hostname=testserver');
30  
31 # You can get single attributes.
32 my $attr = $serv->get_attribute('host_name');
33 # OR
34 my $attr = $serv->get_attribute('all');
35  
36 # Check if service is warning, unknown, critical, or up.
37 if ($serv->is_warning) {
38 print 'Service is warning...';
39 } elsif ($serv->is_unknown) {
40 print 'Service is unknown...';
41 } elsif ($serv->is_critical) {
42 print 'Service is critical...';
43 } else {
44 print 'Service is up...';
45 }
46  
47 # Simple method for obtaining hostname
48 my $name = $serv->get_hostname;
49  
50 # Get warning time
51 my $warning_time = $serv->get_warning_time if $serv->is_warning;
52  
53 # Get unknown time
54 my $unknown_time = $serv->get_unknown_time if $serv->is_unknown;
55  
56 # Get critical time
57 my $critical_time = $serv->get_critical_time if $serv->is_critical;
58  
59 =head1 DESCRIPTION
60  
61 This module is an object oriented approach to Nagios 3.0 status services.
62  
63 =head1 OBJECT CREATION METHOD
64  
65 =over 4
66  
67 =item new
68  
69 my $serv = Nagios::Status::Service->new($nagios_status_log_path [, $host_name]);
70  
71 This class constructs a C<Nagios::Status::Service> object. It requires one parameter. A file
72 path containing the path to the Nagios status.log file. There is one optional parameter. A
73 hostname can be specified to populate the service object. If no hostname is specified, subroutines
74 must be used to populate the service.
75  
76 =back
77  
78 =cut
79  
80 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
81 0 my $class = shift;
82  
83 0 my $self = {
84 status_log => shift,
85 };
86  
87 0 bless $self, $class;
88  
89 0 my ($serv) = @_;
90  
91 0 ▶ $self->_populate($serv) if defined $serv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $serv 0 0 0
92  
93 0 return $self;
94 } # new
95  
96 =pod
97  
98 =head1 METHODS
99  
100 =over 4
101  
102 =item set_attribute
103  
104 print 'Attribute added...' if $serv->set_attribute('host_name=testserver');
105  
106 This method takes one required parameter, a string (attribute=value). Returns
107 TRUE if attribute was added successfully otherwise returns undefined.
108  
109 =cut
110  
111 0 ▶sub set_attribute {
Subroutine set_attribute: not called CC=3 SCAR=24.8
Pod documented
112 0 my ($self, $attr) = @_;
113  
114 0 ▶ if (!defined $attr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr) 0 0 0
115 0 return;
116 } # if
117  
118 # This was a patch written by Jeremy Krieg to handle a bug not reading '='
119 # signs in service status descriptions. Thanks to Jeremy for the patch!
120 0 ▶ if ( $attr =~ /^\s*([^=]+)=(.*?\S)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^\s*([^=]+)=(.*?\S)\s*$/) 0 0 0
121 0 $self->{attributes}->{$1} = $2;
122 0 return 1;
123 }
124  
125 0 return;
126 }
127  
128 =pod
129  
130 =item get_attribute
131  
132 my $attr = $serv->get_attribute($attribute);
133  
134 This method takes one required parameter, an attribute or 'all'. If 'all'
135 is specified, a hash reference of attributes(keys) and values is returned.
136 If an attribute is specified and is found, the value is returned. Otherwise,
137 undefined is returned.
138  
139 =cut
140  
141 0 ▶sub get_attribute {
Subroutine get_attribute: not called CC=3 SCAR=24.8
Pod documented
142 0 my ($self, $attr) = @_;
143  
144 0 ▶ if ($attr eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq 'all') { } 0 0 0
145 0 return $self->{attributes};
146 } else {
147 0 ▶ if (exists $self->{attributes}->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'attributes'}{$attr}) { } 0 0 0
148 0 return $self->{attributes}->{$attr};
149 } else {
150 0 return;
151 } # if/else
152 } # if/else
153 } # get_attributes
154  
155 =pod
156  
157 =item is_warning
158  
159 print 'Service is warning...' if $serv->is_warning;
160  
161 This method take no parameters. Returns TRUE if service
162 is warning. Otherwise, returns FALSE.
163  
164 =cut
165  
166 0 ▶sub is_warning {
Subroutine is_warning: not called CC=2 SCAR=17.9
Pod documented
167 0 my ($self) = @_;
168  
169 0 ▶ if ($self->{attributes}->{last_time_ok} < $self->{attributes}->{last_time_warning}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'last_time_ok'} < $self->{'attributes'}{'last_time_warning'}) { } 0 0 0
170 0 return 1;
171 } else {
172 0 return 0;
173 } # if/else
174 } # is_warning
175  
176 =pod
177  
178 =item is_unknown
179  
180 print 'Service unknown...' if $serv->is_unknown;
181  
182 This method take no parameters. Returns TRUE if service
183 is unknown. Otherwise, returns FALSE.
184  
185 =cut
186  
187 0 ▶sub is_unknown {
Subroutine is_unknown: not called CC=2 SCAR=17.9
Pod documented
188 0 my ($self) = @_;
189  
190 0 ▶ if ($self->{attributes}->{last_time_ok} < $self->{attributes}->{last_time_unknown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'last_time_ok'} < $self->{'attributes'}{'last_time_unknown'}) { } 0 0 0
191 0 return 1;
192 } else {
193 0 return 0;
194 } # if/else
195 } # is_unknown
196  
197 =pod
198  
199 =item is_critical
200  
201 print 'Service critical...' if $serv->is_critical;
202  
203 This method take no parameters. Returns TRUE if service
204 is critical. Otherwise, returns FALSE.
205  
206 =cut
207  
208 0 ▶sub is_critical {
Subroutine is_critical: not called CC=2 SCAR=17.9
Pod documented
209 0 my ($self) = @_;
210  
211 0 ▶ if ($self->{attributes}->{last_time_ok} < $self->{attributes}->{last_time_critical}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'last_time_ok'} < $self->{'attributes'}{'last_time_critical'}) { } 0 0 0
212 0 return 1;
213 } else {
214 0 return 0;
215 } # if/else
216 } # is_critical
217  
218 =pod
219  
220 =item get_hostname
221  
222 my $name = $serv->get_hostname;
223  
224 This method takes no parameters. Returns hostname of
225 service.
226  
227 =cut
228  
229 0 ▶sub get_hostname {
Subroutine get_hostname: not called CC=1 SCAR=6.9
Pod documented
230 0 my ($self) = @_;
231  
232 0 return $self->{attributes}->{host_name};
233 } # get_hostname
234  
235 =pod
236  
237 =item get_warning_time
238  
239 my $warning_time = $serv->get_warning_time;
240  
241 This method takes no parameters. Returns warning time in seconds
242 if service is warning. Otherwise, returns 0 seconds;
243  
244 =cut
245  
246 0 ▶sub get_warning_time {
Subroutine get_warning_time: not called CC=2 SCAR=17.9
Pod documented
247 0 my ($self) = @_;
248  
249 0 ▶ if ($self->is_warning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_warning) { } 0 0 0
250 0 my $cur_time = time;
251 0 my $time = $cur_time - $self->{attributes}->{last_state_change};
252 0 return $time;
253 } else {
254 0 return 0;
255 } # if/else
256 } # get_warning_time
257  
258 =pod
259  
260 =item get_unknown_time
261  
262 my $utime = $serv->get_unknown_time;
263  
264 This method takes no parameters. Returns unknown time in seconds
265 if service is unknown. Otherwise, returns 0 seconds.
266  
267 =cut
268  
269 0 ▶sub get_unknown_time {
Subroutine get_unknown_time: not called CC=2 SCAR=17.9
Pod documented
270 0 my ($self) = @_;
271  
272 0 ▶ if ($self->is_unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_unknown) { } 0 0 0
273 0 my $cur_time = time;
274 0 my $time = $cur_time - $self->{attributes}->{last_state_change};
275 0 return $time;
276 } else {
277 0 return 0;
278 } # if/else
279 } # get_unknown_time
280  
281 =pod
282  
283 =item get_critical_time
284  
285 my $ctime = $serv->get_critical_time;
286  
287 This method takes no parameters. Returns critical time in seconds
288 if service is critical. Otherwise, returns 0 seconds.
289  
290 =cut
291  
292 0 ▶sub get_critical_time {
Subroutine get_critical_time: not called CC=2 SCAR=17.9
Pod documented
293 0 my ($self) = @_;
294  
295 0 ▶ if ($self->is_critical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_critical) { } 0 0 0
296 0 my $cur_time = time;
297 0 my $time = $cur_time - $self->{attributes}->{last_state_change};
298 0 return $time;
299 } else {
300 0 return 0;
301 } # if/else
302 } # get_critical_time
303  
304 =pod
305  
306 =back
307  
308 =cut
309  
310 0 ▶sub _populate {
Subroutine _populate: not called CC=11 SCAR=48.8
311 0 my ($self, $serv) = @_;
312  
313 0 my %attributes;
314 0 my $found = 0;
315 0 my $found_serv = 0;
316  
317 0 ▶ open(STATUS, $self->{status_log}) or croak "Cannot open status log file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STATUS, $self->{"status_log"} or croak("Cannot open status log file: $!") 0 0 0
318  
319 0 ▶ while(my $line = <STATUS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STATUS)) 0 0 0
320 0 ▶ if ($line =~ /^servicestatus\s*{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^servicestatus\s*{/) 0 0 0
321 0 $found = 1;
322 0 next;
323 } # if
324  
325 0 ▶ if ($found and $line =~ /$serv/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found and $line =~ /$serv/) 0 0 0
Condition0%0/3
$found and $line =~ /$serv/
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $line =~ /$serv/
$found $line =~ /$serv/
Truth table0%0/3
A and B
A= $foundB= $line =~ /$serv/
ABresult
0X0
100
111
326 0 $found_serv = 1;
327 }
328  
329 0 ▶ if ($found and $line =~ /}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found and $line =~ /}/) 0 0 0
Condition0%0/3
$found and $line =~ /}/
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $line =~ /}/
$found $line =~ /}/
Truth table0%0/3
A and B
A= $foundB= $line =~ /}/
ABresult
0X0
100
111
330 0 ▶ if ($found_serv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_serv) { } 0 0 0
331 0 foreach (keys %attributes) {
332 0 $self->{attributes}->{$_} = $attributes{$_};
333 } # foreach
334  
335 0 last;
336 } else {
337 0 %attributes = ();
338 0 $found = 0;
339 } # if/else
340 } # if
341  
342 0 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) { } 0 0 0
343 0 next;
344 } else {
345 # Pointed out by Jeremy Krieg, there was duplicate code
346 # here. Thanks!
347 0 $self->set_attribute($line);
348 } # if/else
349 } # while
350  
351 0 close(STATUS);
352 } # _populate
353  
354 =head1 AUTHOR
355  
356 Roy Crowder, C<< <rcrowder at cpan.org> >>
357  
358 Thanks to Jeremy Krieg for patch on release 0.02.
359  
360 =head1 SEE ALSO
361  
362 L<perl>,
363 L<Changes>
364  
365 =head1 BUGS
366  
367 Please report any bugs or feature requests to C<bug-nagios-status-service at rt.cpan.org>, or through
368 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nagios-Status-Service>. I will be notified, and then you'll
369 automatically be notified of progress on your bug as I make changes.
370  
371 =head1 SUPPORT
372  
373 You can find documentation for this module with the perldoc command.
374  
375 perldoc Nagios::Status::Service
376  
377  
378 You can also look for information at:
379  
380 =over 4
381  
382 =item * RT: CPAN's request tracker
383  
384 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Nagios-Status-Service>
385  
386 =item * AnnoCPAN: Annotated CPAN documentation
387  
388 L<http://annocpan.org/dist/Nagios-Status-Service>
389  
390 =item * CPAN Ratings
391  
392 L<http://cpanratings.perl.org/d/Nagios-Status-Service>
393  
394 =item * Search CPAN
395  
396 L<http://search.cpan.org/dist/Nagios-Status-Service/>
397  
398 =back
399  
400 =head1 COPYRIGHT & LICENSE
401  
402 Copyright (c) 2009 WorldSpice Technologies, all rights reserved.
403  
404 This program is free software; you can redistribute it and/or modify it
405 under the same terms as Perl itself.
406  
407 =cut
408  
409 2 1; # End of Nagios::Status::Service
↑ Summary