-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunSql.py
executable file
·108 lines (92 loc) · 4.35 KB
/
runSql.py
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
import os
from subprocess import Popen, PIPE
from optparse import OptionParser
# ------------ Function Definitions ------------ #
def getAllOracleSids():
oracleSids = []
p1 = Popen(["ps", "-ef"], stdout=PIPE)
p2 = Popen(["grep", "pmon"], stdin=p1.stdout, stdout=PIPE)
p3 = Popen(["grep", "-v", "grep"], stdin=p2.stdout, stdout=PIPE)
p4 = Popen(["grep", "-v", "ASM"], stdin=p3.stdout, stdout=PIPE)
p3.stdout.close()
p2.stdout.close()
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p4.communicate()[0].split(os.linesep)
for line in output:
if line:
oracleSids.append(line.split("_")[-1])
return oracleSids
def checkSid(instances):
for instance in instances:
if instance not in getAllOracleSids():
parser.error("Instance \"%s\" not a valid oracle sid" % (instance))
return True
def checkScript(sqlFile):
if not os.path.isfile(sqlFile):
parser.error("File \"%s\" not found." % (os.path.abspath(sqlFile)))
return True
def runSqlQuery(sqlCommand, connectString, instanceName):
session = Popen(['sqlplus', '-S', connectString],
stdin=PIPE, stdout=PIPE, stderr=PIPE)
session.stdin.write(sqlCommand)
return session.communicate()
# ------------ Main Routine ------------ #
instanceNames = []
parser = OptionParser()
parser.add_option("-u", "--user", dest="dbUser",
help="database user", metavar="DB_USER")
parser.add_option("-p", "--password", dest="dbPassword",
help="database password", metavar="DB_PASSWORD")
parser.add_option("-i", "--instance", dest="instanceNames", action="append",
type="string", help="INSTANCE NAME to connect accept " +
"multiple entries [-i <instance1>, ... -i <instanceN>] or " +
"[-i ALL] for all instances",
metavar="INSTANCE_NAME")
parser.add_option("-s", "--script", dest="sqlScript",
help="SQL SCRIPT to be executed", metavar="SQL_SCRIPT")
(options, args) = parser.parse_args()
if not options.instanceNames or not options.sqlScript:
parser.error("Incorrect Number of Arguments")
elif "ALL" in str(options.instanceNames).upper():
checkScript(options.sqlScript)
print("Instances: All")
else:
checkScript(options.sqlScript)
checkSid(options.instanceNames)
if options.dbUser and options.dbPassword:
connectString = "%s/%s" % (options.dbUser, options.dbPassword)
else:
connectString = "/ as sysdba"
sqlCommand = "@" + options.sqlScript
if os.path.dirname(options.sqlScript):
logFile = os.path.dirname(options.sqlScript) + "/log_" + os.path.split(options.sqlScript)[1].replace(".sql", "")
else:
logFile = "log_" + os.path.split(options.sqlScript)[1].replace(".sql", "")
if os.path.isfile(logFile):
os.remove(logFile)
with open(logFile, 'a') as logF:
if "ALL" in str(options.instanceNames).upper():
for sid in getAllOracleSids():
os.environ['ORACLE_SID'] = sid
queryResult, queryError = runSqlQuery(sqlCommand, connectString,
options.instanceNames[0])
logF.write("Result Set %s: \n%s" % (sid,
# time.strftime("%Y%m%d-%H%M"),
queryResult))
if queryError:
logF.write("Eror %s: \n%s" % (sid,
# time.strftime("%Y%m%d-%H%M"),
queryError))
else:
print("Instance: %s" % str(options.instanceNames))
for sid in options.instanceNames:
os.environ['ORACLE_SID'] = sid
queryResult, queryError = runSqlQuery(sqlCommand, connectString,
options.instanceNames[0])
logF.write("Result Set %s: \n%s" % (sid,
# time.strftime("%Y%m%d-%H%M"),
queryResult))
if queryError:
logF.write("Eror %s: \n%s" % (sid,
# time.strftime("%Y%m%d-%H%M"),
queryError))