310.accounting revision 227482
1222625Sed#!/bin/sh
2222625Sed#
3222625Sed# $FreeBSD: head/etc/periodic/daily/310.accounting 227482 2011-11-13 03:01:58Z dougb $
4222625Sed#
5222625Sed
6222625Sed# If there is a global system configuration file, suck it in.
7222625Sed#
8222625Sedif [ -r /etc/defaults/periodic.conf ]
9222625Sedthen
10222625Sed    . /etc/defaults/periodic.conf
11222625Sed    source_periodic_confs
12222625Sedfi
13222625Sed
14222625Sedcase "$daily_accounting_enable" in
15222625Sed    [Yy][Ee][Ss])
16222625Sed	if [ ! -f /var/account/acct ]
17222625Sed	then
18222625Sed	    echo '$daily_accounting_enable is set but /var/account/acct' \
19222625Sed		"doesn't exist"
20222625Sed	    rc=2
21222625Sed	elif [ -z "$daily_accounting_save" ]
22222625Sed	then
23222625Sed	    echo '$daily_accounting_enable is set but ' \
24222625Sed		'$daily_accounting_save is not'
25222625Sed	    rc=2
26222625Sed	else
27222625Sed	    echo ""
28222625Sed	    echo "Rotating accounting logs and gathering statistics:"
29222625Sed
30222625Sed	    cd /var/account
31222625Sed	    rc=0
32222625Sed
33222625Sed	    n=$(( $daily_accounting_save - 1 ))
34222625Sed	    for f in acct.*; do
35222625Sed	    	case "$f" in acct.\*) continue ;; esac	# No files match
36222625Sed	    	m=${f%.gz} ; m=${m#acct.}
37222625Sed		[ $m -ge $n ] && { rm $f || rc=3; }
38222625Sed	    done
39222625Sed
40222625Sed	    m=$n
41222625Sed	    n=$(($n - 1))
42	    while [ $n -ge 0 ]
43	    do
44		[ -f acct.$n.gz ] && { mv -f acct.$n.gz acct.$m.gz || rc=3; }
45		[ -f acct.$n ] &&    { mv -f acct.$n acct.$m || rc=3; }
46		m=$n
47		n=$(($n - 1))
48	    done
49
50	    /etc/rc.d/accounting rotate_log || rc=3
51
52	    rm -f acct.merge && cp acct.0 acct.merge || rc=3
53	    sa -s $daily_accounting_flags /var/account/acct.merge || rc=3
54	    rm acct.merge
55
56	    case "$daily_accounting_compress" in
57		[Yy][Ee][Ss])
58		    gzip -f acct.0 || rc=3;;
59	    esac
60	fi;;
61
62    *)  rc=0;;
63esac
64
65exit $rc
66