zabbix 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/zabbix.rb +3 -0
- data/lib/zabbix/sender.rb +50 -32
- data/lib/zabbix/sender/buffer.rb +24 -0
- data/test/test_zabbix.rb +12 -3
- data/test/zabbix_agentd.conf +1 -1
- data/zabbix.gemspec +57 -0
- metadata +8 -7
- data/.gitignore +0 -21
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/zabbix.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module Zabbix
|
2
2
|
end
|
3
3
|
|
4
|
+
require "socket"
|
5
|
+
require "yajl"
|
4
6
|
require "zabbix/agent/configuration"
|
5
7
|
require "zabbix/sender"
|
6
8
|
require "zabbix/sender/easy"
|
9
|
+
require "zabbix/sender/buffer"
|
7
10
|
|
8
11
|
## we could use zabbix-sender, but that appears to be unecessary; we can instead
|
9
12
|
## simply talk to the zabbit-server. we get there via the parent.
|
data/lib/zabbix/sender.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'socket'
|
2
|
-
require 'yajl'
|
3
|
-
|
4
1
|
class Zabbix::Sender
|
5
2
|
|
6
3
|
DEFAULT_SERVER_PORT = 10051
|
@@ -28,9 +25,16 @@ class Zabbix::Sender
|
|
28
25
|
|
29
26
|
|
30
27
|
def connect
|
31
|
-
TCPSocket.new(@host, @port)
|
28
|
+
@socket ||= TCPSocket.new(@host, @port)
|
32
29
|
end
|
33
30
|
|
31
|
+
def disconnect
|
32
|
+
if @socket
|
33
|
+
@socket.close unless @socket.closed?
|
34
|
+
@socket = nil
|
35
|
+
end
|
36
|
+
return true
|
37
|
+
end
|
34
38
|
|
35
39
|
def send_start(key, opts={})
|
36
40
|
send_data("#{key}.start", 1, opts)
|
@@ -48,35 +52,49 @@ class Zabbix::Sender
|
|
48
52
|
|
49
53
|
def send_data(key, value, opts={})
|
50
54
|
return false unless configured?
|
51
|
-
host
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
# construct json
|
56
|
-
json = Yajl::Encoder.encode({
|
57
|
-
:request => "agent data",
|
58
|
-
:clock => ts,
|
59
|
-
:data => [{
|
60
|
-
:host => host,
|
61
|
-
:key => key,
|
62
|
-
:value => value,
|
63
|
-
:clock => ts
|
64
|
-
}]
|
65
|
-
})
|
55
|
+
host = opts[:host]
|
56
|
+
clock = opts[:ts ]
|
57
|
+
return send_zabbix_request([ cons_zabbix_data_element(host, key, value, clock) ])
|
58
|
+
end
|
66
59
|
|
67
|
-
|
68
|
-
socket.write "ZBXD\x01"
|
69
|
-
socket.write [json.size].pack('q')
|
70
|
-
socket.write json
|
71
|
-
socket.flush
|
60
|
+
private
|
72
61
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
62
|
+
def cons_zabbix_data_element(host, key, value, clock=Time.now.to_i)
|
63
|
+
return {
|
64
|
+
:host => host,
|
65
|
+
:key => key,
|
66
|
+
:value => value,
|
67
|
+
:clock => clock
|
68
|
+
}
|
69
|
+
end
|
79
70
|
|
80
|
-
|
81
|
-
|
71
|
+
def send_zabbix_request(data)
|
72
|
+
status = false
|
73
|
+
request = Yajl::Encoder.encode({
|
74
|
+
:request => 'agent data' ,
|
75
|
+
:clock => Time.now.to_i,
|
76
|
+
:data => data
|
77
|
+
})
|
78
|
+
|
79
|
+
begin
|
80
|
+
sock = connect
|
81
|
+
sock.write "ZBXD\x01"
|
82
|
+
sock.write [request.size].pack('q')
|
83
|
+
sock.write request
|
84
|
+
sock.flush
|
85
|
+
|
86
|
+
# FIXME: check header to make sure it's the message we expect?
|
87
|
+
header = sock.read(5)
|
88
|
+
len = sock.read(8)
|
89
|
+
len = len.unpack('q').shift
|
90
|
+
response = Yajl::Parser.parse(sock.read(len))
|
91
|
+
status = true if response['response'] == 'success'
|
92
|
+
rescue => e
|
93
|
+
## FIXME
|
94
|
+
ensure
|
95
|
+
disconnect
|
96
|
+
end
|
97
|
+
|
98
|
+
return status
|
99
|
+
end
|
82
100
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Zabbix::Sender::Buffer < Zabbix::Sender
|
2
|
+
def initialize(opts={})
|
3
|
+
@buffer = []
|
4
|
+
super(opts)
|
5
|
+
end
|
6
|
+
|
7
|
+
def append(key, value, opts={})
|
8
|
+
return false unless configured?
|
9
|
+
@buffer << {
|
10
|
+
:host => opts[:host] || Socket.gethostname,
|
11
|
+
:clock => opts[:ts ] || Time.now.to_i,
|
12
|
+
:key => key,
|
13
|
+
:value => value,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def flush
|
18
|
+
return false unless @buffer.size > 0
|
19
|
+
ret = send_zabbix_request(@buffer)
|
20
|
+
@buffer.clear
|
21
|
+
return ret
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/test/test_zabbix.rb
CHANGED
@@ -18,15 +18,24 @@ class TestZabbix < Test::Unit::TestCase
|
|
18
18
|
assert_equal true, zbx.configured?
|
19
19
|
end
|
20
20
|
|
21
|
-
should "send
|
21
|
+
should "should send start stop and heartbeat" do
|
22
22
|
config_file = "#{File.dirname(__FILE__)}/zabbix_agentd.conf"
|
23
23
|
zbx = Zabbix::Sender.new :config_file => config_file
|
24
24
|
key = :"foo.bar.baz"
|
25
25
|
host = "testhost.example.com"
|
26
26
|
assert_equal true, zbx.send_start(key, :host => host)
|
27
|
-
assert_equal true, zbx.send_heartbeat(key, "8==D
|
27
|
+
assert_equal true, zbx.send_heartbeat(key, "8==D", :host => host)
|
28
28
|
assert_equal true, zbx.send_stop(key, :host => host)
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
should "should bulk send data" do
|
32
|
+
config_file = "#{File.dirname(__FILE__)}/zabbix_agentd.conf"
|
33
|
+
zbx = Zabbix::Sender::Buffer.new :config_file => config_file
|
34
|
+
key = :"foo.bar.baz"
|
35
|
+
host = "testhost.example.com"
|
36
|
+
zbx.append key, "8===D", :host => host
|
37
|
+
zbx.append key, "8====D", :host => host
|
38
|
+
zbx.append key, "8=====D", :host => host
|
39
|
+
assert_equal true, zbx.flush
|
40
|
+
end
|
32
41
|
end
|
data/test/zabbix_agentd.conf
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
Server=
|
1
|
+
Server=sysmon-002.sc1.yammer.com
|
2
2
|
ServerPort=10051
|
data/zabbix.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{zabbix}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matthew Knopp"]
|
12
|
+
s.date = %q{2010-12-17}
|
13
|
+
s.description = %q{send data to zabbix from ruby}
|
14
|
+
s.email = %q{mknopp@yammer-inc.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lib/zabbix.rb",
|
26
|
+
"lib/zabbix/agent/configuration.rb",
|
27
|
+
"lib/zabbix/sender.rb",
|
28
|
+
"lib/zabbix/sender/buffer.rb",
|
29
|
+
"lib/zabbix/sender/easy.rb",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_zabbix.rb",
|
32
|
+
"test/zabbix_agentd.conf",
|
33
|
+
"zabbix.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/mhat/zabbix}
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{send data to zabbix from ruby}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_zabbix.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zabbix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matthew Knopp
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-17 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -43,7 +43,6 @@ extra_rdoc_files:
|
|
43
43
|
- README.rdoc
|
44
44
|
files:
|
45
45
|
- .document
|
46
|
-
- .gitignore
|
47
46
|
- LICENSE
|
48
47
|
- README.rdoc
|
49
48
|
- Rakefile
|
@@ -51,17 +50,19 @@ files:
|
|
51
50
|
- lib/zabbix.rb
|
52
51
|
- lib/zabbix/agent/configuration.rb
|
53
52
|
- lib/zabbix/sender.rb
|
53
|
+
- lib/zabbix/sender/buffer.rb
|
54
54
|
- lib/zabbix/sender/easy.rb
|
55
55
|
- test/helper.rb
|
56
56
|
- test/test_zabbix.rb
|
57
57
|
- test/zabbix_agentd.conf
|
58
|
+
- zabbix.gemspec
|
58
59
|
has_rdoc: true
|
59
60
|
homepage: http://github.com/mhat/zabbix
|
60
61
|
licenses: []
|
61
62
|
|
62
63
|
post_install_message:
|
63
|
-
rdoc_options:
|
64
|
-
|
64
|
+
rdoc_options: []
|
65
|
+
|
65
66
|
require_paths:
|
66
67
|
- lib
|
67
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|