Class: RightScale::StatsManager
- 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
-
.run ⇒ Object
Create and run manager.
Instance Method Summary collapse
-
#initialize ⇒ StatsManager
constructor
Initialize manager.
-
#manage(options) ⇒ Object
Handle stats request.
-
#parse_args ⇒ Object
Create options hash from command line arguments.
-
#request_agent_stats(agent_name, options) ⇒ Object
Request and display statistics for agent.
-
#request_stats(options) ⇒ Object
Request and display statistics for agents.
Constructor Details
#initialize ⇒ StatsManager
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
.run ⇒ Object
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() init_log if [:verbose] AgentConfig.cfg_dir = [:cfg_dir] [:timeout] ||= DEFAULT_TIMEOUT request_stats() rescue Exception => e fail("#{e}\n#{e.backtrace.join("\n")}") unless e.is_a?(SystemExit) end |
#parse_args ⇒ Object
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 = {:reset => false} [:agent_name] = ARGV[0] unless ARGV[0] =~ /^-/ opts = OptionParser.new do |opts| parse_other_args(opts, ) opts.on('-r', '--reset') do [:reset] = true end opts.on('-T', '--timeout SEC') do |sec| [:timeout] = sec end opts.on('-j', '--json') do [:json] = true end opts.on('-v', '--verbose') do [:verbose] = true end opts.on("-c", "--cfg-dir DIR") do |d| [: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. + "\nUse 'rstat --help' for additional information") end 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, ) res = false = AgentConfig.(agent_name) unless .empty? || (listen_port = [:listen_port]).nil? client = CommandClient.new(listen_port, [:cookie]) command = {:name => :stats, :reset => [:reset]} begin client.send_command(command, verbose = false, [:timeout]) { |r| display(agent_name, r, ) } res = true rescue Exception => e msg = "Could not retrieve #{agent_name} agent stats: #{e}" msg += "\n" + e.backtrace.join("\n") unless e. =~ /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() # Determine candidate agents agent_names = if [:agent_name] [[: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, ) 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 |