aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/osmo_pcap_clean_old
blob: 0f4fc6ca0eacdfe6612d5b4095a4af88a4c85ba3 (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
#! /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 AGE or FILES
METHOD="AGE"
# 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_age()
{
	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_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" == "xAGE" ]; then
		do_cleanup_age "$CLIENT"
	elif [ "x$METHOD" == "xFILES" ]; then
		do_cleanup_files "$CLIENT"
	else
		echo "Error, set METHOD to AGE or FILES"
		exit 1
	fi

	do_zip "$CLIENT"
done