#!/bin/sh 

# name: dbcc.sh (was weekend.sh)
# author: Todd Boss
# purpose: script meant to run from cron that takes as a parameter
#          the SQL server name desiring to have dbcc's run on it.
#
# creation history
# date		author	purpose
# 12/20/96	T.Boss	creation
# 12/23/96	T.Boss	modified to use $server as parameter to script
#		        and functions...much nicer looking code.
# 2/25/97	T.Boss	added CC to list of servers checked
# 3/24/97	T.Boss	added in Disaster Recovery obtainment function,
#			changed name to weekend.sh
# 4/2/97	T.Boss	changed $server.databases to use /tmp not inplace,
#			added exception processing to exclude certain dbs
# 4/3/97	T.Boss	cleaned old servers from pw populating, did major
#			overhaul of get_dr functions to fix master backups, 
#			get buildmaster -yall based on server version
# 4/17/97	T.Boss	added crimson_dev to processing
# 4/18/97	T.Boss	dbcc.sh split from dr.sh
# 4/21/97	T.Boss	moved counter increment to keep accurate count
#			of databases given exception processing.  Moved $sapw
#			inside of EOTSQL script for security
# 7/8/97	T.Boss	modified to work in /export/syb_ops, cleaned up,
#  removed exception databases, added several servers
# 8/5/97	T.Boss	added new_crimson to login list
# 8/11/97	T.Boss	excluded cdr from checking...taking too long.
#  modified to put dbcc output files in dated subdirectory
# 8/15/97	T.Boss	fixed password files, removed CC server

# usage information below: if parameter omitted, script exits.

server=$1

if [ -z "$server" ]
then
   echo "Usage: dbcc.sh "
   exit 1
fi

# get_dbs populates a flat file w/ the databases in a server, used to
# parse through for each dbcc iteration.  File ($server.databases) is
# deleted at end of script.  Required b/c no array functionality exists
# in plain bourne shell

get_dbs ()
{
  server=$1
  ISSA="/export/sybase11/bin/isql -Usa -S$server -I/export/sybase11/interfaces" 
$ISSA -o /tmp/$server.databases << EOTSQL
$sapw
select name from sysdatabases
go
EOTSQL
}

# Exception list processing.  Exception databases are listed in the
# $EXCEPTION_LIST variable, which is then parsed through and compared
# to each $database (inherited at execution time...see main do_dbcc()
# and the call to this function.  If its found, set $found=true and thus
# the execution if clause in do_dbcc() will fail.

EXCEPTION_LIST="pubs2 sybsyntax tempdb cdr"

check_for_exception ()
{
   found=false
   for exception in $EXCEPTION_LIST
   do
      if [ "$database" = "$exception" ]
      then
         found=true
      fi
   done
} 

# main dbcc processing function.  The get_dbs function returns a file
# with 2 lines before and 1 line after the true list of databases; so
# we create a counter, and only process dbccs on the 3rd-secondtolast
# line of the $server.databases file.

do_dbcc ()
{
   counter=0
   linecount=`wc -l /tmp/$server.databases | awk '{ print $1 }'`
   #echo "the linecount is $linecount"
   cat /tmp/$server.databases | while read database
   do
      counter=`expr $counter + 1`
      check_for_exception
      if [ "$found" = "false" ]
      then
         if [ $counter -gt 2 ] && [ $counter -lt $linecount ]
         then
         ISSA="/export/sybase11/bin/isql -Usa -S$server \
            -I/export/sybase11/interfaces -o $server.$database.dbcc.$DATE" 
$ISSA << EOTSQL
$sapw
use master
go
dbcc checkcatalog($database)
go
dbcc checkdb($database)
go
dbcc checkalloc($database)
go
exit
EOTSQL
         fi
      fi
   done
}

##################
# Main processing; case statement exists only b/c different sql servers
# have different sa passwords; these are stored in files in my home
# dir that are read-only to tboss

case $server in
   "prod11")
   sapw=`cat /export/syb_ops/.prod11sapw`
   ;;
   "prod")
   sapw=`cat /export/syb_ops/.prodsapw`
   ;;
   "crimson")
   sapw=`cat /export/syb_ops/.crimsonsapw`
   ;;
   "crimson_dev")
   sapw=`cat /export/syb_ops/.crimson_devsapw`
   ;;
   "peter")
   sapw=`cat /export/syb_ops/.petersapw`
   ;;
   "mdb")
   sapw=`cat /export/syb_ops/.mdbsapw`
   ;;
   "syb_dicom_svr")
   sapw=`cat /export/syb_ops/.syb_dicom_svrsapw`
   ;;
   "new_crimson")
   sapw=`cat /export/syb_ops/.new_crimsonsapw`
   ;;
esac

DATE=`date +%y%m%d`
cd /export/syb_ops/dbcc
mkdir $DATE
cd $DATE
get_dbs $server
do_dbcc

rm -rf /tmp/$server.databases

exit 0