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::Config ('gc_get_config');
31 use Collectd::Graph::Type ();
33 @Collectd::Graph::TypeLoader::ISA = ('Exporter');
34 @Collectd::Graph::TypeLoader::EXPORT_OK = ('tl_load_type');
36 our @ArrayMembers = (qw(data_sources rrd_opts custom_order));
37 our @ScalarMembers = (qw(rrd_title rrd_format rrd_vertical scale));
38 our @DSMappedMembers = (qw(ds_names rrd_colors));
40 our %MemberToConfigMap =
42 data_sources => 'datasources',
44 rrd_title => 'rrdtitle',
45 rrd_opts => 'rrdoptions',
46 rrd_format => 'rrdformat',
47 rrd_vertical => 'rrdverticallabel',
48 rrd_colors => 'color',
49 scale => 'scale', # GenericIO only
50 custom_order => 'order' # GenericStacked only
60 local $SIG{__WARN__} = sub {};
61 local $SIG{__DIE__} = sub {};
65 \$obj = ${module}->new ();
75 sub _load_module_from_config
79 my $module = $conf->{'module'};
82 if ($module && !($module =~ m/::/))
84 $module = "Collectd::Graph::Type::$module";
89 $obj = _create_object ($module);
92 cluck ("Creating an $module object failed");
98 $obj = Collectd::Graph::Type->new ();
101 cluck ("Creating an Collectd::Graph::Type object failed");
106 for (@ScalarMembers) # {{{
109 my $key = $MemberToConfigMap{$member};
112 if (!defined $conf->{$key})
116 $val = $conf->{$key};
118 if (ref ($val) ne '')
120 cluck ("Invalid value type for $key: " . ref ($val));
124 $obj->{$member} = $val;
127 for (@ArrayMembers) # {{{
130 my $key = $MemberToConfigMap{$member};
133 if (!defined $conf->{$key})
137 $val = $conf->{$key};
139 if (ref ($val) eq 'ARRAY')
141 $obj->{$member} = $val;
143 elsif (ref ($val) eq '')
145 $obj->{$member} = [split (' ', $val)];
149 cluck ("Invalid value type for $key: " . ref ($val));
153 for (@DSMappedMembers) # {{{
156 my $key = $MemberToConfigMap{$member};
159 if (!defined $conf->{$key})
164 if (ref ($conf->{$key}) eq 'ARRAY')
166 @val_list = @{$conf->{$key}};
168 elsif (ref ($conf->{$key}) eq '')
170 @val_list = ($conf->{$key});
174 cluck ("Invalid value type for $key: " . ref ($conf->{$key}));
184 if (!defined ($line) || (ref ($line) ne ''))
189 ($ds, $val) = split (' ', $line, 2);
195 $obj->{$member} ||= {};
196 $obj->{$member}{$ds} = $val;
198 } # }}} for (@DSMappedMembers)
201 } # _load_module_from_config
203 sub _load_module_generic
206 my $module = ucfirst (lc ($type));
209 $module =~ s/[^A-Za-z_]//g;
210 $module =~ s/_([A-Za-z])/\U$1\E/g;
212 $obj = _create_object ($module);
215 $obj = Collectd::Graph::Type->new ();
218 cluck ("Creating an Collectd::Graph::Type object failed");
224 } # _load_module_generic
226 =head1 EXPORTED FUNCTIONS
230 =item B<tl_load_type> (I<$type>)
232 Does whatever is necessary to get an object with which to graph RRD files of
240 my $conf = gc_get_config ();
242 if (defined ($conf) && defined ($conf->{'type'}{$type}))
244 return (_load_module_from_config ($conf->{'type'}{$type}));
248 return (_load_module_generic ($type));
256 L<Collectd::Graph::Type::GenericStacked>
258 =head1 AUTHOR AND LICENSE
260 Copyright (c) 2008 by Florian Forster
261 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU
262 General Public License, VersionE<nbsp>2 (GPLv2).
266 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 et fdm=marker :