zabbix_receiver 0.0.1

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: 1db84526dbaff0fd80bb4cef61161ceb0074ba7a
4
+ data.tar.gz: c303aa0a685791c419d8a77610f8408974075701
5
+ SHA512:
6
+ metadata.gz: d15a574d116ca65dee81029ab96f8d92f747654bf2625e77935f5b384ce1e29ae7e87472a7afb78365f98859c450f47f8944fe50c04421e0585d4eced63ba096
7
+ data.tar.gz: de862f50067ed77859ddb88751c00526cfca9c368b31e7a878d749137a92d8a9f2619fd26a8fa4feb9ac3bfea640bba194d876fb918e733cf00171fb327fc48e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zabbix_receiver.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ryota Arai
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # ZabbixReceiver
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'zabbix_receiver'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install zabbix_receiver
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/zabbix_receiver/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,6 @@
1
+ require "zabbix_receiver/server"
2
+ require "zabbix_receiver/version"
3
+
4
+ module ZabbixReceiver
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,111 @@
1
+ require 'json'
2
+ require 'logger'
3
+
4
+ module ZabbixReceiver
5
+ class Server
6
+ ZABBIX_HEADER = "ZBXD\x01"
7
+
8
+ def initialize(options = {})
9
+ @options = {
10
+ proxy_to: {
11
+ port: 10051,
12
+ },
13
+ }.merge(options)
14
+
15
+ validate_options
16
+
17
+ @logger = options[:logger] || Logger.new(StringIO.new)
18
+
19
+ @blocks_on_receive_sender_data = []
20
+ end
21
+
22
+ def on_receive_sender_data(&block)
23
+ @blocks_on_receive_sender_data << block
24
+ end
25
+
26
+ def start(address, port)
27
+ server = TCPServer.open(address, port)
28
+
29
+ while true
30
+ Thread.start(server.accept) do |f|
31
+ accept(f)
32
+ end
33
+ end
34
+ end
35
+
36
+ def accept(f)
37
+ request = parse(f)
38
+
39
+ @logger.debug "Request: #{request}"
40
+
41
+ request_type = request['request']
42
+
43
+ case request_type
44
+ when 'active checks'
45
+ proxy_request(f)
46
+ when 'sender data'
47
+ @blocks_on_receive_sender_data.each do |block|
48
+ block.call(request)
49
+ end
50
+
51
+ count = request['data'].size
52
+
53
+ respond_with(f, {
54
+ "response" => "success",
55
+ "info" => "Processed #{count} Failed 0 Total #{count} Seconds spent 0.000000"
56
+ })
57
+ else
58
+ @logger.error "Unknown request type (#{request_type})"
59
+ end
60
+ ensure
61
+ f && f.close
62
+ end
63
+
64
+ def proxy_request(f)
65
+ f.seek(0)
66
+
67
+ socket = TCPSocket.open(
68
+ @options[:proxy_to][:host],
69
+ @options[:proxy_to][:port],
70
+ )
71
+ socket.write(f.read)
72
+ f.write(socket.read)
73
+ ensure
74
+ socket && socket.close
75
+ end
76
+
77
+ private
78
+
79
+ def parse(f)
80
+ f.seek(0)
81
+
82
+ unless f.read(5) == ZABBIX_HEADER
83
+ @logger.error "Invalid Zabbix request"
84
+ return
85
+ end
86
+
87
+ length = f.read(8).unpack('q').first
88
+ body = f.read
89
+ unless body.size == length
90
+ @logger.error "Length mismatch"
91
+ return
92
+ end
93
+
94
+ JSON.parse(body)
95
+ end
96
+
97
+ def respond_with(f, payload)
98
+ payload = payload.to_json
99
+ f.write(ZABBIX_HEADER + [payload.bytesize].pack('q') + payload)
100
+ end
101
+
102
+ def validate_options
103
+ [:host, :port].each do |k|
104
+ unless @options[:proxy_to][k]
105
+ raise "options[:proxy_to][:#{k}] is required."
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+
@@ -0,0 +1,3 @@
1
+ module ZabbixReceiver
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,91 @@
1
+ require 'zabbix_receiver'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZabbixReceiver::Server do
4
+ let(:options) { {
5
+ proxy_to: {
6
+ host: 'zabbix-server',
7
+ port: 'port',
8
+ }
9
+ } }
10
+ subject(:server) { described_class.new(options) }
11
+
12
+ describe '#accept' do
13
+ context 'with sender data' do
14
+ let(:request) { "ZBXD\u0001b\u0000\u0000\u0000\u0000\u0000\u0000\u0000{\n\t\"request\":\"sender data\",\n\t\"data\":[\n\t\t{\n\t\t\t\"host\":\"hello\",\n\t\t\t\"key\":\"key\",\n\t\t\t\"value\":\"value\"}]}" }
15
+
16
+ it 'receives sender data' do
17
+ io = StringIO.new(request.dup)
18
+
19
+ received_data = nil
20
+ server.on_receive_sender_data do |data|
21
+ received_data = data
22
+ end
23
+
24
+ server.accept(io)
25
+ expected_response = "ZBXD\u0001S\u0000\u0000\u0000\u0000\u0000\u0000\u0000{\"response\":\"success\",\"info\":\"Processed 1 Failed 0 Total 1 Seconds spent 0.000000\"}"
26
+ expect(io.string).to eq(request + expected_response)
27
+ expect(received_data).to eq({
28
+ 'request' => 'sender data',
29
+ 'data' => [{
30
+ 'host' => 'hello',
31
+ 'key' => 'key',
32
+ 'value' => 'value',
33
+ }],
34
+ })
35
+ end
36
+ end
37
+
38
+ context 'with active checks' do
39
+ let(:socket_to_zabbix_server) { double(:socket) }
40
+ let(:request) { "ZBXD\u0001)\x00\x00\x00\x00\x00\x00\x00{\"request\":\"active checks\",\"key\":\"value\"}" }
41
+ let(:response) { "response_from_zabbix_server" }
42
+
43
+ it 'proxies a request to zabbix server' do
44
+ allow(socket_to_zabbix_server).to receive(:seek)
45
+
46
+ expect(TCPSocket).to receive(:open).with('zabbix-server', 'port').and_return(socket_to_zabbix_server)
47
+ expect(socket_to_zabbix_server).to receive(:close)
48
+ expect(socket_to_zabbix_server).to receive(:write).with(request)
49
+ expect(socket_to_zabbix_server).to receive(:read).and_return(response)
50
+
51
+ io = StringIO.new(request.dup)
52
+ server.accept(io)
53
+ expect(io.string).to eq(request + response)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zabbix_receiver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zabbix_receiver"
8
+ spec.version = ZabbixReceiver::VERSION
9
+ spec.authors = ["Ryota Arai"]
10
+ spec.email = ["ryota-arai@cookpad.com"]
11
+ spec.summary = %q{Server to receive sender data from zabbix-agent.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbix_receiver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryota Arai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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
+ description:
56
+ email:
57
+ - ryota-arai@cookpad.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/zabbix_receiver.rb
69
+ - lib/zabbix_receiver/server.rb
70
+ - lib/zabbix_receiver/version.rb
71
+ - spec/spec_helper.rb
72
+ - spec/zabbix_receiver/server_spec.rb
73
+ - zabbix_receiver.gemspec
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Server to receive sender data from zabbix-agent.
98
+ test_files:
99
+ - spec/spec_helper.rb
100
+ - spec/zabbix_receiver/server_spec.rb