]>
Commit | Line | Data |
---|---|---|
fb6e700b MT |
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 | # Run SARG. | |
99 | get_logs ${max_logs} | sarg -f ${SARG_CONFIG} -l - -d ${date} -o ${reports_path} | |
100 | } | |
101 | ||
102 | function get_logs() { | |
103 | local max=${1} | |
104 | ||
105 | if [ -z "${max}" ]; then | |
106 | max=10000 | |
107 | fi | |
108 | ||
109 | local idx=0 | |
110 | while [ ${idx} -le ${max} ]; do | |
111 | file=$(search_log_file ${idx}) | |
112 | ||
113 | # If no log file could be opened, we are done. | |
114 | [ -z "${file}" ] && break | |
115 | ||
116 | case "${file}" in | |
117 | # Logs in plain text. | |
118 | *.log) | |
119 | cat ${file} | |
120 | ;; | |
121 | ||
122 | # GZip compressed log files. | |
123 | *.gz) | |
124 | gzip -d < ${file} | |
125 | ;; | |
126 | ||
127 | # XZ compressed log files. | |
128 | *.xz) | |
129 | xz -d < ${file} | |
130 | ;; | |
131 | ||
132 | # Unhandled stuff. | |
133 | *) | |
134 | echo "Unhandled file type: ${file}" >&2 | |
135 | ;; | |
136 | esac | |
137 | ||
138 | idx=$(( ${idx} + 1 )) | |
139 | done | |
140 | ||
141 | return 0 | |
142 | } | |
143 | ||
144 | function search_log_file() { | |
145 | local idx=${1} | |
146 | ||
147 | if [ "${idx}" = "0" ] && [ -e "${SQUID_LOG}" ]; then | |
148 | echo "${SQUID_LOG}" | |
149 | return 0 | |
150 | fi | |
151 | ||
152 | local algo | |
153 | for algo in gz xz; do | |
154 | file="${SQUID_LOG}.${idx}.${algo}" | |
155 | ||
156 | if [ -e "${file}" ]; then | |
157 | echo "${file}" | |
158 | return 0 | |
159 | fi | |
160 | done | |
161 | ||
162 | return 1 | |
163 | } | |
164 | ||
165 | # Main. | |
166 | ||
167 | case "${1}" in | |
168 | today|daily|weekly|monthly) | |
169 | compile_report ${1} | |
170 | ;; | |
171 | *) | |
172 | echo "${0} - Squid proxy reports creation tool" | |
173 | echo | |
174 | echo "Usage: ${0} [interval]" | |
175 | echo " interval: today, daily, weekly, monthly" | |
176 | echo | |
177 | exit 0 | |
178 | ;; | |
179 | esac | |
180 | ||
181 | exit 0 |