Fix data timestamps.

Apply timezone offsets to data timestamps.
Ignore data timestamps if use_xml_timestamp is set to 0.
This commit is contained in:
Ramon Novoa 2024-02-22 12:22:00 +01:00
parent eba0ce9e80
commit 5373b17bac
2 changed files with 35 additions and 14 deletions

View File

@ -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);

View File

@ -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__