monitoring_protocols 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe 'DataStruct' do
4
+ before do
5
+ @s1 = Class.new(MonitoringProtocols::DataStruct) do
6
+ properties :one, :two
7
+ end
8
+
9
+ @s2 = Class.new(MonitoringProtocols::DataStruct) do
10
+ properties :name, :host
11
+ end
12
+ end
13
+
14
+ it 'can be encoded with msgpack', focus: true do
15
+ obj = @s1.new(:one => 12)
16
+ data = MessagePack.pack(obj)
17
+ h = MessagePack.unpack(data)
18
+ h.class.should == Hash
19
+ h['one'].should == 12
20
+ h['two'].should == nil
21
+ end
22
+
23
+ should 'raise an error if keys are unknown' do
24
+ err = ->{
25
+ @s1.new(tarzan: 42)
26
+ }.should.raise(ArgumentError)
27
+
28
+ err.message.should.include?("tarzan")
29
+ end
30
+
31
+ should 'have disctinct attributes set for each child class' do
32
+ @s1.attributes.should == [:one, :two]
33
+ @s2.attributes.should == [:name, :host]
34
+ end
35
+
36
+ it "can merge data from multiple Hash sources" do
37
+ obj = @s1.new(:one => 12)
38
+ obj.merge_data_from!(:two => 34, :one => nil)
39
+
40
+ obj.one.should == 12
41
+ obj.two.should == 34
42
+ end
43
+
44
+ it "can merge data from multiple Object sources" do
45
+ dummy_class = Struct.new(:one, :two)
46
+
47
+ d1 = dummy_class.new(23)
48
+ d2 = dummy_class.new(nil, 56)
49
+
50
+ obj = @s1.new(d1)
51
+ obj.merge_data_from!(d2)
52
+
53
+ obj.one.should == 23
54
+ obj.two.should == 56
55
+ end
56
+
57
+ it "can merge data selectively from multiple Object sources" do
58
+ dummy_class = Struct.new(:one, :two)
59
+
60
+ d1 = dummy_class.new(23)
61
+ d2 = dummy_class.new(67, 56)
62
+
63
+ obj = @s1.new(d1)
64
+ obj.merge_data_from!(d2, [:two])
65
+
66
+ obj.one.should == 23
67
+ obj.two.should == 56
68
+ end
69
+
70
+ should 'support inheritance' do
71
+ c1 = Class.new(MonitoringProtocols::DataStruct) do
72
+ properties :one, :two
73
+ end
74
+
75
+ c2 = Class.new(c1) do
76
+ properties :three
77
+ end
78
+
79
+
80
+ obj = c2.new(one: 1, two: 2, three: 3)
81
+ c2.attributes.should == [:one, :two, :three]
82
+
83
+ obj.one.should == 1
84
+ obj.two.should == 2
85
+ obj.three.should == 3
86
+ end
87
+
88
+ end
89
+
@@ -0,0 +1,49 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe 'JSON Ruby builder' do
4
+ before do
5
+ @parser_class = MonitoringProtocols::JSON::Parser
6
+ @builder_class = MonitoringProtocols::JSON::Builder
7
+ @now = Time.new.utc
8
+ end
9
+
10
+ should 'generate compressed json for points' do
11
+ expected_hash = {
12
+ 'type' => 'datapoints',
13
+ 'host' => 'linux1',
14
+ 'app_name' => 'system',
15
+
16
+ 'cpu' => {
17
+ 'user' => 43.0,
18
+ 'sys' => 3.4
19
+ }
20
+ }
21
+
22
+ common_data = {
23
+ host: 'linux1',
24
+ app_name: 'system',
25
+ res_name: 'cpu'
26
+ }
27
+
28
+ points = [
29
+ FactoryGirl.build(:data_point, common_data.merge(
30
+ time: @now,
31
+ metric_name: 'user',
32
+ value: 43.0
33
+ )),
34
+
35
+ FactoryGirl.build(:data_point, common_data.merge(
36
+ time: @now,
37
+ metric_name: 'sys',
38
+ value: 3.4
39
+ ))
40
+ ]
41
+
42
+ b = @builder_class.new(points.dup)
43
+ ret = b.build_packet()
44
+ ret.class.should == String
45
+
46
+ ret = freeze_time(@now){ @parser_class.new.parse(ret) }
47
+ ret.should == points.sort
48
+ end
49
+ end
@@ -0,0 +1,353 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe 'JSON Ruby parser' do
4
+ before do
5
+ @parser_class = MonitoringProtocols::JSON::Parser
6
+ @now = Time.new.utc
7
+ end
8
+
9
+ describe 'first packet' do
10
+ before do
11
+ @json = Oj.dump({
12
+ 'type' => 'datapoints',
13
+ 'host' => 'hostname',
14
+ 'app_name' => 'system',
15
+ 'time' => @now.iso8601,
16
+ 'first' => true,
17
+
18
+ 'cpu' => {
19
+ 'user' => 1,
20
+ }
21
+ },
22
+ indent: -1
23
+ )
24
+ end
25
+
26
+ should 'parse buffer' do
27
+ msgs = @parser_class.parse(@json)
28
+ msgs.size.should == 1
29
+
30
+ msgs[0].class.should == MonitoringProtocols::DataPoint
31
+ msgs[0].first == true
32
+ msgs[0].host.should == 'hostname'
33
+ msgs[0].time.to_i.should == @now.to_i
34
+ msgs[0].app_name.should == 'system'
35
+ msgs[0].res_name.should == 'cpu'
36
+ msgs[0].metric_name.should == 'user'
37
+ msgs[0].value.should == 1
38
+ end
39
+ end
40
+
41
+ describe 'invalid value' do
42
+ before do
43
+ @json = Oj.dump({
44
+ 'type' => 'datapoints',
45
+ 'host' => 'hostname',
46
+ 'app_name' => 'system',
47
+ 'time' => @now.iso8601,
48
+ 'first' => true,
49
+
50
+ 'cpu' => {
51
+ 'user' => "",
52
+ }
53
+ },
54
+ indent: -1
55
+ )
56
+ end
57
+
58
+ should 'reject packet' do
59
+ err = ->{
60
+ @parser_class.parse(@json)
61
+ }.should.raise(MonitoringProtocols::ParseError)
62
+
63
+ err.message.should.include?('invalid')
64
+ end
65
+ end
66
+
67
+ describe 'with common app_name' do
68
+
69
+ describe 'One point in buffer' do
70
+ before do
71
+ @json = Oj.dump({
72
+ 'type' => 'datapoints',
73
+ 'host' => 'hostname',
74
+ 'app_name' => 'system',
75
+ 'time' => @now.iso8601,
76
+
77
+ 'cpu' => {
78
+ 'user' => 1,
79
+ }
80
+ },
81
+ indent: -1
82
+ )
83
+ end
84
+
85
+ should 'parse buffer' do
86
+ msgs = @parser_class.parse(@json)
87
+ msgs.size.should == 1
88
+
89
+ msgs[0].class.should == MonitoringProtocols::DataPoint
90
+ msgs[0].host.should == 'hostname'
91
+ msgs[0].time.to_i.should == @now.to_i
92
+ msgs[0].app_name.should == 'system'
93
+ msgs[0].res_name.should == 'cpu'
94
+ msgs[0].metric_name.should == 'user'
95
+ msgs[0].value.should == 1
96
+ end
97
+ end
98
+
99
+
100
+ describe 'Multiple points in buffer' do
101
+ before do
102
+ @json = Oj.dump({
103
+ 'type' => 'datapoints',
104
+ 'host' => 'hostname',
105
+ 'app_name' => 'system',
106
+ 'time' => @now.iso8601,
107
+
108
+ 'cpu' => {
109
+ 'user' => 1,
110
+ 'sys' => 2
111
+ },
112
+
113
+ 'memory' => {
114
+ 'total' => 4,
115
+ 'used' => 1,
116
+ 'free' => 3
117
+ }
118
+ },
119
+ indent: -1
120
+ )
121
+
122
+ end
123
+
124
+ should 'parse buffer' do
125
+ msgs = @parser_class.parse(@json)
126
+ msgs.size.should == 5
127
+
128
+ common_check = ->(m){
129
+ m.class.should == MonitoringProtocols::DataPoint
130
+ m.host.should == 'hostname'
131
+ m.time.to_i.should == @now.to_i
132
+ m.app_name.should == 'system'
133
+ }
134
+
135
+ msgs[0].tap do |m|
136
+ common_check.call(m)
137
+ m.res_name.should == 'cpu'
138
+ m.metric_name.should == 'user'
139
+ m.value.should == 1
140
+ end
141
+
142
+ msgs[1].tap do |m|
143
+ common_check.call(m)
144
+ m.res_name.should == 'cpu'
145
+ m.metric_name.should == 'sys'
146
+ m.value.should == 2
147
+ end
148
+
149
+ msgs[2].tap do |m|
150
+ common_check.call(m)
151
+ m.res_name.should == 'memory'
152
+ m.metric_name.should == 'total'
153
+ m.value.should == 4
154
+ end
155
+
156
+ msgs[3].tap do |m|
157
+ common_check.call(m)
158
+ m.res_name.should == 'memory'
159
+ m.metric_name.should == 'used'
160
+ m.value.should == 1
161
+ end
162
+
163
+ msgs[4].tap do |m|
164
+ common_check.call(m)
165
+ m.res_name.should == 'memory'
166
+ m.metric_name.should == 'free'
167
+ m.value.should == 3
168
+ end
169
+
170
+
171
+
172
+ end
173
+ end
174
+
175
+ end
176
+
177
+
178
+ describe 'with different app_name' do
179
+
180
+ describe 'One point in buffer' do
181
+ before do
182
+ @json = Oj.dump({
183
+ 'type' => 'datapoints',
184
+ 'host' => 'hostname',
185
+ 'time' => @now.iso8601,
186
+
187
+ 'system' => {
188
+ 'cpu' => {
189
+ 'user' => 1,
190
+ }
191
+ }
192
+ },
193
+ indent: -1
194
+ )
195
+ end
196
+
197
+ should 'parse buffer' do
198
+ msgs = @parser_class.parse(@json)
199
+ msgs.size.should == 1
200
+
201
+ msgs[0].class.should == MonitoringProtocols::DataPoint
202
+ msgs[0].host.should == 'hostname'
203
+ msgs[0].time.to_i.should == @now.to_i
204
+ msgs[0].app_name.should == 'system'
205
+ msgs[0].res_name.should == 'cpu'
206
+ msgs[0].metric_name.should == 'user'
207
+ msgs[0].value.should == 1
208
+ end
209
+ end
210
+
211
+
212
+ describe 'Multiple points in buffer' do
213
+ before do
214
+ @now = Time.new.utc
215
+ @json = Oj.dump({
216
+ 'type' => 'datapoints',
217
+ 'host' => 'hostname',
218
+ 'time' => @now.iso8601,
219
+
220
+ 'system2' => {
221
+ 'cpu' => {
222
+ 'user' => 45,
223
+ 'sys' => 27
224
+ }
225
+ },
226
+
227
+ 'icmp-ping' => {
228
+ "1.2.3.4" => {
229
+ "latency" => 0.0,
230
+ "loss" => 100.0
231
+ },
232
+ "192.168.0.32" => {
233
+ "latency" => 3.45,
234
+ "loss" => 4.0
235
+ }
236
+ },
237
+
238
+ 'system' => {
239
+ 'cpu' => {
240
+ 'user' => 1,
241
+ 'sys' => 2
242
+ },
243
+
244
+ 'memory' => {
245
+ 'total' => 4,
246
+ 'used' => 1,
247
+ 'free' => 3
248
+ }
249
+ }
250
+ },
251
+ indent: -1
252
+ )
253
+
254
+ end
255
+
256
+ should 'parse buffer' do
257
+ msgs = @parser_class.parse(@json)
258
+ msgs.size.should == 11
259
+
260
+ common_check = ->(m, app_name = 'system'){
261
+ m.class.should == MonitoringProtocols::DataPoint
262
+ m.host.should == 'hostname'
263
+ m.time.to_i.should == @now.to_i
264
+ m.app_name.should == app_name
265
+ }
266
+
267
+ index = -1
268
+
269
+ msgs[index += 1].tap do |m|
270
+ common_check.call(m, 'system2')
271
+ m.res_name.should == 'cpu'
272
+ m.metric_name.should == 'user'
273
+ m.value.should == 45
274
+ end
275
+
276
+ msgs[index += 1].tap do |m|
277
+ common_check.call(m, 'system2')
278
+ m.res_name.should == 'cpu'
279
+ m.metric_name.should == 'sys'
280
+ m.value.should == 27
281
+ end
282
+
283
+ msgs[index += 1].tap do |m|
284
+ common_check.call(m, 'icmp-ping')
285
+ m.res_name.should == '1.2.3.4'
286
+ m.metric_name.should == 'latency'
287
+ m.value.should == 0.0
288
+ end
289
+
290
+ msgs[index += 1].tap do |m|
291
+ common_check.call(m, 'icmp-ping')
292
+ m.res_name.should == '1.2.3.4'
293
+ m.metric_name.should == 'loss'
294
+ m.value.should == 100.0
295
+ end
296
+
297
+ msgs[index += 1].tap do |m|
298
+ common_check.call(m, 'icmp-ping')
299
+ m.res_name.should == '192.168.0.32'
300
+ m.metric_name.should == 'latency'
301
+ m.value.should == 3.45
302
+ end
303
+
304
+ msgs[index += 1].tap do |m|
305
+ common_check.call(m, 'icmp-ping')
306
+ m.res_name.should == '192.168.0.32'
307
+ m.metric_name.should == 'loss'
308
+ m.value.should == 4.0
309
+ end
310
+
311
+
312
+ msgs[index += 1].tap do |m|
313
+ common_check.call(m)
314
+ m.res_name.should == 'cpu'
315
+ m.metric_name.should == 'user'
316
+ m.value.should == 1
317
+ end
318
+
319
+ msgs[index += 1].tap do |m|
320
+ common_check.call(m)
321
+ m.res_name.should == 'cpu'
322
+ m.metric_name.should == 'sys'
323
+ m.value.should == 2
324
+ end
325
+
326
+ msgs[index += 1].tap do |m|
327
+ common_check.call(m)
328
+ m.res_name.should == 'memory'
329
+ m.metric_name.should == 'total'
330
+ m.value.should == 4
331
+ end
332
+
333
+ msgs[index += 1].tap do |m|
334
+ common_check.call(m)
335
+ m.res_name.should == 'memory'
336
+ m.metric_name.should == 'used'
337
+ m.value.should == 1
338
+ end
339
+
340
+ msgs[index += 1].tap do |m|
341
+ common_check.call(m)
342
+ m.res_name.should == 'memory'
343
+ m.metric_name.should == 'free'
344
+ m.value.should == 3
345
+ end
346
+
347
+ end
348
+ end
349
+
350
+ end
351
+
352
+
353
+ end