update-named-status.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/ruby
  2. # update-named-status.rb - update the named status of routers in our database
  3. #
  4. # Copyright (c) 2007 Peter Palfrader
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. require "yaml"
  24. require 'db'
  25. require 'db-config'
  26. def do_update(verbose)
  27. now = $db.query_row("SELECT max(last_seen) AS max FROM router_claims_nickname")['max']
  28. unless now
  29. STDERR.puts "Could not find the latest last_seen timestamp. Is the database empty still?"
  30. return
  31. end
  32. now = "TIMESTAMP '" + now.to_s + "'"
  33. denamed = $db.do("
  34. UPDATE router_claims_nickname
  35. SET named=false
  36. WHERE named
  37. AND last_seen < #{now} - INTERVAL '6 months'")
  38. puts "de-named: #{denamed}" if verbose
  39. named = $db.do("
  40. UPDATE router_claims_nickname
  41. SET named=true
  42. WHERE NOT named
  43. AND first_seen < #{now} - INTERVAL '2 weeks'
  44. AND last_seen > #{now} - INTERVAL '2 days'
  45. AND NOT EXISTS (SELECT *
  46. FROM router_claims_nickname AS innertable
  47. WHERE named
  48. AND router_claims_nickname.nickname_id=innertable.nickname_id) "+ # if that nickname is already named, we lose.
  49. " AND NOT EXISTS (SELECT *
  50. FROM router_claims_nickname AS innertable
  51. WHERE router_claims_nickname.nickname_id=innertable.nickname_id
  52. AND router_claims_nickname.router_id <> innertable.router_id
  53. AND last_seen > #{now} - INTERVAL '1 month') ") # if nobody else wanted that nickname in the last month we are set
  54. puts "named: #{named}" if verbose
  55. end
  56. if __FILE__ == $0
  57. $db = Db.new($CONFIG['database']['dbhost'], $CONFIG['database']['dbname'], $CONFIG['database']['user'], $CONFIG['database']['password'])
  58. verbose = ARGV.first == "-v"
  59. $db.transaction_begin
  60. do_update verbose
  61. $db.transaction_commit
  62. end