| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Nagios::Status::Host; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23993
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
32
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
847
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Nagios::Status::Host - Nagios 3.0 Container Class for Status Hosts. |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.01 |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Import Module |
|
21
|
|
|
|
|
|
|
use Nagios::Status::Host; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Instantiate the Host object. |
|
24
|
|
|
|
|
|
|
my $host = Nagios::Status::Host->new($nagios_status_log_path); |
|
25
|
|
|
|
|
|
|
# OR |
|
26
|
|
|
|
|
|
|
my $host = Nagios::Status::Host->new($nagios_status_log_path, $host_name); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# You can set single attributes. |
|
29
|
|
|
|
|
|
|
$host->set_attribute('hostname=testserver'); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# You can get single attributes. |
|
32
|
|
|
|
|
|
|
my $attr = $host->get_attribute('host_name'); |
|
33
|
|
|
|
|
|
|
# OR |
|
34
|
|
|
|
|
|
|
my $attr = $host->get_attribute('all'); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Check if host is down, unreachable, or up. |
|
37
|
|
|
|
|
|
|
if ($host->is_down) { |
|
38
|
|
|
|
|
|
|
print 'Host is down...'; |
|
39
|
|
|
|
|
|
|
} elsif ($host->is_unreachable) { |
|
40
|
|
|
|
|
|
|
print 'Host is unreachable...'; |
|
41
|
|
|
|
|
|
|
} else { |
|
42
|
|
|
|
|
|
|
print 'Host is up...'; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Simple method for obtaining hostname |
|
46
|
|
|
|
|
|
|
my $name = $host->get_hostname; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Get downtime |
|
49
|
|
|
|
|
|
|
my $downtime = $host->get_downtime if $host->is_down; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Get unreachable time |
|
52
|
|
|
|
|
|
|
my $unreach_time = $host->get_unreachable_time if $host->is_unreachable; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
This module is an object oriented approach to Nagios 3.0 status hosts. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 OBJECT CREATION METHOD |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=over 4 |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item new |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $host = Nagios::Status::Host->new($nagios_status_log_path [, $host_name]); |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This class constructs a C object. It requires one parameter. A file |
|
67
|
|
|
|
|
|
|
path containing the path to the Nagios status.log file. There is one optional parameter. A |
|
68
|
|
|
|
|
|
|
hostname can be specified to populate the host object. If no hostname is specified, subroutines |
|
69
|
|
|
|
|
|
|
must be used to populate the host. |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=back |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub new { |
|
76
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
77
|
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $self = { |
|
79
|
|
|
|
|
|
|
status_log => shift, |
|
80
|
|
|
|
|
|
|
}; |
|
81
|
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
bless $self, $class; |
|
83
|
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my ($host) = @_; |
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
$self->_populate($host) if defined $host; |
|
87
|
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return $self; |
|
89
|
|
|
|
|
|
|
} # new |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=pod |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 METHODS |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=over 4 |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item set_attribute |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
print 'Attribute added...' if $host->set_attribute('host_name=testserver'); |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
This method takes one required parameter, a string (attribute=value). Returns |
|
102
|
|
|
|
|
|
|
TRUE if attribute was added successfully otherwise returns undefined. |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub set_attribute { |
|
107
|
0
|
|
|
0
|
1
|
|
my ($self, $attr) = @_; |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
|
if (!defined $attr) { |
|
110
|
0
|
|
|
|
|
|
return undef; |
|
111
|
|
|
|
|
|
|
} # if |
|
112
|
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my @attributes = split(/=/, $attr); |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
chomp($attributes[1]); |
|
116
|
0
|
|
|
|
|
|
$attributes[0] =~ s/^\s*(.+)/$1/; |
|
117
|
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
$self->{attributes}->{$attributes[0]} = $attributes[1]; |
|
119
|
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
return 1; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=pod |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item get_attribute |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $attr = $host->get_attribute($attribute); |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
This method takes one required parameter, an attribute or 'all'. If 'all' |
|
130
|
|
|
|
|
|
|
is specified, a hash reference of attributes(keys) and values is returned. |
|
131
|
|
|
|
|
|
|
If an attribute is specified and is found, the value is returned. Otherwise, |
|
132
|
|
|
|
|
|
|
undefined is returned. |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub get_attribute { |
|
137
|
0
|
|
|
0
|
1
|
|
my ($self, $attr) = @_; |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
if ($attr eq 'all') { |
|
140
|
0
|
|
|
|
|
|
return $self->{attributes}; |
|
141
|
|
|
|
|
|
|
} else { |
|
142
|
0
|
0
|
|
|
|
|
if (exists $self->{attributes}->{$attr}) { |
|
143
|
0
|
|
|
|
|
|
return $self->{attributes}->{$attr}; |
|
144
|
|
|
|
|
|
|
} else { |
|
145
|
0
|
|
|
|
|
|
return undef; |
|
146
|
|
|
|
|
|
|
} # if/else |
|
147
|
|
|
|
|
|
|
} # if/else |
|
148
|
|
|
|
|
|
|
} # get_attributes |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=pod |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item is_down |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
print 'Host down...' if $host->is_down; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This method take no parameters. Returns TRUE if host |
|
157
|
|
|
|
|
|
|
is down. Otherwise, returns FALSE. |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub is_down { |
|
162
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
163
|
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
if ($self->{attributes}->{last_time_up} < $self->{attributes}->{last_time_down}) { |
|
165
|
0
|
|
|
|
|
|
return 1; |
|
166
|
|
|
|
|
|
|
} else { |
|
167
|
0
|
|
|
|
|
|
return 0; |
|
168
|
|
|
|
|
|
|
} # if/else |
|
169
|
|
|
|
|
|
|
} # is_down |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=pod |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item is_unreachable |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
print 'Host unreachable...' if $host->is_unreachable; |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This method take no parameters. Returns TRUE if host |
|
178
|
|
|
|
|
|
|
is unreachable. Otherwise, returns FALSE. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub is_unreachable { |
|
183
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
184
|
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
if ($self->{attributes}->{last_time_up} < $self->{attributes}->{last_time_unreachable}) { |
|
186
|
0
|
|
|
|
|
|
return 1; |
|
187
|
|
|
|
|
|
|
} else { |
|
188
|
0
|
|
|
|
|
|
return 0; |
|
189
|
|
|
|
|
|
|
} # if/else |
|
190
|
|
|
|
|
|
|
} # is_unreachable |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=pod |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item get_hostname |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
my $name = $host->get_hostname; |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
This method takes no parameters. Returns hostname of |
|
199
|
|
|
|
|
|
|
host. |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub get_hostname { |
|
204
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
205
|
|
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
return $self->{attributes}->{host_name}; |
|
207
|
|
|
|
|
|
|
} # get_hostname |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=pod |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item get_downtime |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
my $downtime = $host->get_downtime; |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This method takes no parameters. Returns downtime in seconds |
|
216
|
|
|
|
|
|
|
if host is down. Otherwise, returns 0 seconds; |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub get_downtime { |
|
221
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
222
|
|
|
|
|
|
|
|
|
223
|
0
|
0
|
|
|
|
|
if ($self->is_down) { |
|
224
|
0
|
|
|
|
|
|
my $cur_time = time; |
|
225
|
0
|
|
|
|
|
|
my $time = $cur_time - $self->{attributes}->{last_state_change}; |
|
226
|
0
|
|
|
|
|
|
return $time; |
|
227
|
|
|
|
|
|
|
} else { |
|
228
|
0
|
|
|
|
|
|
return 0; |
|
229
|
|
|
|
|
|
|
} # if/else |
|
230
|
|
|
|
|
|
|
} # get_downtime |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=pod |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item get_unreachable_time |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
my $utime = $host->get_unreachable_time; |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
This method takes no parameters. Returns unreachable time in seconds |
|
239
|
|
|
|
|
|
|
if host is unreachable. Otherwise, returns 0 seconds. |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub get_unreachable_time { |
|
244
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
245
|
|
|
|
|
|
|
|
|
246
|
0
|
0
|
|
|
|
|
if ($self->is_unreachable) { |
|
247
|
0
|
|
|
|
|
|
my $cur_time = time; |
|
248
|
0
|
|
|
|
|
|
my $time = $cur_time - $self->{attributes}->{last_state_change}; |
|
249
|
0
|
|
|
|
|
|
return $time; |
|
250
|
|
|
|
|
|
|
} else { |
|
251
|
0
|
|
|
|
|
|
return 0; |
|
252
|
|
|
|
|
|
|
} # if/else |
|
253
|
|
|
|
|
|
|
} # get_unreachable_time |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=pod |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=back |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub _populate { |
|
262
|
0
|
|
|
0
|
|
|
my ($self, $host) = @_; |
|
263
|
|
|
|
|
|
|
|
|
264
|
0
|
|
|
|
|
|
my %attributes; |
|
265
|
0
|
|
|
|
|
|
my $found = 0; |
|
266
|
0
|
|
|
|
|
|
my $found_host = 0; |
|
267
|
|
|
|
|
|
|
|
|
268
|
0
|
0
|
|
|
|
|
open(STATUS, $self->{status_log}) or croak "Cannot open status log file: $!"; |
|
269
|
|
|
|
|
|
|
|
|
270
|
0
|
|
|
|
|
|
while(my $line = ) { |
|
271
|
0
|
0
|
|
|
|
|
if ($line =~ /^hoststatus\s*{/) { |
|
272
|
0
|
|
|
|
|
|
$found = 1; |
|
273
|
0
|
|
|
|
|
|
next; |
|
274
|
|
|
|
|
|
|
} # if |
|
275
|
|
|
|
|
|
|
|
|
276
|
0
|
0
|
0
|
|
|
|
if ($found and $line =~ /$host/) { |
|
277
|
0
|
|
|
|
|
|
$found_host = 1; |
|
278
|
|
|
|
|
|
|
} |
|
279
|
|
|
|
|
|
|
|
|
280
|
0
|
0
|
0
|
|
|
|
if ($found and $line =~ /}/) { |
|
281
|
0
|
0
|
|
|
|
|
if ($found_host) { |
|
282
|
0
|
|
|
|
|
|
foreach (keys %attributes) { |
|
283
|
0
|
|
|
|
|
|
$self->{attributes}->{$_} = $attributes{$_}; |
|
284
|
|
|
|
|
|
|
} # foreach |
|
285
|
|
|
|
|
|
|
|
|
286
|
0
|
|
|
|
|
|
last; |
|
287
|
|
|
|
|
|
|
} else { |
|
288
|
0
|
|
|
|
|
|
%attributes = (); |
|
289
|
0
|
|
|
|
|
|
$found = 0; |
|
290
|
|
|
|
|
|
|
} # if/else |
|
291
|
|
|
|
|
|
|
} # if |
|
292
|
|
|
|
|
|
|
|
|
293
|
0
|
0
|
|
|
|
|
if (!$found) { |
|
294
|
0
|
|
|
|
|
|
next; |
|
295
|
|
|
|
|
|
|
} else { |
|
296
|
0
|
|
|
|
|
|
my @attr = split(/=/, $line); |
|
297
|
|
|
|
|
|
|
|
|
298
|
0
|
|
|
|
|
|
chomp($attr[1]); |
|
299
|
0
|
|
|
|
|
|
$attr[0] =~ s/^\s*(.+)/$1/; |
|
300
|
0
|
|
|
|
|
|
$attributes{$attr[0]} = $attr[1]; |
|
301
|
|
|
|
|
|
|
} # if/else |
|
302
|
|
|
|
|
|
|
} # while |
|
303
|
|
|
|
|
|
|
|
|
304
|
0
|
|
|
|
|
|
close(STATUS); |
|
305
|
|
|
|
|
|
|
} # _populate |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=head1 AUTHOR |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
Roy Crowder, C<< >> |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
L, |
|
314
|
|
|
|
|
|
|
L |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 BUGS |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
319
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
320
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head1 SUPPORT |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
perldoc Nagios::Status::Host |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
You can also look for information at: |
|
330
|
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=over 4 |
|
332
|
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
334
|
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
L |
|
336
|
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
L |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
L |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
=item * Search CPAN |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
L |
|
348
|
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
=back |
|
350
|
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
352
|
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
Copyright (c) 2009 WorldSpice Technologies, all rights reserved. |
|
354
|
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
356
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
357
|
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=cut |
|
359
|
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
1; # End of Nagios::Status::Host |