/
archivelogspace.py
346 lines (229 loc) · 9.16 KB
/
archivelogspace.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
archivelogspace.py
for a given database with a given size of archive log filesystem output
an estimate of space used and percent used of the filesystem after each
archive log is written. assumes we have backups at fixed times every day
which keep a fixed number of hours of archive log history.
pseudo code
get archivelog filesystem size in bytes
get backup times (hours,minutes,seconds) and hours of retention
assuming same backup times every day.
get username password and tnsname for database
read in size in bytes, write time, and name for all archivelogs
set bytes used in filesystem to 0
set last archive log time to 1/1/1900
set list of archive logs on disk to empty list
loop through all archivlogs in order of write time starting with oldest.
if backup time occurs between last archive log time and current archive log time:
remove all archive logs from list that are older than retention time and
subtract their size from bytes used in filesystem
add archivelog size to bytes used in filesytem
output bytes used in filesystem and percent used and date and time and archivelog name
add archivelog to list of archivelogs on disk
"""
import cx_Oracle
from datetime import datetime, time, timedelta
from sys import argv, exit
"""
Functions
"""
def get_log_info(username,password,tnsname):
"""
Return list of archive logs in order by time they were
written and sequence number.
"""
query = """
select
(BLOCKS+1)*BLOCK_SIZE,
COMPLETION_TIME,
NAME file_name
from
V$ARCHIVED_LOG
order by
COMPLETION_TIME,
SEQUENCE#
"""
connect_string = username+'/'+password+'@'+tnsname
con = cx_Oracle.connect(connect_string)
cur = con.cursor()
cur.execute(query)
log_info = []
for result in cur:
log_info.append(result)
cur.close()
con.close()
return log_info
def get_backup_dateetime(last_log_time,curr_log_dttime,backup_time):
"""
Get date and time of backup that runs at the given time and is between
the last log date and time and the current log date and time.
backup_time is just a time of day like 4 am
return None if none found
our assumption is that curr_log_dttime >= last_log_time
but they could be the same day or several days apart.
so I think we need to start with the day of curr_log_dttime and loop
backwards 24 hours at a time trying the backup time for each day to see
if it is between last_log_time and curr_log_dttime
if there are multiple days between the two logs pick the last one because
with the retention it will remove the most logs
"""
last_backup_datetime = datetime.combine(curr_log_dttime.date(),backup_time)
first_backup_datetime = datetime.combine(last_log_time.date(),backup_time)
day_interval = timedelta(days=1)
backup_datetime = last_backup_datetime
while backup_datetime >= first_backup_datetime:
if backup_datetime > last_log_time and backup_datetime < curr_log_dttime:
return backup_datetime
backup_datetime -= day_interval
return None
def check_for_backup(last_log_time,curr_log_dttime,backup_times):
"""
See if any of the backup times occur between the prev and
current logs
"""
for bkp in backup_times:
backup_time = bkp[0]
retention = bkp[1]
# get backup date and time that is between the last and current log datetimes
backup_datetime = get_backup_dateetime(last_log_time,curr_log_dttime,backup_time)
# return if found
if backup_datetime != None:
return backup_datetime,retention
return None, None
def remove_old_logs(backup_datetime,retention,logs_on_disk,bytes_used):
"""
remove logs from disk based on backup retention
"""
# remove logs from logs_on_disk list
# that exceed the backup retention
new_logs = []
for log in logs_on_disk:
# date and time of current log on disk being checked
log_ddtime = log[1]
# first condition checks time is > backup time
# second condition checks if datetime > previous log
# for log datetime + retention
if (log_ddtime + retention) > backup_datetime:
new_logs.append(log)
else:
size_bytes = log[0]
bytes_used -= size_bytes
logs_on_disk = new_logs
return logs_on_disk,bytes_used
def print_info(item,bytes_used,filesystem_size):
"""
print information for one archive log file
"""
if item[2] == None:
log_name = " "
else:
log_name = item[2]
percent_used = round((100*bytes_used)/filesystem_size,2)
log_size_bytes = item[0]
datetime_written = str(item[1])
print(datetime_written+' '+str(percent_used)+'% '+log_name)
def process_one_log(item,bytes_used,last_log_time,logs_on_disk,backup_times,filesystem_size):
"""
Logic for just one log in the list
item is the log entry like
(24576, datetime.datetime(2023, 12, 12, 3, 30, 29), '/myfs/MYDB/archive/MYDB1150995676_1_113.arc')
"""
# if backup time occurs between last archive log time and current archive log time:
# remove all archive logs from list that are older than retention time and
# subtract their size from bytes used in filesystem
# date and time current log was written
curr_log_dttime = item[1]
# Check for backup time between the previous and current logs
backup_datetime,retention = check_for_backup(last_log_time,curr_log_dttime,backup_times)
# Remove logs that backup would remove based on retention
if backup_datetime != None:
logs_on_disk,bytes_used = remove_old_logs(backup_datetime,retention,logs_on_disk,bytes_used)
# add archivelog size to bytes used in filesytem
bytes_used += item[0]
# add archivelog to list of archivelogs on disk
logs_on_disk.append(item)
# set last log time to current
last_log_time = curr_log_dttime
# output bytes used in filesystem and percent used and date and time and archivelog name
print_info(item,bytes_used,filesystem_size)
return bytes_used,last_log_time,logs_on_disk
def process_log_info(log_info,filesystem_size,backup_times):
"""
Main loop
"""
# set bytes used in filesystem to 0
bytes_used = 0
# set last archive log time to 1/1/1900 midnight
last_log_time = datetime(1900,1,1,0,0,0)
# set list of archive logs on disk to empty list
logs_on_disk = []
# keep track of max percent and date
max_percent_used = 0
max_date = last_log_time
# loop through all archivlogs in order of write time starting with oldest.
for item in log_info:
bytes_used,last_log_time,logs_on_disk = process_one_log(item,bytes_used,last_log_time,logs_on_disk,backup_times,filesystem_size)
pct_used = (100*bytes_used)/filesystem_size
if pct_used > max_percent_used:
max_percent_used = pct_used
max_date = last_log_time
return max_percent_used,max_date
def read_file(filename):
"""
Read in file in this format:
archivelog filesystem size in bytes
number of backups per day
one line per backup with 24-hour:minutes:seconds archivlog-retention-hours
"""
with open(filename, encoding="utf-8") as f:
lines = f.read().split('\n')
filesystem_size = int(lines[0])
num_backups = int(lines[1])
backup_times = []
for i in range(num_backups):
backup_line = lines[i+2]
hour = int(backup_line[0:2])
minute = int(backup_line[3:5])
second = int(backup_line[6:8])
backup_time=time(hour,minute,second)
retention_hours = timedelta(hours=int(backup_line[9:]))
backup = [backup_time,retention_hours]
backup_times.append(backup)
return filesystem_size,backup_times
"""
Main program
"""
# Get global variables
if len(argv) != 5:
print("""
Arguments: oracle-username oracle-password tns-name configfile-name
Config file is text file with this format:
archivelog filesystem size in bytes
number of backups per day
one line per backup with 24-hour:minutes:seconds archivlog-retention-hours
for example:
8795958804480
6
02:15:00 168
06:15:00 168
10:45:00 168
14:15:00 168
18:15:00 168
22:15:00 168
""")
exit()
# filename
filename = argv[4]
filesystem_size,backup_times = read_file(filename)
# username password and tnsname for database
username = argv[1]
password = argv[2]
tnsname = argv[3]
# read in size in bytes, write time, and name for all archivelogs
log_info = get_log_info(username,password,tnsname)
# do the main loop printing the space used in the filesystem after
# each archive log is added
max_percent_used,max_date = process_log_info(log_info,filesystem_size,backup_times)
print(' ')
print('Max percent used archivelog filesystem: '+str(round(max_percent_used,2))+'%')
print('Date and time of max percent: '+str(max_date))