blib/lib/Nagios/Status/HostStatus.pm

filter: Statementstmt 15.7% 11 / 70 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 3 / 9 Podpod 100% 4 / 4 totaltotal 15.5% 18 / 116 CC 17 SCAR 37.0
CC
17
Cov
11%
_populate_all
90.0
_populate_hosts
30.0
new
6.0
CRAP 40.4
↑ Summary
1 package Nagios::Status::HostStatus;
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 2 ▶use Nagios::Status::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Nagios::Status::HostStatus - Nagios 3.0 Class to maintain Hosts' Status.
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 2 our $VERSION = '0.01';
18  
19  
20 =head1 SYNOPSIS
21  
22 # Import the Module
23 use Nagios::Status::HostStatus
24  
25 # Instantiate the HostStatus Object
26 my $status = Nagios::Status::HostStatus->new($nagios_status_log_path);
27 # OR
28 my $status = Nagios::Status::HostStatus->new($nagios_status_log_path, $host1, $host2, ...);
29  
30 # Get hosts that are up.
31 my $up = $status->check_up;
32  
33 # Get hosts that are down.
34 my $down = $status->check_down;
35  
36 # Get hosts that are unreachable.
37 my $unreach = $status->check_unreachable;
38  
39 =head1 DESCRIPTION
40  
41 This module is an object oriented approach to Nagios 3.0 status hosts.
42  
43 =head1 OBJECT CREATION METHOD
44  
45 =over 4
46  
47 =item new
48  
49 my $status = Nagios::Status::HostStatus->new($nagios_status_log_path [, $host1 .. $hostN]);
50  
51 This class constructs a C<Nagios::Status::HostStatus> object. It requires one parameter. A file
52 path containing the path to the Nagios status.log file. There is an optional parameter. An array
53 of host names can be specified, whereby only those domains will be populated.
54  
55 =back
56  
57 =cut
58  
59 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
60 0 my $class = shift;
61  
62 0 my $self = {
63 status_log => shift,
64 };
65  
66 0 $self->{up} = ();
67 0 $self->{down} = ();
68 0 $self->{unreachable} = ();
69  
70 0 bless $self, $class;
71  
72 0 my (@hosts) = @_;
73  
74 0 ▶ if (@hosts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hosts) { } 0 0 0
75 0 $self->_populate_hosts(\@hosts);
76 } else {
77 0 $self->_populate_all;
78 } # if/else
79  
80 0 return $self;
81 } # new
82  
83 0 ▶sub _populate_hosts {
Subroutine _populate_hosts: not called CC=5 SCAR=34.0
84 0 my ($self, $hosts) = @_;
85  
86 0 foreach (@$hosts) {
87 0 my $host = Nagios::Status::Host->new($self->{status_log}, $_);
88  
89 0 ▶ if (defined $host) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $host) { } 0 0 0
90 0 ▶ if ($host->is_down) {
Branch: 2 branches
BranchTrueFalseTotal
if ($host->is_down) { } 0 0 0
elsif ($host->is_unreachable) { } 0 0 0
91 0 push @{ $self->{down} }, $host;
92 } elsif ($host->is_unreachable) {
93 0 push @{ $self->{unreachable} }, $host;
94 } else {
95 0 push @{ $self->{up} }, $host;
96 } # if/elsif/else
97 } else {
98 0 $Nagios::Status::HostStatus::errstr = "Host not found: $_";
99 } # if/else
100 } # foreach
101  
102 0 return;
103 } # _populate_hosts
104  
105 0 ▶sub _populate_all {
Subroutine _populate_all: not called CC=9 SCAR=45.0
106 0 my ($self) = @_;
107  
108 0 ▶ open(STATUS, $self->{status_log}) or croak "Could not open Nagios status log: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STATUS, $self->{"status_log"} or croak("Could not open Nagios status log: $!") 0 0 0
109  
110 0 my $found = 0;
111 0 my $host = Nagios::Status::Host->new($self->{status_log});
112  
113 0 ▶ while(my $line = <STATUS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STATUS)) 0 0 0
114 0 ▶ if ($line =~ /^hoststatus\s*{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^hoststatus\s*{/) 0 0 0
115 0 $found = 1;
116 0 next;
117 } # if
118  
119 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
120 0 ▶ if ($host->is_down) {
Branch: 2 branches
BranchTrueFalseTotal
if ($host->is_down) { } 0 0 0
elsif ($host->is_unreachable) { } 0 0 0
121 0 push @{ $self->{down} }, $host;
122 } elsif ($host->is_unreachable) {
123 0 push @{ $self->{unreachable} }, $host;
124 } else {
125 0 push @{ $self->{up} }, $host;
126 } # if/elsif/else
127  
128 0 $host = Nagios::Status::Host->new($self->{status_log});
129 0 $found = 0;
130 } # if
131  
132 0 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) { } 0 0 0
133 0 next;
134 } else {
135 0 $host->set_attribute($line);
136 } # if/else
137 } # while
138  
139 0 close(STATUS);
140  
141 0 return;
142 } # _populate_all
143  
144 =pod
145  
146 =head1 METHODS
147  
148 =over 4
149  
150 =item check_up
151  
152 my $hosts_up = $status->check_up;
153  
154 This method takes no parameters. It returns an array reference
155 to all hosts found to be UP. Otherwise, it returns undefined.
156  
157 =cut
158  
159 0 ▶sub check_up {
Subroutine check_up: not called CC=2 SCAR=17.9
Pod documented
160 0 my ($self) = @_;
161  
162 0 ▶ if ($self->{up}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'up'}) { } 0 0 0
163 0 return $self->{up};
164 } else {
165 0 return undef;
166 } # if/else
167 } # check_up
168  
169 =pod
170  
171 =item check_down
172  
173 my $hosts_down = $status->check_down;
174  
175 This method takes no parameters. It returns an array reference
176 to all hosts found to be DOWN. Otherwise, it returns undefined.
177  
178 =cut
179  
180 0 ▶sub check_down {
Subroutine check_down: not called CC=2 SCAR=17.9
Pod documented
181 0 my ($self) = @_;
182  
183 0 ▶ if ($self->{down}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'down'}) { } 0 0 0
184 0 return $self->{down};
185 } else {
186 0 return undef;
187 } # if/else
188 } # check_down
189  
190 =pod
191  
192 =item check_unreachable
193  
194 my $hosts_unreach = $status->check_unreachable;
195  
196 This method takes no parameters. It returns an array reference
197 to all hosts found to be UNREACHABLE. Otherwise, it returns undefined.
198  
199 =back
200  
201 =cut
202  
203 0 ▶sub check_unreachable {
Subroutine check_unreachable: not called CC=2 SCAR=17.9
Pod documented
204 0 my ($self) = @_;
205  
206 0 ▶ if ($self->{unreachable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'unreachable'}) { } 0 0 0
207 0 return $self->{unreachable};
208 } else {
209 0 return undef;
210 } # if/else
211 } # check_unreachable
212  
213 =head1 AUTHOR
214  
215 Roy Crowder, C<< <rcrowder at cpan.org> >>
216  
217 =head1 SEE ALSO
218  
219 L<Nagios::Status::Host>,
220 L<perl>,
221 L<Changes>
222  
223 =head1 BUGS
224  
225 Please report any bugs or feature requests to C<bug-nagios-status-hoststatus at rt.cpan.org>, or through
226 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nagios-Status-HostStatus>. I will be notified, and then you'll
227 automatically be notified of progress on your bug as I make changes.
228  
229 =head1 SUPPORT
230  
231 You can find documentation for this module with the perldoc command.
232  
233 perldoc Nagios::Status::HostStatus
234  
235  
236 You can also look for information at:
237  
238 =over 4
239  
240 =item * RT: CPAN's request tracker
241  
242 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Nagios-Status-HostStatus>
243  
244 =item * AnnoCPAN: Annotated CPAN documentation
245  
246 L<http://annocpan.org/dist/Nagios-Status-HostStatus>
247  
248 =item * CPAN Ratings
249  
250 L<http://cpanratings.perl.org/d/Nagios-Status-HostStatus>
251  
252 =item * Search CPAN
253  
254 L<http://search.cpan.org/dist/Nagios-Status-HostStatus/>
255  
256 =back
257  
258 =head1 COPYRIGHT & LICENSE
259  
260 Copyright 2009 WorldSpice Technologies, all rights reserved.
261  
262 This program is free software; you can redistribute it and/or modify it
263 under the same terms as Perl itself.
264  
265 =cut
266  
267  
268 2 1; # End of Nagios::Status::HostStatus
↑ Summary