right_agent 2.0.7-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +82 -0
  3. data/Rakefile +113 -0
  4. data/lib/right_agent.rb +59 -0
  5. data/lib/right_agent/actor.rb +182 -0
  6. data/lib/right_agent/actor_registry.rb +76 -0
  7. data/lib/right_agent/actors/agent_manager.rb +232 -0
  8. data/lib/right_agent/agent.rb +1149 -0
  9. data/lib/right_agent/agent_config.rb +480 -0
  10. data/lib/right_agent/agent_identity.rb +210 -0
  11. data/lib/right_agent/agent_tag_manager.rb +237 -0
  12. data/lib/right_agent/audit_formatter.rb +107 -0
  13. data/lib/right_agent/clients.rb +31 -0
  14. data/lib/right_agent/clients/api_client.rb +383 -0
  15. data/lib/right_agent/clients/auth_client.rb +247 -0
  16. data/lib/right_agent/clients/balanced_http_client.rb +369 -0
  17. data/lib/right_agent/clients/base_retry_client.rb +495 -0
  18. data/lib/right_agent/clients/right_http_client.rb +279 -0
  19. data/lib/right_agent/clients/router_client.rb +493 -0
  20. data/lib/right_agent/command.rb +30 -0
  21. data/lib/right_agent/command/agent_manager_commands.rb +150 -0
  22. data/lib/right_agent/command/command_client.rb +136 -0
  23. data/lib/right_agent/command/command_constants.rb +33 -0
  24. data/lib/right_agent/command/command_io.rb +126 -0
  25. data/lib/right_agent/command/command_parser.rb +87 -0
  26. data/lib/right_agent/command/command_runner.rb +118 -0
  27. data/lib/right_agent/command/command_serializer.rb +63 -0
  28. data/lib/right_agent/connectivity_checker.rb +179 -0
  29. data/lib/right_agent/console.rb +65 -0
  30. data/lib/right_agent/core_payload_types.rb +44 -0
  31. data/lib/right_agent/core_payload_types/cookbook.rb +61 -0
  32. data/lib/right_agent/core_payload_types/cookbook_position.rb +46 -0
  33. data/lib/right_agent/core_payload_types/cookbook_repository.rb +116 -0
  34. data/lib/right_agent/core_payload_types/cookbook_sequence.rb +70 -0
  35. data/lib/right_agent/core_payload_types/dev_repositories.rb +100 -0
  36. data/lib/right_agent/core_payload_types/dev_repository.rb +76 -0
  37. data/lib/right_agent/core_payload_types/event_categories.rb +38 -0
  38. data/lib/right_agent/core_payload_types/executable_bundle.rb +130 -0
  39. data/lib/right_agent/core_payload_types/login_policy.rb +72 -0
  40. data/lib/right_agent/core_payload_types/login_user.rb +79 -0
  41. data/lib/right_agent/core_payload_types/planned_volume.rb +94 -0
  42. data/lib/right_agent/core_payload_types/recipe_instantiation.rb +73 -0
  43. data/lib/right_agent/core_payload_types/repositories_bundle.rb +50 -0
  44. data/lib/right_agent/core_payload_types/right_script_attachment.rb +95 -0
  45. data/lib/right_agent/core_payload_types/right_script_instantiation.rb +94 -0
  46. data/lib/right_agent/core_payload_types/runlist_policy.rb +44 -0
  47. data/lib/right_agent/core_payload_types/secure_document.rb +66 -0
  48. data/lib/right_agent/core_payload_types/secure_document_location.rb +63 -0
  49. data/lib/right_agent/core_payload_types/software_repository_instantiation.rb +61 -0
  50. data/lib/right_agent/daemonize.rb +35 -0
  51. data/lib/right_agent/dispatched_cache.rb +109 -0
  52. data/lib/right_agent/dispatcher.rb +272 -0
  53. data/lib/right_agent/enrollment_result.rb +221 -0
  54. data/lib/right_agent/exceptions.rb +87 -0
  55. data/lib/right_agent/history.rb +145 -0
  56. data/lib/right_agent/log.rb +460 -0
  57. data/lib/right_agent/minimal.rb +46 -0
  58. data/lib/right_agent/monkey_patches.rb +30 -0
  59. data/lib/right_agent/monkey_patches/ruby_patch.rb +55 -0
  60. data/lib/right_agent/monkey_patches/ruby_patch/array_patch.rb +29 -0
  61. data/lib/right_agent/monkey_patches/ruby_patch/darwin_patch.rb +24 -0
  62. data/lib/right_agent/monkey_patches/ruby_patch/linux_patch.rb +24 -0
  63. data/lib/right_agent/monkey_patches/ruby_patch/linux_patch/file_patch.rb +30 -0
  64. data/lib/right_agent/monkey_patches/ruby_patch/object_patch.rb +49 -0
  65. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch.rb +32 -0
  66. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/file_patch.rb +60 -0
  67. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/process_patch.rb +63 -0
  68. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/stdio_patch.rb +27 -0
  69. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/time_patch.rb +55 -0
  70. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/win32ole_patch.rb +34 -0
  71. data/lib/right_agent/multiplexer.rb +102 -0
  72. data/lib/right_agent/offline_handler.rb +270 -0
  73. data/lib/right_agent/operation_result.rb +300 -0
  74. data/lib/right_agent/packets.rb +673 -0
  75. data/lib/right_agent/payload_formatter.rb +104 -0
  76. data/lib/right_agent/pending_requests.rb +128 -0
  77. data/lib/right_agent/pid_file.rb +159 -0
  78. data/lib/right_agent/platform.rb +770 -0
  79. data/lib/right_agent/platform/unix/darwin/platform.rb +102 -0
  80. data/lib/right_agent/platform/unix/linux/platform.rb +305 -0
  81. data/lib/right_agent/platform/unix/platform.rb +226 -0
  82. data/lib/right_agent/platform/windows/mingw/platform.rb +447 -0
  83. data/lib/right_agent/platform/windows/mswin/platform.rb +236 -0
  84. data/lib/right_agent/platform/windows/platform.rb +1808 -0
  85. data/lib/right_agent/protocol_version_mixin.rb +69 -0
  86. data/lib/right_agent/retryable_request.rb +195 -0
  87. data/lib/right_agent/scripts/agent_controller.rb +543 -0
  88. data/lib/right_agent/scripts/agent_deployer.rb +400 -0
  89. data/lib/right_agent/scripts/common_parser.rb +160 -0
  90. data/lib/right_agent/scripts/log_level_manager.rb +192 -0
  91. data/lib/right_agent/scripts/stats_manager.rb +268 -0
  92. data/lib/right_agent/scripts/usage.rb +58 -0
  93. data/lib/right_agent/secure_identity.rb +92 -0
  94. data/lib/right_agent/security.rb +32 -0
  95. data/lib/right_agent/security/cached_certificate_store_proxy.rb +77 -0
  96. data/lib/right_agent/security/certificate.rb +102 -0
  97. data/lib/right_agent/security/certificate_cache.rb +89 -0
  98. data/lib/right_agent/security/distinguished_name.rb +56 -0
  99. data/lib/right_agent/security/encrypted_document.rb +83 -0
  100. data/lib/right_agent/security/rsa_key_pair.rb +76 -0
  101. data/lib/right_agent/security/signature.rb +86 -0
  102. data/lib/right_agent/security/static_certificate_store.rb +85 -0
  103. data/lib/right_agent/sender.rb +792 -0
  104. data/lib/right_agent/serialize.rb +29 -0
  105. data/lib/right_agent/serialize/message_pack.rb +107 -0
  106. data/lib/right_agent/serialize/secure_serializer.rb +151 -0
  107. data/lib/right_agent/serialize/secure_serializer_initializer.rb +47 -0
  108. data/lib/right_agent/serialize/serializable.rb +151 -0
  109. data/lib/right_agent/serialize/serializer.rb +159 -0
  110. data/lib/right_agent/subprocess.rb +38 -0
  111. data/lib/right_agent/tracer.rb +124 -0
  112. data/right_agent.gemspec +101 -0
  113. data/spec/actor_registry_spec.rb +80 -0
  114. data/spec/actor_spec.rb +162 -0
  115. data/spec/agent_config_spec.rb +235 -0
  116. data/spec/agent_identity_spec.rb +78 -0
  117. data/spec/agent_spec.rb +734 -0
  118. data/spec/agent_tag_manager_spec.rb +319 -0
  119. data/spec/clients/api_client_spec.rb +423 -0
  120. data/spec/clients/auth_client_spec.rb +272 -0
  121. data/spec/clients/balanced_http_client_spec.rb +576 -0
  122. data/spec/clients/base_retry_client_spec.rb +635 -0
  123. data/spec/clients/router_client_spec.rb +594 -0
  124. data/spec/clients/spec_helper.rb +111 -0
  125. data/spec/command/agent_manager_commands_spec.rb +51 -0
  126. data/spec/command/command_io_spec.rb +93 -0
  127. data/spec/command/command_parser_spec.rb +79 -0
  128. data/spec/command/command_runner_spec.rb +107 -0
  129. data/spec/command/command_serializer_spec.rb +51 -0
  130. data/spec/connectivity_checker_spec.rb +83 -0
  131. data/spec/core_payload_types/dev_repositories_spec.rb +64 -0
  132. data/spec/core_payload_types/dev_repository_spec.rb +33 -0
  133. data/spec/core_payload_types/executable_bundle_spec.rb +67 -0
  134. data/spec/core_payload_types/login_user_spec.rb +102 -0
  135. data/spec/core_payload_types/recipe_instantiation_spec.rb +81 -0
  136. data/spec/core_payload_types/right_script_attachment_spec.rb +65 -0
  137. data/spec/core_payload_types/right_script_instantiation_spec.rb +79 -0
  138. data/spec/core_payload_types/spec_helper.rb +23 -0
  139. data/spec/dispatched_cache_spec.rb +136 -0
  140. data/spec/dispatcher_spec.rb +324 -0
  141. data/spec/enrollment_result_spec.rb +53 -0
  142. data/spec/history_spec.rb +246 -0
  143. data/spec/log_spec.rb +192 -0
  144. data/spec/monkey_patches/eventmachine_spec.rb +62 -0
  145. data/spec/multiplexer_spec.rb +48 -0
  146. data/spec/offline_handler_spec.rb +340 -0
  147. data/spec/operation_result_spec.rb +208 -0
  148. data/spec/packets_spec.rb +461 -0
  149. data/spec/pending_requests_spec.rb +136 -0
  150. data/spec/platform/spec_helper.rb +216 -0
  151. data/spec/platform/unix/darwin/platform_spec.rb +181 -0
  152. data/spec/platform/unix/linux/platform_spec.rb +540 -0
  153. data/spec/platform/unix/spec_helper.rb +149 -0
  154. data/spec/platform/windows/mingw/platform_spec.rb +222 -0
  155. data/spec/platform/windows/mswin/platform_spec.rb +259 -0
  156. data/spec/platform/windows/spec_helper.rb +720 -0
  157. data/spec/retryable_request_spec.rb +306 -0
  158. data/spec/secure_identity_spec.rb +50 -0
  159. data/spec/security/cached_certificate_store_proxy_spec.rb +62 -0
  160. data/spec/security/certificate_cache_spec.rb +71 -0
  161. data/spec/security/certificate_spec.rb +49 -0
  162. data/spec/security/distinguished_name_spec.rb +46 -0
  163. data/spec/security/encrypted_document_spec.rb +55 -0
  164. data/spec/security/rsa_key_pair_spec.rb +55 -0
  165. data/spec/security/signature_spec.rb +66 -0
  166. data/spec/security/static_certificate_store_spec.rb +58 -0
  167. data/spec/sender_spec.rb +1045 -0
  168. data/spec/serialize/message_pack_spec.rb +131 -0
  169. data/spec/serialize/secure_serializer_spec.rb +132 -0
  170. data/spec/serialize/serializable_spec.rb +90 -0
  171. data/spec/serialize/serializer_spec.rb +197 -0
  172. data/spec/spec.opts +2 -0
  173. data/spec/spec.win32.opts +1 -0
  174. data/spec/spec_helper.rb +130 -0
  175. data/spec/tracer_spec.rb +114 -0
  176. metadata +447 -0
@@ -0,0 +1,192 @@
1
+ # === Synopsis:
2
+ # RightAgent Log Level Manager (rlog) - (c) 2009-2011 RightScale Inc
3
+ #
4
+ # rlog is a command line tool for retrieving and setting the log level
5
+ # for a RightAgent
6
+ #
7
+ # === Examples:
8
+ # Retrieve log level for all configured agents:
9
+ # rlog
10
+ #
11
+ # Retrieve log level for a specific agent:
12
+ # rlog AGENT
13
+ #
14
+ # Set log level to debug for all configured agents:
15
+ # rlog [AGENT] --log-level debug
16
+ # rlog [AGENT] -l debug
17
+ #
18
+ # Set log level to debug for a specific agent:
19
+ # rlog AGENT -l debug
20
+ #
21
+ # === Usage
22
+ # rs_set_log_level [AGENT] [--log-level, -l debug|info|warn|error|fatal]
23
+ #
24
+ # options:
25
+ # --log-level, -l LVL Set log level of agent
26
+ # --cfg-dir, -c DIR Set directory containing configuration for all agents
27
+ # --verbose, -v Display debug information
28
+ # --help: Display help
29
+ #
30
+ # No options prints the current agent log level
31
+ #
32
+
33
+ require 'rubygems'
34
+ require 'optparse'
35
+ require File.expand_path(File.join(File.dirname(__FILE__), 'usage'))
36
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'right_agent'))
37
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'actors', 'agent_manager'))
38
+ require File.expand_path(File.join(File.dirname(__FILE__), 'common_parser'))
39
+
40
+ module RightScale
41
+
42
+ class LogLevelManager
43
+
44
+ # Convenience wrapper for creating and running log level manager
45
+ #
46
+ # === Return
47
+ # true:: Always return true
48
+ def self.run
49
+ m = LogLevelManager.new
50
+ m.manage(m.parse_args)
51
+ end
52
+
53
+ # Handle log level request
54
+ #
55
+ # === Parameters
56
+ # options(Hash):: Command line options
57
+ #
58
+ # === Return
59
+ # true:: Always return true
60
+ def manage(options)
61
+ # Initialize configuration directory setting
62
+ AgentConfig.cfg_dir = options[:cfg_dir]
63
+
64
+ # Determine command
65
+ level = options[:level]
66
+ command = { :name => (level ? 'set_log_level' : 'get_log_level') }
67
+ command[:level] = level.to_sym if level
68
+
69
+ # Determine candidate agents
70
+ agent_names = if options[:agent_name]
71
+ [options[:agent_name]]
72
+ else
73
+ AgentConfig.cfg_agents
74
+ end
75
+ fail("No agents configured") if agent_names.empty?
76
+
77
+ # Perform command for each agent
78
+ count = 0
79
+ agent_names.each do |agent_name|
80
+ count += 1 if request_log_level(agent_name, command, options)
81
+ end
82
+ puts("No agents running") if count == 0
83
+ true
84
+ end
85
+
86
+ # Create options hash from command line arguments
87
+ #
88
+ # === Return
89
+ # options(Hash):: Hash of options as defined by the command line
90
+ def parse_args
91
+ options = { :verbose => false }
92
+ options[:agent_name] = ARGV[0] unless ARGV[0] =~ /^-/
93
+
94
+ opts = OptionParser.new do |opts|
95
+
96
+ opts.on('-l', '--log-level LEVEL') do |l|
97
+ fail("Invalid log level '#{l}'") unless AgentManager::LEVELS.include?(l.downcase.to_sym)
98
+ options[:level] = l.downcase
99
+ end
100
+
101
+ opts.on("-c", "--cfg-dir DIR") do |d|
102
+ options[:cfg_dir] = d
103
+ end
104
+
105
+ opts.on('-v', '--verbose') do
106
+ options[:verbose] = true
107
+ end
108
+
109
+ end
110
+
111
+ opts.on_tail('--help') do
112
+ puts Usage.scan(__FILE__)
113
+ exit
114
+ end
115
+
116
+ begin
117
+ opts.parse!(ARGV)
118
+ rescue Exception => e
119
+ exit 0 if e.is_a?(SystemExit)
120
+ fail(e.message + "\nUse 'rlog --help' for additional information")
121
+ end
122
+ options
123
+ end
124
+
125
+ protected
126
+
127
+ # Send log level request to agent
128
+ #
129
+ # === Parameters
130
+ # agent_name(String):: Agent name
131
+ # command(String):: Command request
132
+ # options(Hash):: Command line options
133
+ #
134
+ # === Return
135
+ # (Boolean):: true if agent running, otherwise false
136
+ def request_log_level(agent_name, command, options)
137
+ res = false
138
+ config_options = AgentConfig.agent_options(agent_name)
139
+ unless config_options.empty? || (listen_port = config_options[:listen_port]).nil?
140
+ fail("Could not retrieve #{agent_name} agent listen port") unless listen_port
141
+ client = CommandClient.new(listen_port, config_options[:cookie])
142
+ begin
143
+ client.send_command(command, options[:verbose], timeout = 5) do |level|
144
+ puts "Agent #{agent_name} log level: #{level.to_s.upcase}"
145
+ end
146
+ res = true
147
+ rescue Exception => e
148
+ puts "Command #{command.inspect} to #{agent_name} agent failed (#{e})"
149
+ end
150
+ end
151
+ res
152
+ end
153
+
154
+ # Print error on console and exit abnormally
155
+ #
156
+ # === Parameter
157
+ # message(String):: Error message, default to nil (no message printed)
158
+ # print_usage(Boolean):: Whether script usage should be printed, default to false
159
+ #
160
+ # === Return
161
+ # R.I.P. does not return
162
+ def fail(message = nil, print_usage = false)
163
+ puts "** #{message}" if message
164
+ puts Usage.scan(__FILE__) if print_usage
165
+ exit(1)
166
+ end
167
+
168
+ end
169
+
170
+ end
171
+
172
+ #
173
+ # Copyright (c) 2009-2011 RightScale Inc
174
+ #
175
+ # Permission is hereby granted, free of charge, to any person obtaining
176
+ # a copy of this software and associated documentation files (the
177
+ # "Software"), to deal in the Software without restriction, including
178
+ # without limitation the rights to use, copy, modify, merge, publish,
179
+ # distribute, sublicense, and/or sell copies of the Software, and to
180
+ # permit persons to whom the Software is furnished to do so, subject to
181
+ # the following conditions:
182
+ #
183
+ # The above copyright notice and this permission notice shall be
184
+ # included in all copies or substantial portions of the Software.
185
+ #
186
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
187
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
188
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
189
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
190
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
191
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
192
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,268 @@
1
+ # === Synopsis:
2
+ # RightAgent Statistics Manager (rstat) - (c) 2010-2011 RightScale Inc
3
+ #
4
+ # rstat is a command line tool for displaying operation statistics for RightAgents
5
+ #
6
+ # === Examples:
7
+ # Retrieve statistics for all locally running agents:
8
+ # rstat
9
+ #
10
+ # Retrieve statistics for an agent named AGENT:
11
+ # rstat AGENT
12
+ #
13
+ # Retrieve statistics for an agent in JSON format:
14
+ # rstat AGENT --json
15
+ # rstat AGENT --j
16
+ #
17
+ # Log details of statistics retrieval
18
+ # rstat AGENT -v
19
+ #
20
+ # === Usage:
21
+ # rstat [AGENT] [options]
22
+ #
23
+ # Options:
24
+ # --reset, -r As part of gathering the stats from an agent also reset the stats
25
+ # --timeout, -T SEC Override default timeout in seconds to wait for a response from an agent
26
+ # --json, -j Display the stats data in JSON format
27
+ # --verbose, -v Log debug information
28
+ # --cfg-dir, -c DIR Set directory containing configuration for all agents
29
+ # --help Display help
30
+
31
+ require 'rubygems'
32
+ require 'optparse'
33
+ require File.expand_path(File.join(File.dirname(__FILE__), 'usage'))
34
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'right_agent'))
35
+ require File.expand_path(File.join(File.dirname(__FILE__), 'common_parser'))
36
+
37
+ module RightScale
38
+
39
+ class StatsManager
40
+
41
+ # Default time to wait for a response from an agent
42
+ DEFAULT_TIMEOUT = 5
43
+
44
+ # Create and run manager
45
+ #
46
+ # === Return
47
+ # true:: Always return true
48
+ def self.run
49
+ m = StatsManager.new
50
+ m.manage(m.parse_args)
51
+ end
52
+
53
+ # Initialize manager
54
+ def initialize
55
+ @command_serializer = Serializer.new
56
+ end
57
+
58
+ # Handle stats request
59
+ #
60
+ # === Parameters
61
+ # options(Hash):: Command line options
62
+ #
63
+ # === Return
64
+ # true:: Always return true
65
+ def manage(options)
66
+ init_log if options[:verbose]
67
+ AgentConfig.cfg_dir = options[:cfg_dir]
68
+ options[:timeout] ||= DEFAULT_TIMEOUT
69
+ request_stats(options)
70
+ rescue Exception => e
71
+ fail("#{e}\n#{e.backtrace.join("\n")}") unless e.is_a?(SystemExit)
72
+ end
73
+
74
+ # Create options hash from command line arguments
75
+ #
76
+ # === Return
77
+ # options(Hash):: Parsed options
78
+ def parse_args
79
+ options = {:reset => false}
80
+ options[:agent_name] = ARGV[0] unless ARGV[0] =~ /^-/
81
+
82
+ opts = OptionParser.new do |opts|
83
+ parse_other_args(opts, options)
84
+
85
+ opts.on('-r', '--reset') do
86
+ options[:reset] = true
87
+ end
88
+
89
+ opts.on('-T', '--timeout SEC') do |sec|
90
+ options[:timeout] = sec
91
+ end
92
+
93
+ opts.on('-j', '--json') do
94
+ options[:json] = true
95
+ end
96
+
97
+ opts.on('-v', '--verbose') do
98
+ options[:verbose] = true
99
+ end
100
+
101
+ opts.on("-c", "--cfg-dir DIR") do |d|
102
+ options[:cfg_dir] = d
103
+ end
104
+
105
+ opts.on_tail('--help') do
106
+ puts Usage.scan(__FILE__)
107
+ exit
108
+ end
109
+
110
+ end
111
+
112
+ begin
113
+ opts.parse(ARGV)
114
+ rescue Exception => e
115
+ exit 0 if e.is_a?(SystemExit)
116
+ fail(e.message + "\nUse 'rstat --help' for additional information")
117
+ end
118
+
119
+ options
120
+ end
121
+
122
+ # Request and display statistics for agents
123
+ #
124
+ # === Parameters
125
+ # options(Hash):: Command line options
126
+ #
127
+ # === Return
128
+ # true:: Always return true
129
+ def request_stats(options)
130
+ # Determine candidate agents
131
+ agent_names = if options[:agent_name]
132
+ [options[:agent_name]]
133
+ else
134
+ AgentConfig.cfg_agents
135
+ end
136
+ fail("No agents configured") if agent_names.empty?
137
+
138
+ # Request stats from agents
139
+ count = 0
140
+ agent_names.each do |agent_name|
141
+ begin
142
+ count += 1 if request_agent_stats(agent_name, options)
143
+ rescue Exception => e
144
+ $stderr.puts "Command to #{agent_name} agent failed (#{e})" unless e.is_a?(SystemExit)
145
+ end
146
+ end
147
+ $stderr.puts("No agents running") if count == 0
148
+ end
149
+
150
+ # Request and display statistics for agent
151
+ #
152
+ # === Parameters
153
+ # agent_name(String):: Agent name
154
+ # options(Hash):: Command line options
155
+ #
156
+ # === Return
157
+ # (Boolean):: true if agent running, otherwise false
158
+ def request_agent_stats(agent_name, options)
159
+ res = false
160
+ config_options = AgentConfig.agent_options(agent_name)
161
+ unless config_options.empty? || (listen_port = config_options[:listen_port]).nil?
162
+ client = CommandClient.new(listen_port, config_options[:cookie])
163
+ command = {:name => :stats, :reset => options[:reset]}
164
+ begin
165
+ client.send_command(command, verbose = false, options[:timeout]) { |r| display(agent_name, r, options) }
166
+ res = true
167
+ rescue Exception => e
168
+ msg = "Could not retrieve #{agent_name} agent stats: #{e}"
169
+ msg += "\n" + e.backtrace.join("\n") unless e.message =~ /Timed out/
170
+ fail(msg)
171
+ end
172
+ end
173
+ res
174
+ end
175
+
176
+ protected
177
+
178
+ # Initialize logging
179
+ #
180
+ # === Return
181
+ # true:: Always return true
182
+ def init_log
183
+ Log.program_name = "stats_manager"
184
+ Log.log_to_file_only(true)
185
+ Log.init("stats_manager", Platform.filesystem.temp_dir, :print => true)
186
+ RightSupport::Log::Mixin.default_logger = Log
187
+ true
188
+ end
189
+
190
+ # Parse other arguments unique to given stats manager
191
+ #
192
+ # === Parameters
193
+ # opts(OptionParser):: Options parser with options to be parsed
194
+ # options(Hash):: Storage for options that are parsed
195
+ #
196
+ # === Return
197
+ # true:: Always return true
198
+ def parse_other_args(opts, options)
199
+ true
200
+ end
201
+
202
+ # Display stats returned from an agent in human readable or JSON format
203
+ #
204
+ # === Parameters
205
+ # agent_name(String):: Agent name
206
+ # result(String):: Result packet in JSON format containing stats or error
207
+ # options(Hash):: Command line options:
208
+ # :json(Boolean):: Whether to display in JSON format
209
+ # :name_width(Integer):: Maximum characters in displayed stat name
210
+ # :sub_name_width(Integer):: Maximum characters in displayed sub-stat name
211
+ # :sub_stat_value_width(Integer):: Maximum characters in displayed sub-stat value line
212
+ # :exception_message_width(Integer):: Maximum characters displayed for exception message
213
+ #
214
+ # === Return
215
+ # true:: Always return true
216
+ def display(agent_name, result, options)
217
+ result = RightScale::OperationResult.from_results(@command_serializer.load(result))
218
+ if options[:json]
219
+ $stdout.puts result.content.to_json
220
+ else
221
+ if result.respond_to?(:success?) && result.success?
222
+ stats = RightSupport::Stats.stats_str(result.content, options)
223
+ $stdout.puts "\n#{stats}\n"
224
+ else
225
+ $stderr.puts "\nCould not retrieve #{agent_name} agent stats: #{result.inspect}"
226
+ end
227
+ end
228
+ true
229
+ end
230
+
231
+ # Print failure message and exit abnormally
232
+ #
233
+ # === Parameters
234
+ # message(String):: Failure message
235
+ # print_usage(Boolean):: Whether to display usage information
236
+ #
237
+ # === Return
238
+ # exits the program
239
+ def fail(message, print_usage = false)
240
+ $stderr.puts "** #{message}"
241
+ puts Usage.scan(__FILE__) if print_usage
242
+ exit(1)
243
+ end
244
+
245
+ end
246
+ end
247
+
248
+ #
249
+ # Copyright (c) 2009-2011 RightScale Inc
250
+ #
251
+ # Permission is hereby granted, free of charge, to any person obtaining
252
+ # a copy of this software and associated documentation files (the
253
+ # "Software"), to deal in the Software without restriction, including
254
+ # without limitation the rights to use, copy, modify, merge, publish,
255
+ # distribute, sublicense, and/or sell copies of the Software, and to
256
+ # permit persons to whom the Software is furnished to do so, subject to
257
+ # the following conditions:
258
+ #
259
+ # The above copyright notice and this permission notice shall be
260
+ # included in all copies or substantial portions of the Software.
261
+ #
262
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
263
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
264
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
265
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
266
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
267
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
268
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.