File Coverage

blib/lib/Nagios/Downtime.pm
Criterion Covered Total %
statement 9 107 8.4
branch 0 50 0.0
condition 0 15 0.0
subroutine 3 13 23.0
pod 4 5 80.0
total 16 190 8.4


line stmt bran cond sub pod time code
1             package Nagios::Downtime;
2              
3 1     1   23726 use strict;
  1         3  
  1         40  
4 1     1   5 use warnings;
  1         2  
  1         30  
5 1     1   6 use Carp;
  1         6  
  1         1943  
6              
7             our $AUTOLOAD;
8            
9             sub new {
10 0     0 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;
  0            
16            
17 0           return $self;
18             }
19            
20            
21             sub AUTOLOAD {
22 0     0     my $self = shift;
23 0           my $func_name = $AUTOLOAD;
24 0           $func_name =~ s/.*://; #striping full name
25 0 0         $self->{$func_name} = shift if @_;
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             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             sub start_hostgroup_downtime {
105 0     0 1   my $self = shift;
106 0 0         if (my @missing = _missing($self,['nagios_cmd_file'])) { croak "missing object parameters: @missing"; }
  0            
107              
108 0 0 0       my ($hostgroup_name,$dt_long,$author,$comment) = grep { $#_ == 3 && $_ or croak 'usage: start_hostgroup_downtime(hostname,seconds,author,comment)' } @_;
  0            
109              
110 0           my $dt_start = time;
111 0           my $dt_end = $dt_start + $dt_long;
112              
113 0 0         open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or "can't open $self->{'nagios_cmd_file'} for writing: $!";
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             sub start_host_downtime {
135 0     0 1   my $self = shift;
136 0 0         if (my @missing = _missing($self,['nagios_cmd_file'])) { croak "missing object parameters: @missing"; }
  0            
137              
138 0 0 0       my ($host_name,$dt_long,$author,$comment) = grep { $#_ == 3 && $_ or croak 'usage: start_host_downtime(hostname,seconds,author,comment)' } @_;
  0            
139              
140 0           my $dt_start = time;
141 0           my $dt_end = $dt_start + $dt_long;
142              
143 0 0         open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or "can't open $self->{'nagios_cmd_file'} for writing: $!";
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             sub stop_hostgroup_downtime {
165 0     0 1   my $self = shift;
166 0 0         if (my @missing = _missing($self,['nagios_cmd_file','nagios_hostgroups_file','downtime_dat_file'])) { croak "missing object parameters: @missing";}
  0            
167              
168 0 0         my $hostgroup_name = shift or croak 'usage: stop_hostgroup_downtime(hostgroup_name)';
169              
170 0           my $time_now = time;
171 0 0         open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or croak "can't open $self->{'nagios_cmd_file'} for writing: $!";
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'});
  0            
  0            
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<< >>
190              
191             =head1 BUGS
192              
193             Please report any bugs or feature requests to
194             C, or through the web interface at
195             L.
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
212              
213             =item * CPAN Ratings
214              
215             L
216              
217             =item * RT: CPAN's request tracker
218              
219             L
220              
221             =item * Search CPAN
222              
223             L
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             sub stop_host_downtime {
239 0     0 1   my $self = shift;
240 0 0         if (my @missing = _missing($self,['nagios_cmd_file','downtime_dat_file'])) { croak "missing object parameters: @missing"; }
  0            
241              
242 0 0         my $host_name = shift or croak 'usage: stop_host_downtime(host_name)';
243              
244 0           my $time_now = time;
245 0 0         open my $CMD_FH, '>>', $self->{'nagios_cmd_file'} or croak "can't open $self->{'nagios_cmd_file'} for writing: $!";
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'});
  0            
247 0           close $CMD_FH;
248              
249 0           return 1;
250             }
251              
252             sub _missing {
253 0     0     my ($self,$expected) = @_;
254 0           my @missing = ();
255 0 0         map { if (!$self->{$_}) { push @missing, $_; } } @{$expected};
  0            
  0            
  0            
256 0           return @missing;
257             }
258              
259              
260             sub _get_hostgroup_hosts {
261 0 0 0 0     my ($hostgroup_name,$nagios_hostgroups_file) = grep { $#_ == 1 && $_ or croak 'usage: _get_hostgroup_hosts(hostgrouop_name,nagios_hostgroups_file)' } @_;
  0            
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 0         map { if ($_->{'hostgroup_name'} eq $hostgroup_name) { @hosts = split /,/, $_->{'members'}; }; map { s/^\s+|\s$//g } @hosts;} @data;
  0            
  0            
  0            
  0            
269              
270 0           return @hosts;
271             }
272              
273             sub _get_dt_ids {
274 0 0 0 0     my ($host_name,$downtime_dat_file) = grep { $#_ == 1 && $_ or croak 'usage: _get_dt_ids(host_name,nagios_dat_file)' } @_;
  0            
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 0         map { if ($_->{'host_name'} eq $host_name) { push @return_ids, $_->{'downtime_id'};} } @data;
  0            
  0            
282              
283 0           return @return_ids;
284             }
285              
286             sub _parse_file {
287 0 0 0 0     my ($data_ref,$file_to_read) = grep { $#_ == 1 && $_ or croak 'usage: _parse_file(array_ref,file_to_parse)' } @_;
  0            
288              
289 0           my $index = 0;
290              
291 0 0         open my $DT_DAT_FH, '<', $file_to_read or croak "can't open $file_to_read for reading: $!";
292 0           while (<$DT_DAT_FH>) {
293 0 0         next if /^#/; #skip comments
294 0 0         next if /{$/; #skip start of definition
295 0 0         next if /^\s*$/; #skip empty lines
296 0 0         if (/}$/) { $index++; next; } #when definition ends, increment array index
  0            
  0            
297 0 0         $data_ref->[$index]->{$1} = $3 if /^\s*(.+?)(=|\s+)(.+)\s*$/; #insert stuff as a hash ref into an array ref
298             }
299 0           close $DT_DAT_FH;
300              
301 0           return 1;
302              
303             }
304              
305              
306             1; # End of Nagios::Downtime