2

以下 bash 脚本每天发送一封包含 PHP 错误日志的电子邮件。

#!/bin/bash
 
# phperrlog v1.0
# by vladimir prelovac http://www.prelovac.com/vladimir/
#
# parse error logs on your server and send you daily updates to email
  
# configure options
  EMAIL="tech@domain.com"
   
  WORKDIR="/var/scripts"
  TAIL=50  # number of entries to send
 # IGNORE="/backup" # path to ignore
    
# script starts 'ere
     
 cd $WORKDIR
 rm phperrlog.txt 2>/dev/null
      
 LIST=$(ls /var/log/apache2/*-error.log)
 today=$(date +%Y-%m-%d)
       
 for i in $LIST
  do
    if [ -f $i ]; then
       time=$(date -r $i +%F)
       if [ "$time" == "$today" ]; then
         echo $i >>phperrlog.txt
         echo "---------------------------------" >>phperrlog.txt
         tail -n $TAIL $i >>phperrlog.txt
         echo -e "\n\n\n\n" >>phperrlog.txt
       fi
    fi
 done
                                              
  if [ -f  phperrlog.txt ]; then
    mail -s "PHPERRORLOG SCRIPT: server error logs - $today" $EMAIL  < phperrlog.txt
  fi

如何修改此脚本以排除与此类似的所有错误:

[Thu Aug 02 10:54:33 2012] [error] [client 12.345.67.89] Options 指令禁止的目录索引:/var/www/domain/public/templates/img/

[2012 年 8 月 2 日星期四 11:25:35] [错误] [客户端 12.345.67.89] 客户端被服务器配置拒绝:/var/www/domain/public/templates/sidebar.tpl

我对以下内容更感兴趣:

  • PHP 通知/警告/致命错误
  • 文件不存在
4

2 回答 2

3

grep可以从文件中读取模式

 -f file, --file=file
         Read one or more newline separated patterns from file.  Empty
         pattern lines match every input line.  Newlines are not considered
         part of a pattern.  If file is empty, nothing is matched.

在您的情况下,您必须决定是要使用白名单(您希望在报告中看到的模式列表)还是黑名单(您不想看到的模式列表。收集相关模式后,请替换tail -n $TAIL $i >>phperrlog.txt

grep -f /path/to/whitelist.txt "$i" | tail -n ${TAIL:-50} >> phperrlog.txt

或者

grep -v -f /path/to/blacklist.txt "$i" | tail -n ${TAIL:-50} >> phperrlog.txt

当我注意到我不想再看到的行时,我可能会从黑名单开始,并随着时间的推移向其中添加其他模式。初始黑名单可能包含

Directory index forbidden by Options directive
client denied by server configuration

加工您的样品。

于 2012-08-04T10:04:00.473 回答
0

尝试替换重定向

mail -s "PHPERRORLOG SCRIPT: server error logs - $today" $EMAIL  < phperrlog.txt

例如,使用流程和管道

grep -v '\[error\]' < phperrlog.txt | mail -s "PHPERRORLOG SCRIPT: server error logs - $today" $EMAIL 

或者

grep -v '\[error\]' phperrlog.txt | mail -s "PHPERRORLOG SCRIPT: server error logs - $today" $EMAIL 
于 2012-08-04T12:27:08.127 回答