summaryrefslogtreecommitdiff
path: root/localwebresolve
blob: 434ce6180c3f41315585e397fe12d6079ba0e7be (plain)
  1. #!/bin/bash
  2. # /usr/local/sbin/localwebresolve: Webstats maintenance script
  3. # Written by Jonas Smedegaard <dr@jones.dk>
  4. # halt on errors (NB! this is a bashism...)
  5. set -e
  6. function usage() {
  7. echo "Usage: $(basename $0) <YYYY.MM> [<website> [<website>...]]"
  8. echo " If no website is given, all are attempted"
  9. exit 1
  10. }
  11. function exit1() {
  12. echo "Error: $1"
  13. echo "Exiting..."
  14. exit 1
  15. }
  16. # automagically configure when run from cron dirs
  17. case $1 in
  18. ????.??)
  19. YEARDOTMONTH=$1
  20. ;;
  21. *)
  22. usage
  23. ;;
  24. esac
  25. shift
  26. ROOT=1
  27. user=www-data
  28. group=www-data
  29. LOGROOT=/var/log/apache
  30. WEBROOT=/var/www
  31. # Options: $1=LOGROOT, $2=WEBROOT, $3=WEBSITE, $4=YEARDOTMONTH
  32. function resolveddir() { echo $1/$3; }
  33. function webdirs() { find $1 -type d -mindepth 1 -maxdepth 1 | grep '\.*\.' | sed 's!$1!!'; }
  34. #function logfiles() { $(ls -r $LOGDIR/*-access*.gz) $(ls -r $LOGDIR/access*.??.gz) $(ls -r $LOGDIR/access*.?.gz) $(ls -r $LOGDIR/access*.?); }
  35. function logcontent() { for file in $(find $1/$3 -name "$YEARDOTMONTH.??.gz" ! -name '*00.gz' -type f -mindepth 1 -maxdepth 1 -follow | sort); do zcat $file; done; for file in $(find $1/$3 -name "$YEARDOTMONTH.??" ! -name '*00' -type f -mindepth 1 -maxdepth 1 -follow | sort); do cat $file; done; }
  36. function logremove() { for file in $(find $1/$3 -name "$YEARDOTMONTH.??.gz" ! -name '*00.gz' -type f -mindepth 1 -maxdepth 1 -follow | sort); do rm -f $file; done; for file in $(find $1/$3 -name "$YEARDOTMONTH.??" ! -name '*00' -type f -mindepth 1 -maxdepth 1 -follow | sort); do rm -f $file; done; }
  37. # The above can be overridden
  38. LOCALCONFIG=/etc/local/webresolve.conf
  39. . $LOCALCONFIG || exit1 "Unable to read local config file $LOCALCONFIG"
  40. # variables and functions too boring to be configurable
  41. JDRESOLVE_BIN="/usr/bin/jdresolve"
  42. JDRESOLVE_DB="/var/cache/jdresolve/hosts.db"
  43. #JDRESOLVE_OPTIONS="-r -t 5 --database=$JDRESOLVE_DB --dbfirst --expiredb=48"
  44. # DB access doesn't work currently (no output...)
  45. #JDRESOLVE_OPTIONS="-r --database=$JDRESOLVE_DB --dbfirst --expiredb=48"
  46. JDRESOLVE_OPTIONS="-r"
  47. JDRESOLVE_MERGE_OPTIONS="--mergedb --database=$JDRESOLVE_DB"
  48. LOGRESOLVE_BIN="/usr/sbin/logresolve"
  49. # Webiste/independent checks
  50. test -d $WEBROOT || exit1 "Webroot \"$WEBROOT\" doesn't exist"
  51. test -d $LOGROOT || exit1 "Logroot \"$LOGROOT\" doesn't exist"
  52. if [ -x $JDRESOLVE_BIN ]; then
  53. touch $JDRESOLVE_DB || exit1 "Couldn't touch JDRESOLVE_DB"
  54. fi
  55. # Resolve logs for websites from stdin or all default sites
  56. WEBSITES=$@
  57. if [ "$WEBSITES" = "" ]; then
  58. WEBSITES=$(webdirs $LOGROOT $WEBROOT $WEBSITE)
  59. fi
  60. for WEBSITE in $WEBSITES; do
  61. RESOLVEDDIR=$(resolveddir $LOGROOT $WEBROOT $WEBSITE) || exit1 "Unable to resolve RESOLVEDDIR."
  62. # FIXME test -d $RESOLVEDDIR/.. || exit1 "Directory above RESOLVEDDIR doesn't exist."
  63. if [ $DEBUG ]; then
  64. echo "Resolving logfiles $YEARDOTMONTH for $WEBSITE in $RESOLVEDDIR:"
  65. JDRESOLVE_OPTIONS="$JDRESOLVE_OPTIONS -p"
  66. else
  67. JDRESOLVE_OPTIONS="$JDRESOLVE_DB -n"
  68. fi
  69. LOGDATA="$RESOLVEDDIR/$YEARDOTMONTH.00"
  70. [ -e $LOGDATA ] && exit1 "File $LOGDATA exists already."
  71. [ -e $LOGDATA.gz ] && exit1 "File $LOGDATA exists (compressed) already."
  72. touch $LOGDATA || exit1 "Couldn't touch LOGDATA files."
  73. if [ -x $JDRESOLVE_BIN ]; then
  74. [ $DEBUG ] && echo "$WEBSITE: DNS-resolve new logdata using jdresolve"
  75. # DB access doesn't work currently (no output...)
  76. # logcontent $LOGROOT $WEBROOT $WEBSITE | $JDRESOLVE_BIN $JDRESOLVE_OPTIONS - >> $LOGDATA
  77. # jdresolve $JDRESOLVE_MERGE_OPTIONS $LOGDATA
  78. logcontent $LOGROOT $WEBROOT $WEBSITE | $JDRESOLVE_BIN $JDRESOLVE_OPTIONS - >> $LOGDATA
  79. elif [ -x $LOGRESOLVE_BIN ]; then
  80. [ $DEBUG ] && echo "$WEBSITE: DNS-resolve new logdata using logresolve"
  81. logcontent $LOGROOT $WEBROOT $WEBSITE | $LOGRESOLVE_BIN >> $LOGDATA
  82. else
  83. [ $DEBUG ] && echo "$WEBSITE: Merging new logdata without DNS-resolving (no resolver found)"
  84. logcontent $LOGROOT $WEBROOT $WEBSITE >> $LOGDATA
  85. fi
  86. if [ -s $LOGDATA ]; then
  87. gzip -f9 $LOGDATA
  88. fi
  89. logremove $LOGROOT $WEBROOT $WEBSITE
  90. done