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 enabled. The configuration
parameter for apache would be ScriptAlias. See your apache server documentation
for more details on how to enable cgi-scripts.
sdat -- a perl cgi-script
The requirements that this script has are really keet 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
Key and value pairs must not be longer than 20 characters together and you can have
a maximum of 12 key value pair. For security reasons the keys and values must only
consist of letters, numbers, underscore, dot and minus.
Note that the password is in all the examples and this sdat script hardcoded to "sec".
I recommend to change this string.
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:
# please edit the following 2 lines and adapt them. You can leave them as is for initial testing.
#
my $password="sec"; # you can only store 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