]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/sarg/update-sarg-reports
Merge remote-tracking branch 'amarx/ipsec' into next
[people/teissler/ipfire-2.x.git] / config / sarg / update-sarg-reports
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - An Open Source Firewall Solution #
5 # Copyright (C) 2012 Michael Tremer #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 export LC_ALL=C
23
24 SARG_CONFIG="/etc/sarg/sarg.conf"
25 SQUID_LOG="/var/log/squid/access.log"
26 REPORTS_PATH="/var/log/sarg"
27
28 function date_calc() {
29 local when
30 local range="false"
31
32 case "${1}" in
33 month)
34 when="1 month ago"
35 range="true"
36 ;;
37 week)
38 when="1 week ago"
39 ;;
40 yesterday)
41 when="1 day ago"
42 ;;
43 today)
44 when="today"
45 ;;
46 *)
47 return 1
48 ;;
49 esac
50
51 if [ "${range}" = "true" ]; then
52 echo "$(date --date "${when}" +01/%m/%Y)-$(date --date "${when}" +31/%m/%Y)"
53 else
54 date --date "${when}" +%d/%m/%Y
55 fi
56
57 return 0
58 }
59
60 function compile_report() {
61 local interval=${1}
62
63 local date
64 case "${interval}" in
65 today)
66 date=$(date_calc today)
67 ;;
68 daily)
69 date=$(date_calc yesterday)
70 ;;
71 weekly)
72 date="$(date_calc week)-$(date_calc yesterday)"
73 ;;
74 monthly)
75 date="$(date_calc month)"
76 ;;
77 esac
78 [ -n "${date}" ] || return 1
79
80 # Determine max. number of archived log files to search.
81 local max_logs
82 case "${interval}" in
83 today|daily)
84 max_logs=3
85 ;;
86 weekly)
87 max_logs=14
88 ;;
89 monthly)
90 max_logs=40
91 ;;
92 esac
93
94 # Create reports_path, if not exists.
95 local reports_path="${REPORTS_PATH}/${interval}"
96 mkdir -p ${reports_path}
97
98 # Remove already existant data on today's reports.
99 case "${interval}" in
100 today)
101 rm -rf ${reports_path}/*
102 ;;
103 esac
104
105 # Run SARG.
106 get_logs ${max_logs} | sarg -f ${SARG_CONFIG} -l - -d ${date} -o ${reports_path}
107 }
108
109 function get_logs() {
110 local max=${1}
111
112 if [ -z "${max}" ]; then
113 max=10000
114 fi
115
116 local idx=0
117 while [ ${idx} -le ${max} ]; do
118 file=$(search_log_file ${idx})
119
120 # If no log file could be opened, we are done.
121 [ -z "${file}" ] && break
122
123 case "${file}" in
124 # Logs in plain text.
125 *.log)
126 cat ${file}
127 ;;
128
129 # GZip compressed log files.
130 *.gz)
131 gzip -d < ${file}
132 ;;
133
134 # XZ compressed log files.
135 *.xz)
136 xz -d < ${file}
137 ;;
138
139 # Unhandled stuff.
140 *)
141 echo "Unhandled file type: ${file}" >&2
142 ;;
143 esac
144
145 idx=$(( ${idx} + 1 ))
146 done
147
148 return 0
149 }
150
151 function search_log_file() {
152 local idx=${1}
153
154 if [ "${idx}" = "0" ] && [ -e "${SQUID_LOG}" ]; then
155 echo "${SQUID_LOG}"
156 return 0
157 fi
158
159 local algo
160 for algo in gz xz; do
161 file="${SQUID_LOG}.${idx}.${algo}"
162
163 if [ -e "${file}" ]; then
164 echo "${file}"
165 return 0
166 fi
167 done
168
169 return 1
170 }
171
172 # Main.
173
174 case "${1}" in
175 today|daily|weekly|monthly)
176 compile_report ${1}
177 ;;
178 *)
179 echo "${0} - Squid proxy reports creation tool"
180 echo
181 echo "Usage: ${0} [interval]"
182 echo " interval: today, daily, weekly, monthly"
183 echo
184 exit 0
185 ;;
186 esac
187
188 exit 0