OpenVMS Source Code Demos
ROTATE_APACHE_LOGS
$!============================================================================================
$! Title : CSMIS$COM:rotate_apache_logs.com
$ goto skip_remarks
$! author : Neil Rieck
$! purpose: Rotate Apache log files
$! History:
$! Who When What
$! --- ---------- ----------------------------------------------------------------------------
$! NSR 2012-08-10 Original effort to rotate logs once per day
$! NSR 2012-08-11 added code to (optionally) rotate every hour
$! NSR 2012-08-14 Now on daily rotates will use yesterday's folder
$! Now purge the destingation log archive directory
$! NSR 2012-08-16 Added code to (optionally) cleanup Apache "process" logs on Sunday mornings
$! NSR 2012-08-21 Added code to (optionally) cleanup Apache "process" logs on certain months
$! Added code to (optionally) cleanup Apache "process" logs on a certain date
$! NSR 2012-10-31 Added code to (optionally) cleanup gSOAP server logs
$! NSR 2013-02-06 Added code to (optionally) delete universal logs
$! NSR 2016-02-15 Shift logic from DECNET NODENAMES to TCPIP HOSTNAMES (see notes below)
$!============================================================================================
$! starting on 2016-02-14:
$! node kawc90 will host kawc96.on.bell.ca
$! node kawc96 will host kawc0u.on.bell.ca
$! p.s. we should rename our nodes to help avoid future confusion
$!============================================================================================
$skip_remarks:
$ say "============================================================"
$ say "-i-script : ",f$environment("PROCEDURE")
$ say "============================================================"
$!------------------------------------------------------------------------------
$ run_interval = "D" ! D/aily, H/ourly
$ move_gsoap = "Y" ! Y/es, N/o
$!
$! note: maintenance is over and above log rotation (so do it rarely)
$!
$ mtce_period = "M" ! W/eekly, M/onthly, Y/early, N/ever
$ mtce_days = "SUN,XXX,XXX," ! weekday(s) of total web server shutdown
$ mtce_months = "JAN,APR,JUL,OCT" ! only valid when: mtce_period="M"
$ mtce_date = "19991231" ! only valid when: mtce_period="Y"
$ mtce_hour = "00" ! only used when : run_interval="H"
$!------------------------------------------------------------------------------
$ say :== write sys$output !
$ ask :== inquire/nopunct !
$ dq[0,8] = 34 ! "double quotes"
$ my_job :== csmis$com:rotate_apache_logs.com !
$ my_log :== csmis$log:rotate_apache_logs.log !
$ my_que :== CSMIS$BATCH !
$ my_mode = f$mode() !
$ if (P1 .eqs. "") then P1 = "BLANK" !
$ say "-i-mode: ",my_mode !
$ say "-i-P1 : ",P1 !
$!
$ if (my_mode .eqs. "INTERACTIVE") .and. (P1 .nes. "DEBUG") !
$ then !
$ goto reschedule_for_next_interval !
$ endif !
$!------------------------------------------------------------------------------
$! okay, let's clean up the apache working area
$!------------------------------------------------------------------------------
$ on error then goto fini_error !
$ set on ! prep for PROC NAME
$ if (my_mode .nes. "INTERACTIVE") !
$ then !
$ set proc/name = "ApacheRotate" ! make sure only one job is running (15 chars max)
$ endif !
$!~~~ set noon x leave error detection active for safety sake
$!
$!~~~ my_node = f$getsyi("NODENAME") !
$ my_node = "" ! zap
$ my_node = f$trnlnm("TCPIP$INET_HOST") ! all stacks set this logical)
$ if my_node .nes. "" !
$ then !
$ yada = "APACHE$COMMON:[000000.SPECIFIC."+ my_node + ".LOGS]" ! because servername is part of DNS
$ set def 'yada' !
$ goto do_it !
$ endif !
$ say "-w-oops, not meant for this node so exiting" !
$ exit ! exit without requeing (might raise an eyebrow)
$!==============================================================================
$! attempt a log file cleanup
$!==============================================================================
$do_it: !
$ if (run_interval .eqs. "D") ! daily
$ then !
$ say "-i-daily variation" !
$ day = f$cvtime("YESTERDAY",,"DAY") ! eg. 31
$ target1 = "LOG"+ day +".DIR" ! eg. LOG31.dir
$ target2 = "[.LOG"+ day +"]" ! eg. [.LOG31]
$ goto do_it_continue !
$ endif !
$ if (run_interval .eqs. "H") ! hourly
$ then !
$ say "-i-hourly variation" !
$ prev_hour_day = f$cvtime("-0-01:00:00","ABSOLUTE","DAY") !
$ target1 = "LOG"+ prev_hour_day +".DIR" ! eg. LOG01.dir
$ target2 = "[.LOG"+ prev_hour_day +"]" ! eg. [.LOG01]
$ goto do_it_continue !
$ endif !
$ say "-e-oops, run_interval: ",run_interval," is not supported" !
$ EXIT ! Danger Wil Robinson
$do_it_continue: !
$ if (f$search(target1) .eqs. "") ! if directory does not exist
$ then !
$ cre/dir 'target2' ! then create it
$ endif !
$ dir/width=file=35/siz/date *_log*.*/col=1 ! any logs here?
$ ren/log *_log*.* 'target2' ! move "open log files"
$!~~~ dir/width=file=35/siz/date *.*/col=1 !
$ pur/log/noco/before=yes 'target2' ! don't allow monthly buildup
$!
$! now ask Apache to close-then-reopen the logs files
$!
$ @apache$common:[000000]apache$setup.com ! get some symbols and logicals
$ httpd -k flush
$ httpd -k new
$ wait 0:00:02 !
$!~~~ dir/width=file=35/siz/date *.*/col=1 !
$!------------------------------------------------------------------------------
$! there might be gSOAP server logs here
$!------------------------------------------------------------------------------
$ if (move_gsoap .eqs. "Y") ! if this option is enabled
$ then !
$ say "-i-doing gSOAP support" !
$ target1 = "[-]sent.log" !
$ if (f$search(target1) .nes. "") ! if file exists
$ then !
$ ren/log 'target1' 'target2' ! move gSOAP log file
$ endif !
$ target1 = "[-]recv.log" !
$ if (f$search(target1) .nes. "") ! if file exists
$ then !
$ ren/log 'target1' 'target2' ! move gSOAP log file
$ endif !
$ target1 = "[-]test.log" !
$ if (f$search(target1) .nes. "") ! if file exists
$ then !
$ ren/log 'target1' 'target2' ! move gSOAP log file
$ endif !
$ endif !
$!==============================================================================
$! (optional) weekly/monthly maintenance
$! will: shut down apache
$! delete apache "process" logs + scripts (not client logs)
$! start apache
$!==============================================================================
$ day_name = f$edit(f$extract(0,3,f$cvtime(,,"WEEKDAY")),"UPCASE") ! current day name
$ day_num = f$integer(f$cvtime(,,"DAY")) ! current day number
$!
$ if (mtce_period .eqs. "M") ! M/onthly
$ then !
$ if (day_num .ge. 8) then goto skip_mtce ! we only work in week #1
$ month_num = f$integer(f$cvtime(,,"MONTH")) ! eg. 8
$ month_list = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC" !
$ month_name = f$extract((month_num-1)*4,3, month_list) ! eg. AUG
$ if (f$locate(mtce_days,day_name) .eqs. f$length(mtce_days)) then goto skip_mtce
$ goto start_mtce !
$ endif !
$ if (mtce_period .eqs. "W") ! W/eekly
$ then !
$ if (f$locate(mtce_days,day_name) .eqs. f$length(mtce_days)) then goto skip_mtce
$ goto start_mtce !
$ endif !
$ if (mtce_period .eqs. "Y") ! Y/early
$ then !
$ curr_date = f$cvtime() ! eg. 2012-08-21 13:41:07.09
$ curr_date = curr_date -"-" -"-" ! remove dashes
$ curr_date = f$extract(0,8,curr_date) ! eg. 20120821
$ if (f$length(mtce_date) .ne. 8) then goto skip_mtce ! Danger Wil Robinson
$ if (mtce_date .nes. curr_date) then goto skip_mtce !
$ goto start_mtce !
$ endif !
$ say "-w-oops, mtce_period: ",mtce_period," is not supported " !
$ goto skip_mtce !
$start_mtce: !
$ if (run_interval .eqs. "H") ! if this job is running hourly
$ then !
$ cur_hour = f$cvtime(,,"HOUR") ! get current hour
$ if (cur_hour .nes. mtce_hour) then skip_mtce ! only do once today at the desired hour
$ endif !
$ say "-i-starting periodic maintenance" !
$ set def [-] ! eg. APACHE$COMMON:[000000.SPECIFIC.KAWC15]
$ sh def !
$ set proc/priv=all !
$ set noon ! no stopping after this point
$ @SYS$STARTUP:APACHE$SHUTDOWN ! stop apache
$ wait 0:00:05 ! wait 5 seconds
$ del APACHE$SWS*.log;*/log/noco ! delete all apache process logs
$ del APACHE$SWS*.com;*/log/noco ! delete all apache startup scripts
$ del *universal*.log;*/log/noco ! delete all universal logs
$ @SYS$STARTUP:APACHE$STARTUP ! start apache
$ wait 0:00:02 ! wait 2 seconds
$!--------------------------------------------------------------
$skip_mtce: !
$!==============================================================
$! <<< resubmit to batch queue >>>
$!==============================================================
$reschedule_for_next_interval:
$ if (run_interval .eqs. "D") then goto reschedule_for_daily
$reschedule_for_next_hour:
$ say "-i-next hour logic"
$ next_hour_date = f$cvtime("+0-01:00:00","ABSOLUTE","DATE")
$ next_hour_hour = f$cvtime("+0-01:00:00","ABSOLUTE","HOUR")
$ next_time = dq +next_hour_date +" "+ next_hour_hour +":00:01" +dq
$ goto reschedule_common
$reschedule_for_daily:
$ say "-i-next day logic"
$ next_time = dq + f$cvtime("TOMORROW","ABSOLUTE","DATE") +" 00:00:01"+ dq
$reschedule_common:
$ say "-i-Scheduling to run at ", next_time"
$ submit 'my_job' -
/que='my_que' -
/after='next_time' -
/log='my_log' -
/user=Custodian -
/name="ROTATE_APACHE_LOGS" -
/NOprint
$ pur/nolog/keep=2/noco 'my_log'
$!==============================================================
$! exit(s)
$!==============================================================
$fini:
$ say "-i-normal exit"
$ exit
$fini_error:
$ say "-w-exiting (without reque) due to error"
$ set nover
$ exit