oas-log-collector 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ coverage
5
+ doc/*
6
+ pkg/
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p194@oas-log-collector --create
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in salvatore.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ opyright (c) 2012 Realmedia Latin America LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ OAS Log Collector [![Build Status](https://secure.travis-ci.org/realmedia/oas-log-collector.png)](http://travis-ci.org/realmedia/oas-log-collector)
2
+ =================
3
+
4
+
5
+ Installation
6
+ ------------
7
+
8
+ OAS Log Collector is available through [Rubygems](http://rubygems.org/gems/oas-log-collector) and can be installed via:
9
+
10
+ gem install oas-log-collector
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs = ["test"]
7
+ t.verbose = true
8
+ t.test_files = FileList['test/**/test_*.rb']
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems' unless defined?(Gem)
3
+
4
+ path = File.expand_path('../../lib', __FILE__)
5
+ $:.unshift(path) if File.directory?(path) && !$:.include?(path)
6
+
7
+ require 'dante'
8
+ require 'yaml'
9
+ require 'oas/log_collector/cli'
10
+
11
+ runner = Dante::Runner.new('oas-log-collector')
12
+ runner.description = "OAS Log Collector"
13
+ runner.with_options do |opts|
14
+ opts.on("-C", "--config PATH", String, "Path to YAML config file") do |arg|
15
+ options[:config_file] = File.expand_path(arg)
16
+ end
17
+ end
18
+
19
+ runner.execute do |options|
20
+ begin
21
+ cli = OAS::LogCollector::CLI.new(YAML.load_file(options[:config_file]))
22
+ cli.run
23
+ rescue Interrupt
24
+ # shutdown
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ # Sample configuration file for OAS Log Collector.
2
+ # oas-log-collector -C config.yml
3
+ ---
4
+ :days_to_search: 10
5
+ :files_to_copy: 90
6
+ :sleep_seconds: 56400
7
+ :logs_path: '~/oas-log-collector'
8
+ :source_logs_path: '/home/central/RealMedia/ads/Logs/uv/out'
9
+ :sources:
10
+ - ['de-01', 'central']
11
+ - ['de-02', 'central']
@@ -0,0 +1,41 @@
1
+ require 'oas/log_collector'
2
+ require 'oas/log_collector/manager'
3
+ require 'oas/log_collector/source'
4
+
5
+ module OAS
6
+ module LogCollector
7
+ class CLI
8
+ attr_writer :manager
9
+
10
+ def initialize(opts = {})
11
+ sources = opts.delete(:sources) || []
12
+ sources.each { |host, user, port| (opts[:sources] ||= []) << OAS::LogCollector::Source.new(host, user, port || 22) }
13
+
14
+ options.merge!(opts)
15
+ end
16
+
17
+ def manager
18
+ @manager ||= OAS::LogCollector::Manager.new
19
+ end
20
+
21
+ def options
22
+ OAS::LogCollector.options
23
+ end
24
+
25
+ def run
26
+ loop do
27
+ threads = []
28
+ options[:sources].each do |source|
29
+ threads << Thread.new do
30
+ manager.collect(source)
31
+ end
32
+ end
33
+ threads.each(&:join)
34
+
35
+ sleep options[:sleep_seconds]
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ module OAS
2
+ module LogCollector
3
+ class Manager
4
+
5
+ def collect(source)
6
+ hostname = source.host
7
+
8
+ result = source.find(OAS::LogCollector.options[:source_logs_path], {
9
+ :mtime => "-#{OAS::LogCollector.options[:days_to_search]}",
10
+ :type => 'f',
11
+ :regex => '.*\.log\.[0-9]+\.bz2'
12
+ })
13
+
14
+ files = []
15
+ total = 0
16
+ last_timestamp = last_run = get_last_run(hostname)
17
+ result.sort!.each do |path|
18
+ timestamp = Integer(File.basename(path, ".bz2")[/.*\.log\.([0-9]+)/,1])
19
+ if timestamp > Integer(last_run) && total < OAS::LogCollector.options[:files_to_copy]
20
+ files << path
21
+ total += 1
22
+ last_timestamp = timestamp
23
+ end
24
+ end
25
+
26
+ if files.any?
27
+ source.copy(files, OAS::LogCollector.options[:logs_path])
28
+ set_last_run(hostname, last_timestamp)
29
+ end
30
+ end
31
+
32
+ def get_last_run(hostname)
33
+ OAS::LogCollector.redis do |conn|
34
+ conn.hget("timestamps", hostname) || 0
35
+ end
36
+ end
37
+
38
+ def set_last_run(hostname, timestamp)
39
+ OAS::LogCollector.redis do |conn|
40
+ conn.hset("timestamps", hostname, timestamp)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,49 @@
1
+ require 'net/ssh'
2
+ require 'net/scp'
3
+ require 'fileutils'
4
+
5
+ module OAS
6
+ module LogCollector
7
+ class Source
8
+ class Error < StandardError; end
9
+
10
+ attr_reader :host, :user, :port
11
+
12
+ def initialize(host, user, port = 22)
13
+ @host, @user, @port = host, user, port
14
+ end
15
+
16
+ def find(path, options = {})
17
+ out = ''
18
+ ssh = Net::SSH.start(host, user, :port => port)
19
+ ssh.exec!(build_find(path, options)) do |channel, stream, data|
20
+ raise Error.new(data) if stream == :stderr
21
+ out << data
22
+ end
23
+ ssh.close
24
+ out.split("\n")
25
+ end
26
+
27
+ def copy(files, logs_path)
28
+ dest_path = File.join(logs_path, host)
29
+ FileUtils.mkdir_p(dest_path)
30
+
31
+ Net::SCP.start(host, user, :port => port) do |scp|
32
+ files.each do |path|
33
+ scp.download!(path, File.join(dest_path, File.basename(path)))
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+ def build_find(path, options)
40
+ str = "find #{path}"
41
+ options.each do |k, v|
42
+ str += " -#{k} '#{v}'"
43
+ end
44
+ str
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module OAS
2
+ module LogCollector
3
+ VERSION = "0.1.4"
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ require 'oas/log_collector/version'
2
+ require 'connection_pool'
3
+ require 'redis'
4
+
5
+ module OAS
6
+ module LogCollector
7
+ DEFAULTS = {
8
+ :sources => [],
9
+ :source_logs_path => "/tmp",
10
+ :logs_path => "~/oas-log-collector",
11
+ :days_to_search => 10,
12
+ :files_to_copy => 90,
13
+ :sleep_seconds => 56400,
14
+ :redis_url => 'redis://localhost:6379/0'
15
+ }
16
+
17
+ extend self
18
+
19
+ def options
20
+ @options ||= DEFAULTS.dup
21
+ end
22
+
23
+ def redis(&block)
24
+ @redis ||= ConnectionPool.new(:timeout => 1, :size => options[:sources].size) { Redis.connect(:url => options[:redis_url]) }
25
+ raise ArgumentError, "requires a block" if !block
26
+ @redis.with(&block)
27
+ end
28
+
29
+ def redis=(conn)
30
+ if conn.is_a?(ConnectionPool)
31
+ @redis = conn
32
+ else
33
+ raise ArgumentError, "redis= requires a ConnectionPool"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1 @@
1
+ require 'oas/log_collector'
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "oas/log_collector/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "oas-log-collector"
7
+ s.version = OAS::LogCollector::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Damian Caruso"]
10
+ s.email = ["damian.caruso@gmail.com"]
11
+ s.homepage = "http://github.com/realmedia/oas-log-collector"
12
+ s.description = s.summary = "Description goes here"
13
+
14
+ s.rubyforge_project = "oas-ivc"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "dante", "~> 0.1"
22
+ s.add_dependency "net-ssh", "~> 2.5"
23
+ s.add_dependency "net-scp", "~> 1.0.4"
24
+ s.add_dependency "redis", "~> 3"
25
+ s.add_dependency "connection_pool", "~> 0.9.2"
26
+
27
+ s.add_development_dependency "minitest", "~> 3"
28
+ s.add_development_dependency "rake"
29
+ s.add_development_dependency "simplecov"
30
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
6
+
7
+ require 'oas/log_collector'
8
+ REDIS = ConnectionPool.new(:timeout => 1, :size => 1) { Redis.connect(:url => "redis://localhost/15") }
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+ require 'oas/log_collector/manager'
3
+
4
+ class TestManager < MiniTest::Unit::TestCase
5
+ def setup
6
+ OAS::LogCollector.options.merge!({
7
+ :source_logs_path => "/tmp",
8
+ :files_to_copy => 90,
9
+ :days_to_search => 10,
10
+ :logs_path => "/tmp/logs"
11
+ })
12
+ @result = ["adstream.log.223456789.bz2", "adstream.log.123456789.bz2", "adstream.log.323456789.bz2"]
13
+ @manager = OAS::LogCollector::Manager.new
14
+ @source = Minitest::Mock.new
15
+ @source.expect :host, 'localhost'
16
+ @source.expect :find, @result, ["/tmp", { :mtime => '-10', :type => 'f', :regex => '.*\.log\.[0-9]+\.bz2' }]
17
+
18
+ OAS::LogCollector.redis = REDIS
19
+ OAS::LogCollector.redis {|c| c.flushdb }
20
+ end
21
+
22
+ def test_set_last_run_to_the_last_downloaded_file
23
+ @source.expect :copy, nil, [@result, "/tmp/logs"]
24
+ @manager.collect(@source)
25
+ @source.verify
26
+ assert_equal "323456789", @manager.get_last_run('localhost')
27
+ end
28
+
29
+ def test_do_not_select_more_files_than_allowed
30
+ @manager.set_last_run('localhost', '123456789')
31
+
32
+ OAS::LogCollector.options.merge!({
33
+ :files_to_copy => 1,
34
+ })
35
+
36
+ @source.expect :copy, nil, [["adstream.log.223456789.bz2"], "/tmp/logs"]
37
+ @manager.collect(@source)
38
+ @source.verify
39
+ end
40
+
41
+ def test_select_files_with_timestamp_greater_than_last_run
42
+ @manager.set_last_run('localhost', '223456789')
43
+
44
+ @source.expect :copy, nil, [["adstream.log.323456789.bz2"], "/tmp/logs"]
45
+ @manager.collect(@source)
46
+ @source.verify
47
+ end
48
+ end
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+ require 'oas/log_collector/source'
3
+
4
+ module MySSHSession
5
+ def exec!(command, &block)
6
+ _exec("/tmp/fileone.txt\n/tmp/filetwo.txt", :stdout, &block)
7
+ end
8
+
9
+ def close; end
10
+
11
+ private
12
+ def _exec(data, stream = :stdout, &block)
13
+ if block_given?
14
+ block.call true, stream, data
15
+ else
16
+ return data
17
+ end
18
+ end
19
+ end
20
+
21
+ module MySSHSessionWithError
22
+ include MySSHSession
23
+
24
+ def exec!(command, &block)
25
+ _exec("something was wrong", :stderr, &block)
26
+ end
27
+ end
28
+
29
+ class TestSource < MiniTest::Unit::TestCase
30
+ def setup
31
+ @source = OAS::LogCollector::Source.new('remote-host.com', 'user')
32
+ end
33
+
34
+ def test_find_with_error
35
+ session = Class.new { include MySSHSessionWithError }
36
+ Net::SSH.stub :start, session.new do
37
+ assert_raises OAS::LogCollector::Source::Error do
38
+ @source.find("/tmp")
39
+ end
40
+ end
41
+ end
42
+
43
+ def test_find_successful
44
+ session = Class.new { include MySSHSession }
45
+ Net::SSH.stub :start, session.new do
46
+ result = @source.find("/tmp")
47
+ assert_instance_of Array, result
48
+ assert_equal 2, result.size
49
+ end
50
+ end
51
+
52
+ def test_find_command_builder
53
+ session = Minitest::Mock.new
54
+ session.expect :exec!, "", ["find /tmp -type 'f' -name '*.txt'"]
55
+ session.expect :close, nil
56
+ Net::SSH.stub :start, session do
57
+ result = @source.find("/tmp", { :type => 'f', :name => '*.txt' })
58
+ end
59
+ session.verify
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oas-log-collector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Damian Caruso
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dante
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: net-ssh
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.5'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.5'
46
+ - !ruby/object:Gem::Dependency
47
+ name: net-scp
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.4
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: redis
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3'
78
+ - !ruby/object:Gem::Dependency
79
+ name: connection_pool
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 0.9.2
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.9.2
94
+ - !ruby/object:Gem::Dependency
95
+ name: minitest
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '3'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '3'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rake
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: simplecov
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: Description goes here
143
+ email:
144
+ - damian.caruso@gmail.com
145
+ executables:
146
+ - oas-log-collector
147
+ extensions: []
148
+ extra_rdoc_files: []
149
+ files:
150
+ - .gitignore
151
+ - .rvmrc
152
+ - .travis.yml
153
+ - Gemfile
154
+ - LICENSE
155
+ - README.md
156
+ - Rakefile
157
+ - bin/oas-log-collector
158
+ - examples/config.yml
159
+ - lib/oas-log-collector.rb
160
+ - lib/oas/log_collector.rb
161
+ - lib/oas/log_collector/cli.rb
162
+ - lib/oas/log_collector/manager.rb
163
+ - lib/oas/log_collector/source.rb
164
+ - lib/oas/log_collector/version.rb
165
+ - oas-log-collector.gemspec
166
+ - test/helper.rb
167
+ - test/test_manager.rb
168
+ - test/test_source.rb
169
+ homepage: http://github.com/realmedia/oas-log-collector
170
+ licenses: []
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ none: false
177
+ requirements:
178
+ - - ! '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ segments:
182
+ - 0
183
+ hash: 1520991714995813635
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ segments:
191
+ - 0
192
+ hash: 1520991714995813635
193
+ requirements: []
194
+ rubyforge_project: oas-ivc
195
+ rubygems_version: 1.8.24
196
+ signing_key:
197
+ specification_version: 3
198
+ summary: Description goes here
199
+ test_files: []