| 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 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 |
use Collectd::Graph::Type (); |
| 31 |
|
| 32 |
@Collectd::Graph::Config::ISA = ('Exporter'); |
| 33 |
@Collectd::Graph::Config::EXPORT_OK = (qw(gc_read_config gc_get_config |
| 34 |
gc_get_scalar)); |
| 35 |
|
| 36 |
our $Configuration = undef; |
| 37 |
|
| 38 |
return (1); |
| 39 |
|
| 40 |
=head1 EXPORTED FUNCTIONS |
| 41 |
|
| 42 |
=over 4 |
| 43 |
|
| 44 |
=item B<gc_read_config> (I<$file>) |
| 45 |
|
| 46 |
Reads the configuration from the file located at I<$file>. Returns B<true> when |
| 47 |
successful and B<false> otherwise. |
| 48 |
|
| 49 |
=cut |
| 50 |
|
| 51 |
sub gc_read_config |
| 52 |
{ |
| 53 |
my $file = shift; |
| 54 |
my %conf; |
| 55 |
|
| 56 |
if ($Configuration) |
| 57 |
{ |
| 58 |
return (1); |
| 59 |
} |
| 60 |
|
| 61 |
$file ||= "etc/collection.conf"; |
| 62 |
|
| 63 |
%conf = ParseConfig (-ConfigFile => $file, |
| 64 |
-LowerCaseNames => 1, |
| 65 |
-UseApacheInclude => 1, |
| 66 |
-IncludeDirectories => 1, |
| 67 |
($Config::General::VERSION >= 2.38) ? (-IncludeAgain => 0) : (), |
| 68 |
-MergeDuplicateBlocks => 1, |
| 69 |
-CComments => 0); |
| 70 |
if (!%conf) |
| 71 |
{ |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$Configuration = \%conf; |
| 76 |
return (1); |
| 77 |
} # gc_read_config |
| 78 |
|
| 79 |
=item B<gc_get_config> () |
| 80 |
|
| 81 |
Returns the hash as provided by L<Config::General>. The hash is returned as a |
| 82 |
hash reference. Don't change it! |
| 83 |
|
| 84 |
=cut |
| 85 |
|
| 86 |
sub gc_get_config |
| 87 |
{ |
| 88 |
return ($Configuration); |
| 89 |
} # gc_get_config |
| 90 |
|
| 91 |
=item B<gc_get_config> (I<$key>, [I<$default>]) |
| 92 |
|
| 93 |
Returns the scalar value I<$key> from the config file. If the key does not |
| 94 |
exist, I<$default> will be returned. If no default is given, B<undef> will be |
| 95 |
used in this case. |
| 96 |
|
| 97 |
=cut |
| 98 |
|
| 99 |
sub gc_get_scalar |
| 100 |
{ |
| 101 |
my $key = shift; |
| 102 |
my $default = (@_ != 0) ? shift : undef; |
| 103 |
my $value; |
| 104 |
|
| 105 |
if (!$Configuration) |
| 106 |
{ |
| 107 |
return ($default); |
| 108 |
} |
| 109 |
|
| 110 |
$value = $Configuration->{lc ($key)}; |
| 111 |
if (!defined ($value)) |
| 112 |
{ |
| 113 |
return ($default); |
| 114 |
} |
| 115 |
|
| 116 |
if (ref ($value) ne '') |
| 117 |
{ |
| 118 |
cluck ("Value for `$key' should be scalar, but actually is " |
| 119 |
. ref ($value)); |
| 120 |
return ($default); |
| 121 |
} |
| 122 |
|
| 123 |
return ($value); |
| 124 |
} # gc_get_config |
| 125 |
|
| 126 |
=back |
| 127 |
|
| 128 |
=head1 DEPENDS ON |
| 129 |
|
| 130 |
L<Config::General> |
| 131 |
|
| 132 |
=head1 SEE ALSO |
| 133 |
|
| 134 |
L<Collectd::Graph::Type> |
| 135 |
|
| 136 |
=head1 AUTHOR AND LICENSE |
| 137 |
|
| 138 |
Copyright (c) 2008 by Florian Forster |
| 139 |
E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU |
| 140 |
General Public License, VersionE<nbsp>2 (GPLv2). |
| 141 |
|
| 142 |
=cut |
| 143 |
|
| 144 |
# vim: set shiftwidth=2 softtabstop=2 tabstop=8 et fdm=marker : |