<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Linux環境下Tomcat catalina.out的管理"]]></title>
		<link>https://forum.andowson.com/posts/list/20.page</link>
		<description><![CDATA[Latest messages posted in the topic "Linux環境下Tomcat catalina.out的管理"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Linux環境下Tomcat catalina.out的管理</title>
				<description><![CDATA[ 最近碰到了一個資安的角色分隔(SOD)要求:程式設計師不能擁有線上主機的帳號，但如果程式設計師想要看AP Server的log時，不能登入就很困擾，這時要改由系統管理者或是電腦操作人員OP手動去email轉寄。在Linux環境指令通常如下: 
<br>
[code=plain]mail -s "Catalina.out" developer@andowson.com &lt; /var/robust/worker1/logs/catalina.out[/code] 
<br>
不過Tomcat的catalina.out會不斷長大，經過日積月累這檔案可能會大到好幾GB，用email來寄送可能有灌爆信箱的風險。還好Linux上面還有個logrotate的log檔案輪替機制，可以讓我們設定一段時間後自動去切換一下這個檔案，例如我們可以設計成每日切換一次，然後保留兩年(365+366=731天)，每次切換時將檔案名稱改為日期結尾並順便把昨天的log檔email給程式設計師: 
<br>
/etc/logrotate.d/tomcat: 
<br>
[code=plain]/var/robust/worker1/logs/catalina.out { 
<br>
 rotate 731 
<br>
 daily 
<br>
 copytruncate 
<br>
 dateext 
<br>
 mail developer 
<br>
 mailfirst 
<br>
 maxage 731 
<br>
 missingok 
<br>
 notifempty 
<br>
 nocompress 
<br>
}[/code] 
<br>
這裡可以透過設定/etc/aliases來寄送給多位程式設計師: 
<br>
[code]developer: johndoe@andowson.com, marydoe@andowson.com[/code] 
<br>
每次有修改/etc/aliases後記得要執行一下newaliases這個指令才會生效。 
<br>
<br>
接著我們可以用logrotate -d /etc/logrotate.d/tomcat這個指令來檢查一下: 
<br>
[code][root@www logrotate.d]# logrotate -d /etc/logrotate.d/tomcat 
<br>
reading config file /etc/logrotate.d/tomcat 
<br>
reading config info for /var/robust/worker1/logs/catalina.out 
<br>
<br>
Handling 1 logs 
<br>
<br>
rotating pattern: /var/robust/worker1/logs/catalina.out after 1 days (731 rotations) 
<br>
empty log files are not rotated, old logs mailed to developer 
<br>
considering log /var/robust/worker1/logs/catalina.out 
<br>
 log does not need rotating 
<br>
[root@www logrotate.d]#[/code] 
<br>
沒有錯誤的話就大致完成了，由於Linux一般預設會自動執行logrotate，所以我們不用特別去加設一個cron作業。 
<br>
<br>
注意: 
<br>
請先檢查一下您系統上目前的catalina.out大小，如果像我們是已經都上GB了，第一次要切換這個檔案前，請先將剛剛/etc/logrotate.d/tomcat裡面的這幾行先disable一下: 
<br>
[code]/var/robust/worker1/logs/catalina.out { 
<br>
 rotate 731 
<br>
 daily 
<br>
 copytruncate 
<br>
 dateext 
<br>
 #mail developer 
<br>
 #mailfirst 
<br>
 maxage 731 
<br>
 missingok 
<br>
 notifempty 
<br>
 nocompress 
<br>
}[/code] 
<br>
等第二天確認檔案有切換過去(有多產生一個catalina.out-yyyymmdd的檔案例如catalina.out-20120801)，再將#刪除即可。]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/586/1238.page</guid>
				<link>https://forum.andowson.com/posts/preList/586/1238.page</link>
				<pubDate><![CDATA[Wed, 1 Aug 2012 23:50:06]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:Linux環境下Tomcat catalina.out的管理</title>
				<description><![CDATA[ 第二天: 
<br>
如果順利切換過去，接下來就是將先前/etc/logrotate.d/tomcat中mark掉的mail developer等設定恢復。 
<br>
<br>
再來可能會遇到的問題就是，昨天logrotate後，那個20幾GB的catalina.out現在只是檔名尾巴加上日期，大小還是20幾GB大。如果我們希望也能夠將它切一切，也一樣照日期分成一個一個檔，那應該如何處理呢? 
<br>
<br>
由於沒有現成的工具程式(或是我不知道有這種工具)可以處理catalina.out-20120801，於是我先土法煉鋼來處理這個切檔的工作: 
<br>
先用vi開啟這個檔案(是的，20幾GB)，然後找到昨天的日期第一次出現在這個檔案的行數，再找出檔案尾巴的行數，將這個區塊寫到另一個檔案去，例如catalina.out-20120731，再將原先的catalina.out-20120801檔案刪掉該區塊的內容。這樣子重複執行直到將檔案全部切好為止。 
<br>
<br>
我手動切了三個日期後，就覺得這樣子下去不是辦法，應該可以寫個shell script來處理這件事: 
<br>
1.使用grep -n指令搭配head -1來找出某個日期第一次出現在檔案中的行號: 
<br>
grep -n "2012/7/31" catalina.out-20120801|head -1|awk '{print $1}'|cut -d":" -f1 
<br>
2.然後利用wc -l 來找出檔案尾巴行數: 
<br>
wc -l catalina.out-20120801|awk '{print $1}' 
<br>
3.接下來利用expr將兩個行號相減再加一: 
<br>
expr $end - $start + 1 
<br>
4.再利用tail 指令搭配行數，將自檔尾倒數幾行寫到檔案catalina.out-20120731去 
<br>
tail -$lines catalina.out-20120801 &gt; catalina.out-20120731 
<br>
5.最後再利用sed指令去刪掉剛剛自起始行號到結尾行號中間的檔案內容 
<br>
sed -i -e "${start},${end}d" catalina.out-20120801 
<br>
<br>
搭配一下日期年月日的迴圈後可以組成如下: 
<br>
split_catalina_out.sh: 
<br>
[code]#!/bin/bash 
<br>
file=catalina.out-20120801 
<br>
for y in {2012..2011} 
<br>
do 
<br>
 for m in {12..1} 
<br>
 do 
<br>
 for d in {31..1} 
<br>
 do 
<br>
 target="$y/$m/$d" 
<br>
 target_ym="${y}${m}" 
<br>
 if [ $m -lt 10 ]; then 
<br>
 target_ym="${y}0${m}" 
<br>
 fi 
<br>
 target_date="${target_ym}${d}" 
<br>
 if [ $d -lt 10 ]; then 
<br>
 target_date="${target_ym}0${d}" 
<br>
 fi 
<br>
<br>
 start=`grep -n $target $file|head -1|awk '{print $1}'|cut -d":" -f1` 
<br>
 if [ "$start" == "" ]; then 
<br>
 continue 
<br>
 fi 
<br>
 end=`wc -l $file|awk '{print $1}'` 
<br>
 lines=`expr $end - $start + 1` 
<br>
 echo $target_date 
<br>
 tail -$lines $file &gt; ./catalina.out-$target_date 
<br>
 sed -i -e "${start},${end}d" $file 
<br>
 done 
<br>
 done 
<br>
done[/code] 
<br>
<br>
建議先複製一份catalina.out-20120801到/tmp目錄，測試看看，以免毀了原始紀錄檔。將split_catalina_out.sh放在和catalina.out-20120801相同的目錄下，執行時程式會自動判斷該日期是否存在於檔案中，如不存在將自動跳過。如此不斷重複直到回圈執行完畢。]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/586/1239.page</guid>
				<link>https://forum.andowson.com/posts/preList/586/1239.page</link>
				<pubDate><![CDATA[Thu, 2 Aug 2012 00:26:14]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>