#!/usr/bin/perl # This script generates the trend graphs for the water reading with the # Zoom feature for forensicsin the log files. It takes parameters "unit", # two date text strings "from" and "to" for the time interval, and a "sample" # argument that defines the display rendering. # Arguments are passed to the script via the GET method, there are default # values if no parameters are present. # Naming convention for dates are whatever the date command in unix can # parse, e.g. "3 days ago", "now", "20 Feb 2012" etc. # Directories where the log files reside in #$logdir="/home/chris/construction/BTUmetering/log"; #$gnudir="/home/chris/construction/BTUmetering/gnuplot"; $logdir="MAINPROGROOT/log"; $gnudir="MAINPROGROOT/gnuplot"; # Image directory: #$imgdir="/srv/www/htdocs/img2"; $imgdir="HTDOCROOT/img2"; @allowedcores = ("AHU1", "AHU2", "AHU3", "S14L3", "AHU4", "AHU5", "AHU6", "PCW2"); # Following http://www.tutorialspoint.com/perl/perl_cgi.htm, we extract the # parameters sent via get: local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer = $ENV{'QUERY_STRING'}; } # sanitize input $buffer =~ s/[^\w =&%:+-]//g; # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $value =~ s/[^\w :-]//g; $FORM{$name} = $value; } # Extract the interesting ones $core = $FORM{unit}; $starttime = $FORM{from}; $endtime = $FORM{to}; $sampling = $FORM{sample}; # TODO: We need to check if there are parameters supplied, otherwise supply # defaults. # Generate page header print "content-type: text/html\n\n"; print "CQT utility consumption server \n"; print "

Data zoom view for heat load / CHW temperature

\n"; # Now, we need to check for parameter consistency. $testerror=0; if ($core eq "") { $core = "AHU1"; } # test if unit exists $matches = grep $core eq $_, @allowedcores; if ($matches != 1) { print "

Error

Sorry, logging for this unit is not (yet) implemented.

\n"; $testerror=1; } # test if "to" date is parsable or replace by default if ($testerror==0) { # launder argument (do this later) $et2=$endtime; if ($et2 == "") {$et2 ="now"; $endtime=$et2;}; # retreive time in unix seconds $et=`date -d \"$et2\" +"%s"`; chomp ($et); # test if conversion was successful if ( $et !~ /\d+/) { print "

Error

Could not interpret your \"to\" date specification.

\n"; $testerror=2; } } # test if "from" date is parsable if ($testerror==0) { # launder argument (do this later) $st2=$starttime; if ($st2 == "") {$st2 ="1 month ago";$starttime=$st2;}; # retreive time in unix seconds $st=`date -d \"$st2\" +"%s"`; chomp ($st); # test if conversion was successful if ( $st !~ /\d+/) { print "

Error

Could not interpret your \"from\" date specification.

\n"; $testerror=3; } } # check if time interval is correctly oriented if ($testerror==0) { if ($st >= $et) { print "

Error

Your date range is empty or has the wrong direction.

\n"; $testerror=4; } } # check if time interval overlaps with past if ($testerror==0) { $nw=`date +"%s"`; chomp($nw); if ( $st gt $nw ) { print "

Error

Your date range ($st to $et) is referring to the future, but I can only make statements about the past.

\n"; $testerror=5; } } # test if "sample" date is parsable $sampling =~ s/[^\d]//; if ($sampling eq "") { $sampling = 1; } if ($sampling < 1) { $sampling = 1; } # now we can generate the image and print out a link to it if ($testerror==0) { # convert unix time range into gnuplot time range $ofs=`date -d "1 Jan 2000 00:00SGT" +"%s"`; $stg=$st-$ofs; $etg=$et-$ofs; # find out what part of the tail we need to use for speedup in display. # For this, we calculate the num of minutes from start to now $taillength=int(($nw-$st)/60.); # For debugging # $timespan=1; # $core="AHU2"; # Prepare flexible arguments for gnuplot for different sizes $imagename = $core."_zoom.png"; # compose parameter set for gnuplot $parset="set xrange [$stg:$etg]; num=$taillength; ofmt=\"%H:%M\\n%m/%d\"; core=\"$core\"; evr=$sampling; outfilename=\"$imagename\"; logdir=\"$logdir\"; imgdir=\"$imgdir\" \n"; # generate image open GNUPLOT, "| gnuplot - $gnudir/zoom.gnu >/dev/null"; print GNUPLOT $parset; close GNUPLOT; # Generate web page print "\n"; } # Select other resolutions/ units print "

Show other time span:

1. Select unit:
2. From time/date:
3. To time/date:
4. Sampling:
5.
Format of the entries above:
The time specification can be anything that is understood by the unix date command, e.g. \"now\", \"11:25\", \"12:00 8 June\", \"last week\", \"23 Mar\" etc.
Sampling is the integer number of minutes are between different points on the graph. This helps to reduce noise in long periodes.

"; # Link to main page of this server print "

Back to main page.

"; # Finish html page print "\n\n";