Skip to content

Commit ba77de1

Browse files
daallyxieca
authored andcommitted
[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 <[email protected]> * Update interval for running cron job * Respond to feedback * Change syslog id
1 parent 94f2113 commit ba77de1

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

build_debian.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ build_number: ${BUILD_NUMBER:-0}
399399
built_by: $USER@$BUILD_HOSTNAME
400400
EOF
401401

402+
## Copy over clean-up script
403+
sudo cp ./files/scripts/core_cleanup.py $FILESYSTEM_ROOT/usr/bin/core_cleanup.py
404+
402405
## Copy ASIC config checksum
403406
python files/build_scripts/generate_asic_config_checksum.py
404407
if [[ ! -f './asic_config_checksum' ]]; then
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Attempts to clean up core files every 2 hours
2+
* */2 * * * root /usr/bin/core_cleanup.py > /dev/null 2>&1
3+

files/scripts/core_cleanup.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
import syslog
4+
import os
5+
6+
from collections import defaultdict
7+
from datetime import datetime
8+
9+
SYSLOG_IDENTIFIER = 'core_cleanup.py'
10+
CORE_FILE_DIR = os.path.basename(__file__)
11+
MAX_CORE_FILES = 4
12+
13+
def log_info(msg):
14+
syslog.openlog(SYSLOG_IDENTIFIER)
15+
syslog.syslog(syslog.LOG_INFO, msg)
16+
syslog.closelog()
17+
18+
def log_error(msg):
19+
syslog.openlog(SYSLOG_IDENTIFIER)
20+
syslog.syslog(syslog.LOG_ERR, msg)
21+
syslog.closelog()
22+
23+
def main():
24+
if os.getuid() != 0:
25+
log_error('Root required to clean up core files')
26+
return
27+
28+
log_info('Cleaning up core files')
29+
core_files = [f for f in os.listdir(CORE_FILE_DIR) if os.path.isfile(os.path.join(CORE_FILE_DIR, f))]
30+
31+
core_files_by_process = defaultdict(list)
32+
for f in core_files:
33+
process = f.split('.')[0]
34+
curr_files = core_files_by_process[process]
35+
curr_files.append(f)
36+
37+
if len(curr_files) > MAX_CORE_FILES:
38+
curr_files.sort(reverse = True, key = lambda x: datetime.utcfromtimestamp(int(x.split('.')[1])))
39+
oldest_core = curr_files[MAX_CORE_FILES]
40+
log_info('Deleting {}'.format(oldest_core))
41+
try:
42+
os.remove(os.path.join(CORE_FILE_DIR, oldest_core))
43+
except:
44+
log_error('Unexpected error occured trying to delete {}'.format(oldest_core))
45+
core_files_by_process[process] = curr_files[0:MAX_CORE_FILES]
46+
47+
log_info('Finished cleaning up core files')
48+
49+
if __name__ == '__main__':
50+
main()
51+

0 commit comments

Comments
 (0)