Olá pessoal,

Tudo bem?

Há alguns anos desenvolvi um script para remover traces antigos, bem como compactar o log do listener.

A premissa é que o usuário oracle (ou qualquer outro com o mesmo tipo de privilégio) seja o dono de todos os objetos (listener e bancos de dados).

Implementei esse script em alguns clientes no passado, no entanto, cabe o reforço, TESTE ele antes de colocar em ambientes Produtivos.

Ele é totalmente dinâmico, pegará todos os Oracle Homes e paths usando como ponto de início o oraInventory.

 

O script se chama purge_oracle_files.sh

#!/bin/bash

# Obtain Oracle Inventory Location and create a file

rm -f oraInventory.loc 2>/dev/null
inventory_location=`cat oraInventory.loc`
cat /etc/oraInst.loc | grep "loc" | cut -d "=" -f2 >> $inventory_location


# Creates the log dir

mkdir log 2>/dev/null

# Obtain Oracle Home Names and Locations and create a separated file for each Oracle Home
rm -f oracle_homes *.oh 2>/dev/null
cat ${inventory_location}/ContentsXML/inventory.xml | grep "HOME NAME" | awk '{print $2" "$3}' | grep -v "PLUGIN" |grep -v "agent" | grep -v "REMOVED" | sed 's/NAME=\"//' | sed 's/LOC=\"//' | sed 's/\"//g' >> oracle_homes
cat oracle_homes | while read line; do name=`echo $line | awk '{print $1}'`; touch $name.oh; echo $line | awk '{print $2}' > $name.oh; done

# Convert days in minutes - Required by ADRCI
minutes=$(expr $1 \* 24 \* 60)
logfile=`echo $name.$data.log`


# Create Shell Scripts and Execute them
ls -l *.oh | awk '{print $9}' | while read oh
do
name=`echo ${oh} | cut -d "." -f1`
data=`date +%Y%m%d`
shell=`echo ${oh}.sh | sed 's/.oh//'`
logfile=`echo $name.$data.log`
rm $shell 2>/dev/null
touch $shell
touch log/$logfile
export ORACLE_HOME=`cat $oh`
export PATH=$ORACLE_HOME/bin:/bin:/usr/bin:$PATH
if [ -d "$ORACLE_HOME" ] 
then
LISTENERS=`$ORACLE_HOME/bin/adrci exec="show homes"| grep -v ":" | grep "listener" | grep -v grep | wc -l`
if [[ $LISTENERS -gt 0 ]]
then
ps -ef |grep tns | grep -v grep | grep "LISTENER" | awk '{print $9}' | while read listener
do
echo "lsnrctl <<EOF" >> log_status_off.sh
echo "set current_listener $listener" >> log_status_off.sh
echo "set log_status off" >> log_status_off.sh
echo "EOF" >> log_status_off.sh
done
ps -ef |grep tns | grep -v grep | grep "LISTENER" | awk '{print $9}' | while read listener
do
echo "lsnrctl <<EOF" >> log_status_on.sh
echo "set current_listener $listener" >> log_status_on.sh
echo "set log_status on" >> log_status_on.sh
echo "EOF" >> log_status_on.sh
done
ps -ef |grep tns | grep -v grep | grep "LISTENER" | awk '{print $9}' | while read listener
do
echo "cd" `lsnrctl status $listener | grep "Listener Log" | awk '{print $4}' | sed 's/\/alert\/log.xml/\/trace/g'` >> gzip_listener.sh
echo "gzip *.log" >> gzip_listener.sh
done
$ORACLE_HOME/bin/adrci exec="show homes"| grep -v ":" | grep -v "listener" | while read homepath
do
echo "echo \"Realizando expurgo do Home Path: $homepath\"" >> $shell
echo "$ORACLE_HOME/bin/adrci exec=\"set echo ON; spool log/$logfile append; set homepath $homepath; purge -age $minutes\" >> log/$logfile" >> $shell
#break
done
else
export ORACLE_HOME=`cat $oh` >> $shell
export PATH=$ORACLE_HOME/bin:$PATH >> $shell
echo "export ORACLE_HOME=$ORACLE_HOME" >> $shell
echo "export PATH=$PATH" >> $shell
echo "logfile=log/$logfile" >> $shell
$ORACLE_HOME/bin/adrci exec="show homes"| grep -v ":" | while read homepath
do
echo "echo \"Realizando expurgo do Home Path: $homepath\"" >> $shell
echo "$ORACLE_HOME/bin/adrci exec=\"set echo ON; spool log/$logfile append; set homepath $homepath; purge -age $minutes\" >> log/$logfile" >> $shell
break
done
fi
else
echo "Oracle Home registrado no inventario e inexistente no servidor: $name: $ORACLE_HOME" >> log/$logfile 2>/dev/null
rm $shell 2>/dev/null
fi
chmod +x $shell 2>/dev/null
chmod +x log_status*.sh 2>/dev/null
chmod +x gzip_listener.sh 2>/dev/null
# sh $shell >> log/$logfile 2>/dev/null
sh log_status_off.sh 2>/dev/null
sh gzip_listener.sh 2>/dev/null
sh log_status_on.sh 2>/dev/null
done

 

A execução dele é bem simples:

./purge_oracle_files 10

Onde:

10 é o número de dias que eu quero manter nos meus diretórios.

Essa regra não vale para o(s) log(s) do(s) listener(s). Para o(s) log(s) do(s) listener(s) ele sempre compactará o log atual e iniciará um novo.

 

Espero que seja útil!

 

Um abraço!

 

Vinicius