Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 72ff821

Browse files
author
John Dunham
committed
Adding the rollup logic to the archive. Fixing a typo in the rollup script (aFile->oFile).
Temporary file now removed after good write to weekly.
1 parent 23b0e47 commit 72ff821

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

csmdb/sql/csm_db_history_archive.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from datetime import date
3737
import threading
3838
from multiprocessing.dummy import Pool as ThreadPool
39+
from csm_db_rollup import rollupDir
3940

4041
DEFAULT_LOG='''/var/log/ibm/csm/db/csm_db_archive_script.log'''
4142
DEFAULT_TARGET='''/var/log/ibm/csm/archive'''
@@ -225,6 +226,13 @@ def main(args):
225226
# Verifies path exists.
226227
if not os.path.exists(args.target):
227228
os.makedirs(args.target)
229+
230+
# Make the temp directory for staging the tables for dumping.
231+
# This is used before writing to mitigate potential data loss.
232+
temp_dir = "{0}/tmp".format(args.target)
233+
if not os.path.exists(temp_dir):
234+
os.makedirs(temp_dir)
235+
228236

229237
# Process the script detail info. for screen and logging.
230238
logging.info("DB Name: | {0}".format(args.db))
@@ -243,18 +251,21 @@ def main(args):
243251

244252
pool = ThreadPool(int(args.threads))
245253

246-
tmp_list = pool.map( lambda table: dump_table( args.db, args.user, table, args.count, args.target ), TABLES )
254+
tmp_list = pool.map( lambda table: dump_table( args.db, args.user, table, args.count, temp_dir ), TABLES )
247255

248256
for entry in tmp_list:
249257
if entry is None:0
250258
else:
251259
print entry
252260

253261
for table in RAS_TABLES:
254-
entry = dump_table( args.db, args.user, table, args.count, args.target, True)
262+
entry = dump_table( args.db, args.user, table, args.count, temp_dir, True)
255263
if entry is None:0
256264
else:
257-
print entry
265+
print entry args.target
266+
267+
# After the tables are dumped, it's time to merge them into the weekly report.
268+
rollupDir(temp_dir,"..")
258269

259270
# Process the finishing info. for screen and logging.
260271
ft = datetime.datetime.now()

csmdb/sql/csm_db_rollup.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@
2727

2828
DEFAULT_TARGET='''/var/log/ibm/csm/archive'''
2929

30-
def rollupDir (directory):
30+
def rollupDir (directory, archiveTarget="old"):
3131
''' Performs a rollup operation on a directory, designed to condense in to weeks of data.
3232
This requires the files provided are in the format `<table>.archive.<date>.json`
3333
3434
directory -- A directory containing a collection of archive files to condense (string).
35+
archiveTarget -- A directory to store the rollups in.
3536
'''
3637
# If the directory is not present return early.
3738
if not os.path.exists(directory):
3839
print("{0} does not exist".format(directory))
3940
return
4041

41-
# Create an srchhive directory as needed.
42-
archiveDir="{0}/{1}".format(directory,"old")
42+
# Create an archive directory as needed.
43+
archiveDir="{0}/{1}".format(directory,archiveTarget)
4344
if not os.path.exists(archiveDir):
4445
os.makedirs(archiveDir)
4546

@@ -56,24 +57,23 @@ def rollupDir (directory):
5657
try:
5758
sFile=f.split(".")
5859
weekStr=datetime.strptime(sFile[2],"%Y-%m-%d").strftime("%Y-%U")
59-
aFile="{0}/{1}-{2}.json".format(archiveDir, sFile[0], weekStr)
60+
oFile="{0}/{1}-{2}.json".format(archiveDir, sFile[0], weekStr)
6061
except:
6162
continue
6263

6364
# Get the contents first
6465
contents=""
6566
with open(iFile,'r') as inputFile:
6667
contents=inputFile.read()
67-
6868
# remove and skip empties.
6969
if len(contents) == 0:
70-
os.remove(iFile)
70+
os.remove(iFile)
7171
continue
7272

7373
# Archive the contents.
7474
with open(oFile, 'a') as ofile:
7575
ofile.write(contents)
76-
os.remove(oFile)
76+
os.remove(iFile) # Remove only if the write was good!
7777

7878
def main(args):
7979
target = DEFAULT_TARGET

0 commit comments

Comments
 (0)