Advertisement
Guest User

Untitled

a guest
Oct 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.99 KB | None | 0 0
  1. # 178.47.189.163 - - [12/Dec/2015:18:53:11 +0100] "POST /administrator/index.php HTTP/1.1" 200 4494 "http://almhuette-raith.at/administrator/" "Mozilla/5.0 (Windows NT 6.0; rv:34.0) Gecko/20100101 Firefox/34.0" "-"
  2. # http://www.almhuette-raith.at/apache-log/access.log
  3.  
  4. rx = /^
  5.     (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+                     # ip-address
  6.     .+?\s+
  7.     .+?\s+
  8.     \[(\d{1,2})\/(\w+)\/(\d+):(\d+):(\d+):(\d+)\s\+(\d+)\]\s+   # datetime
  9.     \"(\w+) (.+?) ([\w\/\.]+)\"\s+                              # method resource protocol
  10.    (\d+)\s+                                                    # status
  11.    .+?\s+
  12.    \"(.*?)\"\s+                                                # host
  13.    \"(.*?)\"\s+                                                # user-agent
  14.    \".+?\"
  15.    $/x
  16.  
  17. requests_count = 0
  18. error_4xx_count = 0
  19. error_5xx_count = 0
  20. resources = Hash.new
  21. ips = Hash.new
  22.  
  23. File.open("access.log").each do |line|
  24.  # p line.match(rx)
  25.  requests_count += 1
  26.  res = Hash.new
  27.  res["ip"],
  28.      res["day"],
  29.      res["month"],
  30.      res["year"],
  31.      res["hour"],
  32.      res["minute"],
  33.      res["second"],
  34.      res["zone"],
  35.      res["method"],
  36.      res["resource"],
  37.      res["protocol"],
  38.      res["status"],
  39.      res["host"],
  40.      res["user-agent"] = line.match(rx).captures
  41.  
  42.  if res["status"].start_with? "4"
  43.    error_4xx_count += 1
  44.  elsif res["status"].start_with? "5"
  45.    error_5xx_count += 1
  46.  end
  47.  
  48.  if resources[res["resource"]].nil?
  49.    resources[res["resource"]] = 1
  50.  else
  51.    resources[res["resource"]] += 1
  52.  end
  53.  
  54.  if ips[res["ip"]].nil?
  55.    ips[res["ip"]] = 1
  56.  else
  57.    ips[res["ip"]] += 1
  58.  end
  59.  
  60. end
  61.  
  62. puts "Проанализировано #{requests_count} записей"
  63. puts "Количество 4хх-ошибок #{error_4xx_count} (#{error_4xx_count.to_f / requests_count * 100}%)"
  64. puts "Количество 5хх-ошибок #{error_5xx_count} (#{error_5xx_count.to_f / requests_count * 100}%)"
  65. puts resources
  66. puts ips
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement