發佈日期:
分類:
如何在svn中‧定期把log記錄發送至指定電郵帳戶
要做到以上要求,第一要考慮怎樣把svn內的log內容輸出,之後作相應篩選,再把內容發送至指定電郵帳戶。
01. 建立一個script檔案,用於取得svn資料夾內所有目錄的log內容。
#!/bin/bash ## Delete current log file. rm /svn/svn_update.log ## Output the directory structure to a file. ls /svn > /svn/svn_update.tmp exec< "/svn/svn_update.tmp" ## Read every line from the directory structure file. while read line do ## Read the content of each line that starting with text /svn. if [ -d /svn/$line ] then ## Save the directory information to a file. echo /svn/$line >> /svn/svn_update.log; ## Read svn log and of desired directory and save to a file. svn log file:///svn/$line >> /svn/svn_update.log fi done chmod 777 /data/hd_status/svn_update.log ## Delete the temporary file. rm /data/hd_status/svn_update.tmp
02. 利用php程式,將上一步建立的svn.log檔案作分析,只記錄今日有更新動作的log資料,發送至指定電郵帳戶。
<?php $flag=0; $hd = "/svn/svn_update.log"; $hdw = "/svn/svn.log"; $handlew = fopen($hdw, 'w'); # Record today information. fwrite($handlew, "File is recorded at: ".date('Y-m-d G:i', filemtime( $hd ))."\\n" ); $handle = @fopen($hd, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); # Check the begining string starts from /svn/. if (substr($buffer,0,5) == "/svn/") { # If yes, write the directory name to a file. fwrite($handlew, "SVN working folder: ".$buffer ); $buffer = fgets($handle, 4096); } else { for ($i=0;$i<strlen($buffer);$i++) { if (substr($buffer,$i,1) == "|") { $flag++; # Read the second sign | of line. if ($flag == 2) { # Make sure the data is same as today. if (substr($buffer,$i+2,10) == date('Y-m-d')) { # Write the match remark to a file. fwrite($handlew, " ".$buffer); # If it is a dash line, ommit it. do { $buffer = fgets($handle, 4096); fwrite($handlew, " ".$buffer); } while (substr($buffer,0,60) != "------------------------------------------------------------"); } else { # If it is a dash line, ommit it. do { $buffer = fgets($handle, 4096); } while (substr($buffer,0,60) != "------------------------------------------------------------"); } $flag = 0; break; } } } } } fclose($handle); fwrite($handlew, "End of file"); fclose($handlew); } # Email content. $err = "<p>To whom will concern:</p><p>Please refer to the attachment for the svn update on ".date('Y-m-d').".</p><p>Regards,<br />admin</p>"; if ($err != "") { # Load phpmailer module. require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); # Set email account information. $mail->Host = "ssl://smtp.gmail.com"; $mail->Port = "465"; $mail->SMTPAuth = true; $mail->Username = "user@gmail.com"; $mail->Password = "password"; $mail->From = "user@gmail.com"; # Add recipient email. $mail->AddAddress("user1@gmail.com", "User1"); $mail->AddAddress("user2@gmail.com", "User2"); $mail->WordWrap = 99999; # Attach log file. $mail->AddAttachment("/svn/svn.log"); $mail->IsHTML(true); $mail->Subject = "svn update on ".date('Y-m-d'); $mail->Body = $err; # Send the notice email out. if(!$mail->Send()) { echo "<p class=\\"content_text\\">Message could not be sent.</p>"; echo "<p class=\\"content_text\\">Mailer Error: " . $mail->ErrorInfo . "</p>"; exit; } echo "<p class=\\"content_text\\">Message has been sent.</p>"; } else { echo "<p class=\\"content_text\\">No error found.</p>"; } ?>
發佈留言