6

我有一些在 apache2 上运行的服务器应用程序;Ruby on Rails、PHP 等。

在所有情况下,只要 apache 响应 HTTP 错误 500 内部服务器错误,我都希望 apache 向我发送一封电子邮件。

我怎样才能做到这一点?

4

3 回答 3

11

一种方法是使用发送电子邮件的 php 脚本,以及输出某种 500 消息。然后使用ErrorDocument指令:

ErrorDocument 500 /path/to/script/that/sends/email.php
于 2012-08-31T06:35:42.063 回答
10

无法将 apache 配置为发送电子邮件。

您可以采取一些措施来获得相同的结果:

  1. 您可以使用ErrorDocument 指令让它指向某个 CGI 脚本,即 perl、PHP 或任何其他服务器端执行的脚本。然后,该脚本可以发送电子邮件并响应错误 500 文档。

  2. 您可以使用一些外部工具来查看您的 apache 日志文件并配置这些工具来向您发送电子邮件。有几种工具和方法可以做到这一点。对于 Linux,在https://serverfault.com/questions/45246/linux-monitor-logs-and-email-alerts上建议了许多工具和方法

  3. 编写一个apache模块。我认为 mod_tee 是一个很好的起点。

于 2012-09-03T10:33:10.207 回答
4

创建一个名为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;

$searchstr根据需要进行修改。请注意“500”周围的空格,因为您不想匹配在路径或文件名(以及其他位置)中包含“500”的文件上包含 404 的错误。

将脚本配置为通过 cron 每 X 分钟运行一次。X 的值越大,您收到的电子邮件就越少(对于与您提供的字符串匹配的所有错误,每 X 分钟只有 1 封电子邮件)。cron 作业的结果将自动通过电子邮件发送给您(如果 cron 设置正确)

于 2013-10-11T19:40:00.290 回答