1 package Collectd::Graph::TypeLoader;
5 Collectd::Graph::TypeLoader - Load a module according to the "type"
9 # Copyright (C) 2008,2009 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 ignore_unknown stacking));
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
51 stacking => 'stacking', # GenericStacked only
52 ignore_unknown => 'ignoreunknown' # GenericStacked only
62 local $SIG{__WARN__} = sub { print STDERR "WARNING: " . join (', ', @_) . "\n"; };
63 local $SIG{__DIE__} = sub { print STDERR "FATAL: " . join (', ', @_) . "\n"; };
67 \$obj = ${module}->new ();
77 sub _load_module_from_config
81 my $module = $conf->{'module'};
84 if ($module && !($module =~ m/::/))
86 $module = "Collectd::Graph::Type::$module";
91 $obj = _create_object ($module);
94 cluck ("Creating an $module object failed");
100 $obj = Collectd::Graph::Type->new ();
103 cluck ("Creating an Collectd::Graph::Type object failed");
108 for (@ScalarMembers) # {{{
111 my $key = $MemberToConfigMap{$member};
114 if (!defined $conf->{$key})
118 $val = $conf->{$key};
120 if (ref ($val) ne '')
122 cluck ("Invalid value type for $key: " . ref ($val));
126 $obj->{$member} = $val;
129 for (@ArrayMembers) # {{{
132 my $key = $MemberToConfigMap{$member};
135 if (!defined $conf->{$key})
139 $val = $conf->{$key};
141 if (ref ($val) eq 'ARRAY')
143 $obj->{$member} = $val;
145 elsif (ref ($val) eq '')
147 $obj->{$member} = [split (' ', $val)];
151 cluck ("Invalid value type for $key: " . ref ($val));
155 for (@DSMappedMembers) # {{{
158 my $key = $MemberToConfigMap{$member};
161 if (!defined $conf->{$key})
166 if (ref ($conf->{$key}) eq 'ARRAY')
168 @val_list = @{$conf->{$key}};
170 elsif (ref ($conf->{$key}) eq '')
172 @val_list = ($conf->{$key});
176 cluck ("Invalid value type for $key: " . ref ($conf->{$key}));
186 if (!defined ($line) || (ref ($line) ne ''))
191 ($ds, $val) = split (' ', $line, 2);
197 $obj->{$member} ||= {};
198 $obj->{$member}{$ds} = $val;
200 } # }}} for (@DSMappedMembers)
203 } # _load_module_from_config
205 sub _load_module_generic
208 my $module = ucfirst (lc ($type));
211 $module =~ s/[^A-Za-z_]//g;
212 $module =~ s/_([A-Za-z])/\U$1\E/g;
214 $obj = _create_object ($module);
217 $obj = Collectd::Graph::Type->new ();
220 cluck ("Creating an Collectd::Graph::Type object failed");
226 } # _load_module_generic
228 =head1 EXPORTED FUNCTIONS
232 =item B<tl_load_type> (I<$type>)
234 Does whatever is necessary to get an object with which to graph RRD files of
242 my $conf = gc_get_config ();
244 if (defined ($conf) && defined ($conf->{'type'}{$type}))
246 return (_load_module_from_config ($conf->{'type'}{$type}));
250 return (_load_module_generic ($type));
258 L<Collectd::Graph::Type::GenericStacked>
260 =head1 AUTHOR AND LICENSE
262 Copyright (c) 2008 by Florian Forster
263 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU
264 General Public License, VersionE<nbsp>2 (GPLv2).
268 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 et fdm=marker :