variables :caches => tilecaches, :renders => tilerenders
end
-template "/srv/tilecache/tilecache-fastest-peers" do
- source "tilecache-fastest-peers.erb"
- owner "root"
- group "root"
- mode 0o755
+file "/srv/tilecache/tilecache-fastest-peers" do
+ action :delete
+end
+
+ohai_plugin "tilecache" do
+ template "ohai.rb.erb"
end
--- /dev/null
+Ohai.plugin(:TileCache) do
+ provides "tilecache"
+
+ def tile_siblings
+ recent = Time.now - 600
+ times = Hash.new
+
+ # Find performance reports for last few minutes
+ # Add up total time taken to download tile grouped by remote server
+ # Remove 1 second per successful time report (de-prioritise new servers)
+ # Add 30 seconds per failed time report request
+ Dir.glob("/srv/tilecache/data/**/tilecache-*.txt").each do |path|
+ if File.mtime(path) > recent
+ IO.readlines(path).reverse.take(20).each do |sample|
+ if sample =~ %r{^(\d+\.\d+),(\d+),https://([^/]+)/} then
+ time = Regexp.last_match(1).to_f
+ status = Regexp.last_match(2).to_i
+ host = Regexp.last_match(3)
+
+ if status == 200 then
+ times[host] = times.fetch(host, 0) + time - 1
+ else
+ times[host] = times.fetch(host, 0) + 30
+ end
+ end
+ end
+ end
+ end
+
+ # Sort time reports
+ # Strip to best 4 server names
+ times.to_a.sort_by(&:last).take(4).map(&:first)
+ end
+
+ collect_data(:default) do
+ tilecache Mash.new
+
+ tilecache[:tile_siblings] = tile_siblings
+ end
+end
+++ /dev/null
-#!/bin/bash
-
-# Find performance reports for last few minutes
-# Add up total time taken to download tile grouped by remote server
-# Remove 1 second per successful time report (de-prioritise new servers)
-# Add 30 seconds per failed time report request
-# Sort time reports
-# Strip to best 4 server names
-
-/usr/bin/find /srv/tilecache/data/ -type f -iname 'tilecache-*.txt' -mmin -10 -print0 \
- | /usr/bin/xargs -0 --no-run-if-empty /usr/bin/tail --quiet -n 20 \
- | /usr/bin/awk -F ',' '$2 == 200 {time[$3] += $1; time[$3] -= 1} $2 != 200 {time[$3] +=30} END{for (i in time) print time[i], i}' \
- | /usr/bin/sort -k1n \
- | /usr/bin/cut -d '/' -f 3 \
- | /bin/grep -E '(openstreetmap|localhost)' \
- | /usr/bin/head -n 4