Class: Reacter::Message
- Inherits:
-
Object
show all
- Defined in:
- lib/reacter/parser.rb,
lib/reacter/message.rb,
lib/reacter/parsers/json.rb,
lib/reacter/parsers/tsdb.rb,
lib/reacter/parsers/collectd.rb,
lib/reacter/parsers/graphite.rb,
lib/reacter/parsers/kairosdb.rb
Defined Under Namespace
Classes: CollectdParser, GraphiteParser, JsonParser, KairosdbParser, Parser, TsdbParser
Constant Summary
collapse
- DEFAULT_SOURCE_NAME =
'default'
- DEFAULT_FORMAT =
:json
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(message = {}) ⇒ Message
Returns a new instance of Message.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/reacter/message.rb', line 10
def initialize(message={})
@_data = {
:raw => message,
:time => (Time.now.to_i * 1000),
:attributes => {},
:metric => nil,
:source => DEFAULT_SOURCE_NAME
}.merge(message)
if not @_data.has_key?(:event) and not @_data.has_key?(:value)
@_data[:event] = true
else
@_data[:event] = false
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
38
39
40
|
# File 'lib/reacter/message.rb', line 38
def method_missing(name, *args)
@_data[name.to_sym] or args.first
end
|
Class Method Details
.dump(message, format = nil) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/reacter/message.rb', line 61
def dump(message, format=nil)
load_parsers() unless defined?(@@_parsers)
if @@_parsers
format = (@_format or DEFAULT_FORMAT) unless format
if @@_parsers.has_key?(format.to_sym)
return @@_parsers[format.to_sym].dump(message)
end
end
return nil
end
|
43
44
45
|
# File 'lib/reacter/message.rb', line 43
def format=(format)
@_format = format
end
|
.load_parsers ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/reacter/message.rb', line 47
def load_parsers()
unless defined?(@@_parsers)
@@_parsers = {}
Dir[File.join(File.dirname(__FILE__), 'parsers', '*.rb')].each do |i|
i = File.basename(i,'.rb')
require "reacter/parsers/#{i}"
@@_parsers[i.to_sym] = Message.const_get("#{i.capitalize}Parser")
@@_parsers[i.to_sym].configure(Reacter.get("parsers.#{i.downcase}", {}))
end
end
end
|
.parse(body, format = nil) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/reacter/message.rb', line 75
def parse(body, format=nil)
load_parsers()
format = @_format unless format
body = body.lines if body.is_a?(String)
messages = []
if format.nil?
if body.respond_to?(:each)
body.each do |message|
if message.is_a?(String)
next if message.strip.chomp.empty?
@@_parsers.each do |name, parser|
if parser.detected?(message)
m = parser.parse(message)
(m.is_a?(Array) ? messages += m : messages << m)
next
end
end
elsif message.is_a?(Hash)
messages << message
end
end
end
else
begin
m = @@_parsers[format].parse(message)
(m.is_a?(Array) ? messages += m : messages << m)
rescue
raise "Error parsing format '#{format}'"
end
end
messages.collect{|i| Message.new(i) }
end
|
Instance Method Details
#[](key) ⇒ Object
30
31
32
|
# File 'lib/reacter/message.rb', line 30
def [](key)
@_data[key.to_sym]
end
|
#[]=(key, value) ⇒ Object
34
35
36
|
# File 'lib/reacter/message.rb', line 34
def []=(key, value)
@_data[key.to_sym] = value
end
|
#to_h ⇒ Object
26
27
28
|
# File 'lib/reacter/message.rb', line 26
def to_h
@_data
end
|