aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/osmo_pcap_clean_old
blob: 9409abf6f915f9182763937ac5b0b2304db6c996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /bin/sh

# Script designed to clean up (zip/delete) old files
# Adjust the variables below and then copy/symlink this script
# to /etc/cron/cron.{hourly,daily}

# We want to keep the filenames dated and that confuses logrotate,
# hence this script.

# Method used - either NAME, CTIME, or FILES
METHOD="NAME"
# Maximum age of the logs
MAXAGE=120
# Maximum number of logs to keep
MAXFILES=30
# Zip all files after the first n files
ZIPAFTER=3
# Set to 1 for debug output
VERBOSE=0

# Path where the logfiles reside in
BASEPATH="/var/lib/osmo-pcap/"


# Find the client names present in basepath
# Delete files older than MAXAGE days
# Zip all but the first ZIPAFTER files
cd "$BASEPATH"


do_cleanup_ctime()
{
	find . -ctime +$MAXAGE -name "trace-$1-*.pcap*" |sort -r | while read LOG; do
		[ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\""
		rm -f "$LOG"
	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
	find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do
		if [ $i -gt $MAXFILES ]; then
			[ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\""
			rm -f "$LOG"
		fi
		i=$(($i+1))
	done
}

do_zip()
{
	i=1
	find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do
		if [ $i -gt $ZIPAFTER ]; then
				if [ "${LOG##*.}" != "gz" ]; then
					[ $VERBOSE -eq 1 ] && echo "Compressing file \"$LOG\""
					gzip "$LOG"
				fi
		fi
		i=$(($i+1))
	done
}

# Use an explicit pattern here
find . -name "trace-*.pcap*" |sed -n -e "s/.*trace-\(.\+\)-[0-9]\{8\}_[0-9]\{6\}\.pcap\(\..\+\)\?/\1/p" |sort |uniq | while read CLIENT; do

	[ $VERBOSE -eq 1 ] && echo "Cleaning logs for $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 NAME, CTIME or FILES"
		exit 1
	fi

	do_zip "$CLIENT"
done