zabbix_protocol 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: 68192799b80b8e4af27e6cd775304e0566eec47c
4
+ data.tar.gz: f327dd238150587052b9553adf9e9f17cc5350a4
5
+ SHA512:
6
+ metadata.gz: 9594e301b14992cc73313977997aa1517ebda8c61072651e521ea89613f18a6212710c6e6ab3a13a64b8836b712922d963a6f9151bdae8b061a64cf0a5fb12b0
7
+ data.tar.gz: 4832ed560cc62c9c825bc83396e64261bcf45fc483330f0b4bbafd09f1686db83a63bed19513f199ee442bef9be18f0e907bf1b9f5a4d31ad88b5b68736494bf
data/.gitignore ADDED
@@ -0,0 +1,15 @@
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
15
+ test.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script:
5
+ - bundle install
6
+ - bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zabbix_protocol.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Genki Sugawara
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,89 @@
1
+ # ZabbixProtocol
2
+
3
+ Zabbix protocols builder/parser.
4
+
5
+ see http://www.zabbix.org/wiki/Docs/protocols
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/zabbix_protocol.svg)](http://badge.fury.io/rb/zabbix_protocol)
8
+ [![Build Status](https://travis-ci.org/winebarrel/zabbix_protocol.svg?branch=master)](https://travis-ci.org/winebarrel/zabbix_protocol)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'zabbix_protocol'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install zabbix_protocol
25
+
26
+ ## Usage
27
+
28
+ ### Zabbix 1.4 Passive checks
29
+
30
+ http://www.zabbix.org/wiki/Docs/protocols/zabbix_agent/1.4
31
+
32
+ ```ruby
33
+ require 'socket'
34
+ require 'zabbix_protocol'
35
+
36
+ AGENT_PORT = 10050
37
+
38
+ TCPSocket.open("127.0.0.1", AGENT_PORT) do |sock|
39
+ data = "system.cpu.load[all,avg1]"
40
+
41
+ sock.print ZabbixProtocol.dump(data)
42
+ p ZabbixProtocol.load(sock.read) #=> 0.04
43
+ end
44
+ ```
45
+
46
+ ### Zabbix 1.6 Active agents
47
+
48
+ http://www.zabbix.org/wiki/Docs/protocols/zabbix_agent/1.6
49
+
50
+ ```ruby
51
+ require 'socket'
52
+ require 'zabbix_protocol'
53
+
54
+ SERVER_PORT = 10051
55
+
56
+ TCPSocket.open("127.0.0.1", SERVER_PORT) do |sock|
57
+ data = {"request" => "active checks", "host" => "my server"}
58
+
59
+ sock.print ZabbixProtocol.dump(data)
60
+ p ZabbixProtocol.load(sock.read)
61
+ #=> {"response"=>"success", "data"=>[]}
62
+ end
63
+ ```
64
+
65
+ ### Zabbix sender 1.8 protocol
66
+
67
+ http://www.zabbix.org/wiki/Docs/protocols/zabbix_sender/1.8
68
+
69
+ ```ruby
70
+ require 'socket'
71
+ require 'zabbix_protocol'
72
+
73
+ SERVER_PORT = 10051
74
+
75
+ TCPSocket.open("127.0.0.1", SERVER_PORT) do |sock|
76
+ data = {
77
+ "request" => "sender data",
78
+ "data" => [{
79
+ "host" => "my server",
80
+ "key" => "my.key",
81
+ "value" => "1"
82
+ }]
83
+ }
84
+
85
+ sock.print ZabbixProtocol.dump(data)
86
+ p ZabbixProtocol.load(sock.read)
87
+ #=> {"response"=>"success", "info"=>"Processed 0 Failed 1 Total 1 Seconds spent 0.000018"}
88
+ end
89
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new("spec")
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module ZabbixProtocol
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,66 @@
1
+ require "multi_json"
2
+
3
+ require "zabbix_protocol/version"
4
+
5
+ module ZabbixProtocol
6
+ class Error < StandardError; end
7
+
8
+ # http://www.zabbix.org/wiki/Docs/protocols/zabbix_agent/1.4
9
+ HEADER = "ZBXD"
10
+ VERSION = "\x01"
11
+ DATA_LEN_BYTES = 8
12
+
13
+ MIN_RESPONSE_LEN = HEADER.length + VERSION.length + DATA_LEN_BYTES
14
+
15
+ def self.dump(data)
16
+ if data.is_a?(Hash)
17
+ data = MultiJson.dump(data)
18
+ else
19
+ data = data.to_s
20
+ data << "\n" unless data =~ /\n\z/
21
+ end
22
+
23
+ [
24
+ HEADER,
25
+ VERSION,
26
+ [data.length].pack('Q'),
27
+ data
28
+ ].join
29
+ end
30
+
31
+ def self.load(res_data)
32
+ unless res_data.is_a?(String)
33
+ raise TypeError, "wrong argument type #{res_data.class} (expected String)"
34
+ end
35
+
36
+ if res_data.length < MIN_RESPONSE_LEN
37
+ raise Error, "response data is too short"
38
+ end
39
+
40
+ data = res_data.dup
41
+ header = data.slice!(0, HEADER.length)
42
+
43
+ if header != HEADER
44
+ raise Error, "invalid header: #{header.inspect}"
45
+ end
46
+
47
+ version = data.slice!(0, VERSION.length)
48
+
49
+ if version != VERSION
50
+ raise Error, "unsupported version: #{version.inspect}"
51
+ end
52
+
53
+ data_len = data.slice!(0, DATA_LEN_BYTES)
54
+ data_len = data_len.unpack("Q").first
55
+
56
+ if data_len != data.length
57
+ raise Error, "invalid data length: expected=#{data_len}, actual=#{data.length}"
58
+ end
59
+
60
+ begin
61
+ MultiJson.load(data)
62
+ rescue MultiJson::ParseError
63
+ data
64
+ end
65
+ end
66
+ end
@@ -0,0 +1 @@
1
+ require "zabbix_protocol"
@@ -0,0 +1,68 @@
1
+ describe ZabbixProtocol do
2
+ subject { described_class }
3
+
4
+ context "when request" do
5
+ it "should convert string to zabbix request" do
6
+ res = subject.dump("system.cpu.load[all,avg1]")
7
+ expect(res).to eq "ZBXD\x01\x1A\x00\x00\x00\x00\x00\x00\x00" +
8
+ "system.cpu.load[all,avg1]\n"
9
+ end
10
+
11
+ it "should convert hash to zabbix request" do
12
+ req_data = {
13
+ "request" => "sender.data",
14
+ "data" => [{
15
+ "host" => "LinuxDB3",
16
+ "key" => "db.connections",
17
+ "value" => "43"
18
+ }]
19
+ }
20
+
21
+ res = subject.dump(req_data)
22
+ expect(res).to eq "ZBXD\x01Z\x00\x00\x00\x00\x00\x00\x00" +
23
+ '{"request":"sender.data","data":[{"host":"LinuxDB3","key":"db.connections","value":"43"}]}'
24
+ end
25
+ end
26
+
27
+ context "when response" do
28
+ it "should parse float" do
29
+ res_data = "ZBXD\x01\b\x00\x00\x00\x00\x00\x00\x001.000000"
30
+ data = subject.load(res_data)
31
+ expect(data).to eq 1.0
32
+ end
33
+
34
+ it "should parse hash" do
35
+ res_data = "ZBXD\x01$\x00\x00\x00\x00\x00\x00\x00{\n\t\"response\":\"success\",\n\t\"data\":[]}"
36
+ data = subject.load(res_data)
37
+ expect(data).to eq({"response"=>"success", "data"=>[]})
38
+ end
39
+ end
40
+
41
+ context "when error happen" do
42
+ it "raise error when response is not string" do
43
+ expect {
44
+ subject.load(1)
45
+ }.to raise_error "wrong argument type Fixnum (expected String)"
46
+ end
47
+
48
+ it "raise error when response is too short string" do
49
+ expect {
50
+ subject.load("x")
51
+ }.to raise_error "response data is too short"
52
+ end
53
+
54
+ it "raise error when unsupported version" do
55
+ expect {
56
+ res_data = "ZBXD\x02\b\x00\x00\x00\x00\x00\x00\x001.000000"
57
+ subject.load(res_data)
58
+ }.to raise_error 'unsupported version: "\\u0002"'
59
+ end
60
+
61
+ it "raise error when invalid data length" do
62
+ expect {
63
+ res_data = "ZBXD\x01\x00\x00\x00\x00\x00\x00\x00\x001.000000"
64
+ subject.load(res_data)
65
+ }.to raise_error "invalid data length: expected=0, actual=8"
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zabbix_protocol/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zabbix_protocol"
8
+ spec.version = ZabbixProtocol::VERSION
9
+ spec.authors = ["Genki Sugawara"]
10
+ spec.email = ["sgwr_dts@yahoo.co.jp"]
11
+ spec.summary = %q{Zabbix protocols builder/parser.}
12
+ spec.description = %q{Zabbix protocols builder/parser.}
13
+ spec.homepage = "https://github.com/winebarrel/zabbix_protocol"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "multi_json"
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", ">= 3.0.0"
25
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbix_protocol
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
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: bundler
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: rake
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
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ description: Zabbix protocols builder/parser.
70
+ email:
71
+ - sgwr_dts@yahoo.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/zabbix_protocol.rb
84
+ - lib/zabbix_protocol/version.rb
85
+ - spec/spec_helper.rb
86
+ - spec/zabbix_protocol_spec.rb
87
+ - zabbix_protocol.gemspec
88
+ homepage: https://github.com/winebarrel/zabbix_protocol
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.14
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Zabbix protocols builder/parser.
112
+ test_files:
113
+ - spec/spec_helper.rb
114
+ - spec/zabbix_protocol_spec.rb