ffwd-riemann 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c2536975d8be4dd026413d399842e78f686fa39
4
+ data.tar.gz: d198e325793a91f8488f1aff2f30ae50e2a18556
5
+ SHA512:
6
+ metadata.gz: bf98c1b2cf932d36e151cc49a9e665c53ebaef35a86a626a1e0e67c4ca878b6a622ac35046a1fcab0b86cb6d64b8d5f727398f8602df68a4d14e9ce13b4c6907
7
+ data.tar.gz: d5665c0d9498140a154f47baa2ff810010e23b691ecd0dd5c4c597d9432ee62d9c74b7405d1aeed4756626f8001640401292c2f409929033e24f31286246d801
@@ -0,0 +1,63 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ module FFWD::Plugin::Riemann
17
+ module Connection
18
+ module Serializer
19
+ def self.dump(m)
20
+ m.encode.to_s
21
+ end
22
+
23
+ def self.load(data)
24
+ ::Riemann::Message.decode(data)
25
+ end
26
+ end
27
+
28
+ def serializer
29
+ FFWD::Plugin::Riemann::Connection::Serializer
30
+ end
31
+
32
+ def initialize bind, core, log
33
+ @bind = bind
34
+ @core = core
35
+ @log = log
36
+ end
37
+
38
+ def receive_object(m)
39
+ # handle no events in object.
40
+ if m.events.nil?
41
+ send_ok
42
+ return
43
+ end
44
+
45
+ unless m.events.nil? or m.events.empty?
46
+ events = m.events.map{|e| read_event(e)}
47
+ events.each{|e| @core.input.event e}
48
+ end
49
+
50
+ @bind.increment :received_events, m.events.size
51
+ send_ok
52
+ rescue => e
53
+ @bind.increment :failed_events, m.events.size
54
+ @log.error "Failed to receive object", e
55
+ send_error e
56
+ end
57
+
58
+ protected
59
+
60
+ def send_ok; end
61
+ def send_error(e); end
62
+ end
63
+ end
@@ -0,0 +1,48 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require_relative 'shared'
17
+
18
+ module FFWD::Plugin::Riemann
19
+ module Output
20
+ def send_all events, metrics
21
+ all_events = []
22
+ all_events += events.map{|e| make_event e} unless events.empty?
23
+ all_events += metrics.map{|m| make_metric m} unless metrics.empty?
24
+ return if all_events.empty?
25
+ m = make_message :events => all_events
26
+ send_data encode(m)
27
+ end
28
+
29
+ def send_event event
30
+ e = make_event event
31
+ m = make_message :events => [e]
32
+ send_data encode(m)
33
+ end
34
+
35
+ def send_metric metric
36
+ e = make_metric metric
37
+ m = make_message :events => [e]
38
+ send_data encode(m)
39
+ end
40
+
41
+ def receive_data data
42
+ message = read_message data
43
+ return if message.ok
44
+ @bad_acks = (@bad_acks || 0) + 1
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,146 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'beefcake'
17
+
18
+ require 'riemann/query'
19
+ require 'riemann/attribute'
20
+ require 'riemann/state'
21
+ require 'riemann/event'
22
+ require 'riemann/message'
23
+
24
+ module FFWD::Plugin::Riemann
25
+ MAPPING = [
26
+ [:key, :service, :service=],
27
+ [:value, :metric, :metric=],
28
+ [:host, :host, :host=],
29
+ ]
30
+
31
+ METRIC_MAPPING = MAPPING
32
+
33
+ EVENT_MAPPING = [
34
+ [:state, :state, :state=],
35
+ [:description, :description, :description=],
36
+ [:ttl, :ttl, :ttl=],
37
+ ] + MAPPING
38
+
39
+ module Shared
40
+ def read_attributes e, source
41
+ return if source.nil? or source.empty?
42
+
43
+ attributes = {}
44
+
45
+ source.each do |a|
46
+ attributes[a.key] = a.value
47
+ end
48
+
49
+ e[:attributes] = attributes
50
+ end
51
+
52
+ def write_attributes e, source
53
+ return if source.nil? or source.empty?
54
+
55
+ e.attributes = source.map{|k, v|
56
+ k = k.to_s.dup unless k.nil?
57
+ v = v.dup unless v.nil?
58
+ ::Riemann::Attribute.new(:key => k, :value => v)
59
+ }
60
+ end
61
+
62
+ def read_tags e, source
63
+ return if source.nil? or source.empty?
64
+ e[:tags] = source.to_a
65
+ end
66
+
67
+ def write_tags e, source
68
+ return if source.nil? or source.empty?
69
+ e.tags = source.map{|v| v.dup}
70
+ end
71
+
72
+ def read_time e, source
73
+ return if source.nil?
74
+ e[:time] = Time.at source
75
+ end
76
+
77
+ def write_time e, source
78
+ return if source.nil?
79
+ e.time = source.to_i
80
+ end
81
+
82
+ def make_event event
83
+ e = ::Riemann::Event.new
84
+
85
+ write_attributes e, event.attributes
86
+ write_tags e, event.tags
87
+ write_time e, event.time
88
+
89
+ EVENT_MAPPING.each do |key, reader, writer|
90
+ if (v = event.send(key)).nil?
91
+ next
92
+ end
93
+
94
+ v = v.to_s if v.is_a? Symbol
95
+ e.send writer, v
96
+ end
97
+
98
+ e
99
+ end
100
+
101
+ def make_metric metric
102
+ e = ::Riemann::Event.new
103
+
104
+ write_attributes e, metric.attributes
105
+ write_tags e, metric.tags
106
+ write_time e, metric.time
107
+
108
+ METRIC_MAPPING.each do |key, reader, writer|
109
+ if (v = metric.send(key)).nil?
110
+ next
111
+ end
112
+
113
+ v = v.to_s if v.is_a? Symbol
114
+ e.send writer, v
115
+ end
116
+
117
+ e
118
+ end
119
+
120
+ def read_event event
121
+ e = {}
122
+
123
+ read_attributes e, event.attributes
124
+ read_tags e, event.tags
125
+ read_time e, event.time
126
+
127
+ EVENT_MAPPING.each do |key, reader, writer|
128
+ if (v = event.send(reader)).nil?
129
+ next
130
+ end
131
+
132
+ e[key] = v
133
+ end
134
+
135
+ e
136
+ end
137
+
138
+ def make_message(message)
139
+ ::Riemann::Message.new(message)
140
+ end
141
+
142
+ def read_message(data)
143
+ ::Riemann::Message.decode data
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,22 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ module FFWD
17
+ module Plugin
18
+ module Riemann
19
+ VERSION = "0.1.0"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,142 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'eventmachine'
17
+ require 'beefcake'
18
+
19
+ require 'riemann/query'
20
+ require 'riemann/attribute'
21
+ require 'riemann/state'
22
+ require 'riemann/event'
23
+ require 'riemann/message'
24
+
25
+ require 'ffwd/connection'
26
+ require 'ffwd/handler'
27
+ require 'ffwd/logging'
28
+ require 'ffwd/plugin'
29
+ require 'ffwd/protocol'
30
+
31
+ require_relative 'riemann/connection'
32
+ require_relative 'riemann/shared'
33
+ require_relative 'riemann/output'
34
+
35
+ module FFWD::Plugin::Riemann
36
+ include FFWD::Plugin
37
+ include FFWD::Logging
38
+
39
+ register_plugin "riemann"
40
+
41
+ class OutputTCP < FFWD::Handler
42
+ include FFWD::Plugin::Riemann::Shared
43
+ include FFWD::Plugin::Riemann::Output
44
+
45
+ def self.plugin_type
46
+ "riemann_out"
47
+ end
48
+
49
+ def encode m
50
+ m.encode_with_length
51
+ end
52
+ end
53
+
54
+ class OutputUDP < FFWD::Handler
55
+ include FFWD::Plugin::Riemann::Shared
56
+ include FFWD::Plugin::Riemann::Output
57
+
58
+ def self.plugin_type
59
+ "riemann_out"
60
+ end
61
+
62
+ def encode m
63
+ m.encode
64
+ end
65
+ end
66
+
67
+ class InputTCP < FFWD::Connection
68
+ include EM::Protocols::ObjectProtocol
69
+ include FFWD::Plugin::Riemann::Shared
70
+ include FFWD::Plugin::Riemann::Connection
71
+
72
+ def self.plugin_type
73
+ "riemann_in"
74
+ end
75
+
76
+ def send_ok
77
+ send_object(::Riemann::Message.new(
78
+ :ok => true))
79
+ end
80
+
81
+ def send_error(e)
82
+ send_object(::Riemann::Message.new(
83
+ :ok => false, :error => e.to_s))
84
+ end
85
+ end
86
+
87
+ class InputUDP < FFWD::Connection
88
+ include FFWD::Plugin::Riemann::Shared
89
+ include FFWD::Plugin::Riemann::Connection
90
+
91
+ def self.plugin_type
92
+ "riemann_in"
93
+ end
94
+
95
+ def receive_data(data)
96
+ receive_object serializer.load(data)
97
+ end
98
+ end
99
+
100
+ DEFAULT_HOST = "localhost"
101
+ DEFAULT_PORT = 5555
102
+ DEFAULT_PROTOCOL = 'tcp'
103
+
104
+ OUTPUTS = {:tcp => OutputTCP, :udp => OutputUDP}
105
+ INPUTS = {:tcp => InputTCP, :udp => InputUDP}
106
+
107
+ def self.setup_output opts, core
108
+ opts[:host] ||= DEFAULT_HOST
109
+ opts[:port] ||= DEFAULT_PORT
110
+
111
+ protocol = FFWD.parse_protocol(opts[:protocol] || DEFAULT_PROTOCOL)
112
+
113
+ unless type = OUTPUTS[protocol.family]
114
+ raise "No type for protocol family: #{protocol.family}"
115
+ end
116
+
117
+ protocol.connect opts, core, log, type
118
+ end
119
+
120
+ def self.setup_input opts, core
121
+ opts[:host] ||= DEFAULT_HOST
122
+ opts[:port] ||= DEFAULT_PORT
123
+ protocol = FFWD.parse_protocol(opts[:protocol] || DEFAULT_PROTOCOL)
124
+
125
+ unless connection = INPUTS[protocol.family]
126
+ raise "No connection for protocol family: #{protocol.family}"
127
+ end
128
+
129
+ protocol.bind opts, core, log, connection, log
130
+ end
131
+
132
+ def self.setup_tunnel opts, core, tunnel
133
+ opts[:port] ||= DEFAULT_PORT
134
+ protocol = FFWD.parse_protocol(opts[:protocol] || DEFAULT_PROTOCOL)
135
+
136
+ unless connection = INPUTS[protocol.family]
137
+ raise "No connection for protocol family: #{protocol.family}"
138
+ end
139
+
140
+ protocol.tunnel opts, core, tunnel, log, connection, log
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffwd-riemann
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John-John Tedro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: riemann-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffwd
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-mocks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - udoprog@spotify.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/ffwd/plugin/riemann.rb
77
+ - lib/ffwd/plugin/riemann/output.rb
78
+ - lib/ffwd/plugin/riemann/shared.rb
79
+ - lib/ffwd/plugin/riemann/connection.rb
80
+ - lib/ffwd/plugin/riemann/version.rb
81
+ homepage: https://github.com/spotify/ffwd
82
+ licenses:
83
+ - Apache 2.0
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Riemann support for FFWD.
105
+ test_files: []