From 5a1c292140e2823a1216da23c5ac5221223e295b Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Thu, 22 Sep 2011 17:00:57 +0200 Subject: contrib: Add a new method to the cleanup script With the "NAME" method the filename is used for the age check instead of ctime. The "AGE" method which used ctime has been renamed to "CTIME". --- contrib/osmo_pcap_clean_old | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/contrib/osmo_pcap_clean_old b/contrib/osmo_pcap_clean_old index 0f4fc6c..9409abf 100755 --- a/contrib/osmo_pcap_clean_old +++ b/contrib/osmo_pcap_clean_old @@ -7,8 +7,8 @@ # We want to keep the filenames dated and that confuses logrotate, # hence this script. -# Method used either AGE or FILES -METHOD="AGE" +# Method used - either NAME, CTIME, or FILES +METHOD="NAME" # Maximum age of the logs MAXAGE=120 # Maximum number of logs to keep @@ -28,7 +28,7 @@ BASEPATH="/var/lib/osmo-pcap/" cd "$BASEPATH" -do_cleanup_age() +do_cleanup_ctime() { find . -ctime +$MAXAGE -name "trace-$1-*.pcap*" |sort -r | while read LOG; do [ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\"" @@ -36,6 +36,23 @@ do_cleanup_age() done } +do_cleanup_name() +{ + CUTOFF=$(date -d "${MAXAGE} days ago" +%Y%m%d) + find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do + # Retrieve the date from the filename + # We only need day accuracy so ignore the time + CURRENT=${LOG%_*} + CURRENT=${CURRENT#./trace-$1-} + + # Date is before the cutoff date, delete + if [ $CURRENT -lt $CUTOFF ]; then + [ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\"" + rm -f "$LOG" + fi + done +} + do_cleanup_files() { i=1 @@ -67,12 +84,14 @@ find . -name "trace-*.pcap*" |sed -n -e "s/.*trace-\(.\+\)-[0-9]\{8\}_[0-9]\{6\} [ $VERBOSE -eq 1 ] && echo "Cleaning logs for $CLIENT" - if [ "x$METHOD" == "xAGE" ]; then - do_cleanup_age "$CLIENT" + if [ "x$METHOD" == "xNAME" ]; then + do_cleanup_name "$CLIENT" + elif [ "x$METHOD" == "xCTIME" ]; then + do_cleanup_ctime "$CLIENT" elif [ "x$METHOD" == "xFILES" ]; then do_cleanup_files "$CLIENT" else - echo "Error, set METHOD to AGE or FILES" + echo "Error, set METHOD to NAME, CTIME or FILES" exit 1 fi -- cgit v1.2.3