一个日志的分析脚本

  1. #!/bin/bash
  2. cat << EOF
  3. ------------------------------------
  4. 日志分析小工具
  5. ------------------------------------
  6. EOF
  7.  
  8. LANG=en_US
  9.  
  10. Usage() {
  11. echo "Usage: $0 Logfile"
  12. }
  13.  
  14. if [ $# -eq 0 ] ;then
  15. Usage
  16. exit 0
  17. else
  18. Log=$1
  19. fi
  20.  
  21. Tmpfile=$(mktemp)
  22.  
  23. Year=$(date |awk '{print $6}')
  24. Mon=$(date |awk '{print $2}')
  25. Day=$(date |awk '{print $3}')
  26.  
  27. Yesterday=$((Day-1))
  28.  
  29. echo -e "\n\033[31m-------the ${Year}-${Mon}-${Yesterday} log statistics as shown:--------\033[0m"
  30.  
  31. echo -e "\n\033[32m the UV in total yesterday:\033[0m\n"
  32.  
  33. cat $Log| sed -n "/${Yesterday}\/${Mon}\/${Year}:00/,/${Day}\/${Mon}\/${Year}:00/p" | awk '{print $7}'|wc -l
  34.  
  35. echo -e "\n\033[32m Most of the time yesterday:\033[0m\n"
  36.  
  37. cat $Log |awk '{print $4}' |cut -c 14-18 |sort|uniq -c | sort -nr | head -10
  38.  
  39. echo -e "\n\033[32m Most of the page yesterday:\033[0m\n"
  40.  
  41. cat $Log |sed -n "/${Yesterday}\/${Mon}\/${Year}:00/,/${Day}\/${Mon}\/${Year}:00/p" | awk '{print $11}' |sort |uniq -c |sort -rn |head -10
  42.  
  43. echo -e "\n\033[32m Most of the ip yesterday:\033[0m\n"
  44.  
  45. cat $Log| sed -n "/${Yesterday}\/${Mon}\/${Year}:00/,/${Day}\/${Mon}\/${Year}:00/p" | awk '{print $1}' | sort |uniq -c |sort -rn |head -10 > $Tmpfile
  46. cat $Tmpfile | while read line;
  47. do
  48. num=$(echo $line|awk '{print $1}')
  49. echo -n "访问次数:$num "
  50. ip=$(echo $line|awk '{print $2}')
  51. curl -s ip.cn?ip=$ip
  52. done
posted @ 2018-05-28 11:28  老僧观天下  阅读(161)  评论(0编辑  收藏  举报