最近linux服務器增加至60多臺,服務器上的日誌增多是個麻煩事。以前是每臺服務器上放一個日誌處理腳本,但隨着服務器增加速度變快,部署相對麻煩。有時候一天增加一兩臺服務器,手頭一忙,往往就忘記增加腳本,導致硬盤空間被日誌大量佔用。所以寫了一個批處理日誌處理,署就方便得多了,增減服務器只需要在ip.txt裏改動即可。(密鑰對配置好的前提)
#!/bin/bash
array=(`cat /usr/local/dsh/etc/ip.txt`)
i=0
while [ $i -lt ${#array[@]} ]
do
j=0
host=${array[$i]}
logfile=(`dsh -r ssh -m $host -- find /var/log -size +5M| grep -E '\/[a-z]*\/[a-z]*\/[a-z]*\.[1-9]*'`)
while [ $j -lt ${#logfile[@]} ]
do
dsh -r ssh -m $host -- rm ${logfile[$j]}
echo ${logfile[$j]}
j=$((j+1))
done
i=$((i+1))
done