Class: RightScale::StatsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/right_agent/scripts/stats_manager.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

Default time to wait for a response from an agent

5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatsManager

Initialize manager



54
55
56
# File 'lib/right_agent/scripts/stats_manager.rb', line 54

def initialize
  @command_serializer = Serializer.new
end

Class Method Details

.runObject

Create and run manager

Return

true

Always return true



48
49
50
51
# File 'lib/right_agent/scripts/stats_manager.rb', line 48

def self.run
  m = StatsManager.new
  m.manage(m.parse_args)
end

Instance Method Details

#manage(options) ⇒ Object

Handle stats request

Parameters

options(Hash)

Command line options

Return

true

Always return true



65
66
67
68
69
70
71
72
# File 'lib/right_agent/scripts/stats_manager.rb', line 65

def manage(options)
  init_log if options[:verbose]
  AgentConfig.cfg_dir = options[:cfg_dir]
  options[:timeout] ||= DEFAULT_TIMEOUT
  request_stats(options)
rescue Exception => e
  fail("#{e}\n#{e.backtrace.join("\n")}") unless e.is_a?(SystemExit)
end

#parse_argsObject

Create options hash from command line arguments

Return

options(Hash)

Parsed options



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/right_agent/scripts/stats_manager.rb', line 78

def parse_args
  options = {:reset => false}
  options[:agent_name] = ARGV[0] unless ARGV[0] =~ /^-/

  opts = OptionParser.new do |opts|
    parse_other_args(opts, options)

    opts.on('-r', '--reset') do
      options[:reset] = true
    end

    opts.on('-t', '--timeout SEC') do |sec|
      options[:timeout] = sec
    end

    opts.on('-j', '--json') do
      options[:json] = true
    end

    opts.on('-v', '--verbose') do
      options[:verbose] = true
    end

    opts.on("-c", "--cfg-dir DIR") do |d|
      options[:cfg_dir] = d
    end

    opts.on_tail('--help') do
      puts Usage.scan(__FILE__)
      exit
    end

  end

  begin
    opts.parse(ARGV)
  rescue Exception => e
    exit 0 if e.is_a?(SystemExit)
    fail(e.message + "\nUse 'rstat --help' for additional information")
  end

  options
end

#request_agent_stats(agent_name, options) ⇒ Object

Request and display statistics for agent

Parameters

agent_name(String)

Agent name

options(Hash)

Command line options

Return

(Boolean)

true if agent running, otherwise false



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/right_agent/scripts/stats_manager.rb', line 158

def request_agent_stats(agent_name, options)
  res = false
  config_options = AgentConfig.agent_options(agent_name)
  unless config_options.empty? || (listen_port = config_options[:listen_port]).nil?
    client = CommandClient.new(listen_port, config_options[:cookie])
    command = {:name => :stats, :reset => options[:reset]}
    begin
      client.send_command(command, options[:verbose], options[:timeout]) { |r| display(agent_name, r, options) }
      res = true
    rescue Exception => e
      msg = "Could not retrieve #{agent_name} agent stats: #{e}"
      msg += "\n" + e.backtrace.join("\n") unless e.message =~ /Timed out/
      fail(msg)
    end
  end
  res
end

#request_stats(options) ⇒ Object

Request and display statistics for agents

Parameters

options(Hash)

Command line options

Return

true

Always return true



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/right_agent/scripts/stats_manager.rb', line 129

def request_stats(options)
  # Determine candidate agents
  agent_names = if options[:agent_name]
    [options[:agent_name]]
  else
    AgentConfig.cfg_agents
  end
  fail("No agents configured") if agent_names.empty?

  # Request stats from agents
  count = 0
  agent_names.each do |agent_name|
    begin
      count += 1 if request_agent_stats(agent_name, options)
    rescue Exception => e
      $stderr.puts "Command to #{agent_name} agent failed (#{e})" unless e.is_a?(SystemExit)
    end
  end
  $stderr.puts("No agents running") if count == 0
end