blib/lib/Nagios/Downtime.pm

filter: Statementstmt 10.9% 12 / 110 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 23.0% 3 / 13 Podpod 80.0% 4 / 5 totaltotal 9.2% 19 / 205 CC 32 SCAR 36.8
CC
32
Cov
7%
_parse_file
110.0
start_hostgroup_downtime
30.0
start_host_downtime
30.0
CRAP 39.8
↑ Summary
1 package Nagios::Downtime;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTOLOAD;
8
9 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
10 0 my $class = shift;
11 0 my %hash = @_;
12 0 my $self = {};
13 0 bless $self, $class;
14  
15 0 map { $self->{$_} = $hash{$_};} keys %hash;
16
17 0 return $self;
18 }
19
20
21 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
22 0 my $self = shift;
23 0 my $func_name = $AUTOLOAD;
24 0 $func_name =~ s/.*://; #striping full name
25 0 ▶ $self->{$func_name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
26  
27 0 return $self->{$func_name};
28 }
29
30 =head1 NAME
31  
32 Nagios::Downtime - control downtime schedualing!
33  
34 =head1 VERSION
35  
36 Version 0.01
37  
38 =cut
39  
40 2 our $VERSION = '0.01';
41  
42 =head1 SYNOPSIS
43  
44 This module lets you schedual and cancel downtime
45 for hosts and hostgroups.
46  
47 usage:
48  
49 use Nagios::Downtime;
50  
51 new object:
52  
53 my $dt = Nagios::Downtime->new(
54 downtime_dat_file => '/usr/nagios_2.6/var/downtime.dat',
55 nagios_cmd_file => '/usr/nagios_2.6/var/rw/nagios.cmd',
56 nagios_hostgroups_file => '/etc/nagios/hostgroups.cfg'
57 );
58  
59 or:
60  
61 my $dt = new Nagios::Downtime;
62  
63 $dt->downtime_dat_file('/usr/nagios_2.6/var/downtime.dat');
64 $dt->nagios_cmd_file('/usr/nagios_2.6/var/rw/nagios.cmd');
65 $dt->nagios_hostgroups_file('/etc/nagios/hostgroups.cfg');
66  
67  
68 to schedual a single host 'web1' for 5 hours downtime with commnet
69 'testing module' by author 'quatrix':
70  
71 $dt->start_host_downtime('web1',60 * 60 * 5,'quatrix','testing module');
72  
73 same, but to schedual downtime for an entire host-group 'web-group':
74  
75 $dt->start_hostgroup_downtime('web-group',60 * 60 * 5, 'quatrix','testing module');
76  
77 to cancel schedualed downtime for host 'web1':
78  
79 $dt->stop_host_downtime('web1');
80  
81 to cancel schedualed downtime for entire host-group 'web-group':
82  
83 $dt->stop_hostgroup_downtime('web-group');
84  
85  
86 =head1 EXPORT
87  
88 nothing is exported, you should use it OO style only.
89  
90 =head1 FUNCTIONS
91  
92 =head2 start_hostgroup_downtime(hostgroup_name,dt_long,author,comment)
93  
94 takes 4 arguments: hostgroup_name, downtime time in seconds, author, comment
95  
96 needs 'nagios_cmd_file' defined
97  
98 i.e:
99  
100 $dt->start_hostgroup_downtime('web-group',600,'quatrix','for kicks');
101  
102 =cut
103  
104 0 ▶sub start_hostgroup_downtime {
Subroutine start_hostgroup_downtime: not called CC=5 SCAR=34.0
Pod documented
105 0 my $self = shift;
106 0 ▶ if (my @missing = _missing($self,['nagios_cmd_file'])) { croak "missing object parameters: @missing"; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = _missing($self, ["nagios_cmd_file"])) 0 0 0
107  
108 0 ▶ my ($hostgroup_name,$dt_long,$author,$comment) = grep { $#_ == 3 && $_ or croak 'usage: start_hostgroup_downtime(hostname,seconds,author,comment)' } @_;
Condition0%0/5
$#_ == 3 && $_ || croak('usage: start_hostgroup_downtime(hostname,seconds,author,comment)')
l!l
00
$#_ == 3 and $_
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 3 && $_ || croak('usage: start_hostgroup_downtime(hostname,seconds,author,comment)')
$#_ == 3 $_
Truth table0%0/3
A and B
A= $#_ == 3B= $_
ABresult
111
0X0
100
109  
110 0 my $dt_start = time;
111 0 my $dt_end = $dt_start + $dt_long;
112  
113 0 ▶ open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or "can't open $self->{'nagios_cmd_file'} for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $CMD_FH, ">>", $self->{"nagios_cmd_file"} or qq[can't open $self->{"nagios_cmd_file"} for writing: $!] 0 0 0
114 0 print $CMD_FH qq{[$dt_start] SCHEDULE_HOSTGROUP_SVC_DOWNTIME;$hostgroup_name;$dt_start;$dt_end;1;0;$dt_long;$author;$comment\n};
115 0 print $CMD_FH qq{[$dt_start] SCHEDULE_HOSTGROUP_HOST_DOWNTIME;$hostgroup_name;$dt_start;$dt_end;1;0;$dt_long;$author;$comment\n};
116 0 close $CMD_FH;
117  
118 0 return 1;
119 }
120  
121  
122 =head2 start_host_downtime(host_name,dt_long,author,comment)
123  
124 same as before, but instade hostgroup_name, it takes host_name
125  
126 also, needs 'nagios_cmd_file' defined
127  
128 i.e:
129  
130 $dt->start_host_downtime('web1',600,'quatrix','no one needs to know');
131  
132 =cut
133  
134 0 ▶sub start_host_downtime {
Subroutine start_host_downtime: not called CC=5 SCAR=34.0
Pod documented
135 0 my $self = shift;
136 0 ▶ if (my @missing = _missing($self,['nagios_cmd_file'])) { croak "missing object parameters: @missing"; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = _missing($self, ["nagios_cmd_file"])) 0 0 0
137  
138 0 ▶ my ($host_name,$dt_long,$author,$comment) = grep { $#_ == 3 && $_ or croak 'usage: start_host_downtime(hostname,seconds,author,comment)' } @_;
Condition0%0/5
$#_ == 3 && $_ || croak('usage: start_host_downtime(hostname,seconds,author,comment)')
l!l
00
$#_ == 3 and $_
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 3 && $_ || croak('usage: start_host_downtime(hostname,seconds,author,comment)')
$#_ == 3 $_
Truth table0%0/3
A and B
A= $#_ == 3B= $_
ABresult
111
0X0
100
139  
140 0 my $dt_start = time;
141 0 my $dt_end = $dt_start + $dt_long;
142  
143 0 ▶ open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or "can't open $self->{'nagios_cmd_file'} for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $CMD_FH, ">>", $self->{"nagios_cmd_file"} or qq[can't open $self->{"nagios_cmd_file"} for writing: $!] 0 0 0
144 0 print $CMD_FH qq{[$dt_start] SCHEDULE_HOST_SVC_DOWNTIME;$host_name;$dt_start;$dt_end;1;0;$dt_long;$author;$comment\n};
145 0 print $CMD_FH qq{[$dt_start] SCHEDULE_HOST_DOWNTIME;$host_name;$dt_start;$dt_end;1;0;$dt_long;$author;$comment\n};
146 0 close $CMD_FH;
147  
148 0 return 1;
149 }
150  
151 =head2 stop_hostgroup_downtime(hostgroup_name)
152 this function cancels schedualed downtime for a hostgroup
153 it takes just one argument, hostgroup_name.
154  
155 needs 'nagios_cmd_file' 'nagios_hostgroups_file' and 'downtime_dat_file' defined
156  
157 i.e:
158  
159 $dt->stop_hostgroup_downtime('web-group');
160  
161 =cut
162  
163  
164 0 ▶sub stop_hostgroup_downtime {
Subroutine stop_hostgroup_downtime: not called CC=4 SCAR=30.0
Pod documented
165 0 my $self = shift;
166 0 ▶ if (my @missing = _missing($self,['nagios_cmd_file','nagios_hostgroups_file','downtime_dat_file'])) { croak "missing object parameters: @missing";}
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = _missing($self, ["nagios_cmd_file", "nagios_hostgroups_file", "downtime_dat_file"])) 0 0 0
167  
168 0 ▶ my $hostgroup_name = shift or croak 'usage: stop_hostgroup_downtime(hostgroup_name)';
Branch: 1 branch
BranchTrueFalseTotal
my $hostgroup_name = shift() or croak("usage: stop_hostgroup_downtime(hostgroup_name)") 0 0 0
169  
170 0 my $time_now = time;
171 0 ▶ open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or croak "can't open $self->{'nagios_cmd_file'} for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $CMD_FH, ">>", $self->{"nagios_cmd_file"} or croak(qq[can't open $self->{"nagios_cmd_file"} for writing: $!]) 0 0 0
172 0 map { map { print $CMD_FH qq{[$time_now] DEL_HOST_DOWNTIME;$_\n[$time_now] DEL_SVC_DOWNTIME;$_\n}; } _get_dt_ids($_,$self->{'downtime_dat_file'}); } _get_hostgroup_hosts($hostgroup_name,$self->{'nagios_hostgroups_file'});
173 0 close $CMD_FH;
174  
175 0 return 1;
176 }
177  
178 =head2 stop_host_downtime(host_name)
179 again, like before, but instade of hostgroup_name, it takes hostname
180  
181 needs 'nagios_cmd_file' and 'downtime_dat_file' defined.
182  
183 i.e:
184  
185 $dt->stop_host_downtime('web1');
186  
187 =head1 AUTHOR
188  
189 quatrix, C<< <evil.legacy AT gmail.com> >>
190  
191 =head1 BUGS
192  
193 Please report any bugs or feature requests to
194 C<bug-nagios-downtime at rt.cpan.org>, or through the web interface at
195 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nagios-Downtime>.
196 I will be notified, and then you'll automatically be notified of progress on
197 your bug as I make changes.
198  
199 =head1 SUPPORT
200  
201 You can find documentation for this module with the perldoc command.
202  
203 perldoc Nagios::Downtime
204  
205 You can also look for information at:
206  
207 =over 4
208  
209 =item * AnnoCPAN: Annotated CPAN documentation
210  
211 L<http://annocpan.org/dist/Nagios-Downtime>
212  
213 =item * CPAN Ratings
214  
215 L<http://cpanratings.perl.org/d/Nagios-Downtime>
216  
217 =item * RT: CPAN's request tracker
218  
219 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Nagios-Downtime>
220  
221 =item * Search CPAN
222  
223 L<http://search.cpan.org/dist/Nagios-Downtime>
224  
225 =back
226  
227 =head1 ACKNOWLEDGEMENTS
228  
229 =head1 COPYRIGHT & LICENSE
230  
231 Copyright 2007 quatrix, all rights reserved.
232  
233 This program is free software; you can redistribute it and/or modify it
234 under the same terms as Perl itself.
235  
236 =cut
237  
238 0 ▶sub stop_host_downtime {
Subroutine stop_host_downtime: not called CC=4 SCAR=30.0
Pod documented
239 0 my $self = shift;
240 0 ▶ if (my @missing = _missing($self,['nagios_cmd_file','downtime_dat_file'])) { croak "missing object parameters: @missing"; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = _missing($self, ["nagios_cmd_file", "downtime_dat_file"])) 0 0 0
241  
242 0 ▶ my $host_name = shift or croak 'usage: stop_host_downtime(host_name)';
Branch: 1 branch
BranchTrueFalseTotal
my $host_name = shift() or croak("usage: stop_host_downtime(host_name)") 0 0 0
243  
244 0 my $time_now = time;
245 0 ▶ open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or croak "can't open $self->{'nagios_cmd_file'} for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $CMD_FH, ">>", $self->{"nagios_cmd_file"} or croak(qq[can't open $self->{"nagios_cmd_file"} for writing: $!]) 0 0 0
246 0 map { print $CMD_FH qq{[$time_now] DEL_HOST_DOWNTIME;$_\n[$time_now] DEL_SVC_DOWNTIME;$_\n}; } _get_dt_ids($host_name,$self->{'downtime_dat_file'});
247 0 close $CMD_FH;
248  
249 0 return 1;
250 }
251  
252 0 ▶sub _missing {
Subroutine _missing: not called CC=2 SCAR=17.9
253 0 my ($self,$expected) = @_;
254 0 my @missing = ();
255 0 ▶ map { if (!$self->{$_}) { push @missing, $_; } } @{$expected};
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{$_}) 0 0 0
256 0 return @missing;
257 }
258  
259  
260 0 ▶sub _get_hostgroup_hosts {
Subroutine _get_hostgroup_hosts: not called CC=4 SCAR=30.0
261 0 ▶ my ($hostgroup_name,$nagios_hostgroups_file) = grep { $#_ == 1 && $_ or croak 'usage: _get_hostgroup_hosts(hostgrouop_name,nagios_hostgroups_file)' } @_;
Condition0%0/5
$#_ == 1 && $_ || croak('usage: _get_hostgroup_hosts(hostgrouop_name,nagios_hostgroups_file)')
l!l
00
$#_ == 1 and $_
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 1 && $_ || croak('usage: _get_hostgroup_hosts(hostgrouop_name,nagios_hostgroups_file)')
$#_ == 1 $_
Truth table0%0/3
A and B
A= $#_ == 1B= $_
ABresult
111
0X0
100
262  
263 0 my @data = ();
264 0 my @hosts = ();
265 0 _parse_file(\@data, $nagios_hostgroups_file);
266  
267 #this finds the currect hostgroup, puts it's members into @host and removes trailing and leading whitespace
268 0 ▶ map { if ($_->{'hostgroup_name'} eq $hostgroup_name) { @hosts = split /,/, $_->{'members'}; }; map { s/^\s+|\s$//g } @hosts;} @data;
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"hostgroup_name"} eq $hostgroup_name) 0 0 0
269  
270 0 return @hosts;
271 }
272  
273 0 ▶sub _get_dt_ids {
Subroutine _get_dt_ids: not called CC=4 SCAR=30.0
274 0 ▶ my ($host_name,$downtime_dat_file) = grep { $#_ == 1 && $_ or croak 'usage: _get_dt_ids(host_name,nagios_dat_file)' } @_;
Condition0%0/5
$#_ == 1 && $_ || croak('usage: _get_dt_ids(host_name,nagios_dat_file)')
l!l
00
$#_ == 1 and $_
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 1 && $_ || croak('usage: _get_dt_ids(host_name,nagios_dat_file)')
$#_ == 1 $_
Truth table0%0/3
A and B
A= $#_ == 1B= $_
ABresult
111
0X0
100
275  
276 0 my @data = ();
277 0 my @return_ids = ();
278 0 _parse_file(\@data,$downtime_dat_file);
279  
280 #this gets the downtime ids for a specific host_name
281 0 ▶ map { if ($_->{'host_name'} eq $host_name) { push @return_ids, $_->{'downtime_id'};} } @data;
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"host_name"} eq $host_name) 0 0 0
282  
283 0 return @return_ids;
284 }
285  
286 0 ▶sub _parse_file {
Subroutine _parse_file: not called CC=10 SCAR=47.0
287 0 ▶ my ($data_ref,$file_to_read) = grep { $#_ == 1 && $_ or croak 'usage: _parse_file(array_ref,file_to_parse)' } @_;
Condition0%0/5
$#_ == 1 && $_ || croak('usage: _parse_file(array_ref,file_to_parse)')
l!l
00
$#_ == 1 and $_
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 1 && $_ || croak('usage: _parse_file(array_ref,file_to_parse)')
$#_ == 1 $_
Truth table0%0/3
A and B
A= $#_ == 1B= $_
ABresult
111
0X0
100
288  
289 0 my $index = 0;
290  
291 0 ▶ open my $DT_DAT_FH, '<', $file_to_read or croak "can't open $file_to_read for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $DT_DAT_FH, "<", $file_to_read or croak("can't open $file_to_read for reading: $!") 0 0 0
292 0 ▶ while (<$DT_DAT_FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $DT_DAT_FH)) 0 0 0
293 0 ▶ next if /^#/; #skip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 0 0
294 0 ▶ next if /{$/; #skip start of definition
Branch: 1 branch
BranchTrueFalseTotal
if /{$/ 0 0 0
295 0 ▶ next if /^\s*$/; #skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
296 0 ▶ if (/}$/) { $index++; next; } #when definition ends, increment array index
Branch: 1 branch
BranchTrueFalseTotal
if (/}$/) 0 0 0
297 0 ▶ $data_ref->[$index]->{$1} = $3 if /^\s*(.+?)(=|\s+)(.+)\s*$/; #insert stuff as a hash ref into an array ref
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(.+?)(=|\s+)(.+)\s*$/ 0 0 0
298 }
299 0 close $DT_DAT_FH;
300  
301 0 return 1;
302  
303 }
304  
305  
306 2 1; # End of Nagios::Downtime
↑ Summary