1
package Collectd::Graph::TypeLoader;
5
Collectd::Graph::TypeLoader - Load a module according to the "type"
9
# Copyright (C) 2008 Florian octo Forster <octo at verplant.org>
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.
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
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.
27
use Carp (qw(cluck confess));
29
use Config::General ('ParseConfig');
30
use Collectd::Graph::Type ();
32
@Collectd::Graph::TypeLoader::ISA = ('Exporter');
33
@Collectd::Graph::TypeLoader::EXPORT_OK = ('tl_read_config', 'tl_load_type');
35
our $Configuration = undef;
37
our @ArrayMembers = (qw(data_sources rrd_opts custom_order));
38
our @ScalarMembers = (qw(rrd_title rrd_format rrd_vertical scale));
39
our @DSMappedMembers = (qw(ds_names rrd_colors));
41
our %MemberToConfigMap =
43
data_sources => 'datasources',
45
rrd_title => 'rrdtitle',
46
rrd_opts => 'rrdoptions',
47
rrd_format => 'rrdformat',
48
rrd_vertical => 'rrdverticallabel',
49
rrd_colors => 'color',
50
scale => 'scale', # GenericIO only
51
custom_order => 'order' # GenericStacked only
56
=head1 EXPORTED FUNCTIONS
60
=item B<tl_read_config> (I<$file>)
62
Reads the configuration from the file located at I<$file>.
76
%conf = ParseConfig (-ConfigFile => $file,
78
-UseApacheInclude => 1,
79
-IncludeDirectories => 1,
80
($Config::General::VERSION >= 2.38) ? (-IncludeAgain => 0) : (),
81
-MergeDuplicateBlocks => 1,
88
$Configuration = \%conf;
97
local $SIG{__WARN__} = sub {};
98
local $SIG{__DIE__} = sub {};
102
\$obj = ${module}->new ();
112
sub _load_module_from_config
116
my $module = $conf->{'module'};
119
if ($module && !($module =~ m/::/))
121
$module = "Collectd::Graph::Type::$module";
126
print STDERR "\$module = $module;\n";
127
$obj = _create_object ($module);
130
cluck ("Creating an $module object failed");
136
$obj = Collectd::Graph::Type->new ();
139
cluck ("Creating an Collectd::Graph::Type object failed");
144
for (@ScalarMembers) # {{{
147
my $key = $MemberToConfigMap{$member};
150
if (!defined $conf->{$key})
154
$val = $conf->{$key};
156
if (ref ($val) ne '')
158
cluck ("Invalid value type for $key: " . ref ($val));
162
$obj->{$member} = $val;
165
for (@ArrayMembers) # {{{
168
my $key = $MemberToConfigMap{$member};
171
if (!defined $conf->{$key})
175
$val = $conf->{$key};
177
if (ref ($val) eq 'ARRAY')
179
$obj->{$member} = $val;
181
elsif (ref ($val) eq '')
183
$obj->{$member} = [split (' ', $val)];
187
cluck ("Invalid value type for $key: " . ref ($val));
191
for (@DSMappedMembers) # {{{
194
my $key = $MemberToConfigMap{$member};
197
if (!defined $conf->{$key})
202
if (ref ($conf->{$key}) eq 'ARRAY')
204
@val_list = @{$conf->{$key}};
206
elsif (ref ($conf->{$key}) eq '')
208
@val_list = ($conf->{$key});
212
cluck ("Invalid value type for $key: " . ref ($conf->{$key}));
222
if (!defined ($line) || (ref ($line) ne ''))
227
($ds, $val) = split (' ', $line, 2);
233
$obj->{$member} ||= {};
234
$obj->{$member}{$ds} = $val;
236
print STDERR "\$obj->{$member}{$ds} = $val;\n";
238
} # }}} for (@DSMappedMembers)
241
} # _load_module_from_config
243
sub _load_module_generic
246
my $module = ucfirst (lc ($type));
249
$module =~ s/[^A-Za-z_]//g;
250
$module =~ s/_([A-Za-z])/\U$1\E/g;
252
$obj = _create_object ($module);
255
$obj = Collectd::Graph::Type->new ();
258
cluck ("Creating an Collectd::Graph::Type object failed");
264
} # _load_module_generic
266
=item B<tl_load_type> (I<$type>)
268
Does whatever is necessary to get an object with which to graph RRD files of
277
if (defined $Configuration->{'type'}{$type})
279
return (_load_module_from_config ($Configuration->{'type'}{$type}));
283
return (_load_module_generic ($type));
291
L<Collectd::Graph::Type::GenericStacked>
293
=head1 AUTHOR AND LICENSE
295
Copyright (c) 2008 by Florian Forster
296
E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU
297
General Public License, VersionE<nbsp>2 (GPLv2).
301
# vim: set shiftwidth=2 softtabstop=2 tabstop=8 et fdm=marker :