diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index 80e8bb4bd3..679c4e8dd0 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -377,19 +377,7 @@ sub process_xml_data ($$$$$) { # Modify the timestamp with the timezone_offset logger($pa_config, "Applied a timezone offset of $timestamp to agent " . $data->{'agent_name'}, 10); - - # Calculate the start date to add the offset - my $utimestamp = 0; - eval { - if ($timestamp =~ /(\d+)[\/|\-](\d+)[\/|\-](\d+) +(\d+):(\d+):(\d+)/) { - $utimestamp = strftime("%s", $6, $5, $4, $3, $2 -1 , $1 - 1900); - } - }; - - # Apply the offset if there were no errors - if (! $@ && $utimestamp != 0) { - $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp + ($timezone_offset * 3600))); - } + $timestamp = apply_timezone_offset($timestamp, $timezone_offset); } # Check some variables @@ -630,6 +618,11 @@ sub process_xml_data ($$$$$) { # Single data if (! defined ($module_data->{'datalist'})) { my $data_timestamp = get_tag_value ($module_data, 'timestamp', $timestamp); + if ($pa_config->{'use_xml_timestamp'} eq '0' && defined($timestamp)) { + $data_timestamp = $timestamp; + } + $data_timestamp = apply_timezone_offset($data_timestamp, $timezone_offset); + process_module_data ($pa_config, $module_data, $server_id, $agent, $module_name, $module_type, $interval, $data_timestamp, $dbh, $new_agent); next; } @@ -647,10 +640,10 @@ sub process_xml_data ($$$$$) { $module_data->{'data'} = $data->{'value'}; my $data_timestamp = get_tag_value ($data, 'timestamp', $timestamp); - if ($pa_config->{'use_xml_timestamp'} eq '0' && defined($timestamp)) { $data_timestamp = $timestamp; } + $data_timestamp = apply_timezone_offset($data_timestamp, $timezone_offset); process_module_data ($pa_config, $module_data, $server_id, $agent, $module_name, $module_type, $interval, $data_timestamp, $dbh, $new_agent); diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 580344542c..1070b0d3c0 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -182,6 +182,7 @@ our @EXPORT = qw( check_cron_element cron_check p_pretty_json + apply_timezone_offset ); # ID of the different servers @@ -2990,6 +2991,33 @@ sub p_pretty_json { return $output; } + +################################################################################ +# Apply a timezone offset to the given timestamp. +################################################################################ +sub apply_timezone_offset { + my ($timestamp, $timezone_offset) = @_; + + # Nothing to be done. + return $timestamp if (!defined($timezone_offset) || $timezone_offset == 0); + + # Convert the timestamp to seconds. + my $utimestamp = 0; + eval { + if ($timestamp =~ /(\d+)[\/|\-](\d+)[\/|\-](\d+) +(\d+):(\d+):(\d+)/) { + $utimestamp = strftime("%s", $6, $5, $4, $3, $2 -1 , $1 - 1900); + } + }; + + # Something went wrong. + return $timestamp if ($@); + + # Apply the offset and convert back to timestamp. + $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp + ($timezone_offset * 3600))); + + return $timestamp; +} + 1; __END__