forked from spacetelescope/jwql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request spacetelescope#1456 from bhilbert4/clean-old-log-f…
…iles Add script to automatically delete old log files
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#! /usr/bin/env python | ||
|
||
"""Clean old log files from the collection of log files | ||
Authors | ||
------- | ||
- Bryan Hilbert | ||
Use | ||
--- | ||
To delete log files that are older than some threshold age: | ||
:: | ||
> python clean_old_log_files.py --time_limit 7 # days | ||
""" | ||
import argparse | ||
from datetime import datetime, timedelta | ||
import os | ||
import socket | ||
|
||
from jwql.utils.utils import get_config | ||
|
||
HOSTNAME = socket.gethostname() | ||
configs = get_config() | ||
LOG_BASE_DIR = configs['log_dir'] | ||
|
||
|
||
def define_options(): | ||
"""Create parser to take the time limit. | ||
Returns | ||
------- | ||
parser : argparse.ArgumentParser | ||
Parser containing time limit | ||
""" | ||
usage = 'clean_old_log_files.py -t 14' | ||
parser = argparse.ArgumentParser(usage=usage) | ||
parser.add_argument('-t', '--time_limit', type=int, default=14, | ||
help='Time limit in days. Log files older than this will be deleted.') | ||
return parser | ||
|
||
|
||
def run(time_limit=timedelta(days=14)): | ||
"""Look through log directories and delete log files that are older than ``time_limit``. | ||
Have time_limit default to be 14 days. | ||
Inputs | ||
------ | ||
time_limit : datetime.timdelta | ||
Files older than this time limit will be deleted | ||
""" | ||
now = datetime.now() | ||
|
||
if 'pljwql' in HOSTNAME: | ||
subdir = 'ops' | ||
elif 'tljwql' in HOSTNAME: | ||
subdir = 'test' | ||
else: | ||
# This should cover the dev server as well as local machines | ||
subdir = 'dev' | ||
|
||
log_dir = os.path.join(LOG_BASE_DIR, subdir) | ||
for logtype in os.scandir(log_dir): | ||
if logtype.is_dir(): | ||
for item in os.scandir(logtype): | ||
# We only try to delete log files produced by the machine on which | ||
# this script is running. e.g. log files produced by the test server | ||
# can only be deleted by running this script on the test server. | ||
if HOSTNAME in item.name and item.name[-4:] == '.log': | ||
stat_result = item.stat() | ||
last_modified_time = datetime.fromtimestamp(stat_result.st_mtime) | ||
age = now - last_modified_time | ||
if age > time_limit: | ||
full_path = os.path.join(log_dir, logtype, item) | ||
os.remove(full_path) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = define_options() | ||
args = parser.parse_args() | ||
run(timedelta(days=args.time_limit)) |