Class: ServerController

Inherits:
Pidly::Control
  • Object
show all
Defined in:
lib/instrumental_tools/server_controller.rb

Constant Summary collapse

COMMANDS =
[:start, :stop, :status, :restart, :clean, :kill, :foreground]

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @run_options = options.delete(:run_options) || {}
  super(options)
end

Instance Attribute Details

#current_api_keyObject (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

#pidObject

Returns the value of attribute pid.



9
10
11
# File 'lib/instrumental_tools/server_controller.rb', line 9

def pid
  @pid
end

#run_optionsObject

Returns the value of attribute run_options.



9
10
11
# File 'lib/instrumental_tools/server_controller.rb', line 9

def run_options
  @run_options
end

Instance Method Details

#agentObject



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_addressObject



38
39
40
# File 'lib/instrumental_tools/server_controller.rb', line 38

def collector_address
  [run_options[:collector], run_options[:port]].compact.join(':')
end

#config_file_api_keyObject



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(run_options[:config_file]))
    if config_contents.is_a?(Hash)
      config_contents['api_key']
    end
  end
rescue Exception => e
  puts "Error loading config file %s: %s" % [run_options[:config_file], e.message]
  nil
end

#config_file_available?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/instrumental_tools/server_controller.rb', line 105

def config_file_available?
  File.exists?(run_options[:config_file])
end

#configured_api_keyObject



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

Returns:

  • (Boolean)


113
114
115
# File 'lib/instrumental_tools/server_controller.rb', line 113

def debug?
  !!run_options[:debug]
end

#enable_scripts?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/instrumental_tools/server_controller.rb', line 117

def enable_scripts?
  !!run_options[:enable_scripts]
end

#enabled?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/instrumental_tools/server_controller.rb', line 109

def enabled?
  agent.enabled
end

#foregroundObject



34
35
36
# File 'lib/instrumental_tools/server_controller.rb', line 34

def foreground
  run
end

#hostnameObject



84
85
86
# File 'lib/instrumental_tools/server_controller.rb', line 84

def hostname
  run_options[:hostname]
end

#key_has_changed?Boolean

Returns:

  • (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_intervalObject



80
81
82
# File 'lib/instrumental_tools/server_controller.rb', line 80

def report_interval
  run_options[:report_interval]
end

#runObject



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_executorObject



92
93
94
# File 'lib/instrumental_tools/server_controller.rb', line 92

def script_executor
  @executor ||= MetricScriptExecutor.new(script_location)
end

#script_locationObject



88
89
90
# File 'lib/instrumental_tools/server_controller.rb', line 88

def script_location
  run_options[: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_sleepObject



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_keyObject



42
43
44
# File 'lib/instrumental_tools/server_controller.rb', line 42

def user_specified_api_key
  run_options[:api_key]
end