#!/usr/bin/ksh ############################################################################################ # decomm # # Copyright: (C) 2007, Peter J. Dominey # # http://www.dominey.biz # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ############################################################################################ # Script is used to prepare for a client machine's decommission. # It gathers a list of all jobs defined for that machine, the job's defintions and # the last run dates and statuses for the jobs. # It gathers a list of any dependent jobs. # It also collects all the license keys. # All of that information is saved in a file. # The script can then (optionally) delete all the jobs and keys. # # # Peter Dominey 817-416-6345 12/19/907 # ############################################################################################ # Modifications # By On Details # # ############################################################################################ HOST=`hostname` TMPDIR=/tmp LOGDIR=/tmp SCRIPTDIR=/opt/autotree/scripts DATE=`date +%m%d%y` TIME=`date +%T` TMPFILE1=${TMPDIR}/`basename $0`.tmp1 TMPFILE2=${TMPDIR}/`basename $0`.tmp2 MAILTO='peter.dominey@wachovia.com, rusty.atkins@wachovia.com' INST_LIST='ACT MYR MIA MOB OAK SPI PAY' USAGEMSG="\nUsage: %s: [-m Machine Name][-f filename][-y]\n\tProvide either a Machine name or an Input File (list of machine names) \n\tUse -y Carry out removal of client information from Autosys]\n\n" # Remember the ERRFILE and LOGFILE here and in the Autosys job deffention must match UNIQUE=$$ ERRFILE=${LOGDIR}/${AUTO_JOB_NAME:-`basename $0`}_${AUTO_JOB_PID:-${UNIQUE}}.err LOGFILE=${LOGDIR}/${AUTO_JOB_NAME:-`basename $0`}_${AUTO_JOB_PID:-${UNIQUE}}.log # or if a non unique file is need each time # Remember the ERRFILE and LOGFILE here and in the Autosys job deffention must match #ERRFILE=${LOGDIR}/${AUTO_JOB_NAME:-`basename $0`}_0.err #LOGFILE=${LOGDIR}/${AUTO_JOB_NAME:-`basename $0`}_0.log ############################################################################################### #Inialize files if there should be cleared out each time job is run cat /dev/null >${LOGFILE} cat /dev/null >${ERRFILE} cat /dev/null >${TMPFILE1} cat /dev/null >${TMPFILE2} ################################################################################################ #Define any functions #Define a function to send files back at any point # Send the log file and error file back to a machine sendlogfiles() { mailx -s "Decomm shell script" ${MAILTO} < ${LOGFILE} } # Define function to check for last return code check_errs() { # Function. Parameter 1 is the return code # Para. 2 is text to display on failure. if [ "${1}" -ne "0" ]; then echo "`date +'%b %d %H:%M:%S '`${HOST} $0[$$]: ERROR # ${1}: ${2}" >>${ERRFILE} sendlogfiles exit ${1} fi } # Define function to log whatever messages are wanted log_msg() { # Para. 1 is text to write to the log echo "`date +'%b %d %H:%M:%S '`${HOST} $0[$$]: ${1}" >>${LOGFILE} } ################################ if [ ! $# -gt 0 ] then printf "${USAGEMSG}" $0 exit 2 fi while getopts yhf:[Inputfile]m:[Machinename]x'?' OPTION; do case $OPTION in f|F) MACHINE_LIST_FILE=$OPTARG MACHINEFILE=y ;; y|Y) REMOVE=y printf "-y Option, Not yet implimented\n" exit 2 ;; m|M) MACHINE=$OPTARG MACHINEFILE=n ;; ?|h|H) printf "${USAGEMSG}" exit 2 ;; *) printf "${USAGEMSG}" exit 2 ;; esac done #shift OPTIND-1 shift `expr $OPTIND - 1` #case $MACHINEFILE in # y|Y) if [ ! -f ${MACHINE_LIST_FILE} ]; if [ ${MACHINEFILE} = 'y' ]; then if [ ! -f ${MACHINE_LIST_FILE} ]; then check_errs 2 "Input file un-accessable" else for MACHINE in `cat ${MACHINE_LIST_FILE}` do OUTPUTFILE=${MACHINE}_decomm_info echo "All information for the client (${MACHINE}) will be placed" echo "in the output file: ${OUTPUTFILE}" print -n "." echo "====== Decommissioning Info for ${MACHINE} ====== " >>${OUTPUTFILE} echo " `date ` " >>${OUTPUTFILE} for INST in ${INST_LIST} do print -n "." AUTOSERV=${INST} . /opt/autotree/4.x/autouser/autosys.ksh.$AUTOSERV echo " " >> ${OUTPUTFILE} echo "==================================================" >>${OUTPUTFILE} echo " Gathering job information for Instantance: ${INST} " >>${OUTPUTFILE} echo "==================================================" >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} JOB_COUNT=`sql "select count(job_name) from job where machine like '%${MACHINE}%'"` if [ ${JOB_COUNT} -eq 0 ]; then echo " NO jobs found " >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} else echo " List of Jobs " >>${OUTPUTFILE} echo "--------------------------------------------------" >>${OUTPUTFILE} `sql " select job_name from job where machine like '%${MACHINE}%' " >${TMPFILE1}` cat ${TMPFILE1} >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} echo " Job definitions " >>${OUTPUTFILE} echo "--------------------------------------------------" >>${OUTPUTFILE} for JOB in `cat ${TMPFILE1}` do print -n "." autorep -J ${JOB} -q >>${OUTPUTFILE} done echo " " >> ${OUTPUTFILE} echo " Last run of jobs" >> ${OUTPUTFILE} echo "--------------------------------------------------" >>${OUTPUTFILE} sql "select job_name,dateadd(ss,last_end,'1/1/70') from jobst where machine like '%${MACHINE}%' order by last_end" | sed 's/|/ /g;s/ */ /g' | awk -F" " '{printf("%-30s %-3s %2s %-5s %-10s\n",$1,$2,$3,$4,$5);}' >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} fi done for INST in ${INST_LIST} do print -n "." AUTOSERV=${INST} . /opt/autotree/4.x/autouser/autosys.ksh.$AUTOSERV echo "==================================================" >>${OUTPUTFILE} echo " Gathering License key for Instantance: ${INST} " >>${OUTPUTFILE} echo "==================================================" >>${OUTPUTFILE} echo P |gatekeeper | grep ${MACHINE} >> ${OUTPUTFILE} echo " " >> ${OUTPUTFILE} done done fi fi # n|N) OUTPUTFILE=${MACHINE}_decomm_info if [ ${MACHINEFILE} = 'n' ] then OUTPUTFILE=${MACHINE}_decomm_info #export ${OUTPUTFILE} echo "All information for the client (${MACHINE}) will be placed" echo "in the output file: ${OUTPUTFILE}" print -n "." echo "====== Decommissioning Info for ${MACHINE} ====== " >>${OUTPUTFILE} echo " `date ` " >>${OUTPUTFILE} for INST in ${INST_LIST} do print -n "." AUTOSERV=${INST} . /opt/autotree/4.x/autouser/autosys.ksh.$AUTOSERV echo " " >> ${OUTPUTFILE} echo "==================================================" >>${OUTPUTFILE} echo " Gathering job information for Instantance: ${INST} " >>${OUTPUTFILE} echo "==================================================" >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} JOB_COUNT=`sql "select count(job_name) from job where machine like '%${MACHINE}%'"` if [ ${JOB_COUNT} -eq 0 ] then echo " NO jobs found " >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} else echo " List of Jobs " >>${OUTPUTFILE} echo "--------------------------------------------------" >>${OUTPUTFILE} `sql " select job_name from job where machine like '%${MACHINE}%' " >${TMPFILE1}` cat ${TMPFILE1} >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} echo " Job definitions " >>${OUTPUTFILE} echo "--------------------------------------------------" >>${OUTPUTFILE} for JOB in `cat ${TMPFILE1}` do print -n "." autorep -J ${JOB} -q >>${OUTPUTFILE} done echo " " >> ${OUTPUTFILE} echo " Last run of jobs" >> ${OUTPUTFILE} echo "--------------------------------------------------" >>${OUTPUTFILE} sql "select job_name,dateadd(ss,last_end,'1/1/70') from jobst where machine like '%${MACHINE}%' order by last_end" | sed 's/|/ /g;s/ */ /g' | awk -F" " '{printf("%-30s %-3s %2s %-5s %-10s\n",$1,$2,$3,$4,$5);}' >>${OUTPUTFILE} echo " " >> ${OUTPUTFILE} fi done for INST in ${INST_LIST} do print -n "." AUTOSERV=${INST} . /opt/autotree/4.x/autouser/autosys.ksh.$AUTOSERV echo "==================================================" >>${OUTPUTFILE} echo " Gathering License key for Instantance: ${INST} " >>${OUTPUTFILE} echo "==================================================" >>${OUTPUTFILE} echo P |gatekeeper | grep ${MACHINE} >> ${OUTPUTFILE} echo " " >> ${OUTPUTFILE} done fi # esac print ". DONE" rm ${TMPFILE1} ${TMPFILE2}