update-named-status.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/ruby
  2. require "yaml"
  3. require 'db'
  4. require 'db-config'
  5. def do_update verbose
  6. now = "TIMESTAMP '" + $db.query_row("SELECT max(last_seen) AS max FROM router_claims_nickname")['max'].to_s + "'"
  7. denamed = $db.do("
  8. UPDATE router_claims_nickname
  9. SET named=false
  10. WHERE named
  11. AND last_seen < #{now} - INTERVAL '6 months'")
  12. puts "de-named: #{denamed}" if verbose
  13. named = $db.do("
  14. UPDATE router_claims_nickname
  15. SET named=true
  16. WHERE NOT named
  17. AND first_seen < #{now} - INTERVAL '2 weeks'
  18. AND last_seen > #{now} - INTERVAL '2 days'
  19. AND NOT EXISTS (SELECT *
  20. FROM router_claims_nickname AS innertable
  21. WHERE named
  22. AND router_claims_nickname.nickname_id=innertable.nickname_id) "+ # if that nickname is already named, we lose.
  23. " AND NOT EXISTS (SELECT *
  24. FROM router_claims_nickname AS innertable
  25. WHERE router_claims_nickname.nickname_id=innertable.nickname_id
  26. AND router_claims_nickname.router_id <> innertable.router_id
  27. AND last_seen > #{now} - INTERVAL '1 month') ") # if nobody else wanted that nickname in the last month we are set
  28. puts "named: #{named}" if verbose
  29. end
  30. if __FILE__ == $0
  31. $db = Db.new($CONFIG['database']['dbname'], $CONFIG['database']['user'], $CONFIG['database']['password'])
  32. verbose = ARGV.first == "-v"
  33. $db.transaction_begin
  34. do_update verbose
  35. $db.transaction_commit
  36. end