update-named-status.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = "TIMESTAMP '" + $db.query_row("SELECT max(last_seen) AS max FROM router_claims_nickname")['max'].to_s + "'"
  28. denamed = $db.do("
  29. UPDATE router_claims_nickname
  30. SET named=false
  31. WHERE named
  32. AND last_seen < #{now} - INTERVAL '6 months'")
  33. puts "de-named: #{denamed}" if verbose
  34. named = $db.do("
  35. UPDATE router_claims_nickname
  36. SET named=true
  37. WHERE NOT named
  38. AND first_seen < #{now} - INTERVAL '2 weeks'
  39. AND last_seen > #{now} - INTERVAL '2 days'
  40. AND NOT EXISTS (SELECT *
  41. FROM router_claims_nickname AS innertable
  42. WHERE named
  43. AND router_claims_nickname.nickname_id=innertable.nickname_id) "+ # if that nickname is already named, we lose.
  44. " AND NOT EXISTS (SELECT *
  45. FROM router_claims_nickname AS innertable
  46. WHERE router_claims_nickname.nickname_id=innertable.nickname_id
  47. AND router_claims_nickname.router_id <> innertable.router_id
  48. AND last_seen > #{now} - INTERVAL '1 month') ") # if nobody else wanted that nickname in the last month we are set
  49. puts "named: #{named}" if verbose
  50. end
  51. if __FILE__ == $0
  52. $db = Db.new($CONFIG['database']['dbname'], $CONFIG['database']['user'], $CONFIG['database']['password'])
  53. verbose = ARGV.first == "-v"
  54. $db.transaction_begin
  55. do_update verbose
  56. $db.transaction_commit
  57. end