I have some server applications running on apache2; Ruby on Rails, PHP and others.
In all cases I would like apache to send me an email whenever apache responds a HTTP error 500 Internal server error.
How can I do that?
The location of Apache 2.4 standard error messages is inside the executable. You can easily find them if you peek inside the executable with some hex editor.
This error message indicates that a website visitor tried to access a password protected page without authenticating (with valid username and password). Where 1.2. 3.4 is the IP address of the visitor.
Apache gives 500 Internal Server Error when there is a server-side error that prevents Apache from processing a request and returning a proper response. This can be due to various reasons such as faulty code, inadequate file permissions, missing files referenced in code, etc.
One way you can do this is use a php script that sends email, as well as output some sort of 500 message. Then use the ErrorDocument
directive:
ErrorDocument 500 /path/to/script/that/sends/email.php
There are some things you can do to achieve the same result:
You can use the ErrorDocument directive to let it point to some CGI script, that be a perl, PHP, or any other server-side executed script. The script can then send an email and respond a Error 500 document.
You can use some external tool to watch your apache log files and configure theese tools to send you emails. There are several tools and ways to do this. For Linux many tools and methods are suggested on https://serverfault.com/questions/45246/linux-monitor-logs-and-email-alerts
Write an apache module. I think that mod_tee is a good starting point.
Create a script called log_monitor.sh
:
#!/usr/bin/perl -w
use strict;
my $cachefile="/var/cache/lastpos-apache2-scan4maxclntOrSigKill";
my $logfile="/var/log/httpd/error_log";
my $searchstr=" 500 ";
my $lastpos=0;
if (-f $cachefile) {
open FH,"<".$cachefile;
$lastpos=<FH>;
close FH;
};
my $newpos=(stat $logfile)[7];
open FH,"<".$logfile;
seek FH,$lastpos,0;
while (<FH>) {
print if m/$searchstr/i;
};
close FH;
open FH,">".$cachefile;
print FH $newpos;
close FH;
modify $searchstr
as needed. note the spaces around "500", since you don't want to match errors which contain a 404 on a file that has "500" in its path or filename (among other places).
configure the script to run every X minutes via cron. the greater the value of X, the less emails you will get (only 1 email every X minutes for all the errors that match the strings you supply). the results of the cron job will get emailed to you automatically (if cron is setup properly)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With