#!/usr/bin/ksh ############################################################################################ # manage_autocons # # 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 'manage' autocons sessions. It will remove any autocons session that # has been disassiociated with it's perent process. e.g. any autocons process that has # PPID of 1. It will remove any autocons processes old than a specified number of days, # the default is 3 days. It will do the same thing for autosc sessions and shell sessions # owned by autosys. It can be used to just list the processes detailed above. # # Wachovia # Peter Dominey 817-416-6345 8/19/08 # ############################################################################################ # 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' AUTOCONS=n AUTOSC=n CHKSHELL=n CHKXQL=n DAYS=3 PROCS=n USAGEMSG="Usage: %s [-l][-a][-s][-k] [-x]\n\t -l Only list processes (default is kill processes)\n\t To Include:\n\t -a autoscons\n\t -s autosc\n\t -k shell sessions\n\t -x xql queries\n\t -d Age of processes (in days), default 3 days\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} } get_options() { if [ ! $# -gt 0 ] then printf "${USAGEMSG}" $0 exit 2 fi while getopts ad:[days]hklsx'?' OPTION; do case $OPTION in l|L) LISTONLY=y;; a|A) AUTOCONS=y;; s|S) AUTOSC=y;; k|K) CHKSHELL=y;; d|D) DAYS=$OPTARG;; x|X) CHKXQL=y;; ?|h|H) printf "${USAGEMSG}" exit 2 ;; *) printf "${USAGEMSG}" exit 2 ;; esac done #shift OPTIND-1 shift `expr $OPTIND - 1` } getprocesses() { PROC=${1} # Using this for testing only USER=autosys case $LISTONLY in y) echo "List of any ${PROC} process(es) inherited by init" ps -u ${USER} -o etime,ppid,pid,comm |grep -v grep | nawk '/'"${PROC}"'/ && $2==1 {print $0}' echo "List of any ${PROC} process(es) older than ${DAYS} days (dd hh:mm:ss)" #ps -u ${USER} -o etime,ppid,pid,comm | sed 's/-/ /1' |nawk '/'"${PROC}"'/ && $1 >= '"$DAYS"' {print $0}' | tee ${TMPFILE1} ;; ps -u ${USER} -o etime,ppid,pid,comm |grep -v grep |\ nawk ' BEGIN { printf "%-4s%-9s%-9s%-9s%-20s\n","Days"," Time","PPID","PID","Cmd"} { if ($1 !~ /[0-9].?-/) {sub(/^/,"0 ")} if ($1 ~ /[0-9].?-/) {sub(/-/," ")} if ($1 >= '"${DAYS}"' && $5 ~ /'"${PROC}"'/) {printf "%-4s%-9s%-9s%-9s%-20s\n", $1, $2, $3, $4, $5} } END { print " " }' ;; *) echo "Killing any ${PROC} process(es) inherited by init" PROCS=`ps -u ${USER} -o ppid,pid,etime,comm |grep -v grep | nawk '/'"${PROC}"'/ && $1==1 {print $2}'` if [[ ! -z ${PROCS} ]]; then echo `ps -u ${USER} -o ppid,pid,etime,comm |grep -v grep | nawk '/'"${PROC}"'/ && $1==1 {print $2}'` kill -9 `ps -u ${USER} -o ppid,pid,etime,comm |grep -v grep | nawk '/'"${PROC}"'/ && $1==1 {print $2}'` else echo "Nothing to kill" fi echo "Killing any ${PROC} process(es) older than ${DAYS}" PROCS=`ps -u ${USER} -o etime,ppid,pid,comm |grep -v grep |\ nawk ' { if ($1 !~ /[0-9].?-/) {sub(/^/,"0 ")} if ($1 ~ /[0-9].?-/) {sub(/-/," ")} if ($1 >= '"${DAYS}"' && $5 ~ /'"${PROC}"'/) {print $4} } '| sed '/^$/d'` if [[ ! -z ${PROCS} ]]; then echo ${PROCS} kill -9 `ps -u ${USER} -o etime,ppid,pid,comm |\ nawk ' BEGIN { print " " } { if ($1 !~ /[0-9].?-/) {sub(/^/,"0 ")} if ($1 ~ /[0-9].?-/) {sub(/-/," ")} if ($1 >= '"${DAYS}"' && $5 ~ /'"${PROC}"'/) {print $4} } END { print " " }'` else echo "Nothing to kill" fi ;; esac } get_options $* echo "This applies to autosys owned processes" if [ ${AUTOCONS} = "y" ]; then getprocesses autocons fi if [ ${AUTOSC} = y ]; then getprocesses autosc fi if [ ${CHKSHELL} = y ]; then getprocesses ksh fi if [ ${CHKXQL} = y ]; then getprocesses xql fi # CLEAN UP rm ${TMPFILE1} ${TMPFILE2}