drone_collectd 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ module DroneCollectd
2
+ class CollectdPacket
3
+ # part type
4
+ HOST = 0x0000
5
+ TIME = 0x0001
6
+ PLUGIN = 0x0002
7
+ PLUGIN_INSTANCE = 0x0003
8
+ TYPE = 0x0004
9
+ TYPE_INSTANCE = 0x0005
10
+ VALUES = 0x0006
11
+ INTERVAL = 0x0007
12
+ MESSAGE = 0x0100
13
+ SEVERITY = 0x0101
14
+
15
+ # data type
16
+ COUNTER = 0
17
+ GAUGE = 1
18
+ DERIVE = 2
19
+ ABSOLUTE = 3
20
+
21
+
22
+ attr_accessor :host, :time, :interval
23
+ attr_accessor :plugin, :plugin_instance
24
+ attr_accessor :type, :type_instance
25
+
26
+ def initialize
27
+ @values = []
28
+ @values_type = []
29
+ end
30
+
31
+ def add_value(type, value)
32
+ raise(ArgumentError, "unknown type: #{type}") unless CollectdPacket::const_defined?(type.to_s.upcase)
33
+ data_type = CollectdPacket::const_get(type.to_s.upcase)
34
+
35
+ @values_type << data_type
36
+ @values << value
37
+ end
38
+
39
+ def build_packet
40
+ @pkt = CollectdGenerator::string(HOST, @host)
41
+ @pkt << CollectdGenerator::number(TIME, @time)
42
+ @pkt << CollectdGenerator::number(INTERVAL, @interval)
43
+ @pkt << CollectdGenerator::string(PLUGIN, @plugin)
44
+ @pkt << CollectdGenerator::string(PLUGIN_INSTANCE, @plugin_instance)
45
+ @pkt << CollectdGenerator::string(TYPE, @type)
46
+ @pkt << CollectdGenerator::string(TYPE_INSTANCE, @type_instance)
47
+
48
+ # values part header
49
+ @pkt << [VALUES, 4 + 2 + (@values.size * 9), @values.size].pack('nnn')
50
+ # types
51
+ @pkt << @values_type.pack('C*')
52
+
53
+ # and the values
54
+ @values.each.with_index do |v, i|
55
+ case @values_type[i]
56
+ when COUNTER, ABSOLUTE, DERIVE
57
+ @pkt << [v >> 32, v & 0xffffffff].pack("NN")
58
+
59
+ when GAUGE
60
+ @pkt << [v].pack('E')
61
+
62
+ else
63
+ raise "unknown type: #{@values_type[i]}"
64
+ end
65
+ end
66
+
67
+ @pkt
68
+ end
69
+
70
+ end
71
+
72
+ module CollectdGenerator
73
+ # Encode a string (type 0, null terminated string)
74
+ def self.string(type, str)
75
+ str += "\000"
76
+ str_size = str.respond_to?(:bytesize) ? str.bytesize : str.size
77
+ [type, 4 + str_size].pack("nn") + str
78
+ end
79
+
80
+ # Encode an integer
81
+ def self.number(type, num)
82
+ [type, 12].pack("nn") + [num >> 32, num & 0xffffffff].pack("NN")
83
+ end
84
+ end
85
+
86
+ end