From d7f5e3407af897014251a13646c48bddc40427d2 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 27 Jul 2008 17:21:04 +0200 Subject: [PATCH] contrib/collection3: Move configuration logic into Collectd::Graph::Config. --- contrib/collection3/bin/graph.cgi | 17 ++- contrib/collection3/bin/index.cgi | 5 +- contrib/collection3/etc/collection.conf | 3 +- contrib/collection3/lib/Collectd/Config.pm | 144 +++++++++++++++++++++ .../collection3/lib/Collectd/Graph/TypeLoader.pm | 53 ++------ 5 files changed, 172 insertions(+), 50 deletions(-) create mode 100644 contrib/collection3/lib/Collectd/Config.pm diff --git a/contrib/collection3/bin/graph.cgi b/contrib/collection3/bin/graph.cgi index 08557b47..7c29bb01 100755 --- a/contrib/collection3/bin/graph.cgi +++ b/contrib/collection3/bin/graph.cgi @@ -9,7 +9,8 @@ use Carp (qw(confess cluck)); use CGI (':cgi'); use RRDs (); -use Collectd::Graph::TypeLoader (qw(tl_read_config tl_load_type)); +use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar)); +use Collectd::Graph::TypeLoader (qw(tl_load_type)); use Collectd::Graph::Common (qw(sanitize_type get_selected_files epoch_to_rfc1123)); @@ -28,7 +29,17 @@ Content-Type: text/plain HTTP } -tl_read_config ("$RealBin/../etc/collection.conf"); +gc_read_config ("$RealBin/../etc/collection.conf"); + +if ($GraphWidth) +{ + $GraphWidth =~ s/\D//g; +} + +if (!$GraphWidth) +{ + $GraphWidth = gc_get_scalar ('GraphWidth', 400); +} { # Sanitize begin and end times $End ||= 0; @@ -155,7 +166,7 @@ else } $| = 1; - RRDs::graph ('-', '-a', 'PNG', @timesel, @$args); + RRDs::graph ('-', '-a', 'PNG', '--width', $GraphWidth, @timesel, @$args); if (my $err = RRDs::error ()) { print STDERR "RRDs::graph failed: $err\n"; diff --git a/contrib/collection3/bin/index.cgi b/contrib/collection3/bin/index.cgi index d8059df0..d8769634 100755 --- a/contrib/collection3/bin/index.cgi +++ b/contrib/collection3/bin/index.cgi @@ -27,7 +27,8 @@ use HTML::Entities ('encode_entities'); use Data::Dumper; -use Collectd::Graph::TypeLoader (qw(tl_read_config tl_load_type)); +use Collectd::Graph::Config (qw(gc_read_config)); +use Collectd::Graph::TypeLoader (qw(tl_load_type)); use Collectd::Graph::Common (qw(get_files_from_directory get_all_hosts get_timespan_selection get_selected_files get_host_selection get_plugin_selection)); @@ -57,7 +58,7 @@ if (!exists ($Actions{$action})) exit 1; } -tl_read_config ("$RealBin/../etc/collection.conf"); +gc_read_config ("$RealBin/../etc/collection.conf"); $Actions{$action}->(); exit (0); diff --git a/contrib/collection3/etc/collection.conf b/contrib/collection3/etc/collection.conf index c96e9216..a8be4fda 100644 --- a/contrib/collection3/etc/collection.conf +++ b/contrib/collection3/etc/collection.conf @@ -1,3 +1,4 @@ +GraphWidth 400 Module GenericStacked DataSources count @@ -282,4 +283,4 @@ RRDFormat "%4.1lfV" Color value f00000 -# vim: set sw=2 sts=2 et syntax=apache : +# vim: set sw=2 sts=2 et syntax=apache fileencoding=latin-1 : diff --git a/contrib/collection3/lib/Collectd/Config.pm b/contrib/collection3/lib/Collectd/Config.pm new file mode 100644 index 00000000..d20be359 --- /dev/null +++ b/contrib/collection3/lib/Collectd/Config.pm @@ -0,0 +1,144 @@ +package Collectd::Graph::Config; + +=head1 NAME + +Collectd::Graph::Config - Parse the collection3 config file. + +=cut + +# Copyright (C) 2008 Florian octo Forster +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; only version 2 of the License is applicable. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +use strict; +use warnings; + +use Carp (qw(cluck confess)); +use Exporter (); +use Config::General ('ParseConfig'); +use Collectd::Graph::Type (); + +@Collectd::Graph::Config::ISA = ('Exporter'); +@Collectd::Graph::Config::EXPORT_OK = (qw(gc_read_config gc_get_config + gc_get_scalar)); + +our $Configuration = undef; + +return (1); + +=head1 EXPORTED FUNCTIONS + +=over 4 + +=item B (I<$file>) + +Reads the configuration from the file located at I<$file>. Returns B when +successfull and B otherwise. + +=cut + +sub gc_read_config +{ + my $file = shift; + my %conf; + + if ($Configuration) + { + return (1); + } + + $file ||= "etc/collection.conf"; + + %conf = ParseConfig (-ConfigFile => $file, + -LowerCaseNames => 1, + -UseApacheInclude => 1, + -IncludeDirectories => 1, + ($Config::General::VERSION >= 2.38) ? (-IncludeAgain => 0) : (), + -MergeDuplicateBlocks => 1, + -CComments => 0); + if (!%conf) + { + return; + } + + $Configuration = \%conf; + return (1); +} # gc_read_config + +=item B () + +Returns the hash as provided by L. The hash is returned as a +hash reference. Don't change it! + +=cut + +sub gc_get_config +{ + return ($Configuration); +} # gc_get_config + +=item B (I<$key>, [I<$default>]) + +Returns the scalar value I<$key> from the config file. If the key does not +exist, I<$default> will be returned. If no default is given, B will be +used in this case. + +=cut + +sub gc_get_scalar +{ + my $key = shift; + my $default = (@_ != 0) ? shift : undef; + my $value; + + if (!$Configuration) + { + return ($default); + } + + $value = $Configuration->{lc ($key)}; + if (!defined ($value)) + { + return ($default); + } + + if (ref ($value) ne '') + { + cluck ("Value for `$key' should be scalar, but actually is " + . ref ($value)); + return ($default); + } + + return ($value); +} # gc_get_config + +=back + +=head1 DEPENDS ON + +L + +=head1 SEE ALSO + +L + +=head1 AUTHOR AND LICENSE + +Copyright (c) 2008 by Florian Forster +EoctoEatEverplant.orgE. Licensed under the terms of the GNU +General Public License, VersionE2 (GPLv2). + +=cut + +# vim: set shiftwidth=2 softtabstop=2 tabstop=8 et fdm=marker : diff --git a/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm b/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm index 6223eaa0..c5fe613f 100644 --- a/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm +++ b/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm @@ -27,12 +27,11 @@ use warnings; use Carp (qw(cluck confess)); use Exporter (); use Config::General ('ParseConfig'); +use Collectd::Graph::Config ('gc_get_config'); use Collectd::Graph::Type (); @Collectd::Graph::TypeLoader::ISA = ('Exporter'); -@Collectd::Graph::TypeLoader::EXPORT_OK = ('tl_read_config', 'tl_load_type'); - -our $Configuration = undef; +@Collectd::Graph::TypeLoader::EXPORT_OK = ('tl_load_type'); our @ArrayMembers = (qw(data_sources rrd_opts custom_order)); our @ScalarMembers = (qw(rrd_title rrd_format rrd_vertical scale)); @@ -53,42 +52,6 @@ our %MemberToConfigMap = return (1); -=head1 EXPORTED FUNCTIONS - -=over 4 - -=item B (I<$file>) - -Reads the configuration from the file located at I<$file>. - -=cut - -sub tl_read_config -{ - my $file = shift; - my %conf; - - if ($Configuration) - { - return (1); - } - - %conf = ParseConfig (-ConfigFile => $file, - -LowerCaseNames => 1, - -UseApacheInclude => 1, - -IncludeDirectories => 1, - ($Config::General::VERSION >= 2.38) ? (-IncludeAgain => 0) : (), - -MergeDuplicateBlocks => 1, - -CComments => 0); - if (!%conf) - { - return; - } - - $Configuration = \%conf; - return (1); -} # tl_read_config - sub _create_object { my $module = shift; @@ -123,7 +86,6 @@ sub _load_module_from_config if ($module) { - print STDERR "\$module = $module;\n"; $obj = _create_object ($module); if (!$obj) { @@ -232,8 +194,6 @@ sub _load_module_from_config $obj->{$member} ||= {}; $obj->{$member}{$ds} = $val; - - print STDERR "\$obj->{$member}{$ds} = $val;\n"; } # for (@val_list) } # }}} for (@DSMappedMembers) @@ -263,6 +223,10 @@ sub _load_module_generic return ($obj); } # _load_module_generic +=head1 EXPORTED FUNCTIONS + +=over 4 + =item B (I<$type>) Does whatever is necessary to get an object with which to graph RRD files of @@ -273,10 +237,11 @@ type I<$type>. sub tl_load_type { my $type = shift; + my $conf = gc_get_config (); - if (defined $Configuration->{'type'}{$type}) + if (defined ($conf) && defined ($conf->{'type'}{$type})) { - return (_load_module_from_config ($Configuration->{'type'}{$type})); + return (_load_module_from_config ($conf->{'type'}{$type})); } else { -- 2.11.0