From 1247536ebc5e66a45d155e56005f1f41bc58f5ca Mon Sep 17 00:00:00 2001 From: slerena Date: Fri, 18 May 2007 12:59:24 +0000 Subject: [PATCH] 2007-05-18 Sancho Lerena * pandora_tools.pm, pandora_config.pm, pandora_db.pm: Many small fixes. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@458 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/bin/pandora_config.pm | 7 ++++-- pandora_server/bin/pandora_db.pm | 31 +++++++++++++++---------- pandora_server/bin/pandora_tools.pm | 34 ++++++++++++++++------------ 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/pandora_server/bin/pandora_config.pm b/pandora_server/bin/pandora_config.pm index 9a3da60435..d746d2848b 100755 --- a/pandora_server/bin/pandora_config.pm +++ b/pandora_server/bin/pandora_config.pm @@ -163,11 +163,14 @@ sub pandora_loadconfig { } } close (CFG); + # Process this array with commandline like options # Process input parameters + my @args = @command_line; my $parametro; - my $ltotal=$#args; my $ax; + my $ltotal=$#args; + my $ax; # Has read setup file ok ? if ( $ltotal == 0 ) { @@ -304,7 +307,7 @@ sub pandora_loadconfig { # Dump all errors to errorlog # DISABLED in DEBUGMODE # ENABLE FOR PRODUCTION - # open STDERR, ">>$pa_config->{'errorlogfile'}" or die "Can't write to Errorlog : $!"; + #open STDERR, ">>$pa_config->{'errorlogfile'}" or die "Can't write to Errorlog : $!"; } # End of function declaration diff --git a/pandora_server/bin/pandora_db.pm b/pandora_server/bin/pandora_db.pm index c4d842f828..00f133d97b 100644 --- a/pandora_server/bin/pandora_db.pm +++ b/pandora_server/bin/pandora_db.pm @@ -601,18 +601,17 @@ sub module_generic_data_inc (%$$$$$) { my $a_desc = $datos->{description}->[0]; my $m_data = $datos->{data}->[0]; my $a_max = $datos->{max}->[0]; - my $a_min = $datos->{min}->[0]; - - if (ref($m_data) ne "HASH"){ + my $a_min = $datos->{min}->[0]; + if (is_numeric($m_data)){ $m_data =~ s/\,/\./g; # replace "," by "." $m_data = sprintf("%.2f", $m_data); # Two decimal float. We cannot store more # to change this, you need to change mysql structure $m_data =~ s/\,/\./g; # replace "," by "." - if (ref($a_max) eq "HASH") { + if (!is_numeric($a_max)) { $a_max = ""; } - if (ref($a_min) eq "HASH") { + if (!is_numeric($a_min)) { $a_min = ""; } # my $timestamp = &UnixDate("today","%Y-%m-%d %H:%M:%S"); @@ -650,8 +649,16 @@ sub module_generic_data_inc (%$$$$$) { my $s_idag = $dbh->prepare($query_idag); $s_idag->execute; my @data_row = $s_idag->fetchrow_array(); - $data_anterior = $data_row[2]; - $timestamp_anterior = $data_row[4]; + if (is_numeric($data_row[2])){ + $data_anterior = $data_row[2]; + } else { + $data_anterior = 0; + } + if (is_numeric($data_row[4])){ + $timestamp_anterior = $data_row[4]; + } else { + $timestamp_anterior = 0; + } $diferencia = $m_data - $data_anterior; $timestamp_diferencia = $m_utimestamp - $timestamp_anterior; @@ -667,12 +674,12 @@ sub module_generic_data_inc (%$$$$$) { # Update of tagente_datos_inx (AUX TABLE) if ($no_existe == 1){ - my $query = "INSERT INTO tagente_datos_inc (id_agente_modulo,datos, timestamp) VALUES ($id_agente_modulo, '$m_data', '$m_timestamp', $m_utimestamp)"; + my $query = "INSERT INTO tagente_datos_inc (id_agente_modulo,datos, timestamp, utimestamp) VALUES ($id_agente_modulo, '$m_data', '$m_timestamp', $m_utimestamp)"; $dbh->do($query); } else { # Data exists previously if ($diferencia != 0) { - my $query2 = "UPDATE tagente_datos_inc SET utimestamp = $m_utimestamp, datos = '$m_data' WHERE id_agente_modulo = $id_agente_modulo"; + my $query2 = "UPDATE tagente_datos_inc SET timestamp='$m_timestamp', utimestamp = $m_utimestamp, datos = '$m_data' WHERE id_agente_modulo = $id_agente_modulo"; $dbh->do($query2); } } @@ -855,14 +862,14 @@ sub pandora_writedata (%$$$$$$$$$$){ $timestamp = $dbh->quote($timestamp); # Parse data entry for adecuate SQL representation. $query = "INSERT INTO tagente_datos_string (id_agente_modulo, datos, timestamp, utimestamp, id_agente) VALUES ($id_agente_modulo, $datos, $timestamp, $utimestamp, $id_agente)"; - } else { + } elsif (is_numeric($datos)){ if ($max != $min) { - if ($datos > $max) { + if (int($datos) > $max) { $datos = $max; $outlimit=1; logger($pa_config,"DEBUG: MAX Value reached ($max) for agent $nombre_agente / $nombre_modulo",6); } - if ($datos < $min) { + if (int($datos) < $min) { $datos = $min; $outlimit = 1; logger($pa_config, "DEBUG: MIN Value reached ($min) for agent $nombre_agente / $nombre_modulo",6); diff --git a/pandora_server/bin/pandora_tools.pm b/pandora_server/bin/pandora_tools.pm index 46aa8b3ce0..e5ffbd76cc 100644 --- a/pandora_server/bin/pandora_tools.pm +++ b/pandora_server/bin/pandora_tools.pm @@ -63,21 +63,25 @@ sub daemonize { # Return TRUE if given argument is numeric ########################################################################## -sub getnum { - use POSIX qw(strtod); - my $str = shift; - $str =~ s/^\s+//; - $str =~ s/\s+$//; - $! = 0; - my($num, $unparsed) = strtod($str); - if (($str eq '') || ($unparsed != 0) || $!) { - return undef; - } else { - return $num; - } - } - -sub is_numeric { defined getnum($_[0]) } +sub is_numeric { + $x = $_[0]; + if (!defined ($x)){ + return 0; + } + if ($x eq ""){ + return 0; + } + # Integer ? + if ($x =~ /^-?\d/) { + return 1; + } + # Float ? + if ($x =~ /^-?\d*\./){ + return 1; + } + # If not, this thing is not a number + return 0; +} ########################################################################## # SUB md5check (param_1, param_2)