-
Notifications
You must be signed in to change notification settings - Fork 16
/
spdo
executable file
·157 lines (138 loc) · 6.25 KB
/
spdo
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
#!/bin/env python
##################################################################################################
# Name: spdo #
# Author: Randy Johnson #
# Description: Prints a report of DBA_SQL_PLAN_DIR_OBJECTS. #
# #
# History: #
# #
# Date Ver. Who Change Description #
# ---------- ---- ---------------- ------------------------------------------------------------- #
# 09/29/2015 1.00 Randy Johnson Initial write. #
##################################################################################################
# --------------------------------------
# ---- Import Python Modules -----------
# --------------------------------------
from optparse import OptionParser
from os import environ
from os.path import basename
from sys import argv
from sys import exit
from sys import version_info
from signal import SIGPIPE
from signal import SIG_DFL
from signal import signal
from Oracle import RunSqlplus
from Oracle import SetOracleEnv
from Oracle import ParseConnectString
# --------------------------------------
# ---- Main Program --------------------
# --------------------------------------
if (__name__ == '__main__'):
Cmd = basename(argv[0]).split('.')[0]
CmdDesc = 'SQL Plan Directive Objects'
Version = '1.00'
VersionDate = 'Mon Sep 28 13:35:14 CDT 2015'
DevState = 'Production'
Banner = CmdDesc + ': Release ' + Version + ' ' + DevState + '. Last updated: ' + VersionDate
Sql = ''
SqlHeader = '/***** ' + CmdDesc.upper() + ' *****/'
ErrChk = False
ArgParser = OptionParser()
InStr = ''
ConnStr = ''
OwnerList = []
ExcludeList = []
ExcludeList.append('ANONYMOUS')
ExcludeList.append('CTXSYS')
ExcludeList.append('DBSNMP')
ExcludeList.append('DIP')
ExcludeList.append('DMSYS')
ExcludeList.append('EXFSYS')
ExcludeList.append('MDSYS')
ExcludeList.append('MGMT_VIEW')
ExcludeList.append('OLAPSYS')
ExcludeList.append('ORDPLUGINS')
ExcludeList.append('ORDDATA')
ExcludeList.append('ORDSYS')
ExcludeList.append('OUTLN')
ExcludeList.append('SI_INFORMTN_SCHEMA')
ExcludeList.append('SYS')
ExcludeList.append('SYSMAN')
ExcludeList.append('SYSTEM')
ExcludeList.append('WMSYS')
ExcludeList.append('XDB')
ExcludeList.append('XS$NULL')
# For handling termination in stdout pipe; ex: when you run: oerrdump | head
signal(SIGPIPE, SIG_DFL)
ArgParser.add_option('-f', dest='Filter', action='store_true', default=False, help="filter out Oracle schemas: sys, system, dbsnmp, ...")
ArgParser.add_option('-o', dest='Owners', default='', type=str, help="where owner in (owner1,owner2,owner3, ...)")
ArgParser.add_option('-s', dest='SortBy', default='', type=str, help="order by 1,2,3 ... (default = owner, object_name)")
ArgParser.add_option('--s', dest='Show', action='store_true', default=False, help="print SQL query.")
ArgParser.add_option('--v', dest='ShowVer', action='store_true', default=False, help="print version info.")
# Parse command line arguments
Options, args = ArgParser.parse_args()
Filter = Options.Filter
Owners = Options.Owners
SortBy = Options.SortBy
Show = Options.Show
ShowVer = Options.ShowVer
if (ShowVer == True):
print('\n%s' % Banner)
exit()
if(Owners != ''):
OwnerList = Owners.upper().split(',')
Sql += "column directive_id format 999999999999999999999999 heading 'Directive ID'\n"
Sql += "col owner format a25 heading 'Owner'\n"
Sql += "col object_name format a30 heading 'Object Name'\n"
Sql += "col subobject_name format a30 heading 'Subobject Name'\n"
Sql += "col object_type format a6 heading 'Type'\n"
Sql += "col notes format a71 heading 'Notes'\n"
Sql += "\n"
Sql += " SELECT " + SqlHeader + "\n"
Sql += " directive_id\n"
Sql += " , owner\n"
Sql += " , object_name\n"
Sql += " , subobject_name\n"
Sql += " , object_type\n"
Sql += " , notes\n"
Sql += " FROM dba_sql_plan_dir_objects\n"
Sql += " WHERE 1=1\n"
if (OwnerList != []):
Sql += " AND owner IN ('" + "','".join(OwnerList) + "')\n"
if (Filter and ExcludeList != []):
Sql += " AND owner NOT IN ( '" + "'\n ,'".join(ExcludeList) + "'\n )\n"
if (SortBy != ''):
Sql += "ORDER BY " + SortBy + ";"
else:
Sql += "ORDER BY 2,3;"
Sql = Sql.strip()
if(Show):
print('-----------cut-----------cut-----------cut-----------cut-----------cut-----------')
print(Sql)
print('-----------cut-----------cut-----------cut-----------cut-----------cut-----------')
exit()
# Check/setup the Oracle environment
if (not('ORACLE_SID' in list(environ.keys()))):
print('ORACLE_SID is required.')
exit(1)
else:
# Set the ORACLE_HOME just in case it isn't set already.
if (not('ORACLE_HOME' in list(environ.keys()))):
(OracleSid, OracleHome) = SetOracleEnv(environ['ORACLE_SID'])
# Parse the connect string if any, prompt for username, password if needed.
if (len(args) > 0 and Show == False):
InStr = args[0]
ConnStr = ParseConnectString(InStr)
# Execute the report
if (ConnStr != ''):
(Stdout) = RunSqlplus(Sql, ErrChk, ConnStr)
else:
(Stdout) = RunSqlplus(Sql, ErrChk)
# Print the report
if (Stdout != ''):
print('\n%s' % Stdout)
exit(0)
# --------------------------------------
# ---- End Main Program ----------------
# --------------------------------------