zabbix_protocol 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zabbix_protocol/version.rb +1 -1
- data/lib/zabbix_protocol.rb +17 -17
- data/spec/zabbix_protocol_spec.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b24c8ba95636d6a46255007187c03a3092e14a8c
|
4
|
+
data.tar.gz: 7482b470bee62de53a1af853ec0818e160627410
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b631c8bcaf4dda3dc6ff73e569973153761fbf46ffde4cdd6684f9a19d39a5612ba947f3b2b67045a55acc13a2ec7dad39e6b8f89f847178e36518f509abca77
|
7
|
+
data.tar.gz: 9e45a2f07297fc37620c569a5b623f9c512d63060f649c8212b4f1cb6f0de858fb287655e615e14b5a6c128207869784580e419b74b23270171117ade7fce5ee
|
data/lib/zabbix_protocol.rb
CHANGED
@@ -8,22 +8,22 @@ module ZabbixProtocol
|
|
8
8
|
# http://www.zabbix.org/wiki/Docs/protocols/zabbix_agent/1.4
|
9
9
|
ZABBIX_HEADER = "ZBXD"
|
10
10
|
ZABBIX_VERSION = "\x01"
|
11
|
-
|
11
|
+
PAYLOAD_LEN_BYTES = 8
|
12
12
|
|
13
|
-
|
13
|
+
MIN_DATA_LEN = ZABBIX_HEADER.length + ZABBIX_VERSION.length + PAYLOAD_LEN_BYTES
|
14
14
|
|
15
|
-
def self.dump(
|
16
|
-
if
|
17
|
-
|
15
|
+
def self.dump(payload)
|
16
|
+
if payload.is_a?(Hash)
|
17
|
+
payload = MultiJson.dump(payload)
|
18
18
|
else
|
19
|
-
|
19
|
+
payload = payload.to_s
|
20
20
|
end
|
21
21
|
|
22
22
|
[
|
23
23
|
ZABBIX_HEADER,
|
24
24
|
ZABBIX_VERSION,
|
25
|
-
[
|
26
|
-
|
25
|
+
[payload.length].pack('Q'),
|
26
|
+
payload
|
27
27
|
].join
|
28
28
|
end
|
29
29
|
|
@@ -32,32 +32,32 @@ module ZabbixProtocol
|
|
32
32
|
raise TypeError, "wrong argument type #{data.class} (expected String)"
|
33
33
|
end
|
34
34
|
|
35
|
-
if data.length <
|
35
|
+
if data.length < MIN_DATA_LEN
|
36
36
|
raise Error, "data length is too short (data: #{data.inspect})"
|
37
37
|
end
|
38
38
|
|
39
|
-
|
40
|
-
header =
|
39
|
+
sliced = data.dup
|
40
|
+
header = sliced.slice!(0, ZABBIX_HEADER.length)
|
41
41
|
|
42
42
|
if header != ZABBIX_HEADER
|
43
43
|
raise Error, "invalid header: #{header.inspect} (data: #{data.inspect})"
|
44
44
|
end
|
45
45
|
|
46
|
-
version =
|
46
|
+
version = sliced.slice!(0, ZABBIX_VERSION.length)
|
47
47
|
|
48
48
|
if version != ZABBIX_VERSION
|
49
49
|
raise Error, "unsupported version: #{version.inspect} (data: #{data.inspect})"
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
|
52
|
+
payload_len = sliced.slice!(0, PAYLOAD_LEN_BYTES)
|
53
|
+
payload_len = payload_len.unpack("Q").first
|
54
54
|
|
55
|
-
if
|
56
|
-
raise Error, "invalid
|
55
|
+
if payload_len != sliced.length
|
56
|
+
raise Error, "invalid payload length: expected=#{payload_len}, actual=#{sliced.length} (data: #{data.inspect})"
|
57
57
|
end
|
58
58
|
|
59
59
|
begin
|
60
|
-
MultiJson.load(
|
60
|
+
MultiJson.load(sliced)
|
61
61
|
rescue MultiJson::ParseError
|
62
62
|
data
|
63
63
|
end
|
@@ -55,14 +55,14 @@ describe ZabbixProtocol do
|
|
55
55
|
expect {
|
56
56
|
res_data = "ZBXD\x02\b\x00\x00\x00\x00\x00\x00\x001.000000"
|
57
57
|
subject.load(res_data)
|
58
|
-
}.to raise_error 'unsupported version: "
|
58
|
+
}.to raise_error 'unsupported version: "\u0002" (data: "ZBXD\u0002\b\u0000\u0000\u0000\u0000\u0000\u0000\u00001.000000")'
|
59
59
|
end
|
60
60
|
|
61
|
-
it "raise error when invalid
|
61
|
+
it "raise error when invalid payload length" do
|
62
62
|
expect {
|
63
63
|
res_data = "ZBXD\x01\x00\x00\x00\x00\x00\x00\x00\x001.000000"
|
64
64
|
subject.load(res_data)
|
65
|
-
}.to raise_error 'invalid
|
65
|
+
}.to raise_error 'invalid payload length: expected=0, actual=8 (data: "ZBXD\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u00001.000000")'
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|