免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5788 | 回复: 5
打印 上一主题 下一主题

一個用Perl分析Apache Log的簡單程序.. [复制链接]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-08-19 08:32 |只看该作者 |倒序浏览
發信用的Email..會將純文字的文件寄送出去...

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

  11. #local($to,$subject,$filename,$from,$server_ip,$replyto) =@_;
  12. ## 取得輸入參數
  13. $to=shift;
  14. $subject = shift;
  15. $filename=shift;
  16. $filename=~/(\/.*\/)(.*)/;
  17. ## 取得附加檔名
  18. $file= $2;
  19. $from=shift;
  20. $server_ip=shift;
  21. $replyto= shift;
  22. $fake_from =shift;

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

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


產生前一天日期的代碼

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


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

  1. #!/usr/bin/perl
  2. # check_apache.pl
  3. while(<>;){
  4.   chomp;
  5.   $aa{$_}++;
  6. }
  7. open (FF,">;/apile/record.txt");
  8. my($predate)=`/apile/predate`;
  9. chomp($predate);
  10. print FF "------- APACHE 登錄檔記錄------------\r\n";
  11. print FF "--紀錄日期:$predate      登錄筆數大於500筆者\r\n";
  12. print FF "---------------------------------------------\r\n";
  13. for (reverse sort { $aa{$a}<=>;$aa{$b}} keys %aa){
  14.   if( $aa{$_} >; 500){
  15.      print FF "$_ =>; $aa{$_} \r\n";
  16.   }
  17. }
  18. print FF "---------------------------------------------\r\n";
  19. close(FF);
复制代码



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

  9. /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萬次..讓我的系統非常不穩定..

论坛徽章:
0
2 [报告]
发表于 2003-08-19 11:33 |只看该作者

一個用Perl分析Apache Log的簡單程序..

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

论坛徽章:
0
3 [报告]
发表于 2003-09-10 09:40 |只看该作者

一個用Perl分析Apache Log的簡單程序..

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

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-09-10 09:47 |只看该作者

一個用Perl分析Apache Log的簡單程序..

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

论坛徽章:
0
5 [报告]
发表于 2003-09-10 10:27 |只看该作者

一個用Perl分析Apache Log的簡單程序..

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

  1. #!/usr/bin/perl
  2. my $access;
  3. my $log_file;
  4. my $error_file;
  5. open (ACCESS,"/home/deanej/logs/access.txt") || die "Can't open file";
  6. foreach  (<ACCESS>;) {
  7.   $access.= $_;
  8. }
  9. close (ACCESS);
  10. open (LOG_FILE,"/home/deanej/logs/log_file.txt") || die "Can't open file";
  11. foreach  (<LOG_FILE>;) {
  12. $log_file.=$_;
  13. }
  14. close (LOG_FILE);
  15. open (ERROR_FILE,"/home/deanej/logs/error_file.txt") || die "Can't open file";
  16. foreach  (<ERROR_FILE>;) {
  17.    $error_file.=$_;
  18. }
  19. close (ERROR_FILE);
  20. open (MAIL,"|/usr/sbin/sendmail -t") || die "can't exec sendmail";
  21. $old=select(MAIL);
  22. print<<"EOF";
  23. TO:aa\@localhost
  24. From:aa\@aa.com
  25. Subject:Log file
  26. Mime-Version: 1.0
  27. Content-Type: multipart/mixed;
  28.         boundary="----=_Part_702668_3668633.1015131391343"

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

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

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

  43. $error_file;


  44. ------=_Part_702668_3668633.1015131391343--
  45. EOF
  46. select($old);
复制代码

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-09-10 10:57 |只看该作者

一個用Perl分析Apache Log的簡單程序..

因為我的主機上面沒有sendmail..只能借助公司的mail
server...:)
這樣子就不需要去研究複雜的SendMail了...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP