forked from dwheltzel/Shell-Scripts-for-Oracle-DBAs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathora_funcs.sh
More file actions
209 lines (191 loc) · 4.64 KB
/
ora_funcs.sh
File metadata and controls
209 lines (191 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
#
# File $Id: save_db_configs.sh 2268 2014-01-15 17:53:20Z dheltzel $
# Modified $Author: dheltzel $
# Date $Date: 2014-01-14 17:53:20 +0000 (Wed, 15 Jan 2014) $
# Revision $Revision: 2268 $
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# Sets the Oracle environment, if possible
oe ()
{
# if no argument, try the existing value of ORACLE_SID
TMP_ORACLE_SID=${1:-$ORACLE_SID}
if [ -z "${TMP_ORACLE_SID}" ] ; then
echo "No SID"
return 1
fi
# Set the SID
ORACLE_SID=`ps -ef|grep ora_\\\\pmon|sed -e "s/.*pmon_//"|grep -i ${TMP_ORACLE_SID}|sort -u`
# check that only one SID was found
if [ `echo $ORACLE_SID|wc -w` -gt 1 ] ; then
echo "Too many matching SID's (${ORACLE_SID})"
# Just take the first one
ORACLE_SID=`echo $ORACLE_SID|cut -d" " -f1`
echo "Setting ORACLE_SID to $ORACLE_SID"
fi
if [ -z "${ORACLE_SID}" ] ; then
echo "SID not found"
return 1
fi
ps -e -o command,pid | grep ora_\\\\pmon|grep ${ORACLE_SID}|read cmd pid
#Find ORACLE_HOME from process id
if [ -r /proc/$pid/exe ] ; then
TMP_ORACLE_HOME=$(ls -l /proc/$pid/exe | awk -F'> ' '{print $2}' | sed 's/\/[^\/]*\/[^\/]*$//')
else
# Get the OH from oratab if the process files are not readable
TMP_ORACLE_HOME=`grep "${TMP_ORACLE_SID}" /etc/oratab|cut -d: -f2`
# If it wasn't found, strip off the last char to work on RAC
if [ -z "${TMP_ORACLE_HOME}" ]; then
INST=`expr substr ${TMP_ORACLE_SID} ${#TMP_ORACLE_SID} 1`
TMP_ORACLE_HOME=`grep "${TMP_ORACLE_SID%$INST}" /etc/oratab|cut -d: -f2`
fi
fi
# If we found an OH and it's valid, set the rel OH, otherwise leave it unchanged and hope for the best
if [ -d "${TMP_ORACLE_HOME}" ] ; then
ORACLE_HOME=${TMP_ORACLE_HOME}
fi
# Check that the ORACLE_SID we finally decided on has a running db
ps -ef|grep ora_\\pmon_${ORACLE_SID} >/dev/null
if [ $? = '0' ] ; then
# Everything works! exit with success
echo "${ORACLE_SID}"
export PATH=$ORACLE_HOME/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin
export ORACLE_SID ORACLE_HOME PATH
return 0
else
echo "SID not found"
return 1
fi
}
test_connect ()
{
if [ $# -gt 0 ] ; then
oe $1
fi
# Test the connection
export DB_ROLE=`sqlplus -s / as sysdba <<!
set pages 0
select DATABASE_ROLE from v\\$database;
exit
!`
echo $DB_ROLE | grep ORA- && return 1
return 0
}
is_standby ()
{
if [ $# -gt 0 ] ; then
oe $1
fi
# Test the connection
(sqlplus -s / as sysdba <<!
set pages 0
SELECT SYS_CONTEXT ('USERENV', 'DATABASE_ROLE') FROM DUAL;
exit
!
) | grep PRIMARY >/dev/null && return 1
return 0
}
is_clonedb ()
{
if [ $# -gt 0 ] ; then
oe $1
fi
# Test the connection
(sqlplus -s / as sysdba <<!
set pages 0
select VALUE from v\$parameter where NAME = 'clonedb';
exit
!
) | grep TRUE >/dev/null && return 0
return 1
}
dbname ()
{
if [ $# -gt 0 ] ; then
oe $1
fi
# Get the real database name
export DBNAME=`sqlplus -s / as sysdba <<!
set pages 0
select NAME from v\\$database;
exit
!`
return 0
}
chk_invalid ()
{
sqlplus -s / as sysdba <<!
SET serverout ON
SET FEED OFF
DECLARE
cnt PLS_INTEGER;
BEGIN
SELECT COUNT(*) INTO cnt FROM dba_objects WHERE status <> 'VALID';
IF cnt > 0 THEN
dbms_output.put_line(cnt || ' invalid objects, compiling . . .');
sys.utl_recomp.recomp_serial;
SELECT COUNT(*) INTO cnt FROM dba_objects WHERE status <> 'VALID';
END IF;
dbms_output.put_line(cnt || ' invalid objects');
END;
/
exit
!
}
validate_indexes ()
{
sqlplus -s / as sysdba <<!
set pages 0
set lines 200
set feed off
set head off
prompt set echo on
prompt set feed off
prompt set timi on
prompt set time on
select 'alter index ' || owner || '.' || index_name || ' rebuild nologging online;' from dba_indexes where status = 'UNUSABLE'
union
select 'alter index ' || index_owner || '.' || index_name || ' rebuild partition ' || partition_name || ' nologging online' || ';' from dba_ind_partitions where status = 'UNUSABLE';
exit
!
}
# Checks for the existance of a PDB, 1 argument is required, the name of the PDB to check
pdb_exists ()
{
ORACLE_PDB_SID=ORA\$ROOT
if [ $# -gt 1 ] ; then
return 1
fi
# Test the connection
(sqlplus -s / as sysdba <<!
set pages 0
show pdbs
exit
!
) | grep $1 && return 1
return 0
}
# Checks status of a PDB, 1 argument is required, the name of the PDB to check
is_pdb_open ()
{
ORACLE_PDB_SID=ORA\$ROOT
if [ $# -gt 1 ] ; then
return 1
fi
# Test the connection
(sqlplus -s / as sysdba <<!
set pages 0
show pdbs
exit
!
) | grep $1 | grep "READ WRITE" && return 1
return 0
}