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