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 (3804B)


      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           // unwind sidebar to load theme-toggles
     20           document.querySelector('.nav-gears').click();
     21           // Enable theme: dark-gray
     22           setTimeout(
     23             function() { document.querySelector('.theme-dark-gray').click(); },
     24             250
     25           );
     26         }
     27       };
     28       // run autoTheme function after 1ms delay
     29       setTimeout(autoTheme, 1);
     30   </script>
     31   " | tee --append "${1}" > /dev/null
     32 }
     33 
     34 # set dates --------------------------------------------------------------------
     35 TODAY=$(date +%Y-%m-%d)
     36 THIS_WEEK=$(date +%V --date "${TODAY}")
     37 THIS_MONTH=$(date +%m --date "${TODAY}")
     38 THIS_YEAR=$(date +%Y --date "${TODAY}")
     39 TODAY=$(date +%d\\/%b\\/%Y --date "${TODAY}")
     40 
     41 # create db-paths --------------------------------------------------------------
     42 mkdir -p "${DB_PATH}/all"
     43 mkdir -p "${DB_PATH}/${THIS_YEAR}"
     44 mkdir -p "${DB_PATH}/${THIS_YEAR}/${THIS_MONTH}"
     45 mkdir -p "${DB_PATH}/${THIS_YEAR}/week-${THIS_WEEK}"
     46 
     47 # create output paths ----------------------------------------------------------
     48 mkdir -p "${HTML_PATH}/all"
     49 mkdir -p "${HTML_PATH}/${THIS_YEAR}"
     50 mkdir -p "${HTML_PATH}/${THIS_YEAR}/${THIS_MONTH}"
     51 mkdir -p "${HTML_PATH}/${THIS_YEAR}/week-${THIS_WEEK}"
     52 
     53 # create logs ------------------------------------------------------------------
     54 
     55 # create weekly log
     56 echo "Create this Week's log"
     57 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
     58   grep -v -E "$(cat ./spammers.txt)" | \
     59   goaccess \
     60     --db-path="${DB_PATH}/${THIS_YEAR}/week-${THIS_WEEK}" \
     61     --output-format="${HTML_PATH}/${THIS_YEAR}/week-${THIS_WEEK}/index.html" \
     62     -
     63 
     64 # auto automatic theme to weekly log
     65 autoTheme "${HTML_PATH}/${THIS_YEAR}/week-${THIS_WEEK}/index.html"
     66 
     67 # create monthly log
     68 echo "Create this Month's log"
     69 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
     70   grep -v -E "$(cat ./spammers.txt)" | \
     71   goaccess \
     72     --db-path="${DB_PATH}/${THIS_YEAR}/${THIS_MONTH}" \
     73     --output-format="${HTML_PATH}/${THIS_YEAR}/${THIS_MONTH}/index.html" \
     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     -
     87 
     88 # auto automatic theme to yearly log
     89 autoTheme "${HTML_PATH}/${THIS_YEAR}/index.html"
     90 
     91 # create global log
     92 echo "Create global log"
     93 sed -n '/'${TODAY}'/,$ p' ${LOG_PATH} | \
     94   grep -v -E "$(cat ./spammers.txt)" | \
     95   goaccess \
     96     --db-path="${DB_PATH}/all" \
     97     --output-format="${HTML_PATH}/all/index.html" \
     98     -
     99 
    100 # auto automatic theme to global log
    101 autoTheme "${HTML_PATH}/all/index.html"
    102 
    103 # Cleanup log data base --------------------------------------------------------
    104 
    105 # clean up last year's folder
    106 find "$DB_PATH" -type d \
    107   -name "$(date +%Y --date 'last year')" \
    108   -exec rm -r {} \;
    109 
    110 # clean up last month's folder
    111 find "$DB_PATH" -type d \
    112   -wholename "*/${THIS_YEAR}/$(date +%m --date 'last month')" \
    113   -exec rm -r {} \;
    114 
    115 # clean up last week's folder
    116 find "$DB_PATH" -type d \
    117   -wholename "*/${THIS_YEAR}/week-$(date +%V --date '-7 days')" \
    118   -exec rm -r {} \;
    119 
    120 echo "Done!"