#!/usr/bin/perl # # futomi's CGI Cafe 高機能アクセス解析で月毎に吐かれる # ログファイルを処理するためのスクリプト # 後で似たようなスクリプトを書きたいときに見返すため公開 # hoge/access_log.YYYYMM00.cgiというアクセスログファイルの # 行数を調べてhogeYYYYMM.tgzというファイルに圧縮し、 # 結果をメールで通知する # sendmail, wc, tar, rm, perl, (cron) が必要 # # 【想定環境】 # # 本スクリプトと同じ階層にログファイルのディレクトリや # シンボリックリンクがある # # (path)/bkup.pl # (path)/(dirname)/access_log.YYYYMM00.cgi # ### 設定 ### $sendmail = '/usr/sbin/sendmail'; # sendmail のパス $path = '/home/yosh/logs/'; # cgiファイルとデータのある場所 $mailaddress = 'mail@hogehoge.com'; # メールの宛先 $subject = 'notification from web server'; # メールのタイトル $msgfile = 'msg.txt'; # テンポラリファイル ### メイン ### &init; &compress("www1"); &compress("www2"); &sendmail; exit; ### 初めにやる処理 ### sub init { # 先月の情報を取得 ($month, $year) = (localtime(time))[4,5]; $year += 1900; if ($month == 0) { $month = 12; $year--; } # ファイルを削除 if (-e $msgfile) { unlink $msgfile; } } ### 各種コマンド実行 (wc, tar, rm) ### sub compress { my $source, $target, $cmd; $source = sprintf("%s%s/access_log.%4d%02d00.cgi", $path, $_[0], $year, $month); $source2 = sprintf("access_log.%4d%02d00.cgi", $year, $month); $target = sprintf("%s%s%4d%02d.tgz", $path, $_[0], $year, $month); $cmd = "wc -l $source"; system("echo \"\n[cmd]\$ $cmd\" >> $msgfile"); system("$cmd >> $msgfile 2>> $msgfile"); if (-e $target) { system("echo \"target is already exist\" >> $msgfile"); return; } $cmd = "cd $path$_[0]/; tar cvzf $target $source2"; system("echo \"[cmd]\$ $cmd\" >> $msgfile"); system("$cmd >> $msgfile 2>> $msgfile"); $cmd = "rm -f $source"; system("echo \"[cmd]\$ $cmd\" >> $msgfile"); system("$cmd >> $msgfile 2>> $msgfile"); } ### メール送信部分 ### sub sendmail { my $msg; $msg=""; open(IN, "$msgfile") || exit; while () { $msg .= $_; } close(IN); open(MAIL,"| $sendmail -t") || exit; print MAIL "From: web admin <$mailaddress>\n"; print MAIL "To: $mailaddress\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$msg"; close(MAIL); }