Simple/Config.pm

filter: Statementstmt 100% 107 / 107 Branchbran 79.6% 43 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 5 totaltotal 91.0% 163 / 179 CC 33 SCAR 21.6
CC
33
Cov
93%
validate
16.0
resolve_config_file
8.1
get_config
6.1
CRAP 8.7
« Simple/Output.pm ↑ Summary Simple/Log.pm »
1 #-----------------------------------------------------------------
2 # Monitor::Simple::Config
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # ABSTRACT: See documentation in Monitor::Simple
7 # PODNAME: Monitor::Simple::Config
8 #-----------------------------------------------------------------
9  
10 package Monitor::Simple::Config;
11 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use Monitor::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 40 our $VERSION = '0.2.8'; # VERSION
21  
22 40 our $DEFAULT_CONFIG_FILE = 'monitor-simple-cfg.xml';
23 40 our $ENV_CONFIG_DIR = 'MONITOR_SIMPLE_CFG_DIR';
24  
25 #-----------------------------------------------------------------
26 # Try to locate given $filename and return its full path:
27 # a) as it is - if such file exists
28 # b) as $ENV{MONITOR_SIMPLE_CFG_DIR}/$filename
29 # c) in the directory where the main invoker (script) is located
30 # d) in one of the @INC directories
31 # e) return undef
32 #-----------------------------------------------------------------
33 101 ▶sub resolve_config_file {
Subroutine resolve_config_file: called CC=8 SCAR=20.9
Pod undocumented
34 101 my $self = shift; # invocant
35 101 my $filename = shift;
36 101 ▶ $filename = $DEFAULT_CONFIG_FILE unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 101 101
37 101 ▶ return $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 98 3 101
38  
39 3 my $realfilename;
40 3 ▶ if ($ENV{$ENV_CONFIG_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$Monitor::Simple::Config::ENV_CONFIG_DIR}) 1 2 3
41 1 $realfilename = File::Spec->catdir ($ENV{$ENV_CONFIG_DIR}, $filename);
42 1 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 1 0 1
43 }
44  
45 2 my $dirname = dirname ($0);
46 2 $realfilename = File::Spec->catdir ($dirname, $filename);
47 2 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 0 2 2
48  
49 2 foreach my $prefix (@INC) {
50 17 $realfilename = File::Spec->catfile ($prefix, $filename);
51 17 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 1 16 17
52 }
53 1 return;
54 }
55  
56 #-----------------------------------------------------------------
57 # Return a hashref with the full configuration.
58 #
59 # The configuration is looked for in the given configuration file
60 # ($config_file), or in a default configuration file. The path to both
61 # given and default configuration file is resolved by rules defined in
62 # the subroutine resolve_config_file (elsewhere in this module).
63 # -----------------------------------------------------------------
64 97 ▶sub get_config {
Subroutine get_config: called CC=6 SCAR=18.1
Pod undocumented
65 97 my ($self, $config_file) = @_;
66 97 my $resolved_config_file = $self->resolve_config_file ($config_file); # may be undef
67 97 ▶ LOGDIE ("Cannot find '" . (defined $config_file ? $config_file : 'any') . "' configuration file.\n")
Branch: 2 branches
BranchTrueFalseTotal
unless $resolved_config_file 0 97 97
defined $config_file ? : 0 0 0
68 unless $resolved_config_file;
69 97 my $xs = XML::Simple->new (
70 ForceArray => [ 'service', 'email', 'email-group', 'notifier', 'contains',
71 'prg-test', 'post-test', 'head-test', 'get-test', 'arg' ],
72 GroupTags => { services => 'service', args => 'arg' },
73 KeyAttr => [],
74 SuppressEmpty => undef,
75 );
76 97 my $config;
77 97 eval { $config = $xs->XMLin ($resolved_config_file) };
78 97 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 96 97
79 1 my $msg = $@;
80 1 $msg =~ s{^\s*|\s*$}{};
81 1 chomp ($msg);
82 1 $msg =~ s{ at /.+$}{};
83 1 LOGDIE ("Errors in configuration file '$resolved_config_file': $msg\n");
84 }
85 96 ▶ $config = {} unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 96 96
86  
87 # check the validity of the config file
88 96 my $doc = $self->validate ($config);
89 96 ▶ LOGDIE ("Errors in configuration file '$resolved_config_file': $doc") if $doc;
Branch: 1 branch
BranchTrueFalseTotal
if $doc 1 95 96
90  
91 95 return $config;
92 }
93  
94 #-----------------------------------------------------------------
95 #
96 #-----------------------------------------------------------------
97 96 ▶sub validate {
Subroutine validate: called CC=16 SCAR=27.7
Pod undocumented
98 96 my ($self, $config) = @_;
99 96 my $doc = '';
100  
101 # add default values for missing configuration properties
102 96 my $location = $INC{'Monitor/Simple.pm'}; # Monitor/Simple.pm => /usr/lib/perl/5.10/Monitor/Simple.pm
103 96 $location =~ s{\.pm$}{};
104 $config->{general}->{'plugins-dir'} = File::Spec->catfile ($location, 'plugins')
105 96 ▶ unless $config->{general}->{'plugins-dir'};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"general"}{"plugins-dir"} 96 0 96
106 $config->{general}->{'notifiers-dir'} = File::Spec->catfile ($location, 'notifiers')
107 96 ▶ unless $config->{general}->{'notifiers-dir'};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"general"}{"notifiers-dir"} 96 0 96
108  
109 96 ▶ if (exists $config->{services}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"services"}) 96 0 96
110 96 my $count = 0;
111 96 foreach my $service (@{ $config->{services} }) {
112 564 $count++;
113 # each service must have an ID
114 564 ▶ if ($service->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->{'id'}) { } 563 1 564
115 # copy service ID into NAME if name is missing
116 563 ▶ $service->{name} = $service->{id} unless defined $service->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $service->{"name"} 557 6 563
117 } else {
118 1 $doc .= "Service number $count does not have an ID attribute.\n";
119 1 $service->{name} = 'unidentifed';
120 }
121  
122 564 ▶ if (exists $service->{plugin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $service->{'plugin'}) { } 563 1 564
123 # there can be only one plugin per service
124 563 ▶ if (ref ($service->{plugin}) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $service->{'plugin'} ne 'HASH') { } 1 562 563
125 1 $doc .= "Service '$service->{name}' has more than one plugin tag.\n";
126 } else {
127 # each plugin must have a command
128 $doc .= "Service '$service->{name}' has a plugin without any 'command' attribute.\n"
129 562 ▶ unless $service->{plugin}->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless $service->{"plugin"}{"command"} 1 561 562
130 }
131 } else {
132 # each service must have a plugin
133 1 $doc .= "Service number $count does not have any plugin section.\n";
134 }
135  
136 # each notifier must have a command
137 564 ▶ if (exists $service->{notifier}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $service->{"notifier"}) 4 560 564
138 4 my $ncount = 0;
139 4 foreach my $notifier (@{ $service->{notifier} }) {
140 4 $ncount++;
141 $doc .= "Notifier number $ncount in service '$service->{name}' has no 'command' attribute.\n"
142 4 ▶ unless $notifier->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless $notifier->{"command"} 1 3 4
143 }
144 }
145 }
146 }
147  
148 # each general notifier must have a command
149 96 ▶ if (exists $config->{general}->{notifier}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"general"}{"notifier"}) 4 92 96
150 4 my $ncount = 0;
151 4 foreach my $notifier (@{ $config->{general}->{notifier} }) {
152 4 $ncount++;
153 $doc .= "General notifier number $ncount has no 'command' attribute.\n"
154 4 ▶ unless $notifier->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless $notifier->{"command"} 1 3 4
155 }
156 }
157  
158 96 return $doc;
159 }
160  
161 #-----------------------------------------------------------------
162 # Return a hashref with configuration for a given service (identified
163 # by its $service_id). If such configuration cannot be found, a warning is
164 # issued and undef is returned.
165 #
166 # The service configuration is looked for in the given hashref $config
167 # containing the full configuration.
168 # -----------------------------------------------------------------
169 38 ▶sub extract_service_config {
Subroutine extract_service_config: called CC=4 SCAR=13.9
Pod undocumented
170 38 my ($self, $service_id, $config) = @_;
171 38 ▶ if (exists $config->{services}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"services"}) 38 0 38
172 38 foreach my $service (@{ $config->{services} }) {
173 113 ▶ return $service if $service->{id} eq $service_id;
Branch: 1 branch
BranchTrueFalseTotal
if $service->{"id"} eq $service_id 36 77 113
174 }
175 }
176 # return an undef
177 2 LOGWARN ("Service name '$service_id' was not found in the current configuration.");
178 2 return;
179 }
180  
181 #-----------------------------------------------------------------
182 # Extract proper command-line arguments from the $full_config for the
183 # $service_id and return them as an array (which may be empty but
184 # never undef).
185 #
186 # Returned arguments are taken either from:
187 # {
188 # 'plugin' => {
189 # 'args' => [
190 # ...,
191 # ...,
192 # ],
193 # },
194 # }
195 #
196 # or (if no 'args' exists) they will be as described in
197 # Monitor::Simple::Utils->parse_plugin_args().
198 # -----------------------------------------------------------------
199 17 ▶sub create_plugin_args {
Subroutine create_plugin_args: called CC=3 SCAR=11.0
Pod undocumented
200 17 my ($self, $config_file, $full_config, $service_id) = @_;
201 17 my $service_config = $self->extract_service_config ($service_id, $full_config);
202 17 ▶ return () unless $service_config; # service not found
Branch: 1 branch
BranchTrueFalseTotal
unless $service_config 1 16 17
203  
204 16 ▶ if (exists $service_config->{plugin}->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $service_config->{"plugin"}{"args"}) 13 3 16
205 # args exists: use them
206 13 return @{ $service_config->{plugin}->{args} };
207 }
208 # last resort (but used for the most of our plugins)
209 3 return ('-cfg', $config_file,
210 '-service', $service_id,
211 Monitor::Simple::Log->logging_args());
212 }
213  
214 40 1;
215  
216  
217 =pod
218  
219 =head1 NAME
220  
221 Monitor::Simple::Config - See documentation in Monitor::Simple
222  
223 =head1 VERSION
224  
225 version 0.2.8
226  
227 =head1 AUTHOR
228  
229 Martin Senger <martin.senger@gmail.com>
230  
231 =head1 COPYRIGHT AND LICENSE
232  
233 This software is copyright (c) 2013 by Martin Senger, CBRC-KAUST (Computational Biology Research Center - King Abdullah University of Science and Technology) All Rights Reserved.
234  
235 This is free software; you can redistribute it and/or modify it under
236 the same terms as the Perl 5 programming language system itself.
237  
238 =cut
239  
240  
241 __END__
« Simple/Output.pm ↑ Summary Simple/Log.pm »