pub / goaccess_dashboard

Tiny tool for privacy-preserving web-analytics
git clone https://src.jayvii.de/pub/goaccess_dashboard.git
Home | Log | Files | Exports | Refs | Submodules | README | RSS

create_logs_report.sh (3947B)


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