nfagent 0.9.27 → 0.9.28

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ $hoe = Hoe.spec 'nfagent' do
15
15
  self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
16
  self.rubyforge_name = self.name # TODO this is default value
17
17
  self.extra_deps = [
18
- ['svutil','>= 0.1.2'], ['eventmachine', '>= 0.12.8'], ['squiggle', '>= 0.0.8']
18
+ ['svutil','>= 0.1.3'], ['eventmachine', '>= 0.12.8'], ['squiggle', '>= 0.0.8']
19
19
  ]
20
20
 
21
21
  end
data/bin/squid_log_writer CHANGED
@@ -6,12 +6,14 @@ class Client
6
6
  def initialize(host, port)
7
7
  @host = host
8
8
  @port = port
9
+ @count = 0
9
10
  end
10
11
 
11
12
  def write_safely(data)
12
13
  connect_socket unless @connected
13
14
  begin
14
15
  @client.puts(data)
16
+ sleep 0.01
15
17
  rescue
16
18
  @connected = false
17
19
  end
data/lib/nfagent.rb CHANGED
@@ -36,5 +36,5 @@ require 'nfagent/cli'
36
36
  require 'nfagent/tests'
37
37
 
38
38
  module NFAgent
39
- VERSION = '0.9.27'
39
+ VERSION = '0.9.28'
40
40
  end
data/lib/nfagent/chunk.rb CHANGED
@@ -34,7 +34,7 @@ module NFAgent
34
34
 
35
35
  def dump(key = nil)
36
36
  Payload.new do |payload|
37
- Log.info("Dumping payload from chunk (#{self.size} lines)")
37
+ Log.info("Dumping payload from chunk (#{self.size || 0} lines #{'due to expiry' if expired?}")
38
38
  payload.line_count = self.size
39
39
  payload.chunk_expired = expired?
40
40
  payload.key = key
@@ -44,11 +44,15 @@ module NFAgent
44
44
  end
45
45
 
46
46
  def submit(key = nil)
47
- submitter = Submitter.new(self.dump(key))
48
- submitter.errback { |payload|
49
- payload.write_to_disk(Config.dump_dir)
47
+ Log.info("Submitting...")
48
+ # TODO God knows why EM Deferrable isn't working - defer here is OK
49
+ EM.defer {
50
+ submitter = Submitter.new(self.dump(key))
51
+ submitter.errback { |payload|
52
+ payload.write_to_disk(Config.dump_dir)
53
+ }
54
+ submitter.perform
50
55
  }
51
- submitter.perform
52
56
  # Callback and remove from chunk group
53
57
  end
54
58
  end
@@ -39,6 +39,7 @@ module NFAgent
39
39
  chunk = @chunk_group.fetch!(key, Chunk.new(@chunk_size))
40
40
  chunk << line
41
41
  rescue ChunkExpired, ChunkFull
42
+ Log.info("Chunk full or expired, cannot add lines")
42
43
  reset_chunk(key)
43
44
  end
44
45
  end
@@ -57,9 +58,8 @@ module NFAgent
57
58
 
58
59
  private
59
60
  def reset_chunk(key = nil)
60
- key ||= 'all'
61
- chunk = @chunk_group.delete(key)
62
- chunk.submit
61
+ chunk = @chunk_group.delete(key || 'all')
62
+ chunk.submit(key == 'all' ? nil : key)
63
63
  end
64
64
  end
65
65
  end
@@ -8,14 +8,17 @@ module NFAgent
8
8
  proxy_class.start(SERVICE_HOST, 80) do |http|
9
9
  http.read_timeout = 120 # 2 minutes TODO: Make this a config option with 120 as default
10
10
  req = Net::HTTP::Post.new("/#{end_point}")
11
+ p({"key" => Config.client_key}.merge(data_hash).delete('data'))
11
12
  req.set_form_data({"key" => Config.client_key}.merge(data_hash))
12
13
  ClientResponse.new do |resp|
13
14
  resp.response, resp.message = http.request(req)
15
+ Log.info("Client Returned with '#{resp.message}'")
14
16
  end
15
17
  end
16
18
  rescue Exception => e
17
19
  # Trap Exception class here to ensure we catch Timeout
18
20
  ClientResponse.new do |resp|
21
+ Log.info("Client Error: #{$!}")
19
22
  resp.message = $!
20
23
  end
21
24
  end
data/lib/nfagent/event.rb CHANGED
@@ -13,7 +13,7 @@ module NFAgent
13
13
  if data && data.length > 10
14
14
  @handler.append(data)
15
15
  end
16
- send_data('OK')
16
+ #send_data('OK')
17
17
  end
18
18
 
19
19
  def unbind
@@ -34,7 +34,8 @@ module NFAgent
34
34
  end
35
35
 
36
36
  def write_to_disk(directory)
37
- File.open(File.join(directory, "#{self.checksum}-#{self.attempt}"), "w") do |file|
37
+ filename = [ self.checksum, self.attempt, self.key ].compact.join("-")
38
+ File.open(File.join(directory, filename), "w") do |file|
38
39
  file << self.data
39
40
  end
40
41
  end
@@ -50,14 +51,14 @@ module NFAgent
50
51
  filename && File.exists?(lockfile)
51
52
  end
52
53
 
53
- def self.read_from_file(filename)
54
+ def self.read_from_file(filename, dir = Config.dump_dir)
54
55
  # Ensure the file is only relative
55
56
  filename = File.basename(filename)
56
57
  self.new do |payload|
57
58
  payload.filename = filename
58
- payload.checksum, payload.attempt = filename.split("-")
59
+ payload.checksum, payload.attempt, payload.key = filename.split("-")
59
60
  payload.data = ""
60
- ref = File.join(Config.dump_dir, filename)
61
+ ref = File.join(dir, filename)
61
62
  File.open(ref, "r") do |file|
62
63
  payload.data << file.read
63
64
  end
@@ -70,7 +71,8 @@ module NFAgent
70
71
 
71
72
  def try_again_later
72
73
  # TODO: Move the file to a new name with a later timetamp
73
- FileUtils.mv(File.join(Config.dump_dir, self.filename), File.join(Config.dump_dir, "#{self.checksum}-#{self.attempt}"))
74
+ new_filename = [ self.checksum, self.attempt, self.key ].compact.join("-")
75
+ FileUtils.mv(File.join(Config.dump_dir, self.filename), File.join(Config.dump_dir, new_filename))
74
76
  end
75
77
 
76
78
  private
@@ -15,6 +15,7 @@ module NFAgent
15
15
  response = Client.post(:collector, @payload.to_hash)
16
16
  if response.ok?
17
17
  succeed(@payload)
18
+ Log.info("Submitted #{@payload.line_count} lines")
18
19
  else
19
20
  Log.error "Submission Failed: #{response.message}"
20
21
  fail(@payload)
data/nfagent.conf CHANGED
@@ -1,4 +1,4 @@
1
- client_key = 1234
2
- dump_dir = /tmp/dumps
3
- log_file = /tmp/debug
4
- pid_file = /tmp/nfagent.pid
1
+ client_key = FNX3tyOkG6J7nk2uC3QX
2
+ dump_dir = sandbox/dumps
3
+ log_file = sandbox/debug
4
+ pid_file = sandbox/nfagent.pid
data/test/test_chunk.rb CHANGED
@@ -56,7 +56,7 @@ class TestChunk < ActiveSupport::TestCase
56
56
  end
57
57
 
58
58
  test "submit" do
59
- NFAgent::Submitter.any_instance.expects(:perform)
59
+ EM.expects(:defer)
60
60
  chunk = NFAgent::Chunk.new
61
61
  chunk.submit
62
62
  end
@@ -41,11 +41,11 @@ class TestChunkHandler < ActiveSupport::TestCase
41
41
  assert_equal 1, chunk_handler.chunk_group['jetson'].size
42
42
  end
43
43
 
44
- test "reset chunk after expiry" do
44
+ test "reset chunk after expiry in multi mode" do
45
45
  NFAgent::Config.parse = 'locally'
46
46
  NFAgent::Config.mode = 'multi'
47
47
  NFAgent::Config.mapper = 'MyMapper'
48
- NFAgent::Submitter.any_instance.expects(:perform).times(2)
48
+ NFAgent::Chunk.any_instance.expects(:submit).at_least_once
49
49
  chunk_handler = NFAgent::ChunkHandler.new
50
50
  chunk_handler.append(@logline)
51
51
  Timecop.travel(30) do
@@ -67,11 +67,11 @@ class TestChunkHandler < ActiveSupport::TestCase
67
67
  end
68
68
  end
69
69
 
70
- test "reset chunk after full wuth check_full_or_expired" do
70
+ test "reset chunk after full with check_full_or_expired in multi mode" do
71
71
  NFAgent::Config.parse = 'locally'
72
72
  NFAgent::Config.mode = 'multi'
73
73
  NFAgent::Config.mapper = 'MyMapper'
74
- NFAgent::Submitter.any_instance.expects(:perform).times(2)
74
+ EM.expects(:defer).times(2)
75
75
  chunk_handler = NFAgent::ChunkHandler.new(:chunk_size => 10)
76
76
  9.times { chunk_handler.append(@logline) }
77
77
  9.times { chunk_handler.append(@logline2) }
@@ -91,4 +91,28 @@ class TestChunkHandler < ActiveSupport::TestCase
91
91
  chunk_handler.append("") # Invalid logline
92
92
  assert chunk_handler.chunk_group['all'].nil?
93
93
  end
94
+
95
+ test "reset chunk is passed appropriate key in multi mode" do
96
+ NFAgent::Config.parse = 'locally'
97
+ NFAgent::Config.mode = 'multi'
98
+ NFAgent::Config.mapper = 'MyMapper'
99
+ NFAgent::Chunk.any_instance.expects(:submit).with('acme')
100
+ chunk_handler = NFAgent::ChunkHandler.new
101
+ chunk_handler.append(@logline)
102
+ Timecop.travel(61) do
103
+ chunk_handler.check_full_or_expired
104
+ end
105
+ end
106
+
107
+ test "reset chunk is passed nil key in normal mode" do
108
+ NFAgent::Config.parse = 'remotely'
109
+ NFAgent::Config.mode = 'normal'
110
+ NFAgent::Chunk.any_instance.expects(:submit).with(nil)
111
+ chunk_handler = NFAgent::ChunkHandler.new
112
+ chunk_handler.append(@logline)
113
+ Timecop.travel(61) do
114
+ chunk_handler.check_full_or_expired
115
+ end
116
+ end
117
+
94
118
  end
data/test/test_payload.rb CHANGED
@@ -3,6 +3,7 @@ require 'test/test_helper'
3
3
 
4
4
  class TestPayload < ActiveSupport::TestCase
5
5
  setup do
6
+ FileUtils.rm_f(Dir.glob("test/sandbox/dumps/*"))
6
7
  end
7
8
 
8
9
  test "to_hash" do
@@ -23,4 +24,54 @@ class TestPayload < ActiveSupport::TestCase
23
24
  end
24
25
  assert_equal({ 'payload' => 'data', 'checksum' => 'checksum', 'line_count' => 10, 'chunk_expired' => false, 'key' => '1234' }, payload.to_hash)
25
26
  end
27
+
28
+ test "write-to-disk" do
29
+ payload = NFAgent::Payload.new do |p|
30
+ p.data = "data"
31
+ p.checksum = "checksum"
32
+ p.line_count = 10
33
+ p.chunk_expired = false
34
+ end
35
+ payload.write_to_disk("test/sandbox/dumps")
36
+ assert File.exists?("test/sandbox/dumps/checksum-0")
37
+ # And read back in
38
+ payload = NFAgent::Payload.read_from_file("checksum-0", "test/sandbox/dumps/")
39
+ assert_equal "data", payload.data
40
+ assert_equal 0, payload.attempt
41
+ assert !payload.key
42
+ end
43
+
44
+ test "write-to-disk with non-zero attempt number" do
45
+ payload = NFAgent::Payload.new do |p|
46
+ p.data = "data"
47
+ p.checksum = "checksum"
48
+ p.line_count = 10
49
+ p.chunk_expired = false
50
+ p.attempt = 10
51
+ end
52
+ payload.write_to_disk("test/sandbox/dumps")
53
+ assert File.exists?("test/sandbox/dumps/checksum-10")
54
+ # And read back in
55
+ payload = NFAgent::Payload.read_from_file("checksum-10", "test/sandbox/dumps/")
56
+ assert_equal "data", payload.data
57
+ assert_equal 10, payload.attempt
58
+ end
59
+
60
+ test "write-to-disk with key" do
61
+ payload = NFAgent::Payload.new do |p|
62
+ p.data = "data"
63
+ p.checksum = "checksum"
64
+ p.line_count = 10
65
+ p.chunk_expired = false
66
+ p.key = '1234'
67
+ end
68
+ payload.write_to_disk("test/sandbox/dumps")
69
+ assert File.exists?("test/sandbox/dumps/checksum-0-1234")
70
+ # And read back in
71
+ payload = NFAgent::Payload.read_from_file("checksum-0-1234", "test/sandbox/dumps/")
72
+ assert_equal "data", payload.data
73
+ assert_equal 0, payload.attempt
74
+ assert_equal '1234', payload.key
75
+ end
76
+
26
77
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 27
9
- version: 0.9.27
8
+ - 28
9
+ version: 0.9.28
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Draper
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-08 00:00:00 +10:30
17
+ date: 2011-02-09 00:00:00 +10:30
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,8 +27,8 @@ dependencies:
27
27
  segments:
28
28
  - 0
29
29
  - 1
30
- - 2
31
- version: 0.1.2
30
+ - 3
31
+ version: 0.1.3
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -60,7 +60,7 @@ dependencies:
60
60
  type: :runtime
61
61
  version_requirements: *id003
62
62
  - !ruby/object:Gem::Dependency
63
- name: hoe
63
+ name: rubyforge
64
64
  prerelease: false
65
65
  requirement: &id004 !ruby/object:Gem::Requirement
66
66
  requirements:
@@ -68,11 +68,25 @@ dependencies:
68
68
  - !ruby/object:Gem::Version
69
69
  segments:
70
70
  - 2
71
- - 4
72
71
  - 0
73
- version: 2.4.0
72
+ - 4
73
+ version: 2.0.4
74
74
  type: :development
75
75
  version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: hoe
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 2
85
+ - 6
86
+ - 0
87
+ version: 2.6.0
88
+ type: :development
89
+ version_requirements: *id005
76
90
  description: Logging Agent for NetFox Online
77
91
  email:
78
92
  - daniel@netfox.com