Class: ServerController
- Inherits:
-
Pidly::Control
- Object
- Pidly::Control
- ServerController
- Defined in:
- lib/instrumental_tools/server_controller.rb
Constant Summary collapse
- COMMANDS =
[:start, :stop, :status, :restart, :clean, :kill, :foreground]
Instance Attribute Summary collapse
-
#current_api_key ⇒ Object
readonly
Returns the value of attribute current_api_key.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#run_options ⇒ Object
Returns the value of attribute run_options.
Instance Method Summary collapse
- #agent ⇒ Object
- #build_agent(key, address, enabled) ⇒ Object
- #collector_address ⇒ Object
- #config_file_api_key ⇒ Object
- #config_file_available? ⇒ Boolean
- #configured_api_key ⇒ Object
- #debug? ⇒ Boolean
- #enable_scripts? ⇒ Boolean
- #enabled? ⇒ Boolean
- #foreground ⇒ Object
- #hostname ⇒ Object
-
#initialize(options = {}) ⇒ ServerController
constructor
A new instance of ServerController.
- #key_has_changed? ⇒ Boolean
- #next_run_at(at_moment = Time.now.to_i) ⇒ Object
- #report_interval ⇒ Object
- #run ⇒ Object
- #script_executor ⇒ Object
- #script_location ⇒ Object
- #set_new_agent(key, address) ⇒ Object
- #time_to_sleep ⇒ Object
- #user_specified_api_key ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ServerController
Returns a new instance of ServerController.
29 30 31 32 |
# File 'lib/instrumental_tools/server_controller.rb', line 29 def initialize(={}) @run_options = .delete(:run_options) || {} super() end |
Instance Attribute Details
#current_api_key ⇒ Object (readonly)
Returns the value of attribute current_api_key.
10 11 12 |
# File 'lib/instrumental_tools/server_controller.rb', line 10 def current_api_key @current_api_key end |
#pid ⇒ Object
Returns the value of attribute pid.
9 10 11 |
# File 'lib/instrumental_tools/server_controller.rb', line 9 def pid @pid end |
#run_options ⇒ Object
Returns the value of attribute run_options.
9 10 11 |
# File 'lib/instrumental_tools/server_controller.rb', line 9 def @run_options end |
Instance Method Details
#agent ⇒ Object
73 74 75 76 77 78 |
# File 'lib/instrumental_tools/server_controller.rb', line 73 def agent if key_has_changed? set_new_agent(configured_api_key, collector_address) end @agent end |
#build_agent(key, address, enabled) ⇒ Object
62 63 64 65 |
# File 'lib/instrumental_tools/server_controller.rb', line 62 def build_agent(key, address, enabled) secure_protocol = address.split(':').last != '8000' Instrumental::Agent.new(key, collector: address, enabled: enabled, secure: secure_protocol) end |
#collector_address ⇒ Object
38 39 40 |
# File 'lib/instrumental_tools/server_controller.rb', line 38 def collector_address [[:collector], [:port]].compact.join(':') end |
#config_file_api_key ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/instrumental_tools/server_controller.rb', line 46 def config_file_api_key if config_file_available? config_contents = YAML.load(File.read([:config_file])) if config_contents.is_a?(Hash) config_contents['api_key'] end end rescue Exception => e puts "Error loading config file %s: %s" % [[:config_file], e.] nil end |
#config_file_available? ⇒ Boolean
105 106 107 |
# File 'lib/instrumental_tools/server_controller.rb', line 105 def config_file_available? File.exists?([:config_file]) end |
#configured_api_key ⇒ Object
58 59 60 |
# File 'lib/instrumental_tools/server_controller.rb', line 58 def configured_api_key (user_specified_api_key || config_file_api_key).to_s.strip end |
#debug? ⇒ Boolean
113 114 115 |
# File 'lib/instrumental_tools/server_controller.rb', line 113 def debug? !![:debug] end |
#enable_scripts? ⇒ Boolean
117 118 119 |
# File 'lib/instrumental_tools/server_controller.rb', line 117 def enable_scripts? !![:enable_scripts] end |
#enabled? ⇒ Boolean
109 110 111 |
# File 'lib/instrumental_tools/server_controller.rb', line 109 def enabled? agent.enabled end |
#foreground ⇒ Object
34 35 36 |
# File 'lib/instrumental_tools/server_controller.rb', line 34 def foreground run end |
#hostname ⇒ Object
84 85 86 |
# File 'lib/instrumental_tools/server_controller.rb', line 84 def hostname [:hostname] end |
#key_has_changed? ⇒ Boolean
121 122 123 |
# File 'lib/instrumental_tools/server_controller.rb', line 121 def key_has_changed? current_api_key != configured_api_key end |
#next_run_at(at_moment = Time.now.to_i) ⇒ Object
96 97 98 |
# File 'lib/instrumental_tools/server_controller.rb', line 96 def next_run_at(at_moment = Time.now.to_i) (at_moment - at_moment % report_interval) + report_interval end |
#report_interval ⇒ Object
80 81 82 |
# File 'lib/instrumental_tools/server_controller.rb', line 80 def report_interval [:report_interval] end |
#run ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/instrumental_tools/server_controller.rb', line 125 def run puts "instrument_server version #{Instrumental::Tools::VERSION} started at #{Time.now.utc}" puts "Collecting stats under the hostname: #{hostname}" loop do sleep time_to_sleep if enabled? inspector = SystemInspector.new inspector.load_all count = 0 inspector.gauges.each do |stat, value| metric = [hostname, stat].join(".") agent.gauge(metric, value) if debug? puts [metric, value].join(":") end count += 1 end if enable_scripts? script_executor.run.each do |(stat, value, time)| metric = [hostname, stat].join(".") agent.gauge(metric, value, time) if debug? puts [metric, value].join(":") end count += 1 end end agent.flush agent.stop if debug? puts "Sent #{count} metrics" end end end end |
#script_executor ⇒ Object
92 93 94 |
# File 'lib/instrumental_tools/server_controller.rb', line 92 def script_executor @executor ||= MetricScriptExecutor.new(script_location) end |
#script_location ⇒ Object
88 89 90 |
# File 'lib/instrumental_tools/server_controller.rb', line 88 def script_location [:script_location] end |
#set_new_agent(key, address) ⇒ Object
67 68 69 70 71 |
# File 'lib/instrumental_tools/server_controller.rb', line 67 def set_new_agent(key, address) key = key.to_s.strip @current_api_key = key @agent = build_agent(key, collector_address, key.size > 0) end |
#time_to_sleep ⇒ Object
100 101 102 103 |
# File 'lib/instrumental_tools/server_controller.rb', line 100 def time_to_sleep t = Time.now.to_i [next_run_at(t) - t, 0].max end |
#user_specified_api_key ⇒ Object
42 43 44 |
# File 'lib/instrumental_tools/server_controller.rb', line 42 def user_specified_api_key [:api_key] end |