From ba77de12acb7b3979f437097ad218d33a8c84152 Mon Sep 17 00:00:00 2001 From: Danny Allen <52468448+daall@users.noreply.github.com> Date: Fri, 13 Sep 2019 10:50:31 -0700 Subject: [PATCH] [cron.d] Add cron job to periodically clean-up core files (#3449) * [cron.d] Create cron job to periodically clean-up core files * Create script to scan /var/core and clean-up older core files * Create cron job to run clean-up script Signed-off-by: Danny Allen * Update interval for running cron job * Respond to feedback * Change syslog id --- build_debian.sh | 3 ++ files/image_config/cron.d/core_cleanup | 3 ++ files/scripts/core_cleanup.py | 51 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 files/image_config/cron.d/core_cleanup create mode 100755 files/scripts/core_cleanup.py diff --git a/build_debian.sh b/build_debian.sh index 061efb8d747b..c589d4acf14f 100755 --- a/build_debian.sh +++ b/build_debian.sh @@ -399,6 +399,9 @@ build_number: ${BUILD_NUMBER:-0} built_by: $USER@$BUILD_HOSTNAME EOF +## Copy over clean-up script +sudo cp ./files/scripts/core_cleanup.py $FILESYSTEM_ROOT/usr/bin/core_cleanup.py + ## Copy ASIC config checksum python files/build_scripts/generate_asic_config_checksum.py if [[ ! -f './asic_config_checksum' ]]; then diff --git a/files/image_config/cron.d/core_cleanup b/files/image_config/cron.d/core_cleanup new file mode 100644 index 000000000000..b1c379da9ebb --- /dev/null +++ b/files/image_config/cron.d/core_cleanup @@ -0,0 +1,3 @@ +# Attempts to clean up core files every 2 hours +* */2 * * * root /usr/bin/core_cleanup.py > /dev/null 2>&1 + diff --git a/files/scripts/core_cleanup.py b/files/scripts/core_cleanup.py new file mode 100755 index 000000000000..67620b2397de --- /dev/null +++ b/files/scripts/core_cleanup.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +import syslog +import os + +from collections import defaultdict +from datetime import datetime + +SYSLOG_IDENTIFIER = 'core_cleanup.py' +CORE_FILE_DIR = os.path.basename(__file__) +MAX_CORE_FILES = 4 + +def log_info(msg): + syslog.openlog(SYSLOG_IDENTIFIER) + syslog.syslog(syslog.LOG_INFO, msg) + syslog.closelog() + +def log_error(msg): + syslog.openlog(SYSLOG_IDENTIFIER) + syslog.syslog(syslog.LOG_ERR, msg) + syslog.closelog() + +def main(): + if os.getuid() != 0: + log_error('Root required to clean up core files') + return + + log_info('Cleaning up core files') + core_files = [f for f in os.listdir(CORE_FILE_DIR) if os.path.isfile(os.path.join(CORE_FILE_DIR, f))] + + core_files_by_process = defaultdict(list) + for f in core_files: + process = f.split('.')[0] + curr_files = core_files_by_process[process] + curr_files.append(f) + + if len(curr_files) > MAX_CORE_FILES: + curr_files.sort(reverse = True, key = lambda x: datetime.utcfromtimestamp(int(x.split('.')[1]))) + oldest_core = curr_files[MAX_CORE_FILES] + log_info('Deleting {}'.format(oldest_core)) + try: + os.remove(os.path.join(CORE_FILE_DIR, oldest_core)) + except: + log_error('Unexpected error occured trying to delete {}'.format(oldest_core)) + core_files_by_process[process] = curr_files[0:MAX_CORE_FILES] + + log_info('Finished cleaning up core files') + +if __name__ == '__main__': + main() +