Saturday, October 20, 2012

Monitor Sybase error log


 Monitor Sybase error log

#!/bin/sh
#set -x
##
## This scripts scans the errorlog for errors and notifies if
## problems are found.
##
##
## Usage: check_errorlog_and_notify <ASE Server Name>
##
##
##
VERSION="check_errorlog_and_notify Version 1.2"
## Input Parameters for script
SRVNAME=`echo $1 | tr '[a-z]' '[A-Z]'`
ENV_DIR="/sybase_sa"
ENVFILE="$ENV_DIR/.$SRVNAME.env"

##
## Check for correct number of parameters
if [ $# -lt 1 ]
then
    echo "Usage: $0 <SQL-Server Name> "
    exit 1
fi

EXECUTE_ENV()
{
 ##
 ## Check for existence of ASE file
 if [ ! -s $ENVFILE ]
 then
    echo "$ENVFILE files does not exist for ASE Server ($SRVNAME) .."
    echo "Script exiting ..."
    exit 1
 fi

 ##
 ## Source environment (ASE) file
 . $ENVFILE
}

SET_VARIABLES()
{
 ##
 ## Defining system variables
 SA_PW=`$GET_PW $SRVNAME`

 SEARCHFILE="$SRV_LOG/errorlog_$SRVNAME"
 NUMBERED_SEARCHFILE="$SYB_TMP/errorlog_$SRVNAME"

 STARTLINE_FILE="$SYB_BIN/STARTLINE_FILE"
 KEYWORD_FILE="$SYB_BIN/KEYWORDS"

 TEMP_MSGFILE="$SYB_TMP/check_msgfile.$SRVNAME"
 TEMP_MSGFILE1="$SYB_TMP/check_msgfile1.$SRVNAME"
 TEMP_MSGFILE2="$SYB_TMP/check_msgfile2.$SRVNAME"
 MSGFILE="$SYB_TMP/check_msgfile3.$SRVNAME"

 rm -f $NUMBERED_SEARCHFILE $TEMP_MSGFILE $TEMP_MSGFILE1 2>/dev/null
 rm -f $TEMP_MSGFILE2 $MSGFILE 2>/dev/null
}

FIND_COUNTER()
{
 ##
 ## start at this line in log file
 ##
 if [ -f "$STARTLINE_FILE" ]
 then
    STARTLINE=`cat $STARTLINE_FILE`
 else
    STARTLINE=0
 fi
}

CONTINUE_SEARCH()
{
 ##
 ## are there any lines past our start point ?
 ## if not, we are done
 ##
 if [ $STARTLINE -ge `cat $SEARCHFILE | wc -l` ]
 then
    exit 0
 fi
}

CREATE_NUMBER_FILE()
{
 ##
 ## start at starting line number
 ## put line numbers on errorlog for message output
 ##
 LINE_COUNTER=`expr $STARTLINE + 1`

 cat $SEARCHFILE | tail +$STARTLINE | while read SEARCHLINE
 do
   if [ $LINE_COUNTER -ge $STARTLINE ]
   then
      echo "$LINE_COUNTER  $SEARCHLINE" >> $NUMBERED_SEARCHFILE
   fi

   LINE_COUNTER=`expr $LINE_COUNTER + 1`
   echo $LINE_COUNTER > $STARTLINE_FILE
 done
}

KEYWORD_SEARCH()
{
 ##
 ## now get down to business....
 ## grep for errors. KEY_ERROR file has what we grep for and what we ignore
 ##
 cat $KEYWORD_FILE | tail +7 | while read LINE
 do
   KEY_ERROR=`echo $LINE | cut -d~ -f1`
   IGNORE=`echo $LINE | cut -d~ -f2`
   ROWS=`echo $LINE | cut -d~ -f3`
   NUM=`echo $LINE | cut -d~ -f4`

   if [ $NUM -ne 0 ]
   then
      grep -i "$KEY_ERROR" $NUMBERED_SEARCHFILE > $TEMP_MSGFILE2
      NEW_NUM=5
      LAST_NUM=`expr $NUM + 4`
      while [ $LAST_NUM -ge $NEW_NUM ]
      do
        NEW_ERROR=`echo $LINE | cut -d~ -f$NEW_NUM`
        grep -v "$NEW_ERROR" $TEMP_MSGFILE2 > $TEMP_MSGFILE1
        cp $TEMP_MSGFILE1 $TEMP_MSGFILE2
        NEW_NUM=`expr $NEW_NUM + 1`
      done

      if [ $ROWS -ne 0 ]
      then
         cat $TEMP_MSGFILE2 | nawk '{print $1}' | while read FILE_ROW
         do
           FILE_ROW=`expr $FILE_ROW - $STARTLINE`
           cat $NUMBERED_SEARCHFILE | tail +$FILE_ROW | head -$ROWS >> $TEMP_MSGFILE
         done
      else
         mv $TEMP_MSGFILE2 $TEMP_MSGFILE
      fi
   else
      if [ $ROWS -eq 0 ]
      then
         grep -i "$KEY_ERROR" $NUMBERED_SEARCHFILE > $TEMP_MSGFILE
      else
         grep -i "$KEY_ERROR" $NUMBERED_SEARCHFILE | nawk '{print $1}' | while read FILE_ROW
         do
           FILE_ROW=`expr $FILE_ROW - $STARTLINE`
           cat $NUMBERED_SEARCHFILE | tail +$FILE_ROW | head -$ROWS >> $TEMP_MSGFILE
         done
      fi
   fi

   if [ -s "$TEMP_MSGFILE" ]
   then
      rm -f $MSGFILE
      UNIX_SRVR=`grep -w $SRVNAME $SERVERS | grep -w sql | nawk '{print $4}' | head -1`

      if [ "$IGNORE" = "MAIL" ]
      then
         echo "Sql($SRVNAME):Unix($UNIX_SRVR)-MAILONLY: " > $MSGFILE
         cat $TEMP_MSGFILE >> $MSGFILE
      else
         echo "Sql($SRVNAME):Unix($UNIX_SRVR):" > $MSGFILE
         cat $TEMP_MSGFILE | while read LINE
         do
           echo $LINE | cut -d" " -f5- >> $MSGFILE
         done
      fi

      if [ -s "$MSGFILE" ]
      then
        MSGTEXT=`cat $MSGFILE | sed s/\'/\ /g`
        echo "################### `date` #################################"
        cat $MSGFILE
      fi

      MSGTEXT1=`echo $MSGTEXT | cut -c1-210`
      MSGTEXT="$MSGTEXT1 *END*"
      echo "***END***" >> $MSGFILE

      MSG_CNT1=`cat $MSGFILE | wc -c`
      MSG_CNT=`echo $MSG_CNT1`
      TEXT_CNT1=`echo $MSGTEXT | wc -c`
      TEXT_CNT=`echo $TEXT_CNT1`

      $SYB_BIN/send_messages $SRVNAME "$MSGFILE" "$MSGTEXT" BOTH ALL
   fi
   rm -f $TEMP_MSGFILE
 done
}

CLEANUP()
{
 ##
 ## Mom taught us to clean up after ourselves...
 ## remove our work files
 ##
 rm -f $MSGFILE $TEMP_MSGFILE $TEMP_MSGFILE1 $TEMP_MSGFILE2 $NUMBERED_SEARCHFILE 2>/dev/null
}

##
## MAIN SECTION
EXECUTE_ENV
SET_VARIABLES
FIND_COUNTER
CONTINUE_SEARCH
CREATE_NUMBER_FILE
KEYWORD_SEARCH
CLEANUP

exit

Sybase Monitor config file


 Monitor config file in Sybase

#!/bin/sh
#set -x
##
## This scripts initiates sp_monitorconfig for parameters that have gone over
##
##
## =================================================================
## Change History
##
## Date         Programmer      Revisions
## -----------------------------------------------------------------
## 03/11/2010   Bhupendra
##
## ==================================================================
##
##
VERSION="monitor_config.sp Version 1.0"
## Input Parameters for script
SRVNAME=`echo $1 | tr '[a-z]' '[A-Z]'`
ENV_DIR="/sybase_sa"
ENVFILE="$ENV_DIR/.$SRVNAME.env"

##
## Check for correct number of parameters
if [ $# -lt 1 ]
then
    echo "Usage: $0 <SQL-Server Name> "
    exit 1
fi

EXECUTE_ENV()
{
 ##
 ## Check for existence of ASE file
 if [ ! -s $ENVFILE ]
 then
    echo "$ENVFILE files does not exist for ASE Server ($SRVNAME) .."
    echo "Script exiting ..."
    exit 1
 fi

 ##
 ## Source environment (ASE) file
 . $ENVFILE
}

SET_VARIABLES()
{
 ##
 ## Defining system variables
 SA_PW=`$GET_PW $SRVNAME`
 DATE=`date +%Y%m%d`
 OUTFILE="$SYB_SRV/monitor_config.$SRVNAME.${DATE}"
 touch ${OUTFILE}
}
MONITOR_CONFIG()
{
$ISQL_CMD -Usa -S${SRVNAME}  -w132<< EOF  >> ${OUTFILE}
${SA_PW}
sp_monitorconfig "all"
go
EOF
}
CLEANUP()
{
find $SYB_SRV -mtime +21 -name "monitor_config.$(SRVNAME}*" -exec /bin/rm -f {} \;
}

##
## MAIN SECTION
EXECUTE_ENV
SET_VARIABLES
MONITOR_CONFIG
CLEANUP
exit

SPACE check of sybase directories


 
SPCAE check of sybase directories :

Note : you may need to change some prams as per your env


#!/bin/sh
##
## space_check
##
##      Usage: space_check server_name dir_name threshold
##             server_name - the name of the ASE server
##             dir_name - name of directory to check. ex. /sybase
##             threshold - a number that indicates the percent above which to
##                          notify you. ex. 75
##      example: space_check p4nmb /sybase_sa 85
##          When the /sybase_sa directory is more than 85% full, notification
##          will be sent.
##
## Version 1.0
##
## =================================================================
## Change History
##
## Date         Programmer      Revisions
## -----------------------------------------------------------------
## 21/1/2012 Bhupendra bagde
##
## ==================================================================
##
## Input Parameters for script
SRVNAME=`echo $1 | tr '[a-z]' '[A-Z]'`
DIR_NAME=$2
THRESHOLD=$3
ENV_DIR=/sybase_sa
ENVFILE="$ENV_DIR/.$SRVNAME.env"

##
## Check for correct number of parameters
if [ $# -lt 3 ]
then
    echo "Usage: $0 <SQL Srvr Name> <dir name> <threshold>"
    exit 1
fi

if [ ! -d $DIR_NAME ]
then
   echo "space_check.csh: ** ERROR ** $DIR_NAME is not a valid directory name"
   exit
fi

EXECUTE_ENV()
{
 ##
 ## Check for existence of ASE file
 if [ ! -s $ENVFILE ]
 then
    echo "$ENVFILE files does not exist for ASE Server ($SRVNAME) .."
    echo "Script exiting ..."
    exit 1
 fi

 ##
 ## Source environment (ASE) file
 . $ENVFILE
}

SET_VARIABLES()
{
 TMP_FILE=$SYB_TMP/TMP_FILE.$$
 LNEW=`date +%Y%m%d"-"%R`
 rm -f $TMP_FILE 2>/dev/null
}

CHECK_DISKSPACE()
{
 ##
 ## extract the directory space used and name.
 ##
 XDATA=`df -k $DIR_NAME | grep "$DIR_NAME" | awk '{line=$0;line=substr(line,index(line,"%")-3,50);print line;}'`
 DIRSIZE=`echo $XDATA | cut -d"%" -f1`
 DIRNAME=`echo $XDATA | cut -d"%" -f2`

 ##
 ## Check to see if current size exceeds threshold.
 ##
 if [ $DIRSIZE -gt $THRESHOLD ]
 then
    SERVER=`/usr/ucb/hostname | awk -F"." '{print $1}' | tr '[a-z]' '[A-Z]'`
    MESSAGE="** WARNING ** On server ($SERVER) directory $DIR_NAME has exceeded threshold of $THRESHOLD% it is at $DIRSIZE%."
    echo "** WARNING ** On server ($SERVER) directory $DIR_NAME has exceeded threshold of $THRESHOLD% it is at $DIRSIZE%." > $TMP_FILE
    $SYB_BIN/send_messages $SRVNAME "$TMP_FILE" "$MESSAGE" BOTH ALL
 fi
}

##
## MAIN SECTION
EXECUTE_ENV
SET_VARIABLES
CHECK_DISKSPACE

rm -f $TMP_FILE 2>/dev/null

exit

Rebuild Indexes on Sybase


Shell Script  to re-build indexes
Note You may need to chnage some params as per your env



#!/bin/ksh
##set -x
#
################################################################################
#
# This script takes sql scripts as input to run clustered index rebuilds on various tables. Note, it # DOES NOT reverse-engineer tables, it's hardcoded for the clustered indexes as they exist 
# today, with the assumption that they  will not change. It also does not necessarily do all
# indexes. It is driven by the sql scripts it takes as input, so they can have as many or as few    # tables/indexes as desired. The ultimate goal was to provide flexibility in scheduling, to          # spread the work over several days and/or weeks as needed.
#
################################################################################
#
#
######################################
## Set the Server and Database Name ##
######################################
ENV_DIR="/sybase_sa"
SERVER=`echo $1 | dd conv=ucase 2>/dev/null`
DATABASE=$2
INFILE=$3
ENVFILE="$ENV_DIR/.$SERVER.env"

##
## Check for correct number of parameters
##
if [ $# -lt 3 ]
then
    echo "Usage: $0 <ASE Server Name> <Database Name> <Input script name>"
    exit 1
fi

##
## Check for existance of ASE file
##
if [ ! -s $ENVFILE ]
then
        echo "$ENVFILE.env does not exist ..."
        echo "Script exiting ..."
        exit 1
fi

##
## Source environment (ASE) file
##
. $ENVFILE


#  SET ENVIRONMENT VARIABLES
#
VERSION="Version 1.0"
#SENDMESSAGE=1 #1=on 0=off
#SQLSWITCH=1 #1=on 0=off
#DELHIST=1 #1=on 0=off

SA_PW=`$GET_PW $SERVER`
LNEW=`date +%m""%d""%y"-"%I":"%M%p`
BINDIR="${SYB_SA}/index/bin"
OUTFILE="${SYB_SA}/index/logs/rebuild_clustered.${SERVER}.${DATABASE}.${INFILE}.out"
LOGFILE="${SYB_SA}/index/logs/rebuild_clustered.${SERVER}.${DATABASE}.${INFILE}.log"
echo " "> ${LOGFILE}

#
## MAIN PROCESSING
#

# run the index rebuild sql input file
$ISQL_CMD -Usa -S$SERVER -w132 -D${DATABASE} -i${BINDIR}/${INFILE} -o${OUTFILE}  -P${SA_PW}

#
## Check for Errors and notify if necessary
#

if [ ! -s ${OUTFILE} ]
then
  echo "${OUTFILE} does not exist" >> $LOGFILE
else
  ERR=`egrep "Error|Msg|FAILED CREATING|CT-LIBRARY" $OUTFILE | wc -l`
fi

echo "Check log file '${OUTFILE}' for more details." >>${LOGFILE}
egrep 'Error|Msg|FAILED CREATING|CT-LIBRARY' ${OUTFILE}  >>${LOGFILE}

if [ $ERR -ge 1  ]
   then
      MSGERR="CRITICAL ISSUE - Index rebuild failed for $SERVER, database $DATABASE , Check email or '${OUTFILE}' for more details"
      $SYB_BIN/send_messages $SERVER "$OUTFILE" "$MSGERR" BOTH ALL
    else
      MSGERR="Successful - Index rebuild for $SERVER, database $DATABASE, Check email or '${OUTFILE}' for more details"
      $SYB_BIN/send_messages $SERVER "$OUTFILE" "$MSGERR" MAIL ALL
fi

#
## Move output file to timestamped output file
#
mv ${OUTFILE} ${OUTFILE}.$LNEW

#remove old logs files that are older than 30 days
#
RUTHERE=`find $SYB_SA/index/logs -mtime +30 -name "${OUTFILE}*" | wc -l`
if [ ${RUTHERE} -ge 1 ]
then
  find $SYB_SA/index/logs -mtime +30 -name "${OUTFILE}*" -exec /bin/rm -fr {} \;
fi
exit

Sybase IQ Table load


LOAD TABLE lineitem
(
l_orderkey BINARY WITH NULL BYTE,l_partkey BINARY WITH NULL BYTE,
l_suppkey BINARY WITH NULL BYTE,l_linenumber BINARY WITH NULL BYTE,
l_quantity BINARY WITH NULL BYTE,l_extendedprice BINARY WITH NULL BYTE,
l_discount BINARY WITH NULL BYTE,l_tax BINARY WITH NULL BYTE,
l_returnflag BINARY WITH NULL BYTE,l_linestatus BINARY WITH NULL BYTE,
l_shipdate BINARY WITH NULL BYTE,l_commitdate BINARY WITH NULL BYTE,
l_receiptdate BINARY WITH NULL BYTE,l_shipinstruct BINARY WITH NULL BYTE,
l_shipmode BINARY WITH NULL BYTE,l_comment BINARY WITH NULL BYTE
)

FROM 'C:\\mydata\\lineitem_binary.inp'

FORMAT BINARY
STRIP OFF
QUOTES OFF
ESCAPES OFF
PREVIEW ON
BYTE ORDER HIGH;

COMMIT

Friday, October 19, 2012

Blocked Process

Dealing with the blocked process

Blocking
When a process acquiring a lock on a object for its transactions to complete and at the same time the other process wants to acquire a lock on the same object
on which the first process is having a lock then the second process needs to wait
until the first one releases its lock. So the second process will be blocked by the first process is called as blocking

*** How to identify the blocked process ?
  • select spid,cmd,status,blocked from master..sysprocess where blocked >0
  • sp_who “spid” → gives information about spid ,uname,cmd,status and kill that process

Identifying SQL text of a process
  • dbcc trace on (3604)
      • trace flags
        • 3604 → Displays output on the screen
        • 3605 → redirect the output to error log file

      • dbcc sqltext(spid) {spid →id which is being blocked }

      Multiple TEMP DBs


      Temp DB Creation
      The multiple tempdb feature provides users the ability to create temporary database which can be used for the creation of temporary objects
      • DBA's can bind any user login's as well as application to the temporary db
      • The concept of multiple tempdb's is introduced from the version 12.5.3 and from version 12.5.1 we can bind the users and application to user created temporary db

      Syntax : create temporary database dbname on devname=size
      Example : create temporary database tempdb on tempdeve= 50m

      1>sp_tempdb “add”, “tempdb”, “default”
      2>go

      • we need to add the tempdatabase to the default group
      • once the database has been created this database will be used as an additional temporary database
      • sp_tempdb 'show'

      Binding application to temporary db

      • create temporary database dbname on devname=size

      • 1>sp_tempdb 'bind','AP','applname','db_name','tempdb'
      • 2>go

      Binding user to the temporary db

      • create temporary database dbname on devname=size

      • 1>sp_tempdb 'bind','LG','sa','DB','tempdb'
      • 2>go

      UN-Binding Users/Application from user created database

      • 1>sp_tempdb 'unbind','AP','applname'
      • 2>go
      Dropping the temporary database

      • 1>drop db dbname
      • 2>go