blob: 42fec2d80a8fb654f95d4b52a429b10493e32d70 [file] [log] [blame]
Jon Hall73cf9cc2014-11-20 22:28:38 -08001#!/bin/bash
2
3
4# NOTE: Taken fnd modified from onos.sh
5# pack-rotate-log [packname] "[log-filenames]" [max rotations]
6# Note: [packname] and all the log-files specified by [log-filenames]
7# must reside in same dir
8# Example:
9# pack="/foo/bar/testlogs"
10# logfiles="/foo/bar/test1.log /foo/bar/test*.log"
11# pack-rotate-log $pack "$logfiles" 5
12# => testlogs.tar.bz2 (will contain test1.log test2.log ...)
13# testlogs.tar.bz2 -> testlogs.1.tar.bz2
14# testlogs.1.tar.bz2 -> testlogs.2.tar.bz2
15# ...
16function pack-rotate-log {
17 local packname=$1
18 local logfiles=$2
19 local nr_max=${3:-10}
20 local suffix=".tar.bz2"
21
22 # rotate
23 for i in `seq $(expr $nr_max - 1) -1 1`; do
24 if [ -f ${packname}.${i}${suffix} ]; then
25 mv -f -- ${packname}.${i}${suffix} ${packname}.`expr $i + 1`${suffix}
26 fi
27 done
28 if [ -f ${packname}${suffix} ]; then
29 mv -- ${packname}${suffix} ${packname}.1${suffix}
30 fi
31
32 # pack
33 local existing_logfiles=$( ls -1 $logfiles 2>/dev/null | xargs -n1 basename 2>/dev/null)
34 if [ ! -z "${existing_logfiles}" ]; then
35 tar cjf ${packname}${suffix} -C `dirname ${packname}` -- ${existing_logfiles}
36 for word in ${existing_logfiles}
37 do
38 rm -- `dirname ${packname}`/${word}
39 done
40 fi
41}
42
43
44
45#Begin script
46#NOTE: This seems to break the TestON summary since it mentions the testname
47#echo "Rotating logs for '${1}' test"
48base_name=$1
49root_dir="/home/admin/packet_captures"
50timestamp=`date +%Y_%B_%d_%H_%M_%S`
51#Maybe this should be an argument? pack-and-rotate supports that
52nr_max=20
53
Jon Hall63604932015-02-26 17:09:50 -080054pack-rotate-log ${root_dir}'/'${base_name} "${root_dir}/${base_name}*.pcap ${root_dir}/${base_name}*.log*" ${nr_max}