summaryrefslogtreecommitdiff
path: root/showlog
blob: 96c3803933e0e1877f90447a2b8861607d33e361 (plain)
  1. #!/bin/bash
  2. #
  3. # /usr/local/sbin/showlog
  4. # Copyright 2002 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: showlog,v 1.4 2002-03-07 16:22:51 jonas Exp $
  7. #
  8. # Show topic-specific log entries
  9. #
  10. # halt on errors (NB! this is a bashism...)
  11. set -e
  12. function usage() {
  13. echo "Usage: $(basename $0) ftp|web|weberror|mail|system [<keyword> [<keyword>...]]"
  14. exit 1
  15. }
  16. function exit1() {
  17. echo "Error: $1"
  18. echo "Exiting..."
  19. exit 1
  20. }
  21. tail_history="50"
  22. target=$1
  23. logroot="/var/log"
  24. case "$target" in
  25. ftp)
  26. log="xferlog"
  27. logs="$log.??.gz $log.?.gz $log.? $log"
  28. ;;
  29. web)
  30. logroot="/var/log/apache"
  31. log="access.log"
  32. logs="$log.??.gz $log.?.gz $log.? $log"
  33. ;;
  34. weberror)
  35. logroot="/var/log/apache"
  36. log="error.log"
  37. logs="$log.??.gz $log.?.gz $log.? $log"
  38. ;;
  39. mail)
  40. log="mail.log"
  41. logs="$log.??.gz $log.?.gz $log.? $log"
  42. ;;
  43. system)
  44. log="syslog"
  45. logs="$log.??.gz $log.?.gz $log.? $log"
  46. ;;
  47. *)
  48. usage
  49. ;;
  50. esac
  51. shift
  52. function cat_logs() {
  53. logroot=$1
  54. shift
  55. for pattern in $@; do
  56. for file in `find $logroot -name "$pattern" -type f -mindepth 1 -maxdepth 1 -follow | sort`; do
  57. case $pattern in
  58. *.gz)
  59. zcat $file;;
  60. *.bz2)
  61. bzcat $file;;
  62. *)
  63. cat $file;;
  64. esac
  65. done
  66. done
  67. }
  68. if [ $# \> 0 ]; then
  69. grep_opts=""
  70. for keyword in $@; do
  71. grep_opts="$opts -e $keyword"
  72. done
  73. cat_logs $logroot $logs | grep -i $grep_opts | tail -n $tail_history
  74. [ -f $logroot/$log ] && tail -n 0 -f $logroot/$log | grep -i $grep_opts
  75. else
  76. cat_logs $logroot $logs | tail -n $tail_history
  77. [ -f $logroot/$log ] && tail -n 0 -f $logroot/$log
  78. fi