summaryrefslogtreecommitdiff
path: root/showlog
blob: c6dcbd353b45f784d978c4b99186cfdc3019c446 (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|system [<keyword> [<keyword>...]]"
  8. exit 1
  9. }
  10. function exit1() {
  11. echo "Error: $1"
  12. echo "Exiting..."
  13. exit 1
  14. }
  15. tail_history="50"
  16. target=$1
  17. logroot="/var/log"
  18. case "$target" in
  19. ftp)
  20. log="xferlog"
  21. logs="$log.??.gz $log.?.gz $log.? $log"
  22. ;;
  23. web)
  24. logroot="/var/log/apache"
  25. log="access.log"
  26. logs="$log.??.gz $log.?.gz $log.? $log"
  27. ;;
  28. weberror)
  29. logroot="/var/log/apache"
  30. log="error.log"
  31. logs="$log.??.gz $log.?.gz $log.? $log"
  32. ;;
  33. mail)
  34. log="mail.log"
  35. logs="$log.??.gz $log.?.gz $log.? $log"
  36. ;;
  37. system)
  38. log="syslog"
  39. logs="$log.??.gz $log.?.gz $log.? $log"
  40. ;;
  41. *)
  42. usage
  43. ;;
  44. esac
  45. shift
  46. function cat_logs() {
  47. logroot=$1
  48. shift
  49. for pattern in $@; do
  50. for file in `find $logroot -name "$pattern" -type f -mindepth 1 -maxdepth 1 -follow | sort`; do
  51. case $pattern in
  52. *.gz)
  53. zcat $file;;
  54. *.bz2)
  55. bzcat $file;;
  56. *)
  57. cat $file;;
  58. esac
  59. done
  60. done
  61. }
  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