Saturday, 3 October 2015

Scripts related to TEMP TABLESPACE

Scripts related to TEMP TABLESPACE

To check instance-wise total allocated, total used TEMP for both rac and non-rac

set lines 152
col FreeSpaceGB format 999.999
col UsedSpaceGB format 999.999
col TotalSpaceGB format 999.999
col host_name format a30
col tablespace_name format a30
select tablespace_name,
(free_blocks*8)/1024/1024 FreeSpaceGB,
(used_blocks*8)/1024/1024 UsedSpaceGB,
(total_blocks*8)/1024/1024 TotalSpaceGB,
i.instance_name,i.host_name
from gv$sort_segment ss,gv$instance i where ss.tablespace_name in (select tablespace_name from dba_tablespaces where contents='TEMPORARY') and
i.inst_id=ss.inst_id;

Total Used and Total Free Blocks

select inst_id, tablespace_name, total_blocks, used_blocks, free_blocks  from gv$sort_segment;

Another Query to check TEMP USAGE

col name for a20
SELECT d.status "Status", d.tablespace_name "Name", d.contents "Type", d.extent_management
"ExtManag",
TO_CHAR(NVL(a.bytes / 1024 / 1024, 0),'99,999,990.900') "Size (M)", TO_CHAR(NVL(t.bytes,
0)/1024/1024,'99999,999.999') ||'/'||TO_CHAR(NVL(a.bytes/1024/1024, 0),'99999,999.999') "Used (M)",
TO_CHAR(NVL(t.bytes / a.bytes * 100, 0), '990.00') "Used %"
FROM sys.dba_tablespaces d, (select tablespace_name, sum(bytes) bytes from dba_temp_files group by
tablespace_name) a,
(select tablespace_name, sum(bytes_cached) bytes from
v$temp_extent_pool group by tablespace_name) t
WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = t.tablespace_name(+)
AND d.extent_management like 'LOCAL' AND d.contents like 'TEMPORARY';

Temporary Tablespace groups

SELECT * FROM DATABASE_PROPERTIES where PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE';

select tablespace_name,contents from dba_tablespaces where tablespace_name like '%TEMP%';

select * from dba_tablespace_groups;

Block wise Check

select TABLESPACE_NAME, TOTAL_BLOCKS, USED_BLOCKS, MAX_USED_BLOCKS, MAX_SORT_BLOCKS, FREE_BLOCKS from V$SORT_SEGMENT;

select sum(free_blocks) from gv$sort_segment where tablespace_name = 'TEMP';
To Check Percentage Usage of Temp Tablespace

select (s.tot_used_blocks/f.total_blocks)*100 as "percent used"
from (select sum(used_blocks) tot_used_blocks
from v$sort_segment where tablespace_name='TEMP') s,
(select sum(blocks) total_blocks
from dba_temp_files where tablespace_name='TEMP') f;

To check Used Extents ,Free Extents available in Temp Tablespace

SELECT tablespace_name, extent_size, total_extents, used_extents,free_extents, max_used_size FROM v$sort_segment;

To list all tempfiles of Temp Tablespace

col file_name for a45
select tablespace_name,file_name,bytes/1024/1024,maxbytes/1024/1024,autoextensible from dba_temp_files  order by file_name;

SELECT d.tablespace_name tablespace , d.file_name filename, d.file_id fl_id, d.bytes/1024/1024
size_m
, NVL(t.bytes_cached/1024/1024, 0) used_m, TRUNC((t.bytes_cached / d.bytes) * 100) pct_used
FROM
sys.dba_temp_files d, v$temp_extent_pool t, v$tempfile v
WHERE (t.file_id (+)= d.file_id)
AND (d.file_id = v.file#);

Additional checks

select distinct(temporary_tablespace) from dba_users;

select username,default_tablespace,temporary_tablespace from dba_users order by temporary_tablespace;

SELECT * FROM DATABASE_PROPERTIES where PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE';

Changing the default temporary Tablespace

SQL> alter database default temporary tablespace TEMP;

Database altered.

To add tempfile to Temp Tablespace

alter tablespace  temp  add tempfile '&tempfilepath' size 1800M;

alter tablespace temp add tempfile '/m001/oradata/SID/temp02.dbf' size 1000m;

alter tablespace TEMP add tempfile '/SID/oradata/data02/temp04.dbf' size 1800M autoextend on maxsize 1800M;

To resize the  tempfile in Temp Tablespace

alter database tempfile '/u02/oradata/TESTDB/temp01.dbf' resize 250M

alter database tempfile '/SID/oradata/data02/temp12.dbf' autoextend on maxsize 1800M;

alter tablespace TEMP add tempfile '/SID/oradata/data02/temp05.dbf' size 1800m reuse;

To find Sort Segment Usage by Users

select username,sum(extents) "Extents",sum(blocks) "Block"
from v$sort_usage
group by username;

To find Sort Segment Usage by a particular User

SELECT s.username,s.sid,s.serial#,u.tablespace, u.contents, u.extents, u.blocks
FROM v$session s, v$sort_usage u
WHERE s.saddr=u.session_addr
order by u.blocks desc;

To find Total Free space in Temp Tablespace

select 'FreeSpace  ' || (free_blocks*8)/1024/1024 ||' GB'  from v$sort_segment where tablespace_name='TEMP';

select tablespace_name , (free_blocks*8)/1024/1024  FreeSpaceInGB,
(used_blocks*8)/1024/1024  UsedSpaceInGB,
(total_blocks*8)/1024/1024  TotalSpaceInGB
from v$sort_segment where tablespace_name like '%TEMP%'

To find  Total Space Allocated for Temp Tablespace

select 'TotalSpace ' || (sum(blocks)*8)/1024/1024 ||' GB'  from dba_temp_files where tablespace_name='TEMP';

Get 10 sessions with largest temp usage

cursor bigtemp_sids is
select * from (
select s.sid,
s.status,
s.sql_hash_value sesshash,
u.SQLHASH sorthash,
s.username,
u.tablespace,
sum(u.blocks*p.value/1024/1024) mbused ,
sum(u.extents) noexts,
nvl(s.module,s.program) proginfo,
floor(last_call_et/3600)||':'||
floor(mod(last_call_et,3600)/60)||':'||
mod(mod(last_call_et,3600),60) lastcallet
from v$sort_usage u,
v$session s,
v$parameter p
where u.session_addr = s.saddr
and p.name = 'db_block_size'
group by s.sid,s.status,s.sql_hash_value,u.sqlhash,s.username,u.tablespace,
nvl(s.module,s.program),
floor(last_call_et/3600)||':'||
floor(mod(last_call_et,3600)/60)||':'||
mod(mod(last_call_et,3600),60)
order by 7 desc,3)
where rownum < 11;

Displays the amount of IO for each tempfile

SELECT SUBSTR(t.name,1,50) AS file_name,
f.phyblkrd AS blocks_read,
f.phyblkwrt AS blocks_written,
f.phyblkrd + f.phyblkwrt AS total_io
FROM   v$tempstat f,v$tempfile t
WHERE  t.file# = f.file#
ORDER BY f.phyblkrd + f.phyblkwrt DESC;

select * from (SELECT u.tablespace, s.username, s.sid, s.serial#, s.logon_time, program, u.extents, ((u.blocks*8)/1024) as MB,
i.inst_id,i.host_name
FROM gv$session s, gv$sort_usage u ,gv$instance i
WHERE s.saddr=u.session_addr and u.inst_id=i.inst_id  order by MB DESC) a where rownum<10;

Check for ORA-1652

show parameter background

cd <background dump destination>

ls -ltr|tail

view <alert log file name>

shift + G ---> to get the tail end...

?ORA-1652 ---- to search of the error...

shift + N ---- to step for next reported error...

I used these queries to check some settings:

-- List all database files and their tablespaces:
select  file_name, tablespace_name, status
,bytes   /1000000  as MB
,maxbytes/1000000  as MB_max
from dba_data_files ;

-- What temporary tablespace is each user using?:
select username, temporary_tablespace, default_tablespace from dba_users ;

-- List all tablespaces and some settings:
select tablespace_name, status, contents, extent_management
from dba_tablespaces ;

TABLESPACE_NAME                CONTENTS  EXTENT_MAN STATUS
------------------------------ --------- ---------- ---------
SYSTEM                         PERMANENT DICTIONARY ONLINE
TOOLS                          PERMANENT DICTIONARY ONLINE
TEMP                           TEMPORARY DICTIONARY OFFLINE
TMP                            TEMPORARY LOCAL      ONLINE

Now, the above query and the storage clause of the old 'create tablespace TEMP' command seem to tell us the tablespace only allows temporary objects, so it should be safe to assume that no one created any tables or other permanent objects in TEMP by mistake, as I think Oracle would prevent that. However, just to be absolutely certain, I decided to double-check. Checking for any tables in the tablespace is very easy:

-- Show number of tables in the TEMP tablespace - SHOULD be 0:
select count(*)  from dba_all_tables
where tablespace_name = 'TEMP' ;

Checking for any other objects (views, indexes, triggers, pl/sql, etc.) is trickier, but this query seems to work correctly - note that you'll probably need to connect internal in order to see the sys_objects view:

-- Shows all objects which exist in the TEMP tablespace - should get
-- NO rows for this:
column owner        format a20
column object_type  format a30
column object_name  format a40
select
o.owner  ,o.object_name
,o.object_type
from sys_objects s
,dba_objects o
,dba_data_files df
where df.file_id = s.header_file
and o.object_id = s.object_id
and df.tablespace_name = 'TEMP' ;

Identifying WHO is currently using TEMP Segments

10g onwards

SELECT sysdate,a.username, a.sid, a.serial#, a.osuser, (b.blocks*d.block_size)/1048576 MB_used, c.sql_text
FROM v$session a, v$tempseg_usage b, v$sqlarea c,
     (select block_size from dba_tablespaces where tablespace_name='TEMP') d
    WHERE b.tablespace = 'TEMP'
    and a.saddr = b.session_addr
    AND c.address= a.sql_address
    AND c.hash_value = a.sql_hash_value
    AND (b.blocks*d.block_size)/1048576 > 1024
    ORDER BY b.tablespace, 6 desc;

PeopleSoft Maintenance Scripts

PeopleSoft Maintenance Script


Grant_sysnonym.sh

export ORACLE_SID=fsprd91
export ORACLE_DIR=/fsprd_adm/fsprd91/scripts
export ORACLE_HOME=/u01/app/oracle/product/11.2
PATH=$ORACLE_HOME/bin:$PATH;export PATH

cd $ORACLE_DIR
date > grants.log

rm $ORACLE_DIR/readonly_grants.sql
rm $ORACLE_DIR/public_synonyms.sql
rm $ORACLE_DIR/recompile_invalid_synonyms.sql

sqlplus sysadm/password@fsprd91 @$ORACLE_DIR/create_readonlyrole_grants.sql
date >> grants.log
sqlplus sysadm/password@fsprd91 @$ORACLE_DIR/create_public_synonyms.sql
date >> grants.log
sqlplus / as sysdba @$ORACLE_DIR/create_recompile_invalid_synonyms.sql
date >> grants.log
mailx -s "FSPRD91 Read Only Grants and Public Synonyms Done" primedba < grants.log

create_readonlyrole_grants.sql

set heading off
set echo off
set feedback off
set pages 0
set lines 100
spool /fsprd_adm/fsprd91/scripts/readonly_grants.sql
select 'grant select on sysadm.' || table_name || ' to readonlyrole;' from user_tables;
select 'grant select on sysadm.' || view_name || ' to readonlyrole;' from user_views;
revoke select on PSWEBPROFNVP from readonlyrole;
select 'grant select on PS_PLD_GEO_TREE_VW to prologisone;' from dual;
select 'grant select on PS_PLD_FUNDNAME_VW to prologisone;' from dual;
spool off;
set echo on
start /fsprd_adm/fsprd91/scripts/readonly_grants.sql
exit;


create_public_synonyms.sql
set heading off
set echo off
set feedback off
set pages 0
set lines 100
spool /fsprd_adm/fsprd91/scripts/public_synonyms.sql
select 'CREATE PUBLIC SYNONYM ' || table_name || ' FOR SYSADM.' || table_name || ';'  from user_tables;
select 'CREATE PUBLIC SYNONYM ' || view_name || ' FOR SYSADM.' || view_name || ';'  from user_views;
spool off;
set echo on
start /fsprd_adm/fsprd91/scripts/public_synonyms.sql
exit;

create_recompile_invalid_synonyms.sql
set heading off
set echo off
set feedback off
set pages 0
set lines 100
spool /fsprd_adm/fsprd91/scripts/recompile_invalid_synonyms.sql
SELECT 'ALTER PUBLIC SYNONYM ' || SYNONYM_NAME || ' COMPILE;'
  FROM ALL_SYNONYMS S
  JOIN ALL_OBJECTS O
  ON S.OWNER = O.OWNER
  AND S.SYNONYM_NAME = O.OBJECT_NAME
  WHERE O.OBJECT_TYPE = 'SYNONYM'
  AND S.OWNER = 'PUBLIC'
  AND O.STATUS <> 'VALID';
spool off;
set echo on
start /fsprd_adm/fsprd91/scripts/recompile_invalid_synonyms.sql
exit;

Friday, 2 October 2015

PeopleSoft Action Center to start / stop and bounce the peoplesoft services.

PeopleSoft Action Center Was Designed / Developed by Myself (Zafrulla)
This will keep away the PS admins from doing the same mundane job of bouncing PSFT services.

Web Action Center Advantages

1. Monitoring the PeopleSoft Application Services, like Web server, Application Server and
Process Scheduler Domains.

2. Also, it has the ability to Start, Stop and Bounce the PeopleSoft Services without the needed access to the physical servers.

3. It will also help to clear the cache of the services so that the application users see the
changes immediately

4. Sends Email notification of the action taken to the users email address who initiated the
required action against PeopleSoft services.

5. Enables Proxy-Monitoring which can be used by any user without the access to the
PeopleSoft Servers.




Wednesday, 23 September 2015

html mail to mailx from linux boxes.



mailx -s "Test HTML output in outlook / GMAIL
MIME-Version: 1.0
Content-Type: text/html" -r noreply@prologis.com zafrulla.khan@xyz.com <<-EOF
<pre>
<b>
<h1>
`cat /etc/group`
</h1>
</b>
</pre>
EOF

Check DATABASE Login before proceeding



########## Check to login before proceeding ##################

DBCHECK=/tmp/${ORACLE_SIDD}_check.log
SQL1="SELECT SYSDATE FROM DUAL";
sqlplus -s "$USERID/$PASSWORD@$CONNECT" <<-EOT > ${DBCHECK}
set heading off
set feedback off
${SQL1};
exit;
EOT


if [ $(cat ${DBCHECK}|grep "ORA-"|wc -l) -gt 0 ]; then
        echo "Sorry Login issues with the database ${ORACLE_SIDD}".

        message="[CRITICAL] [IB Messages Monitor: ${ORACLE_SID} DATABASE LOGIN FAILED] [${HOST_NAME}]"
        /bin/mailx -s "$message" -r noreply@prologis.com ${MAILLIST}  <<-EOFL

                Hi PSFT ADMIN Team,

                asynchronous_error_monitor.sh script for Production

                Sorry, there was a problem in logging to the ${ORACLE_SIDD} Database. So, can not continue to check for asynchronous errors.
                Please do fix this issue on Priority. Escalate to the DBA Manager for any serious concerns.

                ______________________________________________________________________________________________
                Oracle Database Availability Status Report: ps -efx|grep pmon
                ----------------------------------------------------------------------------------------------
                $(ps -efx|grep pmon)
                ______________________________________________________________________________________________

                ______________________________________________________________________________________________
                Database Login ERROR Logs
                ----------------------------------------------------------------------------------------------
                $(cat ${DBCHECK})
                ______________________________________________________________________________________________


                ______________________________________________________________________________________________
                Please check the following for user ID: ${USERID}
                ----------------------------------------------------------------------------------------------
                1) Please check login credentials
                2) Check If the user account has been locked / expired
                2) Check DB Wallets are open and allowing the logins
                3) Check if the listeners and database are up and available.
                ______________________________________________________________________________________________

                Note: Ignore this email if the DBA has intentionally stopped the ${ORACLE_SIDD} database for maintenance activities.

If you don't want to see this alert email again. Please remove the entry from the crontab on ${HOST_NAME} host.
Only do this once you have necessary approvals from the DBA Manager.


==============================================================================
Asynchronous error Monitoring script
==============================================================================
This script is located in the directory $(cd $(dirname $0);echo $PWD)
Host Name : $HOST_NAME
Script Name : "${0##*/}"
Version  : 2.0
Script Author : "PwC Env Team"
===============================================================================

Thanks
I am back on Job.


EOFL
exit 1;

else
echo "I am good. The database is up and available";
fi
######################################



how to add one second to the date/time stamp in oracle


how to add one second to the date/time stamp in oracle

SELECT to_char('24-AUG-2015 13:00:00'/(24*60*60),'DD-MON-YYYY HH24:MI:SS') from dual;
SELECT to_char(to_date('24-AUG-2015 23:59:59','DD-MON-YYYY HH24:MI:SS')+1 /(24*60*60),'DD-MON-YYYY HH24:MI:SS') from dual;

Tuesday, 22 September 2015

asynchronous_error_monitor script to monitor async messages for any errors




#! /bin/bash
set -vx
##########################################################################################
# Name    : asynchronous_error_monitor
# Author  : Zafrulla Khan
# Date    : Sep/23/2015
# Usage   : asynchronous_error_monitor.ksh <ORACLE_SID>
# Deployed: Sep/23/2015
##########################################################################################

. $HOME/.bash_profile
ORACLE_SID=$1
HOST_NAME=$(hostname | awk -F_ '{ print $1}')
#UNIX_NODE=$(echo $HOST_NAME|tr 'a-z' 'A-Z')
MAILLIST=$(cat /home/oracle/scripts/ibmessages/DBA_EMAIL_IDS.txt)


# Get access Details from the file

INSTANCEFILE=/home/oracle/scripts/ibmessages/accessdetails.lis

export USERID=$(cat $INSTANCEFILE|grep "${HOST_NAME}"|awk -F: '{print $3}' -)
export PASSWORD=$(cat $INSTANCEFILE|grep "${HOST_NAME}"|awk -F: '{print $4}' -)
export CONNECT=$(cat $INSTANCEFILE|grep "${HOST_NAME}"|awk -F: '{print $1}' -)

echo $ORACLE_SID
echo $USERID
echo $PASSWORD
echo $CONNECT


SCRIPT_DIR=/home/oracle/scripts/ibmessages/queues/
LOG_DIR=/home/oracle/scripts/ibmessages/logs
OUTPUT_FILE=$SCRIPT_DIR/LOGS/sync_errors.log
LAST_CHECKED_DATE_TIME_FILE=${SCRIPT_DIR}/last_date_time_checked.lis
CURRENT_TIMESTAMP=$(/bin/date +%d-%b-%Y" "%H:%M:%S|tr '[:lower:]' '[:upper:]'); echo $CURRENT_TIMESTAMP


/u01/app/oracle/product/11.2.0/db_1/bin/sqlplus -s "$USERID/$PASSWORD@$CONNECT" <<EOT > /home/oracle/scripts/ibmessages/logs/IB_OPERATIONNAME.log
set heading off
set feedback off
select distinct IB_OPERATIONNAME from PSAPMSGSUBCON;
exit;
EOT

cat /home/oracle/scripts/ibmessages/logs/IB_OPERATIONNAME.log|grep -v ^$ | while read LAST_CHECKED_DATE_TIME_FILE
do
if [ ! -f ${SCRIPT_DIR}${LAST_CHECKED_DATE_TIME_FILE} ]
 then echo "01-JAN-2015 00:00:00" > ${SCRIPT_DIR}${LAST_CHECKED_DATE_TIME_FILE}
fi

LAST_RUN_TIMESTAMP=$(cat ${SCRIPT_DIR}$LAST_CHECKED_DATE_TIME_FILE|grep -v ^$)

RETVAL_SQL=0;
RETVAL_SQL1=0;
  RETVAL_SQL=$(
sqlplus -s $USERID/$PASSWORD@$CONNECT <<-EOS
set echo off heading off pagesize 1000  feedback off linesize 150 serveroutput on
select count(*) from PSAPMSGSUBCON where CREATEDTTM between TO_DATE('${LAST_RUN_TIMESTAMP}','DD-MON-YYYY HH24:MI:SS') AND TO_DATE('${CURRENT_TIMESTAMP}','DD-MON-YYYY HH24:MI:SS') AND IB_OPERATIONNAME='${LAST_CHECKED_DATE_TIME_FILE}' and STATUSSTRING='ERROR';
EOS
)
 
    RETVAL_SQL1=$(echo $RETVAL_SQL|grep -v ^$)
 
if [ $RETVAL_SQL1 != 0 ]
then
/u01/app/oracle/product/11.2.0/db_1/bin/sqlplus -s $USERID/$PASSWORD@$CONNECT <<-EOS
 spool /home/oracle/scripts/ibmessages/logs/async_errors.log
 set echo off heading on pagesize 100  feedback on linesize 200 serveroutput on;
 col LASTUPDDTTM format a32;
 col CREATEDTTM format a32;
 select IBTRANSACTIONID, IB_OPERATIONNAME, CREATEDTTM,LASTUPDDTTM, STATUSSTRING from PSAPMSGSUBCON  where CREATEDTTM between TO_DATE('${LAST_RUN_TIMESTAMP}','DD-MON-YYYY HH24:MI:SS') AND TO_DATE('${CURRENT_TIMESTAMP}','DD-MON-YYYY HH24:MI:SS') AND IB_OPERATIONNAME='${LAST_CHECKED_DATE_TIME_FILE}' and STATUSSTRING='ERROR';
 spool off;
EOS

/bin/mailx -s " $ORACLE_SID - [ERROR ASYNC IB MESSAGE] - ${LAST_CHECKED_DATE_TIME_FILE} ERRORS = $RETVAL_SQL1" -r noreply@prologis.com $MAILLIST <<-EOF
Hello PSFT ADMIN Team,

Asynchronous error monitoring script for Production

Found $RETVAL_SQL1 synchronous error(s) in ${LAST_CHECKED_DATE_TIME_FILE} queue

==============================================================================
Between DateTime Interval
------------------------------------------------------------------------------
From : ${LAST_RUN_TIMESTAMP}
To   : ${CURRENT_TIMESTAMP}

`cat /home/oracle/scripts/ibmessages/logs/async_errors.log`

==============================================================================


==============================================================================
Asynchronous error Monitoring script
==============================================================================
This script is located in the directory $(cd $(dirname $0);echo $PWD)
Host Name : $UNIX_NODE
Script Name : "${0##*/}"
Version  : 2.0
Script Author : "PwC Env Team"
===============================================================================

I'm back on Job.
synchronous error monitor script version 2.0

EOF

/u01/app/oracle/product/11.2.0/db_1/bin/sqlplus -s $USERID/$PASSWORD@$CONNECT <<-EOS > ${SCRIPT_DIR}${LAST_CHECKED_DATE_TIME_FILE}
set echo off heading off feedback off;
SELECT TO_CHAR(max(CREATEDTTM)+1/(24*60*60),'DD-MON-YYYY HH24:MI:SS') from PSAPMSGSUBCON where IB_OPERATIONNAME='${LAST_CHECKED_DATE_TIME_FILE}' and STATUSSTRING='ERROR';
EOS

fi

done