/
ibm_db_v1.py
179 lines (148 loc) · 5.32 KB
/
ibm_db_v1.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
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
import threading
import time
import queue
'''
Sample framework to collect data from multiple sources and insert into repos.
Add args for sample interval
Sudip
'''
import ibm_db
print (ibm_db.__version__)
'''
Global variable used by threads and locking
'''
# Hold list of db's to process
global_db_list = []
# Hold data collected from list of db's to process
global_fifo_q = queue.Queue()
#Thread lock
tLock = threading.Lock()
def get_db_list (admin_db, user):
global global_db_list
db_list = []
tLock.acquire()
print ("db_list has acquired a lock")
# Get password
str = open('C:/Users/sxk11/PycharmProjects/Safari_1/test.txt', 'r').read()
conn = ibm_db.connect('ADM1P', 'tu01945', str)
sql = 'SELECT * FROM DB2ADM1P.UDB_DB_NAMES ' \
'where ' \
'db_all_chk_ind = \'Y\' ' \
'and db_alias in (\'LSS1D\', \'EDT5D\', \'EDT6D\', \'DBI1D\') '
# 'db_status = \'D\' '\
stmt = ibm_db.prepare(conn, sql)
ibm_db.execute(stmt)
tuple = ibm_db.fetch_tuple(stmt)
while tuple != False:
print("The ID is : ", tuple[0])
print("The alias name is : ", tuple[3])
db_name = tuple[2]
db_list.append(db_name)
tuple = ibm_db.fetch_tuple(stmt)
print(db_list)
tLock.release()
print ("db_list has completed lock released")
global_db_list = db_list
def run_sql (db_name):
global global_fifo_q
str = open('C:/Users/sxk11/PycharmProjects/Safari_1/test.txt', 'r').read()
#print (db_name)
try:
conn = ibm_db.connect(db_name, 'tu01945', str)
except:
print ("NO connection to :" + db_name)
#print(ibm_db.stmt_errormsg())
#print(ibm_db.stmt_error())
if (ibm_db.conn_error()):
print("conn_error:")
print(ibm_db.conn_error())
while True:
time.sleep(5)
try:
conn = ibm_db.connect(db_name, 'tu01945', str)
if(conn):
print("Connection reconnected to :" + db_name)
break
except:
print("NO connection to :" + db_name)
#Send Notification
#Retry
else:
print ("The connection to " + db_name + " was successful!")
#print (conn)
sql = 'select snapshot_timestamp, db_name, catalog_partition from sysibmadm.snapdb'
#time.sleep(30)
while True:
try:
stmt = ibm_db.exec_immediate(conn, sql)
#stmt = ibm_db.prepare(conn, sql)
#ibm_db.execute(stmt)
except:
print("Transaction couldn't be completed:")
print (ibm_db.stmt_errormsg())
print (ibm_db.stmt_error())
if (ibm_db.conn_error()):
print("conn_error:")
print(ibm_db.conn_error())
while True:
time.sleep(5)
try:
conn = ibm_db.connect(db_name, 'tu01945', str)
if (conn):
print("Connection reconnected to :" + db_name)
break
except:
print("NO connection to :" + db_name)
else:
tuple = ibm_db.fetch_tuple(stmt)
while tuple != False:
#print("The time is : ", tuple[0])
#print("The catalog name is : ", tuple[1])
#print("The catalog partition is : ", tuple[2])
#Store Data in queue
data = (tuple[1], tuple[0], tuple[2])
global_fifo_q.put(data)
#Get next
tuple = ibm_db.fetch_tuple(stmt)
time.sleep(10)
#finally:
# ibm_db.close(conn)
class StoreData:
def __init__(self, target_db, db_user):
self.target_db = target_db
self.db_user = db_user
self.db_pass = open('C:/Users/sxk11/PycharmProjects/Safari_1/test.txt', 'r').read()
self.dbconn = ibm_db.connect(self.target_db, self.db_user, self.db_pass)
print ("Connecting to target...")
print(self.dbconn)
def retrieve_and_store(self):
global global_fifo_q
while True:
if not global_fifo_q.empty():
data_from_q = global_fifo_q.get()
db2_datetime = data_from_q[1].strftime('%Y-%m-%d-%H.%M.%S.%f')
#print (db2_datetime)
#print(data_from_q)
stmt = ibm_db.prepare(self.dbconn, "insert into mrj3017.python_test1 (col1, col2, col3) values (?,?,?)")
ibm_db.execute(stmt, (data_from_q[0],db2_datetime,data_from_q[2],))
def Main():
t1 = threading.Thread(target=get_db_list, name = "Get db list", args=("ADM1P","tu01945",))
t1.start()
t1.join()
print (global_db_list)
#for db_name in global_db_list:
# print (db_name)
for db_name in global_db_list:
t2 = threading.Thread(target=run_sql, name = db_name ,args=(db_name,))
t2.start()
#t2.join()
#t3 = threading.Thread(target=get_q_data(), name = 'Retrieve Q Data')
#t3.start()
s = StoreData("ADM1P", "tu01945")
st = threading.Thread(target=s.retrieve_and_store, name = 'Retrieve Q Data', args=())
st.start()
t2.join()
#t3.join()
print ("Main completeted")
if __name__ == '__main__':
Main()