首页 > 学技术 > 技术网文 > Perl > 正文

[原创] 一個用Perl分析Apache Log的簡單程序..


来源 chinaunix.net kuqin整理

發信用的Email..會將純文字的文件寄送出去...

#!/usr/bin/perl
#---------------------------------------------------------------
#fileName: mail_perl:發送測試用的Email
#---------------------------------------------------------------
use Mail::Sender;
## 幫助Debug使用
use Carp();
local $SIG{__WARN__} = \&Carp::cluck;
## 將Standard Error儲存至檔案
open(STDERR,">;>;/tmp/mail_err.log");

#local($to,$subject,$filename,$from,$server_ip,$replyto) =@_;
## 取得輸入參數
$to=shift;
$subject = shift;
$filename=shift;
$filename=~/(\/.*\/)(.*)/;
## 取得附加檔名
$file= $2;
$from=shift;
$server_ip=shift;
$replyto= shift;
$fake_from =shift;

#print "$to,$subject,$filename,$from,$server_ip,$replyto\n";

# 定義smtp server,client是從哪來的
ref($sender = new Mail::Sender {
          smtp =>; "$server_ip",
          client =>; '10.1.1.1',
          boundary=>; 'this-is-a-mail-boundary-435427' })
    or die "Error in mailing: $Mail::Sender::Error\n";
 # 開啟multi part的object,定義to,from,fake_from,charset,replyto,subject等變數
$sender->;OpenMultipart({to=>;"$to",
                      from =>;"$from",
                      fake_from =>; "$fake_from",
                      charset=>;'Big5',
                      replyto =>;"$replyto",
                      subject =>; "$subject"});
# 寫內文
$sender->;Body({charset=>;'big5',ctype=>;'text/plain'});
$sender->;Send(<<"*END*");
這是一封由系統自動告知信件,請查收..謝謝您!!!
--------------------
*END*
# 開始附加檔案部分,先要瞭解附加檔案是什麼型態,
# 本範例是純文字檔,所以ctype使用的是text/plain,如果是gif要使用image/gif
# ,dispositiion會影響user看到的檔案名稱,如果有/xx/xx/ttt.txt會顯示成
# _xx_xx_ttt.txt ,"_"會被替代成"/"
$sender->;Attach({ description=>;"$file",
         ctype=>;'text/plain',
         disposition=>;"attachment;filename=$file",
         file=>;"$filename"
        })
        || die "Error in attachment: $Mail::Sender::Error\n";
$sender->;Close;
 

產生前一天日期的代碼

#!/usr/bin/perl
# fileName: predate
my %mon=(
  1 =>; "Jan",  2 =>; "Feb",  3 =>; "Mar",  4 =>; "Apr",
  5 =>; "May",  6 =>; "Jun",  7 =>; "Jul",  8 =>; "Aug",
  9 =>; "Sep",  10 =>; "Oct",  11 =>; "Nov",  12 =>; "Dec"
);
my ($sec1,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time-86400);
   $mon++;
   $mon=$mon{$mon};
   $mday = sprintf("%02s",$mday);
   $hour = sprintf("%02s",$hour);
   $min = sprintf("%02s",$min);
   $sec = sprintf("%02s",$sec);
   $year+=1900;
print "$mday/$mon/$year\n";


前一天登錄超過五百次者,將他的IP Address記錄下來...
由大到小排序

#!/usr/bin/perl
# check_apache.pl
while(<>;){
  chomp;
  $aa{$_}++;
}
open (FF,">;/apile/record.txt");
my($predate)=`/apile/predate`;
chomp($predate);
print FF "------- APACHE 登錄檔記錄------------\r\n";
print FF "--紀錄日期:$predate      登錄筆數大於500筆者\r\n";
print FF "---------------------------------------------\r\n";
for (reverse sort { $aa{$a}<=>;$aa{$b}} keys %aa){
  if( $aa{$_} >; 500){
     print FF "$_ =>; $aa{$_} \r\n";
  }
}
print FF "---------------------------------------------\r\n";
close(FF);




#!/usr/bin/ksh
# check_apache.sh
PATH=/usr/bin:/sbin:/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
Date=`/apile/predate`
echo "cat /usr/local/apache/logs/access_log|grep $Date | awk '{printf("%s\n",$1)}'|
/apile/check_apache.pl"
cat /usr/local/apache/log/access_log|grep "$Date" | awk '{printf("%s\n",$1)}'|/apile/check_apache.pl

/apile/mail_perl "apile@abc.cde" " APACHE登錄檔檢查每日定時回報" "/apile/record.txt" "apile@abc.cde" "10.2.2.2" "apile@abc.cde" "FAKEMAIL@fake.fake"



使用方式:crontab -e
00 17 * * 0-5 /apile/check_apache.sh 1>;/tmp/apache.log 2>;/tmp/apache.err


Email附件如下:(record.txt)
引用:
-------APACHE 登錄檔記錄------------
--紀錄日期:17/Aug/2003      登錄筆數大於500筆者
---------------------------------------------
10.1.17.80 =>; 1848 
10.1.17.65 =>; 1834 
10.1.17.66 =>; 1607 
10.1.17.85 =>; 1403 
10.1.17.51 =>; 1345 
10.1.17.52 =>; 1027 
10.1.17.84 =>; 860 
10.1.17.54 =>; 849 
10.1.17.86 =>; 690 
10.1.17.81 =>; 674 
10.1.17.50 =>; 655 
10.1.17.64 =>; 619 
10.1.17.69 =>; 619 
10.1.17.67 =>; 593 
10.1.17.83 =>; 575 
10.1.17.57 =>; 512 
10.1.17.55 =>; 505 
-------------------------------------------



這只是簡單的記錄那個IP Address登錄了幾次...
功能不完整,但還可以用,因為最近我維護的系統
不穩定,就讓我用這個程序抓到有人在三天內連接
了超過5萬次..讓我的系統非常不穩定..



 deathcult 回复于:2003-08-19 11:33:17

Good,感谢Apile提供宝贵的工作经验! :)


 wuhanzhou 回复于:2003-09-10 09:40:07

哥们给我介绍一本APACHE的书吧!


 apile 回复于:2003-09-10 09:47:21

http://linux.vbird.org/
書喔..我不清楚..:)
我都上google..搜尋出來的..
上面這個網站..有篇WWW簡易設定...
可以看看...


 deanej 回复于:2003-09-10 10:27:41

呵呵,我是用sendmail直接发的将一些日志作为附加发送。

#!/usr/bin/perl
my $access;
my $log_file;
my $error_file;
open (ACCESS,"/home/deanej/logs/access.txt") || die "Can't open file";
foreach  (<ACCESS>;) {
  $access.= $_;
}
close (ACCESS);
open (LOG_FILE,"/home/deanej/logs/log_file.txt") || die "Can't open file";
foreach  (<LOG_FILE>;) {
 $log_file.=$_;
}
close (LOG_FILE);
open (ERROR_FILE,"/home/deanej/logs/error_file.txt") || die "Can't open file";
foreach  (<ERROR_FILE>;) {
   $error_file.=$_;
}
close (ERROR_FILE);
open (MAIL,"|/usr/sbin/sendmail -t") || die "can't exec sendmail";
$old=select(MAIL);
print<<"EOF";
TO:aa\@localhost
From:aa\@aa.com
Subject:Log file
Mime-Version: 1.0
Content-Type: multipart/mixed; 
        boundary="----=_Part_702668_3668633.1015131391343"

------=_Part_702668_3668633.1015131391343
Content-Type: text/plain; charset=gb2312
Content-Transfer-Encoding: 8bit
Content-Disposition:inline

$access;
------=_Part_702668_3668633.1015131391343
Content-Type:text/plain; name=log_file.txt
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename=log_file.txt

$log_file;
------=_Part_702668_3668633.1015131391343
Content-Type:text/plain; name=error_file.txt
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename=error_file.txt

$error_file;


------=_Part_702668_3668633.1015131391343--
EOF
select($old);



 apile 回复于:2003-09-10 10:57:05

因為我的主機上面沒有sendmail..只能借助公司的mail
server...:)
這樣子就不需要去研究複雜的SendMail了...




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=142253
转载请注明作者名及原文出处



收藏本页到: