3
3
$:. push ( File . dirname ( $0) )
4
4
require 'optparse'
5
5
require 'socket'
6
+ require 'net/http'
7
+ require 'json'
8
+
9
+ # FIXME this really only works inside twitter. it would be nice to support
10
+ # a general serverset interface (or opensource colony).
11
+ $colony_host = ENV [ "COLONY_HOST" ] || "colony.smf1.twitter.com"
12
+ $colony_port = ENV [ "COLONY_PORT" ] || 9080
6
13
7
14
$options = {
8
15
:config_filename => ENV [ 'HOME' ] + "/.kestrel_cluster" ,
9
16
:server_list => [ ] ,
10
17
:port => 22133 ,
18
+ :verbose => false ,
11
19
}
12
20
21
+ def verbose ( s )
22
+ puts s if $options[ :verbose ]
23
+ end
24
+
13
25
def fetch_stats ( host , port , data )
14
- puts "--- Fetching stats from #{ host } :#{ port } "
26
+ verbose "--- Fetching stats from #{ host } :#{ port } "
15
27
sock = TCPSocket . open ( host , port )
16
28
sock . puts ( "stats" )
17
29
done = false
18
30
while !done && line = sock . gets . chomp
19
31
if ( line == 'END' ) then
20
32
done = true
21
- elsif line =~ /STAT queue_([\w + ]+) (\d +)/
33
+ elsif line =~ /STAT queue_([\w \+ \- ]+) (\d +)/
22
34
key = $1
23
35
value = $2. to_i
24
36
( stat , queue_name ) = case key
25
- when /([\w + ]+)_total_items/ then [ :total_items , $1]
26
- when /([\w + ]+)_expired_items/ then [ :expired_items , $1]
27
- when /([\w + ]+)_mem_items/ then [ :mem_items , $1]
28
- when /([\w + ]+)_items/ then [ :items , $1]
29
- when /([\w + ]+)_mem_bytes/ then [ :mem_bytes , $1]
30
- when /([\w + ]+)_bytes/ then [ :bytes , $1]
31
- when /([\w + ]+)_age/ then [ :age , $1]
37
+ when /([\w \+ \- ]+)_total_items/ then [ :total_items , $1]
38
+ when /([\w \+ \- ]+)_expired_items/ then [ :expired_items , $1]
39
+ when /([\w \+ \- ]+)_mem_items/ then [ :mem_items , $1]
40
+ when /([\w \+ \- ]+)_items/ then [ :items , $1]
41
+ when /([\w \+ \- ]+)_mem_bytes/ then [ :mem_bytes , $1]
42
+ when /([\w \+ \- ]+)_bytes/ then [ :bytes , $1]
43
+ when /([\w \+ \- ]+)_age/ then [ :age , $1]
32
44
end
33
45
34
46
if ( queue_name )
@@ -151,15 +163,21 @@ end
151
163
152
164
parser = OptionParser . new do |opts |
153
165
opts . banner = "Usage: #{ $0} [options] <command>"
154
- opts . separator "Example: #{ $0} -f shards.yml 11 "
166
+ opts . separator "Example: #{ $0} -f kestrel max_age "
155
167
156
168
opts . on ( "-f" , "--file=FILENAME" , "load kestrel server list from file (use '-' for stdin) (default: #{ $options[ :config_filename ] } )" ) do |filename |
157
169
$options[ :config_filename ] = filename
158
170
end
159
171
opts . on ( "-p" , "--port=N" , "use port (default: #{ $options[ :port ] } )" ) do |port |
160
172
$options[ :port ] = port . to_i
161
173
end
162
- opts . on ( "-r" , "--rollup-fanouts" , "roll up stats for fanout queues into a single count" ) do
174
+ opts . on ( "-r" , "--role=ROLE" , "load kestrel server list from colony audubon role (requires colony)" ) do |role |
175
+ $options[ :role ] = role
176
+ end
177
+ opts . on ( "-v" , "--verbose" , "print verbose debugging info as we work" ) do
178
+ $options[ :verbose ] = true
179
+ end
180
+ opts . on ( "-F" , "--rollup-fanouts" , "roll up stats for fanout queues into a single count" ) do
163
181
$options[ :rollup_fanouts ] = true
164
182
end
165
183
@@ -186,9 +204,14 @@ if ARGV.size < 1
186
204
exit 1
187
205
end
188
206
189
- server_file = $options[ :config_filename ] == "-" ? STDIN : File . open ( $options[ :config_filename ] , "r" )
190
- $options[ :server_list ] = server_file . readlines . map { |line | line . chomp }
191
- server_file . close
207
+ if $options[ :role ]
208
+ data = Net ::HTTP . get URI . parse ( "http://#{ $colony_host} :#{ $colony_port} /query?q=mo+audubon.role.#{ $options[ :role ] } " )
209
+ $options[ :server_list ] = JSON . parse ( data ) . values . flatten
210
+ else
211
+ server_file = $options[ :config_filename ] == "-" ? STDIN : File . open ( $options[ :config_filename ] , "r" )
212
+ $options[ :server_list ] = server_file . readlines . map { |line | line . chomp }
213
+ server_file . close
214
+ end
192
215
193
216
command = ARGV [ 0 ] . downcase
194
217
0 commit comments