@echo off
REM DProc.cmd
REM Written by Jeremy Carter
REM Daily Processing for BI Production
REM
REM 0.0.0 - 12/02/08 - Started work on script
REM 0.1.0 - 12/03/08 - Backup portion complete
REM 0.2.0 - 12/04/08 - Extract portion complete
REM 0.3.0 - 12/09/08 - Added Check for completed pull
REM Moved Prep variables to external .ini file
REM 0.4.0 - 01/20/08 - Added code to execute EP Macro
REM 0.5.0 - 01/20/08 - Added Summary subroutine
REM 1.0.0 - 01/21/08 - Added code to Trigger the report run schedule
set VERSION=1.0.0
setlocal ENABLEDELAYEDEXPANSION
REM Set Global variables and other miscellaneous preparation steps for the
script
call
rep
echo.
echo.Log File: %DPROC_LOG%
goto :Skip1
REM Step 1 - Check services to make sure they are all running
call :Log SUBSTART STARTING :SERVICECHECK SUBROUTINE
call :ServiceCheck
if errorlevel 1 (
call :Log SUBEND :SERVICECHECK SUBROUTINE FAILED
call :Log LOG ****************************
call :Log LOG * *
call :Log LOG *!!!SERVICE CHECK FAILED!!!*
call :Log LOG * *
call :Log LOG * Review Log and perform *
call :Log LOG * corrective action *
call :Log LOG * *
call :Log LOG ****************************
goto :Summary
) ELSE (
call :Log SUBEND :SERVICECHECK SUBROUTINE FINISHED SUCCESSFULLY
)
:Skip1
REM Step 2 - Wait for daily pull to complete
call :Log SUBSTART STARTING :CHECK4PULL SUBROUTINE
call :Check4Pull
if errorlevel 1 (
call :Log SUBEND :CHECK4PULL SUBROUTINE FAILED
call :Log LOG ****************************
call :Log LOG * *
call :Log LOG * !!!CHECK PULL FAILED!!! *
call :Log LOG * *
call :Log LOG * Review Log and perform *
call :Log LOG * corrective action *
call :Log LOG * *
call :Log LOG ****************************
goto :Summary
) ELSE (
call :Log SUBEND :CHECK4PULL SUBROUTINE FINISHED SUCCESSFULLY
)
REM Step 3 - Move files from extract location to backup folder - 5 files
every day
call :Log SUBSTART STARTING :BACKUP SUBROUTINE
call :Backup
if errorlevel 1 (
call :Log SUBEND :BACKUP SUBROUTINE FINISHED WITH NON-FATAL ERRORS
) ELSE (
call :Log SUBEND :BACKUP SUBROUTINE FINISHED SUCCESSFULLY
)
REM Step 4 - Create new extract files
call :Log SUBSTART STARTING :EXTRACT SUBROUTINE
call :Extract
if errorlevel 1 (
call :Log SUBEND :EXTRACT SUBROUTINE FAILED
call :Log LOG ****************************
call :Log LOG * *
call :Log LOG * !!!EXTRACT FAILED!!! *
call :Log LOG * *
call :Log LOG * Review Log and perform *
call :Log LOG * corrective action *
call :Log LOG * *
call :Log LOG ****************************
goto :Summary
) ELSE (
call :Log SUBEND :EXTRACT SUBROUTINE FINISHED SUCCESSFULLY
)
REM Step 5 - Kick off Macro in EP.
call :Log SUBSTART STARTING :EPMacro1 SUBROUTINE
call :EPMacro1
if errorlevel 1 (
call :Log SUBEND :EPMacro1 SUBROUTINE FAILED with Error Level: %ERRORLEVEL%
call :Log LOG ****************************
call :Log LOG * *
call :Log LOG * !!!MACRO FAILED!!! *
call :Log LOG * *
call :Log LOG * Review Log and perform *
call :Log LOG * corrective action *
call :Log LOG * *
call :Log LOG ****************************
goto :Summary
) ELSE (
call :Log SUBEND :EPMacro1 SUBROUTINE FINISHED SUCCESSFULLY
)
REM Step 6 - Run ETL script
call :Log SUBSTART STARTING :RunETL SUBROUTINE
call :RunETL
if errorlevel 1 (
call :Log SUBEND :RunETL SUBROUTINE FAILED with Error Level: %ERRORLEVEL%
call :Log LOG ****************************
call :Log LOG * *
call :Log LOG * !!!ETL FAILED!!! *
call :Log LOG * *
call :Log LOG * Review Log and perform *
call :Log LOG * corrective action *
call :Log LOG * *
call :Log LOG ****************************
goto :Summary
) ELSE (
call :Log SUBEND :RunETL SUBROUTINE FINISHED SUCCESSFULLY
)
REM Step 7 - Kick off Report job.
call :Log SUBSTART STARTING :RunReports SUBROUTINE
call :RunReports
if errorlevel 1 (
call :Log SUBEND :RunReports SUBROUTINE FAILED with Error Level:
%ERRORLEVEL%
call :Log LOG ****************************
call :Log LOG * *
call :Log LOG * !!!REPORT RUN FAILED!!! *
call :Log LOG * *
call :Log LOG * Review Log and perform *
call :Log LOG * corrective action *
call :Log LOG * *
call :Log LOG ****************************
goto :Summary
) ELSE (
call :Log SUBEND :RunReports SUBROUTINE FINISHED SUCCESSFULLY
)
REM Step 8 - Summary
call :Log SUBSTART STARTING :SUMMARY SUBROUTINE
set STATUS=COMPLETE
call :Summary
if errorlevel 1 (
call :Log SUBEND :SUMMARY SUBROUTINE FINISHED WITH NON-FATAL ERRORS
) ELSE (
call :Log SUBEND :SUMMARY SUBROUTINE FINISHED SUCCESSFULLY
)
goto :EOF
rep
cd /d "%~dp0"
set MYCD=%CD%
for /f "delims== tokens=1,* eol=;" %%A in (DProc.ini) do set %%A=%%B
if not exist "%CD%\LOG" md "%CD%\LOG"
if not exist "%EXTRACTLOC%" md "%EXTRACTLOC%"
call :Log INTRO
exit /b 0
:ServiceCheck
for /f "delims== tokens=1,* eol=;" %%A in (Services.txt) do (
if "%%A" == "SERVER" set SVR=%%B
if "%%A" == "SERVICE" (
sc \\!SVR! query "%%B" | find "RUNNING"
if ERRORLEVEL 1 (
call :Log LOG %%B Service not started on !SVR! -- Starting
sc \\!SVR! start "%%B" | find "START_PENDING"
if ERRORLEVEL 1 (
call :Log LOG Unable to start %%B service on !SVR!
exit /b 1
)
)
)
)
exit /b 0
:Check4Pull
call %PULLSCRIPT%
if "%ERRORLEVEL%" == "0" (
call :Log LOG Database reports "SUCCESS" for Daily Pull
exit /b 0
)
if "%ERRORLEVEL%" == "10" (
call :Log LOG CheckPull.ini file missing - Unable to check for pull
completion
exit /b 1
)
if "%ERRORLEVEL%" == "20" (
call :Log LOG Unable to create temporary script to check for pull completion
exit /b 1
)
if "%ERRORLEVEL%" == "30" (
call :Log LOG Unable to locate sqlplus.exe - Unable to check for pull
completion
exit /b 1
)
if "%ERRORLEVEL%" == "40" (
call :Log LOG sqlplus did not respond in a timely manner - Unable to check
for pull completion
exit /b 1
)
if "%ERRORLEVEL%" == "50" (
call :Log LOG sqlplus did not respond with a recognized Status in the run
window - Unable to check for pull completion
exit /b 1
)
if "%ERRORLEVEL%" == "100" (
call :Log LOG Database reports "FAIL" for Daily Pull
exit /b 1
)
call :Log LOG Unknown error code: %ERRORLEVEL% - Unable to check for pull
completion
exit /b 1
:Backup
call :Log LOG Extract Location: %EXTRACTLOC%
call :Log LOG Backup Location: %BACKUPLOC%
for /l %%A in (1,1,5) do if not exist "!FILE%%A!" set
BACKUPERROR=!BACKUPERROR!, !FILE%%A! doesn't exist
if "%BACKUPERROR%" == "" (
call :Log LOG All five extract files exist, moving to backup location
)
if not exist "%BACKUPLOC%" md "%BACKUPLOC%"
set BACKUPERROR=
for /l %%A in (1,1,5) do (
move "!FILE%%A!" "%BACKUPLOC%">nul2>nul
if not "!ERRORLEVEL!" == "0" set BACKUPERROR=!BACKUPERROR!, Unable to move
!FILE%%A!
)
if "!BACKUPERROR!" == "" (
call :Log LOG All five extract files moved successfully
exit /b 0
) ELSE (
call :Log LOG Errors in Backup Subroutine:
call :Log LOG %BACKUPERROR:~2%
exit /b 1
)
:Extract
REM Verify Script directory exists and is readable
cd /d %SQLSCRIPTS%
if /i not "%CD%" == "%SQLSCRIPTS%" (
call :Log LOG Unable to change directory to SQL Scripts directory:
call :Log LOG %SQLSCRIPTS%
exit /b 1
)
REM Create temporary sqlplus script to run extract script then quit
echo.@%EXTRACTSCRIPT%>DProc.sql
echo.quit>>DProc.sql
REM Verify needed files exist
if not exist "DProc.sql" (
call :Log LOG Unable to create temporary script in SQL Scripts directory:
call :Log LOG %SQLSCRIPTS%
exit /b 1
)
if not exist "%EXTRACTSCRIPT%" (
call :Log LOG Daily Processing Extract script does not exist:
call :Log LOG %SQLSCRIPTS%\%EXTRACTSCRIPT%
exit /b 1
)
call :Log LOG Daily Processing Extract script exists -- continuing
for /f %%A in ("sqlplus.exe") do (
if not exist "%%~f$PATH:A" (
call :Log LOG Unable to locate sqlplus.exe -- Not in current directory
or in path
exit /b 1
)
)
REM Run sqlplus
call :Log LOG sqlplus command line: sqlplus.exe %EXTRACTSWITCH%
%EXTRACTUSER%/***password***@%EXTRACTSERVER% @DProc.sql
start "SQL Plus -- Daily Extract" /min /D"%SQLSCRIPTS%" "%COMSPEC%" /c
sqlplus.exe %EXTRACTSWITCH% %EXTRACTUSER%/!EXTRACTPASS!@%EXTRACTSERVER%
@DProc.sql
REM Wait for sqlplus to complete
set WAITED=0
:Wait4SQLPlus1
if /i %WAITED% GEQ %MAXSPWAIT% (
call :Log LOG sqlplus command has taken longer than %MAXSPWAIT% seconds
to complete
exit /b 1
)
%MYCD%\sleep 10
set /a WAITED=%WAITED% + 10
tasklist | find /i "sqlplus.exe">nul2>nul && goto :Wait4SQLPlus1
call :Log LOG sqlplus command completed in less than %WAITED% seconds
REM Verify that the extract ran successfully
if errorlevel 1 (
call :Log LOG sqlplus exited with an unsuccessful error code: %ERRORLEVEL%
call :Log LOG Continuing with check of extract files
)
for /l %%A in (1,1,5) do if not exist "!FILE%%A!" set
EXTRACTERROR=!EXTRACTERROR!, !FILE%%A! doesn't exist
if not "%EXTRACTERROR%" == "" (
call :Log LOG One or more extract files missing:
call :Log LOG %EXTRACTERROR:~2%
exit /b 1
)
call :Log LOG All extract files exist
exit /b 0
:EPMacro1
REM Run Macro
"%MACRODIR%" /Macro=%MACRONAME1%
call :Log LOG Sent call to start Macro -- Waiting for it to complete
REM Wait for Macro to complete
set WAITED=0
:Wait4Macro1
if /i %WAITED% GEQ %MAXMACROWAIT% (
call :Log LOG Macro has taken longer than %MAXMACROWAIT% seconds to
complete
exit /b 1
)
%MYCD%\sleep 10
set /a WAITED=%WAITED% + 10
if not exist %MYCD%\Daily.Flag goto :Wait4Macro1
del %MYCD%\Daily.Flag
call :Log LOG Macro completed in less than %WAITED% seconds
exit /b 0
:RunETL
REM Verify Script directory exists and is readable
cd /d %SQLSCRIPTS%
if /i not "%CD%" == "%SQLSCRIPTS%" (
call :Log LOG Unable to change directory to SQL Scripts directory:
call :Log LOG %SQLSCRIPTS%
exit /b 1
)
REM Create temporary sqlplus script to run ETL script then quit
echo.@%ETLSCRIPT%>DProc.sql
echo.quit>>DProc.sql
REM Verify needed files exist
if not exist "DProc.sql" (
call :Log LOG Unable to create temporary script in SQL Scripts directory:
call :Log LOG %SQLSCRIPTS%
exit /b 1
)
if not exist "%ETLSCRIPT%" (
call :Log LOG Daily Processing ETL script does not exist:
call :Log LOG %SQLSCRIPTS%\%ETLSCRIPT%
exit /b 1
)
call :Log LOG Daily Processing ETL script exists -- continuing
for /f %%A in ("sqlplus.exe") do (
if not exist "%%~f$PATH:A" (
call :Log LOG Unable to locate sqlplus.exe -- Not in current directory
or in path
exit /b 1
)
)
REM Run sqlplus
call :Log LOG sqlplus command line: sqlplus.exe %ETLSWITCH%
%ETLUSER%/***password***@%ETLSERVER% @DProc.sql
start "SQL Plus -- Daily ETL" /min /D"%SQLSCRIPTS%" "%COMSPEC%" /c
sqlplus.exe %ETLSWITCH% %ETLUSER%/!ETLPASS!@%ETLSERVER% @DProc.sql
REM Wait for sqlplus to complete
set WAITED=0
:Wait4SQLPlus2
if /i %WAITED% GEQ %MAXSPWAIT% (
call :Log LOG sqlplus command has taken longer than %MAXSPWAIT% seconds
to complete
exit /b 1
)
%MYCD%\sleep 10
set /a WAITED=%WAITED% + 10
tasklist | find /i "sqlplus.exe">nul2>nul && goto :Wait4SQLPlus2
call :Log LOG sqlplus command completed in less than %WAITED% seconds
REM Verify that the ETL ran successfully
if errorlevel 1 (
call :Log LOG sqlplus exited with an unsuccessful error code: %ERRORLEVEL%
exit /b 1
)
exit /b 0
:RunReports
call :Log LOG Running Trigger from: %TRIGGERDIR%
call :Log LOG trigger.bat %TRIGGERGATEWAY% %TRIGGERUSER% ***password***
%TRIGGERSECNS% %TRIGGERNAME%
cd /d %TRIGGERDIR%
trigger.bat %TRIGGERGATEWAY% %TRIGGERUSER% %TRIGGERPASS% %TRIGGERSECNS%
%TRIGGERNAME%
if "%ERRORLEVEL%" == "1" (
cd /d %MYCD%
exit /b 0
)
cd /d %MYCD%
exit /b 1
:Summary
cd /d %MYCD%
echo.[Server]>email.cfg
echo.SMTPSERVER=%SMTPSERVER%>>email.cfg
echo.SMTPPORT=%SMTPPORT%>>email.cfg
echo.SMTPTIMEOUT=%SMTPTIMEOUT%>>email.cfg
echo.>>email.cfg
echo.[DProc]>>email.cfg
echo.FROM=%FROM%@%COMPUTERNAME%.%USERDNSDOMAIN%>>email.cfg
echo.TO=%TO%>>email.cfg
if "%STATUS%" == "COMPLETE" (
echo.SUBJECT=Daily Processes Completed>>email.cfg
echo.BODY1=Processing completed successfully.>>email.cfg
echo.BODY2=.>>email.cfg
echo.BODY3=Check attached log for details.>>email.cfg
) ELSE (
echo.SUBJECT=Daily Processes FAILED>>email.cfg
echo.BODY1=Processing exited without finishing>>email.cfg
echo.BODY2=.>>email.cfg
echo.BODY3=Review attached Log file and perform corrective action>>email.cfg
)
cscript email.vbs email.cfg DProc "%DPROC_LOG%"
exit /b %ERRORLEVEL%
:Log
if "%1" == "INTRO" (
echo.*********************>>"%DPROC_LOG%"
echo.* DProc.cmd *>>"%DPROC_LOG%"
echo.* v%VERSION% *>>"%DPROC_LOG%"
echo.*********************>>"%DPROC_LOG%"
echo.%D_YEAR%-%D_MONTH%-%D_DAY% - %T_HOUR%:%T_MINUTE%>>"%DPROC_LOG%"
)
if "%1" == "SUBSTART" (
set CMDLINE=%*
set CMDLINE=!CMDLINE:~9!
echo.>>"%DPROC_LOG%"
echo.!CMDLINE!>>"%DPROC_LOG%"
)
if "%1" == "SUBEND" (
set CMDLINE=%*
set CMDLINE=!CMDLINE:~7!
echo.!CMDLINE!>>"%DPROC_LOG%"
)
if "%1" == "LOG" (
set CMDLINE=%*
set CMDLINE=!CMDLINE:~4!
echo. !CMDLINE!>>"%DPROC_LOG%"
)
goto :EOF