added a new option called 'include_dir' to be able to load configuration files from a specific directory

This commit is contained in:
Jordi Sanfeliu 2014-03-11 09:26:28 +01:00
parent 0c645d8a09
commit 1525d12534
2 changed files with 56 additions and 8 deletions

View File

@ -46,8 +46,8 @@ $SIG{'TERM'} = 'INT_handler';
$SIG{'CHLD'} = 'CHLD_handler'; $SIG{'CHLD'} = 'CHLD_handler';
$SIG{'HUP' } = 'HUP_handler'; $SIG{'HUP' } = 'HUP_handler';
use constant VERSION => "3.4.92"; use constant VERSION => "3.4.93";
use constant RELDATE => "04-Mar-2014"; use constant RELDATE => "11-Mar-2014";
my @suppsys = ("Linux", "FreeBSD", "OpenBSD", "NetBSD"); my @suppsys = ("Linux", "FreeBSD", "OpenBSD", "NetBSD");
our %config; our %config;
@ -92,7 +92,6 @@ sub HUP_handler {
} }
sub daemonize { sub daemonize {
chdir("/") || die "Can't chdir to /: $!";
open(STDIN, "< /dev/null") || die "Can't read /dev/null: $!"; open(STDIN, "< /dev/null") || die "Can't read /dev/null: $!";
open(STDOUT, ">> $config{log_file}") || die "Can't write to LOG: $!"; open(STDOUT, ">> $config{log_file}") || die "Can't write to LOG: $!";
umask(022) || die "Unable to umask 022: $!"; umask(022) || die "Unable to umask 022: $!";
@ -381,7 +380,7 @@ if(!stat($options{c})) {
die "can't open file '$options{c}': $!"; die "can't open file '$options{c}': $!";
} }
# load configuration file # load main configuration file
my $conf = new Config::General( my $conf = new Config::General(
-ConfigFile => $options{c}, -ConfigFile => $options{c},
); );
@ -412,7 +411,6 @@ $0 = sprintf("%s %s%s%s%s",
$options{d} ? " -d $options{d}" : "", $options{d} ? " -d $options{d}" : "",
$options{v} ? " -v" : ""); $options{v} ? " -v" : "");
daemonize();
logger("Starting Monitorix version " . VERSION . " (pid $$)."); logger("Starting Monitorix version " . VERSION . " (pid $$).");
# save the pidfile # save the pidfile
@ -429,6 +427,7 @@ unless(chdir("/tmp")) {
die "can't chdir to /tmp: $!"; die "can't chdir to /tmp: $!";
} }
daemonize();
if($options{d}) { if($options{d}) {
if($options{d} ne "none" && $options{d} ne "all") { if($options{d} ne "none" && $options{d} ne "all") {
@{$config{debug}} = split(',', $options{d}); @{$config{debug}} = split(',', $options{d});
@ -442,12 +441,60 @@ if($options{d}) {
logger("Changed process name to '$0'."); logger("Changed process name to '$0'.");
} }
# load additional configuration files
if(opendir(DIR, $config{include_dir})) {
my @files = grep { !/^[.]/ } readdir(DIR);
close(DIR);
foreach my $c (sort @files) {
my $conf_inc = new Config::General(
-ConfigFile => $config{include_dir} . "/$c",
);
my %config_inc = $conf_inc->getall;
my $g = $config_inc{graph_name};
if(!$g) {
logger("ERROR: graph name not defined.");
next;
}
if(grep {trim($_) eq $g} (split(',', $config{graph_name}))) {
logger("ERROR: graph name '$config_inc{graph_name}' already exist.");
next;
}
if(!$config_inc{graph_enable}->{$g}) {
logger("ERROR: graph name '$config_inc{graph_name}' doesn't have a 'graph_enable' entry.");
next;
}
if(!$config_inc{graph_title}->{$g}) {
logger("ERROR: graph name '$config_inc{graph_name}' doesn't have a 'graph_title' entry.");
next;
}
if(!$config_inc{$g}) {
logger("ERROR: graph name '$config_inc{graph_name}' doesn't have its own '<block>'.");
next;
}
$config{graph_enable}->{$g} = $config_inc{graph_enable}->{$g};
$config{$g} = $config_inc{$g};
$config{graph_title}->{$g} = $config_inc{graph_title}->{$g};
$config{graph_name} .= ", $g";
foreach my $k (sort keys %{$config_inc{graphs}}) {
$config{graphs}->{$k} = $config_inc{graphs}->{$k};
}
delete $config_inc{graph_name};
delete $config_inc{graph_enable};
delete $config_inc{$g};
delete $config_inc{graph_title};
delete $config_inc{graphs};
@config{keys %config_inc} = values %config_inc;
}
} else {
logger("Can't read directory '$config{include_dir}'. $!");
}
# save the path of the configuration file # save the path of the configuration file
if(open(OUT, "> " . $config{base_dir} . "/cgi/monitorix.conf.path")) { if(open(OUT, "> " . $config{base_dir} . "/cgi/monitorix.conf.path")) {
print(OUT "$options{c}\n"); print(OUT "$options{c}\n");
close(OUT); close(OUT);
} else { } else {
logger("Unable to create the file '$config{base_dir}/cgi/monitorix.conf.path'."); logger("Unable to create the file '$config{base_dir}/cgi/monitorix.conf.path'. $!");
} }
flush_accounting_rules(\%config, $options{d}); flush_accounting_rules(\%config, $options{d});
@ -474,14 +521,14 @@ foreach (split(',', $config{graph_name} . ", traffacct")) {
eval "use $g qw(" . $init . " " . $g . "_update)"; eval "use $g qw(" . $init . " " . $g . "_update)";
if($@) { if($@) {
logger("WARNING: unable to find module '$g'"); logger("WARNING: unable to find module '$g'.");
next; next;
} }
{ {
no strict "refs"; no strict "refs";
eval { &$init($g, \%config, $d); }; eval { &$init($g, \%config, $d); };
} }
logger("WARNING: unexpected errors in function $init()") if($@); logger("WARNING: unexpected errors in function $init().") if($@);
} }
} }

View File

@ -16,6 +16,7 @@ show_gaps = n
global_zoom = 1 global_zoom = 1
max_historic_years = 1 max_historic_years = 1
accept_selfsigned_certs = y accept_selfsigned_certs = y
include_dir = /etc/monitorix/conf.d
base_dir = /var/lib/monitorix/www/ base_dir = /var/lib/monitorix/www/
base_lib = /var/lib/monitorix/ base_lib = /var/lib/monitorix/