To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / extra / soundsoftware / get-apache-log-stats.rb @ 1454:02a05da0bedc
History | View | Annotate | Download (4.3 KB)
| 1 |
|
|---|---|
| 2 |
# Read an Apache log file in SoundSoftware site format from stdin and
|
| 3 |
# produce some per-project stats.
|
| 4 |
#
|
| 5 |
# Invoke with e.g.
|
| 6 |
#
|
| 7 |
# cat /var/log/apache2/code-access.log | \
|
| 8 |
# script/runner -e production extra/soundsoftware/get-apache-log-stats.rb
|
| 9 |
|
| 10 |
|
| 11 |
# Use the ApacheLogRegex parser, a neat thing
|
| 12 |
# See http://www.simonecarletti.com/blog/2009/02/apache-log-regex-a-lightweight-ruby-apache-log-parser/
|
| 13 |
require 'apachelogregex'
|
| 14 |
|
| 15 |
# This is the format defined in our httpd.conf
|
| 16 |
vhost_combined_format = '%v:%p %h %{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"'
|
| 17 |
|
| 18 |
parser = ApacheLogRegex.new(vhost_combined_format)
|
| 19 |
|
| 20 |
# project name -> count of hg clones
|
| 21 |
clones = Hash.new(0) |
| 22 |
|
| 23 |
# project name -> count of hg pulls
|
| 24 |
pulls = Hash.new(0) |
| 25 |
|
| 26 |
# project name -> count of hg pushes
|
| 27 |
pushes = Hash.new(0) |
| 28 |
|
| 29 |
# project name -> count of hg archive requests (i.e. Download as Zip)
|
| 30 |
zips = Hash.new(0) |
| 31 |
|
| 32 |
# project name -> count of hits to pages under /projects/projectname
|
| 33 |
hits = Hash.new(0) |
| 34 |
|
| 35 |
# project name -> Project object
|
| 36 |
@projects = Hash.new |
| 37 |
|
| 38 |
parseable = 0
|
| 39 |
unparseable = 0
|
| 40 |
|
| 41 |
def is_public_project?(project) |
| 42 |
if !project
|
| 43 |
false
|
| 44 |
elsif project =~ /^\d+$/ |
| 45 |
# ignore numerical project ids, they are only used when editing projects
|
| 46 |
false
|
| 47 |
elsif @projects.key?(project) |
| 48 |
@projects[project].is_public?
|
| 49 |
else
|
| 50 |
pobj = Project.find_by_identifier(project)
|
| 51 |
if pobj
|
| 52 |
@projects[project] = pobj
|
| 53 |
pobj.is_public? |
| 54 |
else
|
| 55 |
print "Project not found: ", project, "\n" |
| 56 |
false
|
| 57 |
end
|
| 58 |
end
|
| 59 |
end
|
| 60 |
|
| 61 |
def print_stats(h) |
| 62 |
h.keys.sort { |a,b| h[b] <=> h[a] }.each do |p|
|
| 63 |
if h[p] > 0 |
| 64 |
print h[p], " ", @projects[p].name, " [", p, "]\n" |
| 65 |
end
|
| 66 |
end
|
| 67 |
end
|
| 68 |
|
| 69 |
STDIN.each do |line| |
| 70 |
|
| 71 |
record = parser.parse(line) |
| 72 |
|
| 73 |
# most annoyingly, the parser can't handle the comma-separated list
|
| 74 |
# in X-Forwarded-For where it has more than one element. If it has
|
| 75 |
# failed, remove any IP addresses or the word "unknown" with
|
| 76 |
# trailing commas and try again
|
| 77 |
if not record |
| 78 |
filtered = line.gsub(/(unknown|([0-9]+\.){3}[0-9]+),\s*/, "") |
| 79 |
record = parser.parse(filtered) |
| 80 |
end
|
| 81 |
|
| 82 |
# discard, but count, unparseable lines
|
| 83 |
if not record |
| 84 |
print "Line not parseable: ", line, "\n" |
| 85 |
unparseable += 1
|
| 86 |
next
|
| 87 |
end
|
| 88 |
|
| 89 |
# discard everything that isn't a 200 OK response
|
| 90 |
next if record["%>s"] != "200" |
| 91 |
|
| 92 |
# discard anything apparently requested by a crawler
|
| 93 |
next if record["%{User-Agent}i"] =~ /(bot|slurp|crawler|spider|Redmine)\b/i |
| 94 |
|
| 95 |
# pull out request e.g. GET / HTTP/1.0
|
| 96 |
request = record["%r"]
|
| 97 |
|
| 98 |
# split into method, path, protocol
|
| 99 |
if not request =~ /^[^\s]+ ([^\s]+) [^\s]+$/ |
| 100 |
print "Line not parseable (bad method, path, protocol): ", line, "\n" |
| 101 |
unparseable += 1
|
| 102 |
next
|
| 103 |
end
|
| 104 |
|
| 105 |
# get the path e.g. /projects/weevilmatic and split on /
|
| 106 |
path = $~[1] |
| 107 |
components = path.split("/")
|
| 108 |
|
| 109 |
# should have at least two elements unless path is "/"; first should
|
| 110 |
# be empty (begins with /)
|
| 111 |
if path != "/" and (components.size < 2 or components[0] != "") |
| 112 |
print "Line not parseable (degenerate path): ", line, "\n" |
| 113 |
unparseable += 1
|
| 114 |
next
|
| 115 |
end
|
| 116 |
|
| 117 |
if components[1] == "hg" |
| 118 |
|
| 119 |
# path is /hg/project?something or /hg/project/something
|
| 120 |
|
| 121 |
project = components[2].split("?")[0] |
| 122 |
if not is_public_project?(project) |
| 123 |
next
|
| 124 |
end
|
| 125 |
|
| 126 |
if components[2] =~ /&roots=00*$/ |
| 127 |
clones[project] += 1
|
| 128 |
elsif components[2] =~ /cmd=capabilities/ |
| 129 |
pulls[project] += 1
|
| 130 |
elsif components[2] =~ /cmd=unbundle/ |
| 131 |
pushes[project] += 1
|
| 132 |
elsif components[3] == "archive" |
| 133 |
zips[project] += 1
|
| 134 |
end
|
| 135 |
|
| 136 |
elsif components[1] == "projects" |
| 137 |
|
| 138 |
# path is /projects/project or /projects/project/something
|
| 139 |
|
| 140 |
project = components[2]
|
| 141 |
project = project.split("?")[0] if project |
| 142 |
if not is_public_project?(project) |
| 143 |
next
|
| 144 |
end
|
| 145 |
|
| 146 |
hits[project] += 1
|
| 147 |
|
| 148 |
end
|
| 149 |
|
| 150 |
parseable += 1
|
| 151 |
end
|
| 152 |
|
| 153 |
# Each clone is also a pull; deduct it from the pulls hash, because we
|
| 154 |
# want that to contain only non-clone pulls
|
| 155 |
|
| 156 |
clones.keys.each do |project|
|
| 157 |
pulls[project] -= 1
|
| 158 |
end
|
| 159 |
|
| 160 |
print parseable, " parseable\n"
|
| 161 |
print unparseable, " unparseable\n"
|
| 162 |
|
| 163 |
|
| 164 |
print "\nMercurial clones:\n"
|
| 165 |
print_stats clones |
| 166 |
|
| 167 |
print "\nMercurial pulls (excluding clones):\n"
|
| 168 |
print_stats pulls |
| 169 |
|
| 170 |
print "\nMercurial pushes:\n"
|
| 171 |
print_stats pushes |
| 172 |
|
| 173 |
print "\nMercurial archive (zip file) downloads:\n"
|
| 174 |
print_stats zips |
| 175 |
|
| 176 |
print "\nProject page hits (excluding crawlers):\n"
|
| 177 |
print_stats hits |
| 178 |
|
| 179 |
|