2 ###############################################################################
4 # IPFire.org - An Open Source Firewall Solution #
5 # Copyright (C) 2012 Michael Tremer #
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. #
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. #
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/>. #
20 ###############################################################################
24 SARG_CONFIG
="/etc/sarg/sarg.conf"
25 SQUID_LOG
="/var/log/squid/access.log"
26 REPORTS_PATH
="/var/log/sarg"
28 function date_calc
() {
51 if [ "${range}" = "true" ]; then
52 echo "$(date --date "${when}" +01/%m/%Y)-$(date --date "${when}" +31/%m/%Y)"
54 date --date "${when}" +%d
/%m
/%Y
60 function compile_report
() {
66 date=$
(date_calc today
)
69 date=$
(date_calc yesterday
)
72 date="$(date_calc week)-$(date_calc yesterday)"
75 date="$(date_calc month)"
78 [ -n "${date}" ] ||
return 1
80 # Determine max. number of archived log files to search.
94 # Create reports_path, if not exists.
95 local reports_path
="${REPORTS_PATH}/${interval}"
96 mkdir
-p ${reports_path}
98 # Remove already existant data on today's reports.
101 rm -rf ${reports_path}/*
106 get_logs
${max_logs} | sarg -f ${SARG_CONFIG} -l - -d ${date} -o ${reports_path}
109 function get_logs
() {
112 if [ -z "${max}" ]; then
117 while [ ${idx} -le ${max} ]; do
118 file=$
(search_log_file
${idx})
120 # If no log file could be opened, we are done.
121 [ -z "${file}" ] && break
124 # Logs in plain text.
129 # GZip compressed log files.
134 # XZ compressed log files.
141 echo "Unhandled file type: ${file}" >&2
145 idx
=$
(( ${idx} + 1 ))
151 function search_log_file
() {
154 if [ "${idx}" = "0" ] && [ -e "${SQUID_LOG}" ]; then
160 for algo
in gz xz
; do
161 file="${SQUID_LOG}.${idx}.${algo}"
163 if [ -e "${file}" ]; then
175 today|daily|weekly|monthly
)
179 echo "${0} - Squid proxy reports creation tool"
181 echo "Usage: ${0} [interval]"
182 echo " interval: today, daily, weekly, monthly"