monitoring_protocols 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'factory_girl'
2
+
3
+ FactoryGirl.define do
4
+ factory :collectd_data_common do
5
+ time { Time.now }
6
+ host "localhost"
7
+ plugin "memory"
8
+ plugin_instance nil
9
+
10
+ type "memory"
11
+ type_instance "active"
12
+ end
13
+
14
+ factory :collectd_data_point, :parent => :collectd_data_common, :class => "MonitoringProtocols::Collectd::NetworkMessage" do
15
+ values { [rand(200)] }
16
+ interval 10
17
+ end
18
+
19
+ factory :collectd_notification, :parent => :collectd_data_common, :class => "MonitoringProtocols::Collectd::NetworkMessage" do
20
+ severity 1
21
+ message "notification message"
22
+ end
23
+
24
+
25
+ factory :data_common do
26
+ time { Time.now }
27
+ host "localhost"
28
+ app_name "system"
29
+ res_name "memory"
30
+ metric_name "active"
31
+
32
+ initialize_with{ new(time: time) }
33
+ end
34
+
35
+ factory :data_point, :parent => :data_common, :class => "MonitoringProtocols::DataPoint" do
36
+ value { rand(200) }
37
+ end
38
+
39
+ factory :notification, :parent => :data_common, :class => "MonitoringProtocols::Notification" do
40
+ severity :error
41
+ message "notification message"
42
+ end
43
+
44
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'eetee'
5
+
6
+ $LOAD_PATH.unshift( File.expand_path('../../lib' , __FILE__) )
7
+ require 'monitoring_protocols'
8
+
9
+ require 'eetee/ext/mocha'
10
+ require 'eetee/ext/time'
11
+ # require 'eetee/ext/em'
12
+
13
+ require File.expand_path('../factories', __FILE__)
14
+
15
+ # Thread.abort_on_exception = true
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe 'Collectd Builder' do
4
+ before do
5
+ @builder = MonitoringProtocols::Collectd::Builder.new
6
+
7
+ @builder.host = "localhost"
8
+ @builder.time = 1
9
+ @builder.interval = 10
10
+ @builder.plugin = "plugin"
11
+ @builder.plugin_instance = "plugin_instance"
12
+ @builder.type = "type"
13
+ @builder.type_instance = "type_instance"
14
+ @builder.add_value(:counter, 42)
15
+ @builder.add_value(:gauge, 5.24)
16
+ end
17
+
18
+ it 'can generate a packet' do
19
+ expected = [
20
+ "\x00\x00\x00\x0elocalhost\x00", # host
21
+ "\x00\x01\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x01", # time
22
+ "\x00\x07\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x0a", # interval
23
+ "\x00\x02\x00\x0bplugin\x00", # plugin
24
+ "\x00\x03\x00\x14plugin_instance\x00", # plugin_instance
25
+ "\x00\x04\x00\x09type\x00", # type
26
+ "\x00\x05\x00\x12type_instance\x00", # type_instance
27
+ "\x00\x06\x00\x18\x00\x02", # value headers
28
+ "\00\01", # types
29
+ "\x00\x00\x00\x00\x00\x00\x00\x2a", # value
30
+ "\xf6\x28\x5c\x8f\xc2\xf5\x14\x40" # value2
31
+ ]
32
+
33
+ if "".respond_to?(:encode)
34
+ expected = expected.map{|s| s.force_encoding('ASCII-8BIT') }
35
+ end
36
+
37
+ data = @builder.build_packet
38
+
39
+ data[0,14].should == expected[0]
40
+ data[14,12].should == expected[1]
41
+ data[26,12].should == expected[2]
42
+ data[38,11].should == expected[3]
43
+ data[49,20].should == expected[4]
44
+ data[69, 9].should == expected[5]
45
+ data[78,18].should == expected[6]
46
+
47
+ data[96,6].should == expected[7]
48
+
49
+ # types
50
+ data[102,2].should == expected[8]
51
+
52
+ # value1
53
+ data[104,8].should == expected[9]
54
+ # value2
55
+ data[112,8].should == expected[10]
56
+ end
57
+
58
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe 'Collectd Network Message' do
4
+
5
+ describe 'data point' do
6
+ before do
7
+ @time_obj = Time.now
8
+ @time = @time_obj.to_i
9
+
10
+ @point = FactoryGirl.build(:collectd_data_point,
11
+ time: @time,
12
+ type: 'memory',
13
+ type_instance: 'active'
14
+ )
15
+
16
+ @point.time.should == @time
17
+ end
18
+
19
+ should 'return formatted plugin' do
20
+ @point.plugin = "plugin"
21
+ @point.plugin_instance = nil
22
+
23
+ @point.plugin_display.should == "plugin"
24
+
25
+ @point.plugin_instance = "instance"
26
+ @point.plugin_display.should == "plugin/instance"
27
+ end
28
+
29
+ should 'return formatted type' do
30
+ @point.type = "type"
31
+ @point.type_instance = nil
32
+
33
+ @point.type_display.should == "type"
34
+
35
+ @point.type_instance = "instance"
36
+ @point.type_display.should == "type/instance"
37
+ end
38
+
39
+
40
+ should 'convert datapoint to DataPoint' do
41
+ d = @point.convert_content()
42
+ d.class.should == Array
43
+ d.size.should == 1
44
+
45
+ d = d.first
46
+ d.class.should == MonitoringProtocols::DataPoint
47
+
48
+ d.first.should != true
49
+ d.host.should == @point.host
50
+ d.time.gmt_offset.should == 0
51
+ d.time.iso8601().should == @time_obj.getutc().iso8601()
52
+
53
+ d.app_name.should == @point.plugin
54
+ d.res_name.should == @point.type
55
+ d.metric_name.should == @point.type_instance
56
+ d.value.should == d.value
57
+ end
58
+
59
+ end
60
+
61
+
62
+ describe 'notification' do
63
+ before do
64
+ @point = FactoryGirl.build(:collectd_notification,
65
+ type: 'memory',
66
+ type_instance: 'active',
67
+
68
+ severity: 1,
69
+ message: 'ahhhhhhh'
70
+ )
71
+ end
72
+
73
+
74
+ should 'convert notification to Notification' do
75
+ d = @point.convert_content()
76
+ d.class.should == Array
77
+ d.size.should == 1
78
+ d = d.first
79
+ d.class.should == MonitoringProtocols::Notification
80
+
81
+ d.host.should == @point.host
82
+ d.app_name.should == @point.plugin
83
+ d.res_name.should == @point.type
84
+ d.metric_name.should == @point.type_instance
85
+
86
+ d.severity.should == :warn
87
+ d.message.should == "ahhhhhhh"
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
@@ -0,0 +1,212 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ class EEtee::Context
4
+ def builder
5
+ MonitoringProtocols::Collectd::Builder
6
+ end
7
+ end
8
+
9
+ describe 'Collectd Ruby parser' do
10
+ before do
11
+ @builder_class = MonitoringProtocols::Collectd::Builder
12
+ @packet = ->(cmd, *args){
13
+ @builder_class.send(cmd, *args)
14
+ }
15
+
16
+ @parser_class = MonitoringProtocols::Collectd::Parser
17
+ end
18
+
19
+ describe 'Simple packets' do
20
+ it 'can parse numbers' do
21
+ type, val, buffer = @parser_class.parse_part( builder.number(1, 122) )
22
+ buffer.should == ""
23
+ val.should == 122
24
+
25
+ type, val, _ = @parser_class.parse_part( builder.number(1, 2500) )
26
+ val.should == 2500
27
+
28
+ type, val, _ = @parser_class.parse_part( builder.number(1, 356798) )
29
+ val.should == 356798
30
+ end
31
+
32
+ should 'parse strings' do
33
+ type, str, _ = @parser_class.parse_part( builder.string(0, "hostname1") )
34
+ str.should == 'hostname1'
35
+
36
+ type, str, _ = @parser_class.parse_part( builder.string(0, "string with spaces") )
37
+ str.should == 'string with spaces'
38
+
39
+ type, str, _ = @parser_class.parse_part( builder.string(0, "a really long string with many words in it") )
40
+ str.should == 'a really long string with many words in it'
41
+ end
42
+
43
+ should 'parse values' do
44
+ buffer = builder.values(
45
+ [builder::COUNTER, builder::DERIVE, builder::DERIVE],
46
+ [1034, -4567, 34]
47
+ )
48
+
49
+ type, values, rest = @parser_class.parse_part( buffer )
50
+ rest.should == ""
51
+ values.should == [1034, -4567, 34]
52
+ end
53
+ end
54
+
55
+ describe 'One notification in buffer' do
56
+ before do
57
+ @now = Time.new.to_i
58
+
59
+ @pkt = ""
60
+ @pkt << builder.number(1, @now)
61
+ @pkt << builder.string(0, 'hostname')
62
+
63
+ @pkt << builder.string(2, 'plugin')
64
+ @pkt << builder.string(3, 'plugin_inst')
65
+ @pkt << builder.string(4, 'type')
66
+ @pkt << builder.string(5, 'type_inst')
67
+ @pkt << builder.number(257, 2) # severity
68
+ @pkt << builder.string(256, 'a message')
69
+ end
70
+
71
+ should 'parse the notification' do
72
+ data, rest = @parser_class.parse_packet(@pkt)
73
+
74
+ rest.should == ""
75
+
76
+ data.class.should == MonitoringProtocols::Collectd::NetworkMessage
77
+ data.host.should == 'hostname'
78
+ data.time.should == @now
79
+ data.plugin.should == 'plugin'
80
+ data.plugin_instance.should == 'plugin_inst'
81
+ data.type.should == 'type'
82
+ data.type_instance.should == 'type_inst'
83
+ data.message.should == 'a message'
84
+ data.severity.should == 2
85
+
86
+ end
87
+ end
88
+
89
+ describe 'One packet in buffer' do
90
+ before do
91
+ @now = Time.new.to_i
92
+ @interval = 10
93
+
94
+ pkt = builder.new
95
+ pkt.time = @now
96
+ pkt.host = 'hostname'
97
+ pkt.interval = @interval
98
+ pkt.plugin = 'plugin'
99
+ pkt.plugin_instance = 'plugin_inst'
100
+ pkt.type = 'type'
101
+ pkt.type_instance = 'type_inst'
102
+
103
+ pkt.add_value(:counter, 1034)
104
+ pkt.add_value(:gauge, 3.45)
105
+
106
+ @pkt = pkt.build_packet
107
+ end
108
+
109
+ should 'parse buffer' do
110
+ data, rest = @parser_class.parse_packet(@pkt)
111
+
112
+ data.class.should == MonitoringProtocols::Collectd::NetworkMessage
113
+ data.host.should == 'hostname'
114
+ data.time.should == @now
115
+ data.interval.should == @interval
116
+ data.plugin.should == 'plugin'
117
+ data.plugin_instance.should == 'plugin_inst'
118
+ data.type.should == 'type'
119
+ data.type_instance.should == 'type_inst'
120
+
121
+ data.values.size.should == 2
122
+ data.values[0].should == 1034
123
+ data.values[1].should == 3.45
124
+
125
+ rest.should == ""
126
+ end
127
+ end
128
+
129
+ describe "Multiple packets in buffer" do
130
+ before do
131
+ @now = Time.new.to_i
132
+ @interval = 10
133
+
134
+ pkt = builder.new
135
+ pkt.time = @now
136
+ pkt.host = 'hostname'
137
+ pkt.interval = @interval
138
+ pkt.plugin = 'plugin'
139
+ pkt.plugin_instance = 'plugin_inst'
140
+ pkt.type = 'type'
141
+ pkt.type_instance = 'type_inst'
142
+
143
+ pkt.add_value(:counter, 1034)
144
+ pkt.add_value(:gauge, 3.45)
145
+
146
+ @pkt = pkt.build_packet
147
+
148
+ @pkt << builder.string(2, 'plugin2')
149
+ @pkt << builder.string(3, 'plugin2_inst')
150
+ @pkt << builder.string(4, 'type2')
151
+ @pkt << builder.string(5, 'type2_inst')
152
+
153
+ @pkt << builder.values([builder::COUNTER], [42])
154
+
155
+
156
+ @pkt << builder.string(5, 'type21_inst')
157
+ @pkt << builder.values([builder::GAUGE], [3.1415927])
158
+ end
159
+
160
+ should 'parse buffer' do
161
+ data = @parser_class.parse(@pkt)
162
+
163
+ data.size.should == 3
164
+
165
+ data[0].class.should == MonitoringProtocols::Collectd::NetworkMessage
166
+
167
+ data[0].host.should == 'hostname'
168
+ data[0].time.should == @now
169
+ data[0].interval.should == @interval
170
+ data[0].plugin.should == 'plugin'
171
+ data[0].plugin_instance.should == 'plugin_inst'
172
+ data[0].type.should == 'type'
173
+ data[0].type_instance.should == 'type_inst'
174
+ data[0].values.size.should == 2
175
+ data[0].values[0].should == 1034
176
+ data[0].values[1].should == 3.45
177
+
178
+ data[1].host.should == 'hostname'
179
+ data[1].time.should == @now
180
+ data[1].interval.should == @interval
181
+ data[1].plugin.should == 'plugin2'
182
+ data[1].plugin_instance.should == 'plugin2_inst'
183
+ data[1].type.should == 'type2'
184
+ data[1].type_instance.should == 'type2_inst'
185
+ data[1].values.size.should == 1
186
+ data[1].values[0].should == 42
187
+
188
+ data[2].host.should == 'hostname'
189
+ data[2].time.should == @now
190
+ data[2].interval.should == @interval
191
+ data[2].plugin.should == 'plugin2'
192
+ data[2].plugin_instance.should == 'plugin2_inst'
193
+ data[2].type.should == 'type2'
194
+ data[2].type_instance.should == 'type21_inst'
195
+ data[2].values.size.should == 1
196
+ data[2].values[0].should == 3.1415927
197
+
198
+ end
199
+
200
+ # should 'parse using feed interface' do
201
+ # parser = @parser.new
202
+
203
+ # ret = parser.feed(@pkt[0,20])
204
+ # ret.size.should == []
205
+
206
+ # ret = parser.feed(@pkt[21..-1])
207
+ # ret.size.should == 2
208
+ # end
209
+
210
+ end
211
+
212
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe 'Core' do
4
+ should 'return parser for collectd' do
5
+ MonitoringProtocols.get_parser(:collectd).class.should ==
6
+ MonitoringProtocols::Collectd::Parser
7
+ end
8
+
9
+ should 'return builder for collectd' do
10
+ MonitoringProtocols.get_builder(:collectd).class.should ==
11
+ MonitoringProtocols::Collectd::Builder
12
+ end
13
+
14
+
15
+ should 'return nil for unknown protocol' do
16
+ MonitoringProtocols.get_parser(:i_am_invalid).should == nil
17
+ end
18
+
19
+ end