You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.8 KiB

  1. #!/bin/bash
  2. logs=""
  3. page="/"
  4. ignore=""
  5. countUnique=""
  6. logcheck() {
  7. if [ ! -f /var/log/apache2/$logs/access.log ]; then
  8. usage
  9. elif [ "$countUnique" != "" ] && [ "$ignore" != "" ]; then
  10. cat /var/log/apache2/$logs/access.log | grep -w "$page" | cut -d- -f1 | grep -v "$ignore" | sort -u | wc -l
  11. elif [ "$countUnique" != "" ] && [ "$ignore" == "" ];then
  12. cat /var/log/apache2/$logs/access.log | grep -w "$page" | cut -d- -f1 | sort -u | wc -l
  13. elif [ "$countUnique" == "" ] && [ "$ignore" != "" ];then
  14. cat /var/log/apache2/$logs/access.log | grep -w "$page" | cut -d- -f1 | grep -v "$ignore"
  15. else
  16. cat /var/log/apache2/$logs/access.log | grep -w "$page" | cut -d- -f1
  17. fi
  18. }
  19. usage() {
  20. cat <<EOF
  21. logcheck
  22. Description: A quick way to search apache2 logs for IP addresses and unique hits.
  23. Usage: logcheck [-h] [(-l <logs>)] [(-p <page>)] [-u] [(-i <ignore>)]
  24. -h Show this help message
  25. -l Choose the logs to check
  26. -p Choose what page to check (checks "/" by default)
  27. -u Count IP Adresses in logs
  28. -i Ignore an IP or set of IP's
  29. Examples:
  30. logcheck -h
  31. logcheck -l example.com -i 136.206 -u
  32. logcheck -l example.com -p /about -i 136.206.5.71
  33. logcheck -l example.com -p /contact -i 136.206.5.71 -u
  34. EOF
  35. }
  36. # Accepts only w, m, y, and h
  37. # Anything else results in the help message
  38. while getopts ":l:p:i:uh" opt; do
  39. case "${opt}" in
  40. h)
  41. usage
  42. exit 0
  43. ;;
  44. l)
  45. logs=$OPTARG
  46. ;;
  47. p)
  48. page=$OPTARG
  49. ;;
  50. u)
  51. countUnique="Y"
  52. ;;
  53. i)
  54. ignore=$OPTARG
  55. ;;
  56. *)
  57. usage
  58. exit 0
  59. ;;
  60. esac
  61. done
  62. logcheck || exit 1
  63. exit 0