diff --git a/includes/common.php b/includes/common.php index 0a10d69dd..aaa4d6ea2 100644 --- a/includes/common.php +++ b/includes/common.php @@ -2,6 +2,15 @@ ## Common Functions +function sgn($int){ + if($int < 0) + return -1; + elseif($int == 0) + return 0; + else + return 1; +} + function get_sensor_rrd($device, $sensor) { global $config; diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 6b48674fc..a09b09c80 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -87,7 +87,7 @@ function sensor_low_limit($class, $current) $limit = $current * 0.7; break; case 'voltage': - $limit = $current * 0.85; + $limit = $current * (1 - (sgn($current) * 0.15)); break; case 'humidity': $limit = "70"; @@ -119,7 +119,7 @@ function sensor_limit($class, $current) $limit = $current * 1.60; break; case 'voltage': - $limit = $current * 1.15; + $limit = $current * (1 + (sgn($current) * 0.15)); break; case 'humidity': $limit = "70"; diff --git a/scripts/removespikes.pl b/scripts/removespikes.pl new file mode 100644 index 000000000..b26a0774c --- /dev/null +++ b/scripts/removespikes.pl @@ -0,0 +1,140 @@ +#!/usr/bin/perl -w +# +# matapicos v2.1 - Vins Vilaplana $tempfile`; + +# Scan the XML dump checking the variations and exponent deviations +open(FICH,"<$tempfile") + || die "$0: Cannot open file $tempfile:\n $! - $@"; + +while () { + chomp; + $linea=$_; + $cdo=0; + if ($linea=~/^(.*)/) { $tstamp=$1; } + if ($linea=~/(.*)$/) { $tresto=$1; } + if (/\s\d\.\d+e.(\d+)\s<\/v>/) { + @dump = split(/<\/v>/, $tresto); + for ($lino=0; $lino<=$#dump-1; $lino++) { # scans DS's within each row + if ( $dump[$lino]=~/\d\.\d+e.(\d+)\s/ ) { # make sure it is a number (and not NaN) + $a=substr("0$lino",-2).":".$1; + $exp{$a}++; # store exponents + $tot{substr("0$lino",-2)}++; # and keep a per DS total + } + } + } +} + +close FICH; + +########################################################################### +# Scan the hash to get the percentage variation of each value +foreach $lino (sort keys %exp) { + ($a)=$lino=~/^(\d+)\:/; + $por{$lino}=(100*$exp{$lino})/$tot{$a}; +} + +if ($DEBUG) { + # Dumps percentages for debugging purposes + print "--percentages--\n"; + foreach $lino (sort keys %exp) { + print $lino."--".$exp{$lino}."/"; + ($a)=$lino=~/^(\d+)\:/; + print $tot{$a}." = ".$por{$lino}."%\n"; + } + print "\n\n\n"; +} + + +########################################################################### +# Open the XML dump, and create a new one removing the spikes: +open(FICH,"<$tempfile") || + die "$0: Cannot open $tempfile for reading: $!-$@"; +open(FSAL,">$tempfile.xml") || + die "$0: Cannot open $tempfile.xml for writing: $!-$@"; + +$linbak=''; +$cont=0; +while () { + chomp; + $linea=$_; + $cdo=0; + if ($linea=~/^(.*)/) { $tstamp=$1; } # Grab timestamp + if ($linea=~/(.*)$/) { $tresto=$1; } # grab rest-of-line :-) + if (/\s\d\.\d+e.(\d+)\s<\/v>/) { # are there DS's? + @dump=split(/<\/v>/, $tresto); # split them + if ($linbak ne '') { + for ($lino=0;$lino<=$#dump-1;$lino++) { # for each DS: + if ($dump[$lino]=~/\d\.\d+e.(\d+)\s/) { # grab number (and not a NaN) + $a=$1*1; # and exponent + $b=substr("0$lino",-2).":$1"; # calculate the max percentage of this DS + if ($por{$b}< $LIMIT) { # if this line represents less than $LIMIT + $linea=$tstamp.$linbak; # we dump it. + $cdo=1; + $tresto=$linbak; + } + } + } + } + $linbak=$tresto; + if ($cdo==1) { + print "Chopping peak at $tstamp\n"; + $cont++; } + } + + print FSAL "$linea\n"; +} +close FICH; +close FSAL; + +########################################################################### +# Cleanup and move new file to the place of original one +# and original one gets backed up. +if ($cont == 0) { print "No peaks found.!\n"; } +else { + rename($ARGV[0],"$ARGV[0].old"); + $lino="rrdtool restore $tempfile.xml $ARGV[0]"; + system($lino); + die "$0: Unable to execute the rrdtool restore on $ARGV[0] - $! - $@\n" if $!; +} + +# cleans up the files created +unlink("$tempfile"); +unlink("$tempfile.xml"); +