Class: CDMon::IPUpdater
- Inherits:
-
Object
- Object
- CDMon::IPUpdater
- Defined in:
- lib/ip_updater.rb
Instance Method Summary collapse
-
#initialize(config_file = "../config/cdmon.yml") ⇒ IPUpdater
constructor
A new instance of IPUpdater.
- #update ⇒ Object
Constructor Details
#initialize(config_file = "../config/cdmon.yml") ⇒ IPUpdater
Returns a new instance of IPUpdater.
12 13 14 15 16 |
# File 'lib/ip_updater.rb', line 12 def initialize(config_file = "../config/cdmon.yml") Config.load(config_file) @resolver = Resolv::DNS.new(:nameserver => Config.dns, :search => ["localhost"], :dots => 1) CDMon.log_level = Config.log_level end |
Instance Method Details
#update ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ip_updater.rb', line 18 def update url = URI.parse(Config.service_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = (url.scheme == 'https') #require 'debug' begin Config.users.keys.each do |usr| request = Net::HTTP::Get.new(Config.get_cdmon_ip_path_for(usr)) Config.hosts_for(usr).each do |host| host_ip_by_cdmon = @resolver.getaddress(host).to_s values = parse_response(http.request(request).body) current_cdmon_ip = values["newip"] if values.has_key?("newip") unless host_ip_by_cdmon == current_cdmon_ip request = Net::HTTP::Get.new(Config.cdmon_ip_path_for(usr, current_cdmon_ip)) response = parse_response(http.request(request).body) case response["resultat"] when Config::CDMON_OK_IP CDMon.log_all("IP Succesfully updated") when Config::CDMON_BAD_IP CDMon.log_all("Bad IP Provided") when Config::CDMON_ERROR_LOGIN CDMon.log_all("Login Error") end end end end rescue SocketError CDMon.log_all("SocketError: Probably the internet connection is broken") rescue Resolv::ResolvError CDMon.log_all("ResolvError: Cannot get DNS results") end end |