File Coverage

lib/Nagios/Config.pm
Criterion Covered Total %
statement 54 82 65.8
branch 14 32 43.7
condition 5 18 27.7
subroutine 9 12 75.0
pod 1 3 33.3
total 83 147 56.4


line stmt bran cond sub pod time code
1             ###########################################################################
2             # #
3             # Nagios::Config #
4             # Maintained by Duncan Ferguson #
5             # Written by Albert Tobey #
6             # Copyright 2003-2009, Albert P Tobey #
7             # Copyright 2009, Albert P Tobey and Duncan Ferguson #
8             # #
9             # This program is free software; you can redistribute it and/or modify it #
10             # under the terms of the GNU General Public License as published by the #
11             # Free Software Foundation; either version 2, or (at your option) any #
12             # later version. #
13             # #
14             # This program is distributed in the hope that it will be useful, but #
15             # WITHOUT ANY WARRANTY; without even the implied warranty of #
16             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
17             # General Public License for more details. #
18             # #
19             ###########################################################################
20             package Nagios::Config;
21 6     6   119252 use warnings;
  6         13  
  6         271  
22 6     6   35 use strict qw( subs vars );
  6         13  
  6         210  
23 6     6   33 use Carp;
  6         18  
  6         431  
24 6     6   3031 use Nagios::Object::Config;
  6         19  
  6         245  
25 6     6   3081 use Nagios::Config::File;
  6         17  
  6         251  
26 6     6   45 use Nagios::Object qw(%nagios_setup);
  6         13  
  6         818  
27 6     6   33 use Symbol qw(gensym);
  6         12  
  6         261  
28 6     6   31 use File::Basename;
  6         11  
  6         4691  
29             @Nagios::Config::ISA = qw( Nagios::Object::Config Nagios::Config::File );
30              
31             # NOTE: due to CPAN version checks this cannot currently be changed to a
32             # standard version string, i.e. '0.21'
33             our $VERSION = '36';
34             our $fast_mode = undef;
35              
36             =head1 NAME
37              
38             Nagios::Config - Parser for the Nagios::Object set of perl modules
39              
40             =head1 DESCRIPTION
41              
42             Ties all of the Nagios::Object modules together, doing all the parsing and
43             background stuff so you don't have to.
44              
45             All of the methods of Nagios::Object::Config and Nagios::Config::File are
46             inherited by this module.
47              
48             =head1 SYNOPSIS
49              
50             my $nagios_cfg = Nagios::Config->new( "nagios.cfg" );
51              
52             my @host_objects = $nagios_cfg->list_hosts();
53              
54             =head1 METHODS
55              
56             =over 4
57              
58             =item new()
59              
60             Create a new Nagios::Config object, which will parse a Nagios main
61             configuration file and all of it's object configuration files.
62             The resource configuration file is not parsed - for that, use Nagios::Config::File.
63              
64             my $cf = Nagios::Config->new( Filename => $configfile );
65             my $cf = Nagios::Config->new( Filename => $configfile, Version => 1 );
66             my $cf = Nagios::Config->new( Filename => $configfile, Version => 2 );
67              
68             =cut
69              
70             sub new {
71 4 50   4 1 2403 my $class = ref( $_[0] ) ? ref(shift) : shift;
72 4         8 my $filename = undef;
73 4         7 my $version = undef;
74 4         6 my $allow_missing_files = undef;
75 4         8 my $force_relative_files = undef;
76              
77 4 50       17 if ( @_ % 2 == 0 ) {
78 4         10 my %args = ();
79 4   66     33 for ( my $i = 0; $i <= @_ && defined $_[$i]; $i += 2 ) {
80 7         44 $args{ lc $_[$i] } = $_[ $i + 1 ];
81             }
82 4 50       14 if ( $args{filename} ) {
83 4         9 $filename = $args{filename};
84             }
85 4 100       11 if ( $args{version} ) {
86 2         5 $version = $args{version};
87             }
88 4 100       17 if ( $args{allow_missing_files} ) {
89 1         2 $allow_missing_files = 1;
90             }
91 4 50       16 if ( $args{force_relative_files} ) {
92 0         0 $force_relative_files = 1;
93             }
94             }
95             else {
96 0         0 croak "single argument form of new() no longer supported\n",
97             "try Nagios::Config->new( Filename => \$file );";
98             }
99              
100 4         19 my $main_cfg = Nagios::Config::File->new($filename);
101 4         48 my $obj_cfgs = Nagios::Object::Config->new( Version => $version );
102              
103             # parse all object configuration files
104 4 50       19 if ( my $files = $main_cfg->get('cfg_file') ) {
105 4         9 foreach my $file (@$files) {
106 29 50       70 if ($force_relative_files) {
107 0         0 $file = _modpath( $filename, $file );
108             }
109 29 100 100     140 next if ( $allow_missing_files && !-e $file );
110 28         86 $obj_cfgs->parse($file);
111             }
112             }
113              
114             # parse all files in cfg_dir(s)
115 4 50       31 if ( my $dir = $main_cfg->get('cfg_dir') ) {
116 0         0 my @dir_files = ();
117 0         0 foreach my $cfgdir (@$dir) {
118 0         0 recurse_dir( \@dir_files, $cfgdir );
119             }
120 0         0 foreach my $file (@dir_files) {
121 0 0       0 if ($force_relative_files) {
122 0         0 $file = _modpath( $filename, $file );
123             }
124 0 0 0     0 next if ( $allow_missing_files && !-e $file );
125 0         0 $obj_cfgs->parse($file);
126             }
127             }
128              
129             # set up the important parts of the Nagios::Config::File instance
130 4         15 $obj_cfgs->{filename} = $filename;
131 4         13 $obj_cfgs->{file_attributes} = $main_cfg->{file_attributes};
132              
133             # resolve and register Nagios::Object tree
134 4 50       12 if ( !$fast_mode ) {
135 4         21 $obj_cfgs->resolve_objects();
136 4         18 $obj_cfgs->register_objects();
137             }
138             else {
139 0         0 warn "EXPERIMENTAL: possible breakage with fast_mode enabled";
140             }
141              
142 4         116 return bless $obj_cfgs, $class;
143             }
144              
145             sub recurse_dir {
146 0     0 0   my ( $file_list, $dir ) = @_;
147 0           my $fh = gensym;
148 0           opendir( $fh, $dir );
149 0           while ( my $file = readdir $fh ) {
150 0 0 0       if ( !-d "$dir/$file" && $file =~ /\.cfg$/ ) {
    0 0        
      0        
151 0           push( @$file_list, "$dir/$file" );
152             }
153             elsif ( -d "$dir/$file" && $file !~ /^\./ && $file ne 'CVS' ) {
154 0           recurse_dir( $file_list, "$dir/$file" );
155             }
156             }
157             }
158              
159             sub _modpath {
160 0     0     my ( $main_cfg, $sub_cfg ) = @_;
161 0           my $cfgfile = File::Basename::basename($sub_cfg);
162 0           my $subdir = File::Basename::dirname($main_cfg);
163 0           return $subdir . '/' . $cfgfile;
164             }
165              
166             sub fast_mode {
167 0 0   0 0   if ( $_[1] ) { $fast_mode = $_[1] }
  0            
168 0           $Nagios::Object::fast_mode = $fast_mode;
169 0           $Nagios::Object::Config::fast_mode = $fast_mode;
170 0           return $fast_mode;
171             }
172              
173             1;
174              
175             __END__