From cfc4ae1e51fa480377b1f58d8d3b7b10cf079314 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 8 Aug 2016 12:18:42 -0400 Subject: [PATCH] MT#20499 add cleanup script for old cdr files Change-Id: I46b3471e5ac6f0e026dfa1d1454e7b9eff9ec1be --- cleanup-old-cdr-files.pl | 49 +++++++++++++++++++++++++++ cleanup-tools.cron | 1 + debian/ngcp-cleanup-tools-ce.install | 1 + debian/ngcp-cleanup-tools-pro.install | 1 + 4 files changed, 52 insertions(+) create mode 100755 cleanup-old-cdr-files.pl diff --git a/cleanup-old-cdr-files.pl b/cleanup-old-cdr-files.pl new file mode 100755 index 0000000..47fc6a8 --- /dev/null +++ b/cleanup-old-cdr-files.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Config::Any; +use File::Find; + +my $cfgs = Config::Any->load_files({files => ['/etc/ngcp-cleanup-tools/cdr-files-cleanup.yml'], + use_ext => 1, flatten_to_hash => 1}); +if (!$cfgs || !values(%{$cfgs})) { + die("no config found"); +} +my %cfg = map {%{$_}} values(%{$cfgs}); + +($cfg{enabled} // 'no') =~ /1|yes/ or exit(0); + +my $now = time(); +my $maxage; + +for my $p (@{$cfg{paths}}) { + $maxage = $p->{max_age_days} // $cfg{max_age_days}; + $maxage or next; + $maxage *= 86400; + my @paths = ($p->{path}); + if ($p->{wildcard} =~ /1|yes/) { + @paths = glob($p->{path}); + } + for my $path (@paths) { + -d $path or next; + finddepth(\&recurser, $path); + } +} + +sub recurser { + my $path = $File::Find::name; + if (-d $path) { + #print("directory: $path\n"); + rmdir($path); # ignore errors + } + elsif (-f $path) { + #print("file: $path\n"); + my @sb = stat($path); + @sb or return; + my $age = $now - $sb[9]; + $age < $maxage and return; + #print("delete: $path\n"); + unlink($path); + } +} diff --git a/cleanup-tools.cron b/cleanup-tools.cron index c4fbbb3..95c6ccd 100644 --- a/cleanup-tools.cron +++ b/cleanup-tools.cron @@ -4,5 +4,6 @@ [% ELSE -%] 00 03 * * * root /usr/sbin/acc-cleanup.pl > /dev/null 2>&1 [% END -%] +29 03 * * * root /usr/sbin/cleanup-old-cdr-files.pl > /dev/null 2>&1 #05 03 * * * root /usr/sbin/binlog-purge.pl > /dev/null 2>&1 */59 * * * * root [ -d /var/spool/asterisk/voicemail/default/ ] && find /var/spool/asterisk/voicemail/default/ -maxdepth 1 -mmin +59 -type d ! -name default -exec rm -rf '{}' \; || true diff --git a/debian/ngcp-cleanup-tools-ce.install b/debian/ngcp-cleanup-tools-ce.install index 27edb7b..fcd2318 100644 --- a/debian/ngcp-cleanup-tools-ce.install +++ b/debian/ngcp-cleanup-tools-ce.install @@ -1,3 +1,4 @@ acc-cleanup.conf etc/ngcp-cleanup-tools/ acc-cleanup.pl usr/sbin/ +cleanup-old-cdr-files.pl usr/sbin/ ce/cleanup-tools etc/cron.d/ diff --git a/debian/ngcp-cleanup-tools-pro.install b/debian/ngcp-cleanup-tools-pro.install index f856366..c7fe254 100644 --- a/debian/ngcp-cleanup-tools-pro.install +++ b/debian/ngcp-cleanup-tools-pro.install @@ -1,5 +1,6 @@ acc-cleanup.conf etc/ngcp-cleanup-tools/ acc-cleanup.pl usr/sbin/ +cleanup-old-cdr-files.pl usr/sbin/ moh-cleanup usr/sbin/ elasticsearch-remove-old-indices usr/sbin/ pro/cleanup-tools etc/cron.d/