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