line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Status::HostStatus; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22407
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
4
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
72
|
|
5
|
1
|
|
|
1
|
|
905
|
use Nagios::Status::Host; |
|
1
|
|
|
|
|
1041
|
|
|
1
|
|
|
|
|
797
|
|
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
|
|
|
|
|
|
|
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 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
|
|
|
|
|
|
|
sub new { |
60
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
if (@hosts) { |
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
|
|
|
|
|
|
|
sub _populate_hosts { |
84
|
0
|
|
|
0
|
|
|
my ($self, $hosts) = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
foreach (@$hosts) { |
87
|
0
|
|
|
|
|
|
my $host = Nagios::Status::Host->new($self->{status_log}, $_); |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
if (defined $host) { |
90
|
0
|
0
|
|
|
|
|
if ($host->is_down) { |
|
|
0
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
push @{ $self->{down} }, $host; |
|
0
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} elsif ($host->is_unreachable) { |
93
|
0
|
|
|
|
|
|
push @{ $self->{unreachable} }, $host; |
|
0
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} else { |
95
|
0
|
|
|
|
|
|
push @{ $self->{up} }, $host; |
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub _populate_all { |
106
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
open(STATUS, $self->{status_log}) or croak "Could not open Nagios status log: $!"; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $found = 0; |
111
|
0
|
|
|
|
|
|
my $host = Nagios::Status::Host->new($self->{status_log}); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
while(my $line = ) { |
114
|
0
|
0
|
|
|
|
|
if ($line =~ /^hoststatus\s*{/) { |
115
|
0
|
|
|
|
|
|
$found = 1; |
116
|
0
|
|
|
|
|
|
next; |
117
|
|
|
|
|
|
|
} # if |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
0
|
|
|
|
if ($found and $line =~ /}/) { |
120
|
0
|
0
|
|
|
|
|
if ($host->is_down) { |
|
|
0
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
push @{ $self->{down} }, $host; |
|
0
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
} elsif ($host->is_unreachable) { |
123
|
0
|
|
|
|
|
|
push @{ $self->{unreachable} }, $host; |
|
0
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
} else { |
125
|
0
|
|
|
|
|
|
push @{ $self->{up} }, $host; |
|
0
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} # if/elsif/else |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$host = Nagios::Status::Host->new($self->{status_log}); |
129
|
0
|
|
|
|
|
|
$found = 0; |
130
|
|
|
|
|
|
|
} # if |
131
|
|
|
|
|
|
|
|
132
|
0
|
0
|
|
|
|
|
if (!$found) { |
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
|
|
|
|
|
|
|
sub check_up { |
160
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
if ($self->{up}) { |
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
|
|
|
|
|
|
|
sub check_down { |
181
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
182
|
|
|
|
|
|
|
|
183
|
0
|
0
|
|
|
|
|
if ($self->{down}) { |
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
|
|
|
|
|
|
|
sub check_unreachable { |
204
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
205
|
|
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
if ($self->{unreachable}) { |
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<< >> |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 SEE ALSO |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L, |
220
|
|
|
|
|
|
|
L, |
221
|
|
|
|
|
|
|
L |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 BUGS |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
226
|
|
|
|
|
|
|
the web interface at L. 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 |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
L |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item * CPAN Ratings |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
L |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=item * Search CPAN |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
L |
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
|
|
|
|
|
|
|
1; # End of Nagios::Status::HostStatus |