Webstats Emailer Script

awstats

I wrote a perl script to email myself the current webstats HTML webpage.

The Problem

We use awstats to trend the webserver logs on www

It’s an excellent package with one glaring fault: No Email Notification!

The Solution

To remedy this, I’ve written a perl script to do this. It simply loads the page source using curl, and creates an HTML email.

The script is named ‘webstats_emailer.pl’ and is installed on sysadmin server.

‘webstats_emailer.pl’ crontab entries on sysadmin server:

0 8 * * * /usr/local/bin/webstats_emailer.pl
30 16 * * * /usr/local/bin/webstats_emailer.pl

webstats_emailer.pl

#!/usr/bin/perl -w
#script to email web stats daily as an HML email

#Problems: all links to images are relative to the webserver, so the html email doesn't display images

#Get webpage
$curl = `/opt/csw/bin/curl "http://www.gemini.edu/stats/cgi-bin/awstats.pl?config=gemini.edu&framename=mainright"`;

email('Illmatic Webstats',$curl);

#Function that emails message
sub email {
        my ($subject,$message) = @_;

        #get hostname
        #so we know what server ran this script
        my $hostname = `/usr/bin/hostname`;
        chop($hostname);

        my $sender = $hostname . "@xx.edu";

        use Net::SMTP;

        $smtp = Net::SMTP->new('mailserver', Debug => 0, Hello => 'gemini.edu');

        $smtp->mail($sender);
        $smtp->to('xxx@xxx.edu');

        $smtp->data();
        $smtp->datasend("Mime-Version: 1.0n");
        $smtp->datasend("Content-type: text/html;");
        $smtp->datasend("charset="iso-8859-1"n");
        $smtp->datasend("From: $hostnamen");
        $smtp->datasend("To: Webmastern");
        $smtp->datasend("Subject: $subjectn");
        $smtp->datasend("n");
        $smtp->datasend("$messagen");
        $smtp->dataend();

        $smtp->quit;
}