Skip to content

Instantly share code, notes, and snippets.

@zph
Created January 15, 2014 14:43
Show Gist options
  • Save zph/8437472 to your computer and use it in GitHub Desktop.
Save zph/8437472 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Copyright (c) 2013-4 Virtual Hold Technology. All Rights Reserved.
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
require 'rubygems'
require 'configliere'
require 'concurrent'
require 'pp'
require 'relay'
require 'vht/monitoring/file_collector'
DEFAULT_ENDPOINT = 'http://localhost/vhqwatchws/vhqwatchws.asmx?WSDL'
DEFAULT_CLOUD_ENDPOINT = 'http://localhost:8080/api'
DEFAULT_TENANT = 'VHT'
DEFAULT_INTERVAL = 10
DEFAULT_TIMEOUT = 60
module Vht
module Monitoring
class RelayDaemon
def self.run
boss = Concurrent::Supervisor.new
if Settings[:filename] && Settings[:filename] != ''
collector = FileCollector.new(Settings[:filename], Settings[:tenant]) # testing
else
collector = Collector.new(Settings[:endpoint], Settings[:tenant]) # actual
end
relay = Relay.new(Settings[:cloud_endpoint], Settings[:cloud_customer_guid], Settings[:tenant])
task = Concurrent::TimerTask.new( execution_interval: Settings[:interval], timeout_interval: Settings[:timeout], run_now: true ) {
begin
result = collector.collect
relay.post(result)
rescue Savon::UnknownOperationError, SocketError => e
# TODO: this needs to be coupled with logic on server side to alert to lack of receiving of data
puts "ERROR: Cannot communicate with VHT QWatch WebService - do you have the correct endpoint specified?"
boss.stop
rescue => e
puts "ERROR: #{e}"
puts e.backtrace
pp e
end
}
boss.add_worker(task)
boss.add_worker(relay)
Signal.trap('INT') { boss.stop }
boss.run
end
end
end
end
if $0 == __FILE__
Settings.define :endpoint, env_var: 'VHT_ENDPOINT'
Settings.define :filename, env_var: 'VHT_FILENAME'
Settings.define :tenant, env_var: 'VHT_TENANT'
Settings.define :interval, env_var: 'VHT_INTERVAL', type: Integer
Settings.define :timeout, env_var: 'VHT_TIMEOUT', type: Integer
Settings.define :cloud_endpoint, env_var: 'VHT_CLOUD'
Settings.define :cloud_customer_guid, env_var: 'VHT_CLOUD'
Settings({
endpoint: DEFAULT_ENDPOINT,
tenant: DEFAULT_TENANT,
cloud_endpoint: DEFAULT_CLOUD_ENDPOINT,
interval: DEFAULT_INTERVAL,
timeout: DEFAULT_TIMEOUT
})
Settings.use(:commandline)
Settings.resolve!
Settings.read(Settings[:config]) if Settings[:config]
Settings.resolve!
Vht::Monitoring::RelayDaemon.run
end
# Copyright (c) 2013-4 Virtual Hold Technology. All Rights Reserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment