summaryrefslogtreecommitdiff
path: root/showlog
blob: f3f857a21d29adc696dd95c2c88616a71c02210d (plain)
  1. #!/bin/bash
  2. # /usr/local/sbin/showlog: Show topic-specific log entries
  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) ftp|web|weberror|mail [<keyword> [<keyword>...]]"
  8. exit 1
  9. }
  10. function exit1() {
  11. echo "Error: $1"
  12. echo "Exiting..."
  13. exit 1
  14. }
  15. target=$1
  16. case "$target" in
  17. ftp|web|weberror|mail)
  18. ;;
  19. *)
  20. usage
  21. ;;
  22. esac
  23. shift
  24. logroot="/var/log"
  25. tail_history="100"
  26. function cat_logs() {
  27. logroot=$1
  28. shift
  29. for pattern in $@; do
  30. for file in `find $logroot -name "$pattern" -type f -mindepth 1 -maxdepth 1 -follow | sort`; do
  31. case $pattern in
  32. *.gz)
  33. zcat $file;;
  34. *.bz2)
  35. bzcat $file;;
  36. *)
  37. cat $file;;
  38. esac
  39. done
  40. done
  41. }
  42. case "$target" in
  43. ftp)
  44. log="xferlog"
  45. logs="$log.??.gz $log.?.gz $log.? $log"
  46. ;;
  47. web)
  48. logroot="/var/log/apache"
  49. log="access.log"
  50. logs="$log.??.gz $log.?.gz $log.? $log"
  51. ;;
  52. weberror)
  53. logroot="/var/log/apache"
  54. log="error.log"
  55. logs="$log.??.gz $log.?.gz $log.? $log"
  56. ;;
  57. mail)
  58. log="mail.log"
  59. logs="$log.??.gz $log.?.gz $log.? $log"
  60. ;;
  61. esac
  62. if [ $# \> 0 ]; then
  63. grep_opts=""
  64. for keyword in $@; do
  65. grep_opts="$opts -e $keyword"
  66. done
  67. cat_logs $logroot $logs | grep -i $grep_opts | tail -n $tail_history
  68. [ -f $logroot/$log ] && tail -n 0 -f $logroot/$log | grep -i $grep_opts
  69. else
  70. cat_logs $logroot $logs | tail -n $tail_history
  71. [ -f $logroot/$log ] && tail -n 0 -f $logroot/$log
  72. fi