hotdog 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hotdog/application.rb +2 -2
- data/lib/hotdog/commands/hosts.rb +12 -2
- data/lib/hotdog/commands.rb +86 -83
- data/lib/hotdog/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1a630c98690e6d1df7a32b7baf48e24776da49a
|
4
|
+
data.tar.gz: ab4daa9a831412d598eb235e3e4ed8a4516b2dcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb2c5b244339014184b56d4c1ab68bce58bc8fa2f71e84eef15319a8157be1532304647760fcd0b41d200cd8e1fdb895e6ca6a8309c7cd79d3ce9f6e67379c6f
|
7
|
+
data.tar.gz: 00ea3e8ddc439588571c788a0ec4f2546fb084d81ef18749b92cb83ab239ce2ea6df3f7c1ad21f17ca9f487794c805a1c5eb1fd007857d81dc58b3f9be9e3746
|
data/lib/hotdog/application.rb
CHANGED
@@ -17,14 +17,14 @@ module Hotdog
|
|
17
17
|
debug: false,
|
18
18
|
environment: "default",
|
19
19
|
minimum_expiry: 3600, # 1 hour
|
20
|
-
random_expiry:
|
20
|
+
random_expiry: 5940, # 99 hours
|
21
21
|
fixed_string: false,
|
22
22
|
force: false,
|
23
23
|
format: "plain",
|
24
24
|
headers: false,
|
25
25
|
listing: false,
|
26
26
|
logger: Logger.new(STDERR),
|
27
|
-
max_time:
|
27
|
+
max_time: 5,
|
28
28
|
api_key: ENV["DATADOG_API_KEY"],
|
29
29
|
application_key: ENV["DATADOG_APPLICATION_KEY"],
|
30
30
|
print0: false,
|
@@ -5,17 +5,27 @@ module Hotdog
|
|
5
5
|
class Hosts < BaseCommand
|
6
6
|
def run(args=[])
|
7
7
|
application.run_command("init")
|
8
|
-
update_hosts(@options.dup)
|
9
8
|
|
10
9
|
if args.empty?
|
10
|
+
update_hosts(@options.dup)
|
11
11
|
@hosts_q1 ||= @db.prepare(<<-EOS)
|
12
12
|
SELECT DISTINCT host_id FROM hosts_tags;
|
13
13
|
EOS
|
14
14
|
logger.debug("hosts_q1()")
|
15
15
|
result = @hosts_q1.execute().to_a.reduce(:+)
|
16
16
|
else
|
17
|
+
if args.map { |host_name| glob?(host_name) }.any?
|
18
|
+
update_hosts(@options.dup)
|
19
|
+
else
|
20
|
+
args.each do |host_name|
|
21
|
+
@hosts_q4 ||= @db.prepare("INSERT OR IGNORE INTO hosts (name) VALUES (?);")
|
22
|
+
logger.debug("hosts_q4(%s)" % [host_name.inspect])
|
23
|
+
@hosts_q4.execute(host_name)
|
24
|
+
update_host_tags(host_name, @options.dup)
|
25
|
+
end
|
26
|
+
end
|
17
27
|
result = args.map { |host_name|
|
18
|
-
if host_name
|
28
|
+
if glob?(host_name)
|
19
29
|
@hosts_q2 ||= @db.prepare(<<-EOS)
|
20
30
|
SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
|
21
31
|
INNER JOIN hosts ON hosts_tags.host_id = hosts.id
|
data/lib/hotdog/commands.rb
CHANGED
@@ -65,13 +65,13 @@ module Hotdog
|
|
65
65
|
@get_hosts_q6 ||= @db.prepare(<<-EOS)
|
66
66
|
SELECT expires_at FROM hosts_tags WHERE host_id = ? LIMIT 1;
|
67
67
|
EOS
|
68
|
-
logger.debug("get_hosts_q6()")
|
68
|
+
logger.debug("get_hosts_q6(%s)" % [host_id.inspect])
|
69
69
|
@get_hosts_q6.execute(host_id).map { |row| Time.at(row.first).strftime("%Y-%m-%dT%H:%M:%S") }.first
|
70
70
|
when "host"
|
71
71
|
@get_hosts_q1 ||= @db.prepare(<<-EOS)
|
72
72
|
SELECT name FROM hosts WHERE id = ? LIMIT 1;
|
73
73
|
EOS
|
74
|
-
logger.debug("get_hosts_q1()")
|
74
|
+
logger.debug("get_hosts_q1(%s)" % [host_id.inspect])
|
75
75
|
@get_hosts_q1.execute(host_id).map { |row| row.first }.first
|
76
76
|
else
|
77
77
|
if not glob?(tag_name)
|
@@ -80,7 +80,7 @@ module Hotdog
|
|
80
80
|
INNER JOIN tags ON hosts_tags.tag_id = tags.id
|
81
81
|
WHERE hosts_tags.host_id = ? AND tags.name = ?;
|
82
82
|
EOS
|
83
|
-
logger.debug("get_hosts_q2()")
|
83
|
+
logger.debug("get_hosts_q2(%s, %s)" % [host_id.inspect, tag_name.inspect])
|
84
84
|
@get_hosts_q2.execute(host_id, tag_name).map { |row| row.first }.join(",")
|
85
85
|
else
|
86
86
|
@get_hosts_q5 ||= @db.prepare(<<-EOS)
|
@@ -88,7 +88,7 @@ module Hotdog
|
|
88
88
|
INNER JOIN tags ON hosts_tags.tag_id = tags.id
|
89
89
|
WHERE hosts_tags.host_id = ? AND tags.name GLOB ?;
|
90
90
|
EOS
|
91
|
-
logger.debug("get_hosts_q5()")
|
91
|
+
logger.debug("get_hosts_q5(%s, %s)", host_id.inspect, tag_name.inspect)
|
92
92
|
@get_hosts_q5.execute(host_id, tag_name).map { |row| row.first }.join(",")
|
93
93
|
end
|
94
94
|
end
|
@@ -109,7 +109,7 @@ module Hotdog
|
|
109
109
|
INNER JOIN tags ON hosts_tags.tag_id = tags.id
|
110
110
|
WHERE hosts_tags.host_id = ?;
|
111
111
|
EOS
|
112
|
-
logger.debug("get_hosts_q3()")
|
112
|
+
logger.debug("get_hosts_q3(%s)" % [host_id.inspect])
|
113
113
|
tag_names = @get_hosts_q3.execute(host_id).map { |row| row.first }
|
114
114
|
tag_names.each do |tag_name|
|
115
115
|
fields << tag_name unless fields.index(tag_name)
|
@@ -138,8 +138,7 @@ module Hotdog
|
|
138
138
|
def update_hosts(options={})
|
139
139
|
if suspended?
|
140
140
|
return
|
141
|
-
|
142
|
-
@db.transaction do
|
141
|
+
else
|
143
142
|
if not options[:force]
|
144
143
|
# Update host list on every expirations to update frequently.
|
145
144
|
@update_hosts_q1 ||= @db.prepare("SELECT MIN(expires_at) FROM hosts_tags;")
|
@@ -188,8 +187,7 @@ module Hotdog
|
|
188
187
|
def update_tags(options={})
|
189
188
|
if suspended?
|
190
189
|
return
|
191
|
-
|
192
|
-
@db.transaction do
|
190
|
+
else
|
193
191
|
resume_host_tags
|
194
192
|
|
195
193
|
if options[:force]
|
@@ -203,8 +201,8 @@ module Hotdog
|
|
203
201
|
SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
|
204
202
|
WHERE hosts_tags.expires_at < ?;
|
205
203
|
EOS
|
206
|
-
logger.debug("update_tags_q2()")
|
207
|
-
hosts = @update_tags_q2.execute(Time.new.to_i)
|
204
|
+
logger.debug("update_tags_q2(%s)" % [Time.new.to_i])
|
205
|
+
hosts = @update_tags_q2.execute(Time.new.to_i).map { |row| row.first }
|
208
206
|
end
|
209
207
|
hosts.each_with_index do |host_id, i|
|
210
208
|
@update_tags_q3 ||= @db.prepare("DELETE FROM hosts_tags WHERE host_id = ? AND hosts_tags.expires_at < ?;")
|
@@ -231,21 +229,23 @@ module Hotdog
|
|
231
229
|
EMPTY_EXPIRES_AT = Time.at(0).to_i
|
232
230
|
|
233
231
|
def suspend_host_tags()
|
234
|
-
@
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
232
|
+
@db.transaction do
|
233
|
+
@suspended = true
|
234
|
+
@suspend_host_tags_q1 ||= @db.prepare("INSERT OR IGNORE INTO hosts (name) VALUES (?);")
|
235
|
+
logger.debug("suspend_host_tags_q1(%s)" % [EMPTY_HOST_NAME.inspect])
|
236
|
+
@suspend_host_tags_q1.execute(EMPTY_HOST_NAME)
|
237
|
+
@suspend_host_tags_q2 ||= @db.prepare("INSERT OR IGNORE INTO tags (name, value) VALUES (?, ?);")
|
238
|
+
logger.debug("suspend_host_tags_q2(%s, %s)" % [EMPTY_TAG_NAME.inspect, EMPTY_TAG_VALUE.inspect])
|
239
|
+
@suspend_host_tags_q2.execute(EMPTY_TAG_NAME, EMPTY_TAG_VALUE)
|
240
|
+
@suspend_host_tags_q3 ||= @db.prepare(<<-EOS)
|
241
|
+
INSERT OR REPLACE INTO hosts_tags (host_id, tag_id, expires_at)
|
242
|
+
SELECT host.id, tag.id, ? FROM
|
243
|
+
( SELECT id FROM hosts WHERE name = ?) AS host,
|
244
|
+
( SELECT id FROM tags WHERE name = ? AND value = ? ) AS tag;
|
245
|
+
EOS
|
246
|
+
logger.debug("suspend_host_tags_q3(%s, %s, %s, %s)" % [EMPTY_EXPIRES_AT.inspect, EMPTY_HOST_NAME.inspect, EMPTY_TAG_NAME.inspect, EMPTY_TAG_VALUE.inspect])
|
247
|
+
@suspend_host_tags_q3.execute(EMPTY_EXPIRES_AT, EMPTY_HOST_NAME, EMPTY_TAG_NAME, EMPTY_TAG_VALUE)
|
248
|
+
end
|
249
249
|
end
|
250
250
|
|
251
251
|
def resume_host_tags()
|
@@ -261,76 +261,79 @@ module Hotdog
|
|
261
261
|
if suspended?
|
262
262
|
# stop updating if the `update_host_tags` has already been suspended
|
263
263
|
return
|
264
|
-
end
|
265
|
-
if Integer === host_name
|
266
|
-
host_id = host_name
|
267
|
-
@update_host_tags_q1 ||= @db.prepare("SELECT name FROM hosts WHERE id = ? LIMIT 1;")
|
268
|
-
logger.debug("update_host_tags_q1(%s)" % [host_id.inspect])
|
269
|
-
host_name = @update_host_tags_q1.execute(host_id).map { |row| row.first }.first
|
270
264
|
else
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
265
|
+
if Integer === host_name
|
266
|
+
host_id = host_name
|
267
|
+
@update_host_tags_q1 ||= @db.prepare("SELECT name FROM hosts WHERE id = ? LIMIT 1;")
|
268
|
+
logger.debug("update_host_tags_q1(%s)" % [host_id.inspect])
|
269
|
+
host_name = @update_host_tags_q1.execute(host_id).map { |row| row.first }.first
|
270
|
+
else
|
271
|
+
@update_host_tags_q2 ||= @db.prepare("SELECT id, name FROM hosts WHERE LOWER(name) = LOWER(?) LIMIT 1;")
|
272
|
+
logger.debug("update_host_tags_q2(%s)" % [host_name.inspect])
|
273
|
+
host_id, host_name = @update_host_tags_q2.execute(host_name).map { |row| row }.first
|
274
|
+
end
|
275
275
|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
276
|
+
if not options[:force]
|
277
|
+
# Update host tags less frequently.
|
278
|
+
# Don't need to run updates on every expiration.
|
279
|
+
@update_host_tags_q3 ||= @db.prepare("SELECT AVG(expires_at) FROM hosts_tags WHERE host_id = ?;")
|
280
|
+
logger.debug("update_host_tags_q3(%s)" % [host_id.inspect])
|
281
|
+
if expires_at = @update_host_tags_q3.execute(host_id).map { |row| row.first }.first
|
282
|
+
if Time.new.to_i < expires_at
|
283
|
+
logger.debug("%s: next update will run after %s." % [host_name, Time.at(expires_at)])
|
284
|
+
return
|
285
|
+
else
|
286
|
+
logger.debug("%s: average expires_at was %s. start updating." % [host_name, Time.at(expires_at)])
|
287
|
+
end
|
285
288
|
else
|
286
|
-
logger.debug("%s:
|
289
|
+
logger.debug("%s: expires_at not found. start updateing." % [host_name])
|
287
290
|
end
|
288
|
-
else
|
289
|
-
logger.debug("%s: expires_at not found. start updateing." % [host_name])
|
290
291
|
end
|
291
|
-
end
|
292
292
|
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
293
|
+
code, result = @dog.host_tags(host_name)
|
294
|
+
logger.debug("dog.host_tags(%s) #==> [%s, %s]" % [host_name.inspect, code.inspect, result.inspect])
|
295
|
+
if code.to_i / 100 != 2
|
296
|
+
case code.to_i
|
297
|
+
when 404 # host not found on datadog
|
298
|
+
@update_host_tags_q7 ||= @db.prepare("DELETE FROM hosts_tags WHERE host_id IN ( SELECT id FROM hosts WHERE LOWER(name) = LOWER(?) );")
|
299
|
+
logger.debug("update_host_tags_q7(%s)" % [host_name.inspect])
|
300
|
+
@update_host_tags_q7.execute(host_name)
|
301
|
+
end
|
302
|
+
raise("dog.host_tags(%s) returns (%s: %s)" % [host_name.inspect, code.inspect, result.inspect])
|
301
303
|
end
|
302
|
-
raise("dog.host_tags(%s) returns (%s: %s)" % [host_name.inspect, code.inspect, result.inspect])
|
303
|
-
end
|
304
304
|
|
305
|
-
|
306
|
-
|
305
|
+
expires_at = Time.new.to_i + (options[:minimum_expiry] + rand(options[:random_expiry]))
|
306
|
+
logger.debug("%s: expires_at=%s" % [host_name, Time.at(expires_at)])
|
307
|
+
|
308
|
+
@db.transaction do
|
309
|
+
result["tags"].each do |tag|
|
310
|
+
tag_name, tag_value = tag.split(":", 2)
|
311
|
+
tag_value ||= ""
|
307
312
|
|
308
|
-
|
309
|
-
|
310
|
-
|
313
|
+
if options.has_key?(:tags) and not options[:tags].empty? and not options[:tags].index(tag_name)
|
314
|
+
next
|
315
|
+
else
|
316
|
+
@update_host_tags_q4 ||= @db.prepare("INSERT OR IGNORE INTO tags (name, value) VALUES (?, ?);")
|
317
|
+
logger.debug("update_host_tags_q4(%s, %s)" % [tag_name.inspect, tag_value.inspect])
|
318
|
+
@update_host_tags_q4.execute(tag_name, tag_value)
|
319
|
+
@update_host_tags_q5 ||= @db.prepare(<<-EOS)
|
320
|
+
INSERT OR REPLACE INTO hosts_tags (host_id, tag_id, expires_at)
|
321
|
+
SELECT host.id, tag.id, ? FROM
|
322
|
+
( SELECT id FROM hosts WHERE name = ? ) AS host,
|
323
|
+
( SELECT id FROM tags WHERE name = ? AND value = ? ) AS tag;
|
324
|
+
EOS
|
325
|
+
logger.debug("update_host_tags_q5(%s, %s)" % [expires_at, host_name, tag_name, tag_value])
|
326
|
+
@update_host_tags_q5.execute(expires_at, host_name, tag_name, tag_value)
|
327
|
+
end
|
328
|
+
end
|
311
329
|
|
312
|
-
|
313
|
-
|
314
|
-
else
|
315
|
-
@update_host_tags_q4 ||= @db.prepare("INSERT OR IGNORE INTO tags (name, value) VALUES (?, ?);")
|
316
|
-
logger.debug("update_host_tags_q4(%s, %s)" % [tag_name.inspect, tag_value.inspect])
|
317
|
-
@update_host_tags_q4.execute(tag_name, tag_value)
|
318
|
-
@update_host_tags_q5 ||= @db.prepare(<<-EOS)
|
319
|
-
INSERT OR REPLACE INTO hosts_tags (host_id, tag_id, expires_at)
|
320
|
-
SELECT host.id, tag.id, ? FROM
|
321
|
-
( SELECT id FROM hosts WHERE name = ? ) AS host,
|
322
|
-
( SELECT id FROM tags WHERE name = ? AND value = ? ) AS tag;
|
330
|
+
@update_host_tags_q6 ||= @db.prepare(<<-EOS)
|
331
|
+
DELETE FROM hosts_tags WHERE host_id = ? and expires_at <= ?;
|
323
332
|
EOS
|
324
|
-
logger.debug("
|
325
|
-
@
|
333
|
+
logger.debug("update_host_tags_q6(%s, %s)" % [host_id.inspect, Time.new.to_i.inspect])
|
334
|
+
@update_host_tags_q6.execute(host_id, Time.new.to_i)
|
326
335
|
end
|
327
336
|
end
|
328
|
-
|
329
|
-
@update_host_tags_q6 ||= @db.prepare(<<-EOS)
|
330
|
-
DELETE FROM hosts_tags WHERE host_id = ? and expires_at <= ?;
|
331
|
-
EOS
|
332
|
-
logger.debug("update_host_tags_q6(%s, %s)" % [host_id.inspect, Time.new.to_i.inspect])
|
333
|
-
@update_host_tags_q6.execute(host_id, Time.new.to_i)
|
334
337
|
end
|
335
338
|
end
|
336
339
|
end
|
data/lib/hotdog/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hotdog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yamashita Yuu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|