]> git.pld-linux.org Git - packages/calamaris.git/blob - calamaris-croniface
- Release: 5
[packages/calamaris.git] / calamaris-croniface
1 #!/bin/sh
2 #
3 # $Id$
4 #
5 # This file is to be called by crond in order to create statistical reports
6 # on usage of caching proxy. It is a wrapper for Calamaris - reads
7 # configuration and calls Calamaris with appropriate arguments.
8 #
9 # One argument should be passed. It can be 'daily' or 'weekly' in order to
10 # select what actions to take.
11 #
12 # Author:       Micha³ Kochanowicz <mkochano@pld.org.pl>
13
14 # Default configuration.
15 SAVE_FORMAT="HTML"
16 SAVE_OWNER=
17 SAVE_PERM="u=rw,g=r,o=r"
18 SEND_FORMAT="plain"
19 SEND_TO="root"
20
21 # DAILY ---------------------------------------------------------------
22 DAILY_ACTION="send"
23 DAILY_FILES=
24 # If you have an idea of better default setting for this variable you are
25 # welcome to change it, but please don't assume that caching proxy is also
26 # a http server...
27 DAILY_SAVE_AS="/dev/null"
28
29 # WEEKLY --------------------------------------------------------------
30 WEEKLY_ACTION="send"
31 WEEKLY_FILES=
32 # See comment above.
33 WEEKLY_SAVE_AS="/dev/null"
34
35 HTML_LOGO="<H1>Caching Proxy Statistics</H1>"
36 CALAMARIS_ARGS="-a"
37
38 # Read configuration.
39 [ -f /etc/sysconfig/calamaris ] && . /etc/sysconfig/calamaris
40
41 # Parameter passed to Calamaris.
42 ARG_HTML="$CALAMARIS_ARGS -F html -l $LOGO"
43 ARG_MAIL="$CALAMARIS_ARGS -a -m Calamaris report"
44 MAIL_SUBJ="Calamaris Report"
45
46 # Calls Calamaris. Requires following arguments:
47 # $1 - Input file (globs are OK).
48 # $2 - Arguments list.
49 call_calamaris() {
50         (
51         for FILE in $1; do
52                 if [[ $FILE = *.gz ]]; then
53                         zcat $FILE
54                 else if [[ $FILE = *.bz2 ]]; then
55                         bzcat $FILE
56                 else
57                         cat $FILE
58                 fi
59                 fi
60         done
61         ) | calamaris $2
62 }
63
64 # Processes arguments and prepares arguments for Calamaris. Requires following
65 # arguments:
66 # $1 - Input file (globs are OK).
67 # $2 - Action: "save", "send" or both.
68 # $3 - Save filename.
69 make_stats() {
70         # Generate statistics and save them.
71         if [[ $2 = *save* ]]; then
72                 if [ "$SAVE_FORMAT" = "HTML" ]; then
73                         ARG="$ARG_HTML"
74                 else
75                         ARG="$ARG_PLAIN"
76                 fi
77                 call_calamaris "$1" "$ARG" > $3
78                 [ -n "$SAVE_OWNER" ] || chown $SAVE_OWNER $3
79                 [ -n "$SAVE_PERM" ] || chmod $SAVE_PERM $3
80         fi
81
82         # Generate statistics and send them.
83         if [[ $2 = *send* ]]; then
84                 if [ "$SEND_FORMAT" = "HTML" ]; then
85                         ARG="$ARG_HTML"
86                 else
87                         ARG="$ARG_PLAIN"
88                 fi
89                 # If statistics have to be mailed in same format as they were
90                 # saved we don't need to call Calamaris again.
91                 if [[ $2 = *save* && $SAVE_FORMAT = $SEND_FORMAT ]]; then
92                         (
93                         cat $3
94                         ) | mail -s "$MAIL_SUBJ" $SEND_TO
95                 else
96                         call_calamaris "$1" "$ARG" | mail -s "$MAIL_SUBJ" $SEND_TO
97                 fi
98         fi
99 }
100
101
102 if [ "$1" = "daily" ]; then
103         make_stats "$DAILY_FILES" "$DAILY_ACTION" "$DAILY_SAVE_AS"
104 elif [ "$1" = "weekly" ]; then
105         make_stats "$WEEKLY_FILES" "$WEEKLY_ACTION" "$WEEKLY_SAVE_AS"
106 else
107         echo 'Make up your mind!' >&2
108 fi
109
This page took 0.055952 seconds and 3 git commands to generate.