| 1 |
package Collectd::Graph::Config; |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Collectd::Graph::Config - Parse the collection3 config file. |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
# Copyright (C) 2008,2009 Florian octo Forster <octo at verplant.org> |
| 10 |
# |
| 11 |
# This program is free software; you can redistribute it and/or modify it under |
| 12 |
# the terms of the GNU General Public License as published by the Free Software |
| 13 |
# Foundation; only version 2 of the License is applicable. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 18 |
# details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License along with |
| 21 |
# this program; if not, write to the Free Software Foundation, Inc., |
| 22 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
|
| 27 |
use Carp (qw(cluck confess)); |
| 28 |
use Exporter (); |
| 29 |
use Config::General ('ParseConfig'); |
| 30 |
|
| 31 |
@Collectd::Graph::Config::ISA = ('Exporter'); |
| 32 |
@Collectd::Graph::Config::EXPORT_OK = (qw(gc_read_config gc_get_config |
| 33 |
gc_get_scalar)); |
| 34 |
|
| 35 |
our $Configuration = undef; |
| 36 |
|
| 37 |
return (1); |
| 38 |
|
| 39 |
=head1 EXPORTED FUNCTIONS |
| 40 |
|
| 41 |
=over 4 |
| 42 |
|
| 43 |
=item B<gc_read_config> (I<$file>) |
| 44 |
|
| 45 |
Reads the configuration from the file located at I<$file>. Returns B<true> when |
| 46 |
successful and B<false> otherwise. |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
sub gc_read_config |
| 51 |
{ |
| 52 |
my $file = shift; |
| 53 |
my %conf; |
| 54 |
|
| 55 |
if ($Configuration) |
| 56 |
{ |
| 57 |
return (1); |
| 58 |
} |
| 59 |
|
| 60 |
$file ||= "etc/collection.conf"; |
| 61 |
|
| 62 |
%conf = ParseConfig (-ConfigFile => $file, |
| 63 |
-LowerCaseNames => 1, |
| 64 |
-UseApacheInclude => 1, |
| 65 |
-IncludeDirectories => 1, |
| 66 |
($Config::General::VERSION >= 2.38) ? (-IncludeAgain => 0) : (), |
| 67 |
-MergeDuplicateBlocks => 1, |
| 68 |
-CComments => 0); |
| 69 |
if (!%conf) |
| 70 |
{ |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$Configuration = \%conf; |
| 75 |
return (1); |
| 76 |
} # gc_read_config |
| 77 |
|
| 78 |
=item B<gc_get_config> () |
| 79 |
|
| 80 |
Returns the hash as provided by L<Config::General>. The hash is returned as a |
| 81 |
hash reference. Don't change it! |
| 82 |
|
| 83 |
=cut |
| 84 |
|
| 85 |
sub gc_get_config |
| 86 |
{ |
| 87 |
return ($Configuration); |
| 88 |
} # gc_get_config |
| 89 |
|
| 90 |
=item B<gc_get_config> (I<$key>, [I<$default>]) |
| 91 |
|
| 92 |
Returns the scalar value I<$key> from the config file. If the key does not |
| 93 |
exist, I<$default> will be returned. If no default is given, B<undef> will be |
| 94 |
used in this case. |
| 95 |
|
| 96 |
=cut |
| 97 |
|
| 98 |
sub gc_get_scalar |
| 99 |
{ |
| 100 |
my $key = shift; |
| 101 |
my $default = (@_ != 0) ? shift : undef; |
| 102 |
my $value; |
| 103 |
|
| 104 |
if (!$Configuration) |
| 105 |
{ |
| 106 |
return ($default); |
| 107 |
} |
| 108 |
|
| 109 |
$value = $Configuration->{lc ($key)}; |
| 110 |
if (!defined ($value)) |
| 111 |
{ |
| 112 |
return ($default); |
| 113 |
} |
| 114 |
|
| 115 |
if (ref ($value) ne '') |
| 116 |
{ |
| 117 |
cluck ("Value for `$key' should be scalar, but actually is " |
| 118 |
. ref ($value)); |
| 119 |
return ($default); |
| 120 |
} |
| 121 |
|
| 122 |
return ($value); |
| 123 |
} # gc_get_config |
| 124 |
|
| 125 |
=back |
| 126 |
|
| 127 |
=head1 DEPENDS ON |
| 128 |
|
| 129 |
L<Config::General> |
| 130 |
|
| 131 |
=head1 SEE ALSO |
| 132 |
|
| 133 |
L<Collectd::Graph::Type> |
| 134 |
|
| 135 |
=head1 AUTHOR AND LICENSE |
| 136 |
|
| 137 |
Copyright (c) 2008 by Florian Forster |
| 138 |
E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU |
| 139 |
General Public License, VersionE<nbsp>2 (GPLv2). |
| 140 |
|
| 141 |
=cut |
| 142 |
|
| 143 |
# vim: set shiftwidth=2 softtabstop=2 tabstop=8 et fdm=marker : |