~ubuntu-branches/ubuntu/utopic/calamaris/utopic

« back to all changes in this revision

Viewing changes to .pc/use_squid3-log.diff/calamaris-cron-script

  • Committer: Package Import Robot
  • Author(s): Daniel Echeverry
  • Date: 2013-08-26 19:38:21 UTC
  • mfrom: (13.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20130826193821-0pq5lhkzvoig58yj
Tags: 2.99.4.0-19
* debian/patches
  + add use_squid3-log.diff  
    + Use squid3 log files if they are present. Closes: #666457
      + Thanks to Brian Murray for the patch!
* Bump Standards-Version 3.9.4 (no changes)
* Move to the proper entry
  + Switch compat level 8 to 9
* debian/copyright
  + Extend copyright holder years
* Rename config, dirs, templates, to identify easily

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