summaryrefslogtreecommitdiff
path: root/localwebresolve
blob: d2859ca8433549a27095a0414942ecac3b9306c7 (plain)
  1. #!/bin/bash
  2. #
  3. # /usr/local/sbin/localwebresolve
  4. # Copyright 2002 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localwebresolve,v 1.9 2004-05-11 11:28:31 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_OPTIONS="-r -t 5"
  48. JDRESOLVE_DB="/var/cache/jdresolve/hosts.db"
  49. JDRESOLVE_EXPIRY="48"
  50. LOGRESOLVE_BIN="/usr/sbin/logresolve"
  51. # Website/independent checks
  52. test -d $WEBROOT || exit1 "Webroot \"$WEBROOT\" doesn't exist"
  53. test -d $LOGROOT || exit1 "Logroot \"$LOGROOT\" doesn't exist"
  54. if [ -x "$JDRESOLVE_BIN" -a -n "$JDRESOLVE_DB" ]; then
  55. JDRESOLVE_OPTIONS="$JDRESOLVE_OPTIONS --database=$JDRESOLVE_DB"
  56. test -d $(dirname "$JDRESOLVE_DB") || exit1 "Cache dir for jdresolve doesn't exist"
  57. if [ -n "$JDRESOLVE_EXPIRY" ]; then
  58. if [ $DEBUG ]; then
  59. jdresolve $JDRESOLVE_OPTIONS --expiredb="$JDRESOLVE_EXPIRY"
  60. else
  61. jdresolve $JDRESOLVE_OPTIONS --expiredb="$JDRESOLVE_EXPIRY" > /dev/null 2>&1
  62. fi
  63. fi
  64. fi
  65. # Resolve logs for websites from stdin or all default sites
  66. WEBSITES=$@
  67. if [ "$WEBSITES" = "" ]; then
  68. WEBSITES=$(webdirs $LOGROOT $WEBROOT $WEBSITE)
  69. fi
  70. for WEBSITE in $WEBSITES; do
  71. RESOLVEDDIR=$(resolveddir $LOGROOT $WEBROOT $WEBSITE) || exit1 "Unable to resolve RESOLVEDDIR."
  72. # FIXME test -d $RESOLVEDDIR/.. || exit1 "Directory above RESOLVEDDIR doesn't exist."
  73. if [ $DEBUG ]; then
  74. echo "Resolving logfiles $YEARDOTMONTH for $WEBSITE in $RESOLVEDDIR:"
  75. JDRESOLVE_OPTIONS="$JDRESOLVE_OPTIONS -p"
  76. else
  77. JDRESOLVE_OPTIONS="$JDRESOLVE_OPTIONS -n"
  78. fi
  79. LOGDATA="$RESOLVEDDIR/$YEARDOTMONTH.00"
  80. [ -e $LOGDATA ] && exit1 "File $LOGDATA exists already."
  81. [ -e $LOGDATA.gz ] && exit1 "File $LOGDATA exists (compressed) already."
  82. touch $LOGDATA || exit1 "Couldn't touch LOGDATA files."
  83. if [ -x $JDRESOLVE_BIN ]; then
  84. [ $DEBUG ] && echo "$WEBSITE: DNS-resolve new logdata using jdresolve"
  85. logcontent $LOGROOT $WEBROOT $WEBSITE | $JDRESOLVE_BIN $JDRESOLVE_OPTIONS - >> $LOGDATA
  86. elif [ -x $LOGRESOLVE_BIN ]; then
  87. [ $DEBUG ] && echo "$WEBSITE: DNS-resolve new logdata using logresolve"
  88. logcontent $LOGROOT $WEBROOT $WEBSITE | $LOGRESOLVE_BIN >> $LOGDATA
  89. else
  90. [ $DEBUG ] && echo "$WEBSITE: Merging new logdata without DNS-resolving (no resolver found)"
  91. logcontent $LOGROOT $WEBROOT $WEBSITE >> $LOGDATA
  92. fi
  93. if [ -s $LOGDATA ]; then
  94. gzip -f9 $LOGDATA
  95. fi
  96. logremove $LOGROOT $WEBROOT $WEBSITE
  97. done