From 2380a18c6df38050b0e6ddbccee88454dbb9a219 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 3 Apr 2018 09:46:44 +0200 Subject: [PATCH] Minor changes in PluginTools for 1801 --- pandora_server/lib/PandoraFMS/PluginTools.pm | 106 +++++++++++++++---- 1 file changed, 88 insertions(+), 18 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/PluginTools.pm b/pandora_server/lib/PandoraFMS/PluginTools.pm index 0d92952026..5eba8910ca 100644 --- a/pandora_server/lib/PandoraFMS/PluginTools.pm +++ b/pandora_server/lib/PandoraFMS/PluginTools.pm @@ -605,8 +605,11 @@ sub transfer_xml { $file_name .= "_" . time() . ".data"; } - $conf->{temp} = $conf->{tmp} if (empty($conf->{temp}) && defined($conf->{tmp})); - $conf->{temp} = $conf->{temporal} if (empty($conf->{temporal}) && defined($conf->{temporal})); + logger($conf, "transfer_xml", "Failed to generate file name."); + + $conf->{temp} = $conf->{tmp} if (empty($conf->{temp}) && defined($conf->{tmp})); + $conf->{temp} = $conf->{temporal} if (empty($conf->{temp}) && defined($conf->{temporal})); + $conf->{temp} = $conf->{__system}->{tmp} if (empty($conf->{temp}) && defined($conf->{__system})) && (ref($conf->{__system}) eq "HASH"); $file_path = $conf->{temp} . "/" . $file_name; @@ -734,7 +737,7 @@ sub print_execution_result { } print_module($conf, { - name => "Plugin execution result", + name => "Plugin execution result " . $0, type => "generic_proc", value => (defined($value)?$value:0), desc => $msg, @@ -745,12 +748,12 @@ sub print_execution_result { ## Plugin devolution in case of error ################################################################################ sub print_error { - my ($conf, $msg, $value) = @_; + my ($conf, $msg, $value, $always_show) = @_; $value = 0 unless defined($value); - if (!(is_enabled($conf->{informational_modules}))) { - return 0; + if (!(is_enabled($conf->{informational_modules}) || is_enabled($always_show))) { + exit 1; } if (is_enabled($conf->{'as_server_plugin'})) { @@ -760,7 +763,7 @@ sub print_error { } print_module($conf, { - name => (empty($conf->{'global_plugin_module'})?"Plugin execution result":$conf->{'global_plugin_module'}), + name => (empty($conf->{'global_plugin_module'})?"Plugin execution result " . $0:$conf->{'global_plugin_module'}), type => "generic_proc", value => $value, desc => $msg, @@ -786,32 +789,39 @@ sub print_stderror { my $log_aux_flag = 0; sub logger { my ($conf, $tag, $message) = @_; - my $file = $conf->{log}; - print_error($conf, "Log file undefined\n") unless defined $file; + my $file = $conf->{'log'}; + + print_error($conf, "[ERROR] Log file is not defined.", 0, 1) unless defined($file); # Log rotation - if (-e $file && (stat($file))[7] > 32000000) { + if (defined($file) && -e $file && (stat($file))[7] > 32000000) { rename ($file, $file.'.old'); } my $LOGFILE; if ($log_aux_flag == 0) { # Log starts if (! open ($LOGFILE, "> $file")) { - print_error ($conf, "[ERROR] Could not open logfile '$file'"); + print_error ($conf, "[ERROR] Could not open logfile '$file'", 0, 1); } $log_aux_flag = 1; } else { if (! open ($LOGFILE, ">> $file")) { - print_error ($conf, "[ERROR] Could not open logfile '$file'"); + print_error ($conf, "[ERROR] Could not open logfile '$file'", 0, 1); } } - $message = '' if empty($message); - $message = "[" . $tag . "] " . $message if empty($tag); + if (empty($message)) { + $message = $tag; + $message = "" if empty($message); + } + else { + $message = "[" . $tag . "] " . $message unless empty($tag); + } - if (!(empty($conf->{agent_name}))){ - $message = "[" . $conf->{agent_xml_name} . "] " . $message; + + if (!(empty($conf->{'agent_name'}))){ + $message = "[" . $conf->{'agent_name'} . "] " . $message; } print $LOGFILE strftime ("%Y-%m-%d %H:%M:%S", localtime()) . " - " . $message . "\n"; @@ -824,7 +834,7 @@ sub logger { sub is_enabled { my $value = shift; - if ((defined ($value)) && ($value > 0)){ + if ((defined ($value)) && looks_like_number($value) && ($value > 0)){ # return true return 1; } @@ -931,6 +941,7 @@ sub init_system { $system{grep} = "findstr"; $system{echo} = "echo"; $system{wcl} = "wc -l"; + $system{tmp} = ".\\"; } else { $system{devnull} = "/dev/null"; @@ -940,6 +951,7 @@ sub init_system { $system{grep} = "grep"; $system{echo} = "echo"; $system{wcl} = "wc -l"; + $system{tmp} = "/tmp"; if ($^O =~ /hpux/i) { $system{os} = "HPUX"; @@ -970,6 +982,41 @@ sub get_sys_environment { ################################################################################ # Parses any configuration, from file (1st arg to program) or direct arguments +# +# Custom evals are defined in an array reference of hash references: +# +# $custom_eval = [ +# { +# 'exp' => 'regular expression to match', +# 'target' => \&target_custom_method_to_parse_line +# }, +# { +# 'exp' => 'another regular expression to search', +# 'target' => \&target_custom_method_to_parse_line2 +# }, +# ] +# +# Target is an user defined function wich will be invoked with following +# arguments: +# +# $config : The configuration read to the point the regex matches +# $exp : The matching regex which fires this action +# $line : The complete line readed from the file +# $file_pointer : A pointer to the file which is being parsed. +# $current_entity : The current_entity (optional) when regex matches +# +# E.g. +# +# sub target_custom_method_to_parse_line { +# my ($config, $exp, $line, $file_pointer, $current_entity) = @_; +# +# if ($line =~ /$exp/) { +# $config->{'my_key'} = complex_operation_on_data($1,$2,$3); +# } +# +# return $config; +# } +# ################################################################################ sub read_configuration { my ($config, $separator, $custom_eval) = @_; @@ -1034,11 +1081,16 @@ sub parse_arguments { # $current_entity : The current_entity (optional) when regex matches # ################################################################################ +sub parse_configuration; sub parse_configuration { my ($conf_file, $separator, $custom_eval, $detect_entities, $entities_list) = @_; + my @arguments = @_; + shift(@arguments); + $separator = "=" unless defined($separator); + my $_CFILE; my $_config; @@ -1124,6 +1176,24 @@ sub parse_configuration { next; } } + if ($key =~ /^include$/i) { + my $file_included = trim($value); + my $aux; + eval { + $aux = parse_configuration($file_included, @arguments); + }; + if($@) { + Carp::croak ("Failed to parse configuration"); + } + + if (empty($_config)) { + $_config = $aux; + } + elsif (!empty($aux) && (ref ($aux) eq "HASH")) { + $_config = merge_hashes($_config, $aux); + } + next; + } $_config->{trim($key)} = trim($value); } close ($_CFILE); @@ -1138,7 +1208,7 @@ sub parse_configuration { return $global_config unless empty($global_config); } - + return $_config; }