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