~ubuntu-branches/ubuntu/wily/calamaris/wily

« back to all changes in this revision

Viewing changes to calamaris-cron-script

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Frauenfelder
  • Date: 2004-06-08 14:26:25 UTC
  • Revision ID: james.westby@ubuntu.com-20040608142625-5bqiw8figzmdpg8j
Tags: 2.59-1
New upstream release. Closes: #253093: New upstream version of Calamaris

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
 
 
3
set -e
 
4
 
 
5
# calamaris: daily cron script.
 
6
# This script should be run before the one for squid or oops. According to the
 
7
# man page of run-parts this is okay: squid and oops come after calamaris in
 
8
# the alphabet.
 
9
 
 
10
# Date: 1998-10-07
 
11
 
 
12
# DEBUG=1
 
13
 
 
14
CONFFILE=/etc/calamaris.conf
 
15
CALAMARIS=/usr/bin/calamaris
 
16
 
 
17
if [ ! -x /usr/bin/calamaris ]; then
 
18
    exit 0
 
19
fi
 
20
 
 
21
CALAMARISOPTIONS='-a -f auto'
 
22
ME=/etc/cron.daily/calamaris
 
23
WEEKFILES=forweekly.1:forweekly.2:forweekly.3:forweekly.4:forweekly.5:forweekly.6:forweekly.0
 
24
 
 
25
# today
 
26
DAYOFWEEK=`date +"%w"`
 
27
DAYOFMONTH=`date +"%d" | bc -l`
 
28
# WEEKOFYEAR=`date +"%W"`
 
29
# MONTH=`date +"%B"`
 
30
 
 
31
# read configuration file: /etc/calamaris.conf
 
32
# daily
 
33
eval `awk -F: \
 
34
'(!/�#/) && ($1 == "daily") {
 
35
        print "DAYMAIL=" $2; print "DAYWEB=" $3;
 
36
        print "DAYDO=" $4; print "DAYTITLE=" $5;
 
37
}' $CONFFILE`
 
38
 
 
39
# weekly
 
40
eval `awk -F: \
 
41
'(!/�#/) && ($1 == "weekly") {
 
42
        print "WEEKMAIL=" $2; print "WEEKWEB=" $3;
 
43
        print "WEEKDO=" $4; print "WEEKTITLE=" $5;
 
44
}' $CONFFILE`
 
45
 
 
46
# monthly
 
47
eval `awk -F: \
 
48
'(!/�#/) && ($1 == "monthly") {
 
49
        print "MONTHMAIL=" $2; print "MONTHWEB=" $3;
 
50
        print "MONTHDO=" $4; print "MONTHTITLE=" $5;
 
51
}' $CONFFILE`
 
52
 
 
53
# squid or oops?
 
54
CACHE=auto
 
55
eval `awk -F= \
 
56
'(!/�#/) && ($1 == "cache") {
 
57
        print "CACHE=" $2;
 
58
}' $CONFFILE`
 
59
 
 
60
# look for cache log files
 
61
if [ "$CACHE" = "auto" ]; then
 
62
    if [ -r /var/log/squid/access.log ]; then
 
63
        CACHE=squid
 
64
        CACHELOGDIR=/var/log/squid
 
65
    fi
 
66
    if [ -r /var/log/oops/access.log ]; then
 
67
        CACHE=oops
 
68
        CACHELOGDIR=/var/log/oops
 
69
    fi
 
70
    if [ "$CACHE" = "auto" ]; then
 
71
        echo "/etc/cron.daily/calamaris: no cache log files found, exiting cleanly"
 
72
        exit 0
 
73
    fi
 
74
else
 
75
    CACHELOGDIR=/var/log/$CACHE
 
76
    if [ ! -r $CACHELOGDIR/access.log ]; then
 
77
        echo "/etc/cron.daily/calamaris: no cache log files found in $CACHELOGDIR, exiting cleanly"
 
78
        exit 0
 
79
    fi
 
80
fi
 
81
 
 
82
# change to working dir
 
83
LOGDIR=/var/log/calamaris
 
84
cd $LOGDIR || exit 1
 
85
 
 
86
# if we need monthly or weekly reports save a summary
 
87
if [ "$WEEKDO" != "nothing" ]; then
 
88
    CALAMARISOPTIONSOLD="$CALAMARISOPTIONS"
 
89
    CALAMARISOPTIONS="$CALAMARISOPTIONS -o forweekly.$DAYOFWEEK"
 
90
else
 
91
    if [ "$MONTHDO" != "nothing" ]; then
 
92
        CALAMARISOPTIONSOLD="$CALAMARISOPTIONS"
 
93
        CALAMARISOPTIONS="$CALAMARISOPTIONS -o formonthly.$DAYOFMONTH"
 
94
    fi
 
95
fi
 
96
 
 
97
# do the daily report
 
98
case "$DAYDO" in
 
99
    nothing) if [ "$WEEKDO" != "nothing" ]; then
 
100
        cat $CACHELOGDIR/access.log | \
 
101
            nice -39 $CALAMARIS $CALAMARISOPTIONS > /dev/null
 
102
        fi
 
103
    ;;
 
104
    mail) if [ -x /usr/sbin/sendmail ]; then (
 
105
            echo "To: $DAYMAIL"
 
106
            echo "From: Calamaris <root>"
 
107
            cat $CACHELOGDIR/access.log | \
 
108
                nice -39 $CALAMARIS $CALAMARISOPTIONS -F mail -H "$DAYTITLE"
 
109
        ) | /usr/sbin/sendmail -t
 
110
        fi
 
111
    ;;
 
112
    web) cat $CACHELOGDIR/access.log | \
 
113
            nice -39 $CALAMARIS $CALAMARISOPTIONS -F html -H "$DAYTITLE" > $DAYWEB
 
114
    ;;
 
115
    both) cat $CACHELOGDIR/access.log | \
 
116
            nice -39 $CALAMARIS $CALAMARISOPTIONS -F html -H "$DAYTITLE" > $DAYWEB
 
117
        if [ -x /usr/sbin/sendmail ]; then (
 
118
            echo "To: $DAYMAIL"
 
119
            echo "From: Calamaris <root>"
 
120
            cat $CACHELOGDIR/access.log | \
 
121
                nice -39 $CALAMARIS $CALAMARISOPTIONS -F mail -H "$DAYTITLE"
 
122
        ) | /usr/sbin/sendmail -t
 
123
        fi
 
124
    ;;
 
125
    *) echo "the 'todo' for the daily Squid report in $CONFFILE"
 
126
        echo -n "is '$DAYDO' instead of one out of "
 
127
        echo "(nothing, mail, web, both)." >&2
 
128
        exit 1
 
129
    ;;
 
130
esac
 
131
 
 
132
if [ "$MONTHDO" != "nothing" -a "$WEEKDO" != "nothing" ]; then
 
133
    cp forweekly.$DAYOFWEEK formonthly.$DAYOFMONTH
 
134
fi
 
135
 
 
136
# do the weekly report on Sunday <=> $DAYOFWEEK==0
 
137
if [ -n "$CALAMARISOPTIONSOLD" ]; then
 
138
    CALAMARISOPTIONS="$CALAMARISOPTIONSOLD"
 
139
fi
 
140
 
 
141
if [ "$DAYOFWEEK" = "0" ]; then
 
142
    # make sure, all forweekly files exist, ie. touch them
 
143
    touch `echo $WEEKFILES | sed "s/:/ /g"`
 
144
    case "$WEEKDO" in
 
145
        nothing) 
 
146
        ;;
 
147
        mail) if [ -x /usr/sbin/sendmail ]; then (
 
148
                echo "To: $WEEKMAIL"
 
149
                echo "From: Calamaris <root>"
 
150
                nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
 
151
                    -z -F mail -H "$WEEKTITLE"
 
152
            ) | /usr/sbin/sendmail -t
 
153
            fi
 
154
        ;;
 
155
        web) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
 
156
                -z -F html -H "$WEEKTITLE" > $WEEKWEB
 
157
        ;;
 
158
        both) if [ "$DEBUG" = "1" ]; then
 
159
                echo "Weekly stats."
 
160
                echo "Doing web stats: nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES -zwH \"$WEEKTITLE\" into $WEEKWEB"
 
161
            fi
 
162
            nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
 
163
                -z -F html -H "$WEEKTITLE" > $WEEKWEB
 
164
            if [ "$DEBUG" = "1" ]; then
 
165
                echo "Doing mail stats: nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES -zmH \"$WEEKTITLE\""
 
166
            fi
 
167
            if [ -x /usr/sbin/sendmail ]; then (
 
168
                echo "To: $WEEKMAIL"
 
169
                echo "From: Calamaris <root>"
 
170
                nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
 
171
                    -z -F mail -H "$WEEKTITLE"
 
172
            ) | /usr/sbin/sendmail -t
 
173
            fi
 
174
            if [ "$DEBUG" = "1" ]; then echo "Finished weekly stats."; fi
 
175
        ;;
 
176
        *) echo "the 'todo' for the weekly Squid report in $CONFFILE"
 
177
            echo -n "is '$WEEKDO' instead of one out of "
 
178
            echo "(nothing, mail, web, both)." >&2
 
179
            exit 1
 
180
        ;;
 
181
    esac
 
182
    # remove all forweekly files (could disturb next week's statistics if
 
183
    # computer is switched of one day)
 
184
    rm -f `echo $WEEKFILES | sed "s/:/ /g"`
 
185
fi
 
186
 
 
187
# do the monthly report
 
188
if [ "$DAYOFMONTH" = "1" ]; then
 
189
    i=2
 
190
    MONTHFILES=formonthly.1
 
191
    yesterday=`date +"%d" -d "yesterday"`
 
192
    end=`echo 1+$yesterday | bc -l`
 
193
    while [ "$i" != $end ]; do
 
194
        MONTHFILES=$MONTHFILES:formonthly.$i
 
195
        i=`echo 1+$i | bc -l`
 
196
    done
 
197
    # make sure, all formonthly files exist, ie. touch them
 
198
    touch `echo $MONTHFILES | sed "s/:/ /g"`
 
199
    case "$MONTHDO" in
 
200
        nothing)
 
201
        ;;
 
202
        mail) if [ -x /usr/sbin/sendmail ]; then (
 
203
                echo "To: $MONTHMAIL"
 
204
                echo "From: Calamaris <root>"
 
205
                nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
 
206
                    -z -F mail -H "$MONTHTITLE"
 
207
              ) | /usr/sbin/sendmail -t
 
208
              fi
 
209
        ;;
 
210
        web) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
 
211
                -z -F html -H "$MONTHTITLE" > $MONTHWEB
 
212
        ;;
 
213
        both) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
 
214
                -z -F html -H "$MONTHTITLE" > $MONTHWEB
 
215
              if [ -x /usr/sbin/sendmail ]; then (
 
216
                echo "To: $MONTHMAIL"
 
217
                echo "From: Calamaris <root>"
 
218
                nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
 
219
                    -z -F mail -H "$MONTHTITLE"
 
220
              ) | /usr/sbin/sendmail -t
 
221
              fi
 
222
        ;;
 
223
        *) echo "the 'todo' for the weekly Squid report in $CONFFILE"
 
224
            echo -n "is '$MONTHDO' instead of one out of "
 
225
            echo "(nothing, mail, web, both)." >&2
 
226
            exit 1
 
227
        ;;
 
228
    esac
 
229
    # remove all formonthly files (could disturb next month's statistics if
 
230
    # computer is switched of one day)
 
231
    rm -f formonthly.*
 
232
fi
 
233
 
 
234
exit 0