From 020ab08f5d0d7a1fe02c8985b265a141845c02b4 Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Fri, 9 Nov 2012 12:37:40 +0100 Subject: [PATCH] 3.0: improving main function --- ng/monitorix | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/ng/monitorix b/ng/monitorix index 796d076..9b29725 100755 --- a/ng/monitorix +++ b/ng/monitorix @@ -21,8 +21,12 @@ require 5.006; -use warnings; use strict; +use warnings; +use FindBin qw($Bin); +use lib $Bin . '/lib'; + +use Monitorix; use RRDs; use POSIX; use Config::General; @@ -34,6 +38,7 @@ use DBI; use Getopt::Std; use Cwd 'abs_path'; use locale; + use Data::Dumper; # Force a standard locale @@ -98,12 +103,13 @@ sub ALRM_handler { foreach my $g (@graphs) { my ($t) = split('_', $g); logger("$myself: calling $g()") unless !$options{d}; - if(!grep {trim($_) eq $t} (@graphs_debug)) { - undef($t); - } - eval {&$g($t);}; - if($@) { - logger("$g(): $@"); + undef($t) if(!grep {trim($_) eq $t} (@graphs_debug)); + { + no strict "refs"; + eval {&$g($t);}; + if($@) { + logger("$g(): $@"); + } } } if(lc($config{graph_enable}->{pc}) eq "y" && lc($config{pc_enable_monthly_reports}) eq "y") { @@ -118,13 +124,6 @@ sub ALRM_handler { alarm(1); } -sub logger { - my ($msg) = @_; - - $msg = localtime() . " - " . $msg; - print("$msg\n"); -} - sub daemonize { if(fork) { exit(0); # parent exits @@ -159,14 +158,6 @@ EOF exit(1); } -sub trim { - my $str = shift; - - $str =~ s/^\s+//; - $str =~ s/\s+$//; - return $str; -} - sub create_index { my $myself = (caller(0))[3]; @@ -507,18 +498,27 @@ if(open(OUT, "> " . $config{base_dir} . "/cgi-bin/monitorix.conf.path")) { logger("Unable to create the file '$config{base_dir}/cgi-bin/monitorix.conf.path'."); } -logger("Initializing graphs.") unless !$options{d}; flush_accounting_rules(); +logger("Initializing graphs.") unless !$options{d}; foreach (split(',', $config{graph_name})) { my $g = trim($_); if(lc($config{graph_enable}->{$g}) eq "y") { - my $func = $g . "_init"; - eval {&$func();}; - logger("WARNING: unexpected errors in function $func()") if($@); + my $init = $g . "_init"; + eval "use $g qw(" . $init . " " . $g . "_update)"; + if($@) { + logger("WARNING: unable to find module '$g'"); + next; + } + { + no strict "refs"; + eval {&$init($g, \%config, \@graphs);}; + } + logger("WARNING: unexpected errors in function $init()") if($@); } } +print Dumper(@graphs); if(!scalar(@graphs)) { logger("nothing to do, exiting."); exit(0);