Plugin/Config.pm

filter: Statementstmt 85.4% 47 / 55 Branchbran 54.1% 13 / 24 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 7 / 8 Podpod 75.0% 3 / 4 totaltotal 75.5% 71 / 94 CC 18 SCAR 25.0
CC
18
Cov
75%
read
21.1
read_string
8.3
write
2.0
CRAP 12.2
« Plugin/Functions.pm ↑ Summary Plugin/Range.pm »
1 package Nagios::Monitoring::Plugin::Config;
2
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use base qw(Config::Tiny);
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 13 my $FILENAME1 = 'plugins.ini';
9 13 my $FILENAME2 = 'nagios-plugins.ini';
10 13 my $CURRENT_FILE = undef;
11
12 # Config paths ending in nagios (search for $FILENAME1)
13 13 my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios);
14 # Config paths not ending in nagios (search for $FILENAME2)
15 13 my @CONFIG_PATH = qw(/etc /usr/local/etc /etc/opt);
16
17 # Override Config::Tiny::read to default the filename, if not given
18 sub read
19 13 ▶{
Subroutine read: called CC=11 SCAR=30.5
Pod documented
20 13 my $class = shift;
21
22 13 ▶ unless ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]) 13 0 13
23 SEARCH: {
24 13 ▶ if ($ENV{NAGIOS_CONFIG_PATH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"NAGIOS_CONFIG_PATH"}) 13 0 13
25 13 for (split /:/, $ENV{NAGIOS_CONFIG_PATH}) {
26 26 my $file = File::Spec->catfile($_, $FILENAME1);
27 26 ▶ unshift(@_, $file), last SEARCH if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 13 13 26
28 13 $file = File::Spec->catfile($_, $FILENAME2);
29 13 ▶ unshift(@_, $file), last SEARCH if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 0 13 13
30 }
31 }
32 0 for (@NAGIOS_CONFIG_PATH) {
33 0 my $file = File::Spec->catfile($_, $FILENAME1);
34 0 ▶ unshift(@_, $file), last SEARCH if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 0 0 0
35 }
36 0 for (@CONFIG_PATH) {
37 0 my $file = File::Spec->catfile($_, $FILENAME2);
38 0 ▶ unshift(@_, $file), last SEARCH if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 0 0 0
39 }
40 }
41
42 # Use die instead of croak, so we can pass a clean message downstream
43 13 ▶ die "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location.\n" unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 13 13
44 }
45
46 13 $CURRENT_FILE = $_[0];
47 13 $class->SUPER::read( @_ );
48 }
49
50 # Straight from Config::Tiny - only changes are repeated property key support
51 # Would be nice if we could just override the per-line handling ...
52 sub read_string
53 13 ▶{
Subroutine read_string: called CC=8 SCAR=21.2
Pod documented
54 13 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 13 13
55 13 my $self = bless {}, $class;
56 13 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 13 13
57
58 # Parse the file
59 13 my $ns = '_';
60 13 my $counter = 0;
61 13 foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
62 455 $counter++;
63
64 # Skip comments and empty lines
65 455 ▶ next if /^\s*(?:\#|\;|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(?:\#|\;|$)/ 104 351 455
66
67 # Handle section headers
68 351 ▶ if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\[\s*(.+?)\s*\]\s*$/) 104 247 351
69 # Create the sub-hash if it doesn't exist.
70 # Without this sections without keys will not
71 # appear at all in the completed struct.
72 104 ▶ $self->{$ns = $1} ||= {};
Condition50%1/2
$$self{$ns = $1} ||= {}
l!l
0104
MC/DC0%0/1
$$self{$ns = $1} ||= {}
$$self{$ns = $1}
Truth table50%1/2
A
A= $$self{$ns = $1}
Aresult
11
00
73 104 next;
74 }
75
76 # Handle properties
77 247 ▶ if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([^=]+?)\s*=\s*(.*?)\s*$/) 247 0 247
78 247 push @{$self->{$ns}->{$1}}, $2;
79 247 next;
80 }
81
82 0 return $self->_error( "Syntax error at line $counter: '$_'" );
83 }
84
85 13 $self;
86 }
87
88 0 ▶sub write { croak "Write access not permitted" }
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
89
90 # Return last file used by read();
91 13 ▶sub np_getfile { return $CURRENT_FILE; }
Subroutine np_getfile: called CC=1 SCAR=0.0
Pod undocumented
92
93 13 1;
94
95 =head1 NAME
96
97 Nagios::Monitoring::Plugin::Config - read nagios plugin .ini style config files
98
99 =head1 SYNOPSIS
100
101 # Read given nagios plugin config file
102 $Config = Nagios::Monitoring::Plugin::Config->read( '/etc/Nagios/Monitoring/Plugins.ini' );
103
104 # Search for and read default nagios plugin config file
105 $Config = Nagios::Monitoring::Plugin::Config->read();
106
107 # Access sections and properties (returns scalars or arrayrefs)
108 $rootproperty = $Config->{_}->{rootproperty};
109 $one = $Config->{section}->{one};
110 $Foo = $Config->{section}->{Foo};
111
112 =head1 DESCRIPTION
113
114 Nagios::Monitoring::Plugin::Config is a subclass of the excellent Config::Tiny,
115 with the following changes:
116
117 =over 4
118
119 =item
120
121 Repeated keys are allowed within sections, returning lists instead of scalars
122
123 =item
124
125 Write functionality has been removed i.e. access is read only
126
127 =item
128
129 Nagios::Monitoring::Plugin::Config searches for a default nagios plugins file if no explicit
130 filename is given to C<read()>. The current standard locations checked are:
131
132 =over 4
133
134 =item /etc/Nagios/Monitoring/Plugins.ini
135
136 =item /usr/local/nagios/etc/plugins.ini
137
138 =item /usr/local/etc/nagios /etc/opt/Nagios/Monitoring/Plugins.ini
139
140 =item /etc/nagios-plugins.ini
141
142 =item /usr/local/etc/nagios-plugins.ini
143
144 =item /etc/opt/nagios-plugins.ini
145
146 =back
147
148 To use a custom location, set a C<NAGIOS_CONFIG_PATH> environment variable
149 to the set of directories that should be checked. The first C<plugins.ini> or
150 C<nagios-plugins.ini> file found will be used.
151
152 =back
153
154
155 =head1 SEE ALSO
156
157 L<Config::Tiny>, L<Nagios::Monitoring::Plugin>
158
159
160 =head1 AUTHORS
161
162 This code is maintained by the Nagios Plugin Development Team:
163 L<https://nagios-plugins.org/>.
164
165
166 =head1 COPYRIGHT and LICENCE
167
168 Copyright (C) 2006-2015 by Nagios Plugin Development Team
169
170 This library is free software; you can redistribute it and/or modify
171 it under the same terms as Perl itself.
172
173 =cut
174
« Plugin/Functions.pm ↑ Summary Plugin/Range.pm »