summaryrefslogtreecommitdiff
path: root/showlog
blob: a6d57a04298101e2ccb97a7dee99bba4747117ca (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. LOCALCONFIG=/etc/local/showlog.conf
  43. . $LOCALCONFIG || exit1 "Unable to read local config file $LOCALCONFIG"
  44. case "$target" in
  45. ftp)
  46. log="xferlog"
  47. logs="$log.??.gz $log.?.gz $log.? $log"
  48. ;;
  49. web)
  50. logroot="/var/log/apache"
  51. log="access.log"
  52. logs="$log.??.gz $log.?.gz $log.? $log"
  53. ;;
  54. weberror)
  55. logroot="/var/log/apache"
  56. log="error.log"
  57. logs="$log.??.gz $log.?.gz $log.? $log"
  58. ;;
  59. mail)
  60. log="mail.log"
  61. logs="$log.??.gz $log.?.gz $log.? $log"
  62. ;;
  63. esac
  64. if [ $# \> 0 ]; then
  65. grep_opts=""
  66. for keyword in $@; do
  67. grep_opts="$opts -e $keyword"
  68. done
  69. cat_logs $logroot $logs | grep -i $grep_opts | tail -n $tail_history
  70. [ -f $logroot/$log ] && tail -n 0 -f $logroot/$log | grep -i $grep_opts
  71. else
  72. cat_logs $logroot $logs | tail -n $tail_history
  73. [ -f $logroot/$log ] && tail -n 0 -f $logroot/$log
  74. fi