REM =============================================================================
REM # Listener Text Log Rotation and XML Log Wiping out
REM # Author: Amos Geng@Emeralit.com
REM # Date: 02:53 PM EST, 03/03/2016
REM # Platform: Windows 2008 R2 Enterprise Edition
REM # Description: 
REM #	<1> Rotate the listener log. 	
REM # 	<2> 7 zip to compress the rotated log.
REM #	<3> Wiping out the XML format listener log by retention policy. 
REM #	<4> Required parameters in micore_env.bat: 	
REM # 			a) LISTENER_NAME, LISTENER_LOG_LOCATION, LISTENER_LOG_RETENTION
REM #			b) LISTENER_LOG_LOCATION_XML, LISTENER_LOG_RETENTION_XML
REM # 			c) ZIP_EXE_LOCATION, ORACLE_HOME, LOG_LOCATION
REM # 	<5> Enhencement in next version: 
REM #			a) Only Oracle version 11g+ compatiable.
REM # 			b) Only fitting for single listener server.   
REM =============================================================================

@echo on

goto main

:func_set_variable
call G:\micore\scripts\micore_env.bat
set filedatetime=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set filedatetime=%filedatetime: =%
set script_name=micore_rotate_listener
set script_ver=1.0
set status_log=%LOG_LOCATION%\listener_log_rotation_%filedatetime%.log
goto:EOF


:func_log_header
echo ======================================= >  %status_log%
echo * MiCORE Listener Log Rotation          >> %status_log%
echo * Date: %filedatetime%                  >> %status_log%
echo * Server: %computername%                >> %status_log%
echo ======================================= >> %status_log% 
echo.>> %status_log%
echo.>> %status_log%			  
goto:EOF


:func_rotate_listener_log
set LISTENER_NAME=%~1

echo ***** Rotating Listener Log *****                                                                                       >> %status_log%

REM === Step 1 ===
echo Step 1 - Setting current listener name %LISTENER_NAME% ...                                                              >> %status_log%
%ORACLE_HOME%\bin\lsnrctl set current_listener %LISTENER_NAME%

REM === Step 2 ===
echo Step 2 - Setting listener log status off ...                                                                            >> %status_log%
%ORACLE_HOME%\bin\lsnrctl set log_status off

REM === Step 3 ===
echo Step 3 - Rotating current listener log via renaming ...                                                                 >> %status_log%
set "rotated_file_name=%LISTENER_NAME%_%filedatetime%.log"
ren %LISTENER_LOG_LOCATION%\%LISTENER_NAME%.log %rotated_file_name%

REM === Step 4 ===
echo Step 4 - Setting listener log status on ...                                                                             >> %status_log%
%ORACLE_HOME%\bin\lsnrctl set log_status on

REM === Step 5 ===
echo Step 5 - Zipping the rotated listener log ...                                                                           >> %status_log%
"%ZIP_EXE_LOCATION%"\7z a -tzip %LISTENER_LOG_LOCATION%\%rotated_file_name%.zip %LISTENER_LOG_LOCATION%\%rotated_file_name%

REM === Step 6 ===															
echo Step 6 - Zipping done, and deleting the rotated listener log ...                                                        >> %status_log%
del %LISTENER_LOG_LOCATION%\%rotated_file_name%

REM === Step 7 ===
echo Step 7 - Wiping out the expired zipped listener log ...                                                                 >> %status_log%
forfiles /p "%LISTENER_LOG_LOCATION%" /m %%i_*.zip /d -%LISTENER_LOG_RETENTION% /c "CMD /C del @FILE"

REM === Step 8 ===
echo Step 8 - Rotation done.                                                                                                 >> %status_log%

echo.                                                                                                                        >> %status_log%
echo.                                                                                                                        >> %status_log%
echo.                                                                                                                        >> %status_log%
echo ***** Oracle %LISTENER_NAME% Status After Rotation *****                                                                >> %status_log%
%ORACLE_HOME%\bin\lsnrctl status                                                                                             >> %status_log%
echo.                                                                                                                        >> %status_log%
echo.                                                                                                                        >> %status_log%
echo.                                                                                                                        >> %status_log%
goto:EOF


:func_cleanup_listener_log_xml
echo ***** Wiping out the XML Listener Log by Retention *****                                                                >> %status_log%
forfiles /p "%LISTENER_LOG_LOCATION_XML%" /m *.xml /d -%LISTENER_LOG_RETENTION_XML% /c "CMD /C del @FILE"
echo XML format listener log has been cleaned out via retention policy                                                       >> %status_log%
echo.                                                                                                                        >> %status_log%
goto:EOF


:func_email_log
echo to: informative_support@micoresolutions.com > %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
echo from: lyceumrobotic@benepaytech.com >> %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
echo subject: Informative - %CLIENT_NAME%:%HOST%:Listener Log Rotation Completed >> %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
echo.>> %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
type %status_log% >> %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
echo.>> %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
echo This email was sent by MiCORE maintenance script %script_name% version %script_ver% >> %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
Msmtp -t < %LOG_LOCATION%\listener_log_email_%filedatetime%.txt
goto:EOF


:main
call :func_set_variable
call :func_log_header

for %%i in  (%LISTENER_NAME%) do (
	call :func_rotate_listener_log %%i
)

call :func_cleanup_listener_log_xml
call :func_email_log
goto:EOF