From: Florian Forster Date: Tue, 8 Feb 2011 15:43:22 +0000 (+0100) Subject: contrib/collection3: Set @INC at runtime, too. X-Git-Tag: collectd-5.1.0~71 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e7474a34d1a1ac59497796b8decd3d64c35f5a6a;p=collectd.git contrib/collection3: Set @INC at runtime, too. Apparently the @INC variable is reset by mod_perl, which causes later require()s to fail. --- diff --git a/contrib/collection3/bin/graph.cgi b/contrib/collection3/bin/graph.cgi index 40408fd5..2b3f2fe1 100755 --- a/contrib/collection3/bin/graph.cgi +++ b/contrib/collection3/bin/graph.cgi @@ -30,8 +30,8 @@ BEGIN { if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$}) { - $BASE_DIR = $1; - unshift (@INC, "$BASE_DIR/lib"); + $::BASE_DIR = $1; + unshift (@::INC, "$::BASE_DIR/lib"); } } } @@ -48,20 +48,66 @@ use Collectd::Graph::Common (qw(sanitize_type get_selected_files epoch_to_rfc1123 flush_files)); use Collectd::Graph::Type (); -$::MODPERL = 1; - -my $have_init = 0; -sub init +sub base_dir { - if ($have_init) + if (defined $::BASE_DIR) + { + return ($::BASE_DIR); + } + + if (!defined ($ENV{'SCRIPT_FILENAME'})) { return; } - #gc_read_config ("$RealBin/../etc/collection.conf"); - gc_read_config ("$BASE_DIR/etc/collection.conf"); + if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$}) + { + $::BASE_DIR = $1; + return ($::BASE_DIR); + } - $have_init = 1; + return; +} + +sub lib_dir +{ + my $base = base_dir (); + + if ($base) + { + return "$base/lib"; + } + else + { + return "../lib"; + } +} + +sub sysconf_dir +{ + my $base = base_dir (); + + if ($base) + { + return "$base/etc"; + } + else + { + return "../etc"; + } +} + +sub init +{ + my $lib_dir = lib_dir (); + my $sysconf_dir = sysconf_dir (); + + if (!grep { $lib_dir eq $_ } (@::INC)) + { + unshift (@::INC, $lib_dir); + } + + gc_read_config ("$sysconf_dir/collection.conf"); } sub main @@ -74,6 +120,8 @@ sub main my $OutputFormat = 'PNG'; my $ContentType = 'image/png'; + init (); + if (param ('format')) { my $temp = param ('format') || ''; @@ -98,8 +146,6 @@ HTTP $ContentType = 'text/plain'; } - init (); - if ($GraphWidth) { $GraphWidth =~ s/\D//g; diff --git a/contrib/collection3/bin/index.cgi b/contrib/collection3/bin/index.cgi index 679c1eea..027961fa 100755 --- a/contrib/collection3/bin/index.cgi +++ b/contrib/collection3/bin/index.cgi @@ -30,8 +30,8 @@ BEGIN { if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$}) { - $BASE_DIR = $1; - unshift (@INC, "$BASE_DIR/lib"); + $::BASE_DIR = $1; + unshift (@::INC, "$::BASE_DIR/lib"); } } } @@ -65,19 +65,66 @@ my %Actions = show_selection => \&action_show_selection ); -my $have_init = 0; -sub init +sub base_dir { - if ($have_init) + if (defined $::BASE_DIR) + { + return ($::BASE_DIR); + } + + if (!defined ($ENV{'SCRIPT_FILENAME'})) { return; } - print STDERR "INC = (" . join (', ', @INC) . ");\n"; + if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$}) + { + $::BASE_DIR = $1; + return ($::BASE_DIR); + } + + return; +} + +sub lib_dir +{ + my $base = base_dir (); - gc_read_config ("$BASE_DIR/etc/collection.conf"); + if ($base) + { + return "$base/lib"; + } + else + { + return "../lib"; + } +} + +sub sysconf_dir +{ + my $base = base_dir (); + + if ($base) + { + return "$base/etc"; + } + else + { + return "../etc"; + } +} + +sub init +{ + my $lib_dir = lib_dir (); + my $sysconf_dir = sysconf_dir (); + + if (!grep { $lib_dir eq $_ } (@::INC)) + { + unshift (@::INC, $lib_dir); + } - $have_init = 1; + gc_read_config ("$sysconf_dir/collection.conf"); } sub main @@ -339,7 +386,7 @@ sub action_show_selection $types->{$type} = tl_load_type ($file->{'type'}); if (!$types->{$type}) { - cluck ("tl_load_type (" . $file->{'type'} . ") failed"); + warn ("tl_load_type (" . $file->{'type'} . ") failed"); next; } } diff --git a/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm b/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm index 9078110b..5a0b522b 100644 --- a/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm +++ b/contrib/collection3/lib/Collectd/Graph/TypeLoader.pm @@ -92,7 +92,8 @@ sub _load_module_from_config $obj = _create_object ($module); if (!$obj) { - cluck ("Creating an $module object failed"); + #cluck ("Creating an $module object failed"); + warn ("Creating an $module object failed"); return; } }