From 2a2353b20f2b5da84e6428c42b160e3a4d190b00 Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Thu, 25 Jul 2019 16:33:14 -0300 Subject: [PATCH] PT-1663 pt-stalk improvements Implemented --retention-size and --retention-count --- bin/pt-stalk | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/bin/pt-stalk b/bin/pt-stalk index 2d65a88f..33adef2b 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -1352,6 +1352,8 @@ sleep_ok() { purge_samples() { local dir="$1" local retention_time="$2" + local retention_count="$3" + local retention_size="$4" # Delete collect files which more than --retention-time days old. find "$dir" -type f -mtime +$retention_time -exec rm -f '{}' \; @@ -1363,6 +1365,30 @@ purge_samples() { find "$oprofile_dir" -depth -type d -name 'pt_collect_*' \ -mtime +$retention_time -exec rm -rf '{}' \; fi + + targetCnt=$(($retention_count + 0)) + if [ $targetCnt -gt 0 ]; then + targetCnt=$(($retention_count + 1)) + files_to_delete=$(find $dir -type f -exec basename {} \; | cut -f1 -d- | sort -r | uniq | tail -n +${targetCnt}) + for prefix in $files_to_delete; do + echo "deleting files ${dir}${prefix}* according to the --retention-count param" + rm -f ${dir}${prefix}* 2>/dev/null + done + fi + + targetSize=$(($retention_size + 0)) + if [ $targetSize -gt 0 ]; then + files_to_delete=$(find $dir -type f -exec basename {} \; | cut -f1 -d- | sort -r | uniq | tail -n +1) + for prefix in $files_to_delete; do + current_size=$(du -BM $dir | cut -f1 -d"M") + if [ $current_size -gt $targetSize ]; then + echo "deleting files ${dir}${prefix}* according to the --retention-size param" + rm -f ${dir}${prefix}* 2>/dev/null + else + break + fi + done + fi } sigtrap() { @@ -1471,7 +1497,7 @@ stalk() { # Purge old collect files. if [ -d "$OPT_DEST" ]; then - purge_samples "$OPT_DEST" "$OPT_RETENTION_TIME" + purge_samples "$OPT_DEST" "$OPT_RETENTION_TIME" "$OPT_RETENTION_COUNT" "$OPT_RETENTION_SIZE" fi fi @@ -1496,7 +1522,7 @@ stalk() { # One final purge of old collect files, but only if in collect mode. if [ "$OPT_COLLECT" -a -d "$OPT_DEST" ]; then - purge_samples "$OPT_DEST" "$OPT_RETENTION_TIME" + purge_samples "$OPT_DEST" "$OPT_RETENTION_TIME" "$OPT_RETENTION_COUNT" "$OPT_RETENTION_SIZE" fi # Before exiting, the last collector may still be running. @@ -2120,6 +2146,20 @@ The filename prefix for diagnostic samples. By default, all files created by the same L<"--collect"> instance have a timestamp prefix based on the current local time, like C<2011_12_06_14_02_02>, which is December 6, 2011 at 14:02:02. +=item --retention-count + +type: int; default: 0 + +Keep the data for the last N runs. If N > 0, the program will keep the data for the last +N runs and will delete the older data. + +=item --retention-size + +type: int; default: 0 + +Keep up to --retention-size MB of data. It will keep at least 1 run even if the size is bigger +than the specified in this parameter + =item --retention-time type: int; default: 30