-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccs-log-compress
executable file
·141 lines (95 loc) · 2.63 KB
/
ccs-log-compress
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
### Compress/archive CCS log files.
logdir=/var/log/ccs
PN=${0##*/}
nice_level=10
## Minimum age of logs to operate on. 1 = yesterday.
min_days=2
## Maximum number of days to loop over.
## If running this every day, this could be small.
max_days=100
## d=delete, m=move, anything else=do nothing
archive_flag=m
## Archive files older than this many days.
archive_days=30
## If archiving by moving, where to put the files.
archive_dir=/gpfs/slac/lsst/fs3/g/data/ccs-logs
DEBUG=
## Date format used by ccs log files.
dformat="+%Y%m%d"
die () {
echo "$@" 1>&2
exit 1
}
ncompress=0
compress () {
local log=$1
[[ $log == *.gz ]] && return 0 # don't recompress
## If log.gz already exists, let's not abort. Better than using gzip -f.
## TODO compress to ${log}_#.gz instead?
[ -e "$log.gz" ] && return 0
(( ncompress++ ))
gzip "$log"
}
narchive=0
archive () {
local log=$1
## Only archive compressed files.
[[ $log == *.gz ]] || {
compress "$log" || return $?
log=$log.gz
}
case $archive_flag in
d)
${DEBUG:+echo} rm "$log"
return $? ;;
m)
[ -d ${archive_dir%/*} ] || {
## eg gpfs missing.
echo "Cannot find $archive_dir parent"
return 1
}
## TODO option to use rsync instead.
local dest=$archive_dir/${HOSTNAME%%.*}
mkdir -p "$dest" || return 1
(( narchive++ ))
${DEBUG:+echo} mv "$log" "$dest"
return $?
;;
*)
return 0 ;;
esac
}
exitfunc () {
rm -f "$lockfile"
[ "$ncompress" -gt 0 ] && echo "Compressed $ncompress"
[ "$narchive" -gt 0 ] && echo "Archived $narchive"
return 0
}
## Main body.
cd $logdir || die "cd $logdir error"
lockfile=$logdir/.$PN.lock
if [ -e "$lockfile" ]; then
die "$lockfile exists"
else
echo $$ > "$lockfile"
trap exitfunc EXIT
fi
exec > "$logdir/$PN.log" 2>&1
renice $nice_level $$ > /dev/null
shopt -s extglob
for ((n=min_days; n<=max_days; n++)); do
olddate=$(date "$dformat" -d "$n days ago")
## Avoid *.lck files (perhaps we should delete old ones?).
for log in ccs-logs-*"$olddate".log.+([0-9.])?(.gz); do
[ -e "$log" ] || break
## TODO skip if .lck exists?
### [ e $log.lck ] && continue
if [ "$n" -gt $archive_days ]; then
archive "$log" || die "error archiving $log"
else
compress "$log" || die "error compressing $log"
fi
done # $log
done # $n
exit 0