9.7. Notification on Error Conditions
Problem
You want to receive email notification when there’s an error condition on your server.
Solution
Point the ErrorDocument directive to a CGI program that sends mail, rather than to a static document:
ErrorDocument 404 /cgi-bin/404.cgi404.cgi looks like the following:
#!/usr/bin/perl
use Mail::Sendmail;
use strict;
my $message = qq~
Document not found: $ENV{REQUEST_URI}
Link was from: $ENV{HTTP_REFERER}
~;
my %mail = (
To => 'admin@server.com',
From => 'website@server.com',
Subject => 'Broken link',
Message => $message,
);
sendmail(%mail);
print "Content-type: text/plain\n\n";
print "Document not found. Admin has been notified\n";Discussion
This recipe is provided as an example, rather than as a recommendation. On a Web site of any significant size or traffic level, actually putting this into practice generates a substantial quantity of email, even on a site that is very well maintained. This is because people mistype URLs, and other sites, over which you have no control, will contain incorrect links to your site. It may be educational, however, to put something like this in place, at least briefly, to gain an appreciation for the scale of your own Web site.
The ErrorDocument directive
will cause all 404 (Document Not
Found) requests to be handled by the specified URL, and so
your CGI program gets run and is passed environment variables that
will be used in the script itself to figure out what link is bad and
where the request came from.
The script ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access