Perl cgi-script to store and view data records on your own server

This directory contains a perl script which will run on any Linux web server that has the cgi-interface enabeled. The configuration paramter for apache to enable cgi-scripts is ScriptAlias. See your apache server documentation for more details about ScriptAlias.

The requirements that this script has are really keept minimal to ensure that it will run in many diverse server installations. It requires only plain perl (no special extension and needed).

The script accepts data from the ethernet board in the following format:
pw=YourPassword&key1=value1&key2=value2

example:
pw=sec&k1=1.2&k2=2
Key and value pairs must not be longer than 20 characters together and you can have a maximum of 16 key value pair. For security reasons the keys and values must only consist of letters, numbers, underscore, dot and minus.

If you pass the parameter "ot" then not only the time at which the record arrived at the server will be shown but as well the time at which the data was generated. The "ot" is in units of minutes. This is useful if data has to be buffered locally on the client during a network problem. "ot" should be zero if this is not a record that needed to be re-transmitted.

Installing the sdat perl script

Copy the file sdat into the cgi-bin directory of your web server and make it executable.
chmod 755 sdat
To see if it works simply point your web browser to the URL that you have assigned to the sdat script e.g http://your.nice.server/cgi-bin/sdat

For production use you should open the script and edit the following 2 lines. You can leave them as they are for initial testing.
my $password="sec"; # you can only store data or view data if you know this password, please use only letters and numbers.
my $dbfile="/tmp/sdat.txt"; # the directory where the data storage file sdat.txt resides needs to be writable for the web server process

Rotating log files with stored data

The sdat cgi-script stores it's up-loaded data in a text file. (see variable $dbfile in the script). You should rotate that file from time to time to avoid that it gets too big. You can use the logrotate facility as provided by many linux installations or the rotatefile script included here. The sdat cgi-script will actually present both the data of sdat.txt and sdat.txt.0 (if it exists). This way you will still see some old records stored previously after file rotation.

You can add the rotatefile script to a cron-tab and run it in intervals (once a week or once a month dependent on your data volume). You can put the following script in /etc/cron.monthly/sdat_rotate and rotatefile command into /usr/bin:
#!/bin/sh
# run this from crontab once a month (or once a week if you want)
dbfile="/where/ever/you/have/it/sdat.txt";
if [ -f "$dbfile" ]; then
	rotatefile "$dbfile"
fi


© tuxgraphics