[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Debian JP master SVN www commits (rev.547)



=======================================================
Repository: /org/svn.debian.or.jp/repos
  Revision: 547
  Commiter: yasu
      Date: 2008-02-03 13:38:37 +0900 (日, 03  2月 2008)
=======================================================
Log:



=======================================================
Changed:

A   cdn/trunk/check-surrogates.rb

Copied: cdn/trunk/check-surrogates.rb (from rev 535, cdn/branches/non-geoip/check-surrogates.rb)
===================================================================
--- cdn/trunk/check-surrogates.rb	                        (rev 0)
+++ cdn/trunk/check-surrogates.rb	2008-02-03 04:38:37 UTC (rev 547)
@@ -0,0 +1,127 @@
+#!/usr/bin/env ruby
+
+require 'socket'
+require 'syslog'
+require 'timeout'
+require 'net/http'
+require 'uri'
+require 'resolv'
+require 'find'
+require 'time'
+
+class CheckSurrogate
+  attr :lines
+
+  def initialize
+    @lines = ''
+    @slog = Syslog.open(__FILE__,   
+                        Syslog::Constants::LOG_PID |
+                        Syslog::Constants::LOG_CONS,
+                        Syslog::Constants::LOG_DAEMON)
+    @last_modified = nil
+  end
+
+  def checkhttp(host, tracefile, first_surrogate=nil, port=80)
+    begin 
+      h = Net::HTTP.new(host, port)
+      h.open_timeout = 8 
+      h.read_timeout = 8
+      case response = h.head("/debian/project/trace/#{tracefile}", {"User-Agent" => "Debian-cdn-mirror-ping/1.0"})
+      when Net::HTTPSuccess
+        if first_surrogate
+          @last_modified = Time.parse(response['last-modified'])
+          @slog.info("#{host} return #{response.code} #{@last_modified} set")
+          return true
+        elsif @last_modified && @last_modified == Time.parse(response['last-modified'])
+          @slog.info("#{host} return #{response.code} #{@last_modified} equal")
+          return true
+        elsif @last_modified && @last_modified != Time.parse(response['last-modified'])
+          @slog.info("#{host} return #{response.code} #{response['last-modified']} different timestamp. Ignore this host.")
+          return nil
+        else
+          @slog.info("#{host} return #{response.code} (please set $first_surrogate")
+          return true
+        end
+      else
+        @slog.info("#{host} return #{response.code}")
+        return nil
+      end
+    rescue Timeout::Error
+      @slog.info("#{host} timeout (#{$!})")
+      return nil
+    rescue Errno::ECONNREFUSED
+      @slog.info("#{host} refused open (#{$!})")
+      return nil
+    rescue
+      @slog.info("#{host} some error (#{$!})")
+      return nil
+    end
+  end
+
+  def make_surrogate_line(listfile)
+    domain = listfile.gsub(/\.\/lists_/, '')
+    domain.gsub!(/_/, '.')
+
+    surrogates = ''
+    @last_modified = nil
+    $surrogates = ''
+    $tracefile = ''
+    $first_surrogate = ''
+    require listfile
+
+    if $tracefile && $first_surrogate
+      checkhttp($first_surrogate, $tracefile, true)
+    end
+
+    surrogates = $surrogates
+
+    s_active = Hash.new
+    surrogates.each do |t_ip, t_value|
+      if checkhttp(t_ip, $tracefile)
+        s_active[t_ip.gsub('.',',')] = t_value
+      else
+      end
+    end
+
+    @lines += "\t\"#{domain}\" => [\n"
+    s_active.each do |k,v|
+      @lines += "\t\t" + '[[' + k + '], ' + v + '],' + "\n"
+    end
+    @lines += "\t],\n"
+
+  end
+end
+
+domains = Array.new
+listfiles = Array.new
+Find.find(File.dirname(__FILE__)) do |f|
+  if f =~ /^\.\/lists_([\w_]+)\.rb$/
+    cl = $1
+    listfiles.push f.gsub(/\.rb$/,'')
+  end
+end
+
+cs = CheckSurrogate.new
+
+listfiles.each do |f|
+  cs.make_surrogate_line(f)
+end
+
+foo = File.open("addr", 'w')
+foo.puts "##" + Time.now.to_s + "\n\n"
+foo.puts '$addr_db = {
+  "default" => {
+    "ns.cdn.araki.net" => [
+      [[210,157,158,38], 0],
+    ],
+    "localhost" => [
+      [[127,0,0,1], 0],
+    ],
+'
+foo.puts cs.lines
+
+foo.puts '
+  },
+}'
+
+foo.close