create_logs_report.sh (3422B)
1 #!/usr/bin/env bash
2
3 # configuration ----------------------------------------------------------------
4
5 export LC_TIME=en
6 DB_PATH="$HOME/goaccess/${1}"
7 HTML_PATH="/var/www/traffic.${1}"
8 LOG_PATH="/var/log/apache2/${1}_access.log"
9
10 # define functions -------------------------------------------------------------
11
12 # HACK: enable automatic dark mode (default is bright)
13 function autoTheme() {
14 echo "
15 <script>
16 // Define autoTheme function
17 function autoTheme() {
18 // If browser/client prefers dark color-scheme
19 if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
20 // unwind sidebar to load theme-toggles
21 document.querySelector('.nav-gears').click();
22 // Enable theme: dark-gray
23 document.querySelector('.theme-dark-gray').click();
24 }
25 };
26 // run autoTheme function after 1ms delay
27 setTimeout(autoTheme, 1);
28 </script>
29 " | tee --append "${1}" > /dev/null
30 }
31
32 # set dates --------------------------------------------------------------------
33 TODAY=$(date +%Y-%m-%d)
34 THIS_WEEK=$(date +%V --date "${TODAY}")
35 THIS_MONTH=$(date +%m --date "${TODAY}")
36 THIS_YEAR=$(date +%Y --date "${TODAY}")
37 TODAY=$(date +%d\\/%b\\/%Y --date "${TODAY}")
38
39 # create db-paths --------------------------------------------------------------
40 mkdir -p "${DB_PATH}/all"
41 mkdir -p "${DB_PATH}/${THIS_YEAR}"
42 mkdir -p "${DB_PATH}/${THIS_YEAR}/${THIS_MONTH}"
43 mkdir -p "${DB_PATH}/${THIS_YEAR}/week-${THIS_WEEK}"
44
45 # create output paths ----------------------------------------------------------
46 mkdir -p "${HTML_PATH}/all"
47 mkdir -p "${HTML_PATH}/${THIS_YEAR}"
48 mkdir -p "${HTML_PATH}/${THIS_YEAR}/${THIS_MONTH}"
49 mkdir -p "${HTML_PATH}/${THIS_YEAR}/week-${THIS_WEEK}"
50
51 # create logs ------------------------------------------------------------------
52
53 # create weekly log
54 echo "Create this Week's log"
55 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
56 grep -v -E "$(cat ./spammers.txt)" | \
57 goaccess \
58 --db-path="${DB_PATH}/${THIS_YEAR}/week-${THIS_WEEK}" \
59 --output-format="${HTML_PATH}/${THIS_YEAR}/week-${THIS_WEEK}/index.html" \
60 --load-from-disk --keep-db-files \
61 -
62
63 # auto automatic theme to weekly log
64 autoTheme "${HTML_PATH}/${THIS_YEAR}/week-${THIS_WEEK}/index.html"
65
66 # create monthly log
67 echo "Create this Month's log"
68 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
69 grep -v -E "$(cat ./spammers.txt)" | \
70 goaccess \
71 --db-path="${DB_PATH}/${THIS_YEAR}/${THIS_MONTH}" \
72 --output-format="${HTML_PATH}/${THIS_YEAR}/${THIS_MONTH}/index.html" \
73 --load-from-disk --keep-db-files \
74 -
75
76 # auto automatic theme to monthly log
77 autoTheme "${HTML_PATH}/${THIS_YEAR}/${THIS_MONTH}/index.html"
78
79 # create yearly log
80 echo "Create this Year's log"
81 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
82 grep -v -E "$(cat ./spammers.txt)" | \
83 goaccess \
84 --db-path="${DB_PATH}/${THIS_YEAR}" \
85 --output-format="${HTML_PATH}/${THIS_YEAR}/index.html" \
86 --load-from-disk --keep-db-files \
87 -
88
89 # auto automatic theme to yearly log
90 autoTheme "${HTML_PATH}/${THIS_YEAR}/index.html"
91
92 # create global log
93 echo "Create global log"
94 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
95 grep -v -E "$(cat ./spammers.txt)" | \
96 goaccess \
97 --db-path="${DB_PATH}/all" \
98 --output-format="${HTML_PATH}/all/index.html" \
99 --load-from-disk --keep-db-files \
100 -
101
102 # auto automatic theme to global log
103 autoTheme "${HTML_PATH}/all/index.html"
104
105 echo "Done!"