fluent-plugin-mongo 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +6 -0
  2. data/VERSION +1 -1
  3. data/bin/mongo-tail +83 -0
  4. metadata +4 -3
data/README.rdoc CHANGED
@@ -58,6 +58,12 @@ Use mongo_backup type. mongo_backup alwalys use capped collection.
58
58
  Fluent tag is similar to database.collection in Mongo.
59
59
  This feature makes configuration more easily.
60
60
 
61
+ == Tool
62
+
63
+ You can tail mongo capped collection.
64
+
65
+ mongo-tail -f
66
+
61
67
  == Copyright
62
68
 
63
69
  Copyright:: Copyright (c) 2011- Masahiro Nakagawa
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
data/bin/mongo-tail ADDED
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # tail like CLI for mongo capped collection
4
+
5
+ require 'optparse'
6
+ require 'json'
7
+
8
+ require 'mongo'
9
+
10
+ TailConfig = {
11
+ :d => 'fluent',
12
+ :c => 'out_mongo_backup',
13
+ :h => 'localhost',
14
+ :p => 27017,
15
+ :n => 10
16
+ }
17
+
18
+ OptionParser.new { |opt|
19
+ opt.on('-d VAL', 'The name of database') { |v| TailConfig[:d] = v }
20
+ opt.on('-c VAL', 'The name of collection') { |v| TailConfig[:c] = v }
21
+ opt.on('-h VAL', 'The host of mongodb server') { |v| TailConfig[:h] = v }
22
+ opt.on('-p VAL', 'The port of mongodb server') { |v| TailConfig[:p] = Integer(v) }
23
+ opt.on('-n VAL', 'The number of documents') { |v| TailConfig[:n] = Integer(v) }
24
+ opt.on('-f', 'This option causes tail to not stop when end of collection is reached, but rather to wait for additional data to be appended to the collection') { |v|
25
+ TailConfig[:f] = true
26
+ }
27
+
28
+ opt.parse!(ARGV)
29
+ }
30
+
31
+ def get_capped_collection(conf)
32
+ db = Mongo::Connection.new(conf[:h], conf[:p]).db(conf[:d])
33
+ if db.collection_names.include?(conf[:c])
34
+ collection = db.collection(conf[:c])
35
+ if collection.capped?
36
+ collection
37
+ else
38
+ puts "#{conf[:c]} is not capped. mongo-tail can not tail normal collection."
39
+ end
40
+ else
41
+ puts "#{conf[:c]} not found: server = #{conf[:h]}:#{conf[:p]}"
42
+ end
43
+ end
44
+
45
+ def create_cursor_conf(collection, conf)
46
+ skip = collection.count - conf[:n]
47
+ cursor_conf = {}
48
+ cursor_conf[:skip] = skip if skip > 0
49
+ cursor_conf
50
+ end
51
+
52
+ def tail_n(collection, conf)
53
+ collection.find({}, create_cursor_conf(collection, conf)).each { |doc|
54
+ puts doc.to_json
55
+ }
56
+ end
57
+
58
+ def tail_forever(collection, conf)
59
+ cursor_conf = create_cursor_conf(collection, conf)
60
+ cursor_conf[:tailable] = true
61
+
62
+ cursor = Mongo::Cursor.new(collection, cursor_conf)
63
+ loop {
64
+ # TODO: Check more detail of cursor state if needed
65
+ cursor = Mongo::Cursor.new(collection, cursor_conf) unless cursor.alive?
66
+
67
+ if doc = cursor.next_document
68
+ puts doc.to_json
69
+ else
70
+ sleep 1
71
+ end
72
+ }
73
+ rescue
74
+ # ignore Mongo::OperationFailuer at CURSOR_NOT_FOUND
75
+ end
76
+
77
+ exit unless collection = get_capped_collection(TailConfig)
78
+
79
+ if TailConfig[:f]
80
+ tail_forever(collection, TailConfig)
81
+ else
82
+ tail_n(collection, TailConfig)
83
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Masahiro Nakagawa
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-10-03 00:00:00 +09:00
17
+ date: 2011-10-05 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - AUTHORS
60
60
  - Rakefile
61
61
  - VERSION
62
+ - bin/mongo-tail
62
63
  - lib/fluent/plugin/out_mongo.rb
63
64
  - lib/fluent/plugin/out_mongo_backup.rb
64
65
  - README.rdoc