Class: HQ::MongoDB::CheckCollectionSize::Script

Inherits:
Tools::CheckScript
  • Object
show all
Defined in:
lib/hq/mongodb/check-collection-size/script.rb

Instance Method Summary collapse

Constructor Details

#initializeScript

Returns a new instance of Script.



14
15
16
17
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 14

def initialize
	super
	@name = "MongoDB collection size"
end

Instance Method Details

#analyse_collection(full_collection_name, stats) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 285

def analyse_collection full_collection_name, stats

	# work out total efficiency

	total_data_size =
		stats[:total][:data_size]

	total_storage_size =
		stats[:total][:storage_size]

	total_efficiency =
		total_data_size * 100 / total_storage_size

	sharding_enabled =
		stats[:sharding_enabled]

	# work out critical and warning status

	critical = false
	warning = false

	if @opts[:total_critical] \
		&& total_data_size >= @opts[:total_critical]

		critical = true

	elsif @opts[:total_warning] \
		&& total_data_size >= @opts[:total_warning]

		warning = true

	end

	if sharding_enabled == false

		if @opts[:unsharded_critical] \
			&& total_data_size >= @opts[:unsharded_critical]

			critical = true

		elsif @opts[:unsharded_warning] \
			&& total_data_size >= @opts[:unsharded_warning]

			warning = true

		end

	end

	if @opts[:efficiency_size] \
		&& total_data_size >= @opts[:efficiency_size]

		if @opts[:efficiency_critical] \
			&& total_efficiency < @opts[:efficiency_critical]

			total_critical = true

		elsif @opts[:efficiency_warning] \
			&& total_efficiency < @opts[:efficiency_warning]

			warning = true

		end

	end

	# update counters

	if critical
		@critical_count += 1
	elsif warning
		@warning_count += 1
	else
		@ok_count += 1
	end

	@biggest = total_data_size \
		if total_data_size > @biggest

	# output message(s)

	if @opts[:verbose] && (critical || warning)

		sharding_string =
			if sharding_enabled
				"sharded"
			else
				"unsharded"
			end

		status_string =
			if critical
				"*** CRITICAL ***"
			elsif warning
				"warning"
			else
				"ok"
			end

		print_n \
			"%s %s %d%% %s %s" % [
				"#{full_collection_name}",
				byte_size(total_storage_size),
				total_efficiency,
				sharding_string,
				status_string,
			]

		if @opts[:breakdown]

			collection_data_size =
				stats[:collection][:data_size]

			collection_storage_size =
				stats[:collection][:storage_size]

			collection_efficiency =
				collection_data_size * 100 / collection_storage_size

			print_n \
				"  data %s %d%%" % [
					byte_size(collection_storage_size),
					collection_efficiency,
				]

			stats[:indexes].each do
				|index_name,
				index_stats|

				index_data_size =
					index_stats[:data_size]

				index_storage_size =
					index_stats[:storage_size]

				index_efficiency =
					index_data_size * 100 / index_storage_size

				print_n \
					"  %s %s %d%%" % [
						index_name,
						byte_size(index_storage_size),
						index_efficiency,
					]

			end

		end

	end

end

#byte_size(bytes) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 442

def byte_size bytes

	size = bytes.to_f

	series = [
		"bytes",
		"kilobytes",
		"megabytes",
		"gigabytes",
		"terabytes",
	]

	while size >= 1024
		size /= 1024
		series.shift
	end

	unit = series.shift

	return "%.3g %s" % [ size, unit ]

end

#decode_bytes(string) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 465

def decode_bytes string

	case string

		when /^([0-9]+)b?$/

		when /^([0-9]+)k$/
			$1.to_i * 1024

		when /^([0-9]+)m$/
			$1.to_i * 1024 * 1024

		when /^([0-9]+)g$/
			$1.to_i * 1024 * 1024 * 1024

		when /^([0-9]+)t$/
			$1.to_i * 1024 * 1024 * 1024 * 1024

		else
			raise "Invalid size: #{string}"

	end

end

#get_collection_stats(db_name, coll_name) ⇒ Object



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 89

def get_collection_stats db_name, coll_name

	mongo =
		Thread.current[:mongo]

	db =
		mongo.db(db_name)

	# get collection stats

	coll_stats =
		get_namespace_stats \
			db_name,
			coll_name

	# get index stats

	prefix_size =
		db_name.length + 1 + coll_name.length + 1

	index_names =
		db["system.indexes"] \
			.find({
				"ns" => "#{db_name}.#{coll_name}",
			})
			.map { |row| row["name"] }

	index_stats = Hash[
		index_names.map do
			|index_name|
			[
				index_name,
				get_namespace_stats(
					db_name,
					"#{coll_name}.$#{index_name}")
			]
		end
	]

	# work out overall figures

	total_stats =
		index_stats.values.reduce coll_stats do
			|memo, object|
			{
				:data_size =>
					memo[:data_size] + object[:data_size],
				:storage_size =>
					memo[:storage_size] + object[:storage_size],
			}
		end

	# get sharding info

	config_db =
		mongo.db("config")

	collection_row =
		config_db["collections"]
			.find_one({
				"_id" => "#{db_name}.#{coll_name}"
			})

	sharding_enabled =
		collection_row && collection_row["key"] ? true : false

	# and return

	return {
		:total => total_stats,
		:collection => coll_stats,
		:indexes => index_stats,
		:sharding_enabled => sharding_enabled,
	}

end

#get_namespace_stats(db_name, namespace_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 69

def get_namespace_stats db_name, namespace_name

	mongo =
		Thread.current[:mongo]

	db =
		mongo.db(db_name)

	stats =
		db.command({
			"collStats" => namespace_name
		})

	return {
		:data_size => stats["size"],
		:storage_size => stats["storageSize"],
	}

end

#perform_checksObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 166

def perform_checks

	# connect

	mongo =
		Mongo::MongoClient.new \
			@opts[:hostname],
			@opts[:port],
			:slave_ok => true

	# stats to collect

	@biggest = 0

	@warning_count = 0
	@critical_count = 0
	@ok_count = 0
	@error_count = 0

	# thread pool

	@thread_pool =
		Tools::ThreadPool.new

	@thread_pool.init_hook do

		Thread.current[:mongo] =
			Mongo::MongoClient.new \
				@opts[:hostname],
				@opts[:port],
				:slave_ok => true

	end

	@thread_pool.start \
		@opts[:threads]

	# get collection names (in parallel)

	collection_name_futures =
		mongo.database_names.map do
			|database_name|

			Tools::Future.new @thread_pool, database_name do
				|database_name|

				mongo =
					Thread.current[:mongo]

				mongo.db(database_name)
					.collection_names
					.map {
						|collection_name|
						[ database_name, collection_name ]
					}

			end

		end

	collection_names =
		collection_name_futures
			.map {
				|future|
				future.get
			}
			.flatten(1)

	# analyse collections (in parallel)

	collection_stat_futures = Hash[
		collection_names.map do
			|database_name, collection_name|

			future =
				Tools::Future.new \
					@thread_pool,
					database_name,
					collection_name do
						|database_name, collection_name|
						get_collection_stats(
							database_name,
							collection_name)
					end

			[
				"#{database_name}.#{collection_name}",
				future,
			]

		end
	]

	collection_stat_futures.each do
		|full_collection_name, future|

		stats = future.get

		analyse_collection \
			full_collection_name,
			stats

	end

	# results

	critical "#{@critical_count} critical" \
		if @critical_count > 0

	warning "#{@warning_count} warning" \
		if @warning_count > 0

	unknown "#{@error_count} errors" \
		if @error_count > 0

	message "biggest is #{byte_size @biggest}"

end


438
439
440
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 438

def print_n str
	puts "#{str}\n"
end

#process_argsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hq/mongodb/check-collection-size/script.rb', line 19

def process_args

	@opts, @args =
		Tools::Getopt.process ARGV, [

			{ :name => :hostname,
				:default => "localhost" },

			{ :name => :port,
				:default => 27017,
				:convert => :to_i },

			{ :name => :total_warning,
				:convert => method(:decode_bytes) },

			{ :name => :total_critical,
				:convert => method(:decode_bytes) },

			{ :name => :unsharded_warning,
				:convert => method(:decode_bytes) },

			{ :name => :unsharded_critical,
				:convert => method(:decode_bytes) },

			{ :name => :efficiency_warning,
				:convert => :to_i },

			{ :name => :efficiency_critical,
				:convert => :to_i },

			{ :name => :efficiency_size,
				:convert => method(:decode_bytes) },

			{ :name => :verbose,
				:boolean => true },

			{ :name => :threads,
				:convert => :to_i,
				:default => 20 },

			{ :name => :breakdown,
				:boolean => true }

		]

	@args.empty? \
		or raise "Extra args on command line"

end