lib/rubix/auto_sender.rb in rubix-0.5.7 vs lib/rubix/auto_sender.rb in rubix-0.5.8
- old
+ new
@@ -128,11 +128,11 @@
# @return [Array<Rubix::Template>] the templates used to create this host
attr_accessor :templates
# @return [Array<Rubix::Application>] The applications used to create items
attr_accessor :applications
-
+
#
# == Initialization ==
#
# Create a new sender with the given +settings+.
@@ -170,11 +170,11 @@
def fast?
settings['fast']
end
# Will this sender auto-vivify hosts, groups, items, &c.?
- #
+ #
# @return [true, false]
def auto_vivify?
!fast?
end
@@ -250,13 +250,13 @@
raise ConnectionError.new("Must specify the name of a host to send data for.") unless settings['host']
raise ValidationError.new("Must define at least one host group.") if auto_vivify? && (settings['host_groups'].nil? || settings['host_groups'].empty?)
end
public
-
+
#
- # == Sending Data ==
+ # == Sending Data ==
#
# Run this sender.
#
# Will read from the correct source of data and exit the Ruby
@@ -274,25 +274,25 @@
end
exit(0)
end
protected
-
+
# Process each line of a file.
#
# @param [String] path the path to the file to process
def process_file path
f = File.new(path)
process_file_handle(f)
f.close
end
-
+
# Process each line of standard input.
def process_stdin
process_file_handle($stdin)
end
-
+
# Process each line read from the pipe.
#
# The pipe will be opened in a non-blocking read mode. This
# sender will wait 'pipe_read_sleep' seconds between successive
# empty reads.
@@ -308,11 +308,11 @@
# coming down the pipe.
sleep settings['pipe_read_sleep']
end
f.close
end
-
+
# Process each line of a given file handle.
#
# @param [File] f the file to process
def process_file_handle f
begin
@@ -332,11 +332,11 @@
end
end
end
public
-
+
# Process a single line of text.
#
# @param [String] line
def process_line line
if looks_like_json?(line)
@@ -392,11 +392,11 @@
# {'key': 'snap.crackle.pop', 'value': 8 }
# ]
# }
#
# Or when sending for another host:
- #
+ #
# {
# 'host': 'shazaam',
# 'applications': 'silly',
# 'data': [
# {'key': 'foo.bar.baz', 'value': 10},
@@ -405,16 +405,16 @@
# }
#
# @param [String] line a line of JSON data
def process_line_of_json_in_new_pipe line
begin
- json = JSON.parse(line)
- rescue JSON::ParserError => e
+ json = MultiJson.load(line)
+ rescue MultiJson::DecodeError => e
error("Malformed JSON")
return
end
-
+
data = json.delete('data')
unless data && data.is_a?(Array)
error("A line of JSON input must a have an Array key 'data'")
return
end
@@ -439,11 +439,11 @@
value = point['value']
unless key && value
warn("The elements of the 'data' Array must be Hashes with a 'key' and a 'value'")
next
end
-
+
tsv_line = [key, value].map(&:to_s).join("\t")
daughter_pipe.process_line(tsv_line)
end
end
@@ -485,11 +485,11 @@
# @param [String, Fixnum, Float] value
def ensure_item_exists key, value
item = Item.find(:key => key, :host_id => host.id)
unless item
Item.new(:key => key, :host_id => host.id, :applications => applications, :value_type => Item.value_type_from_value(value)).save
-
+
# There is a time lag of about 15-30 seconds between (successfully)
# creating an item on the Zabbix server and having the Zabbix accept
# new data for that item.
#
# If it is crucial that *every single* data point be written, dial
@@ -501,10 +501,10 @@
# If you don't care that you're going to lose the first few data
# points you send to Zabbix, then don't worry about it.
sleep settings['create_item_sleep']
end
end
-
+
# Parse the +text+ output by +zabbix_sender+.
#
# @param [String] key
# @param [String] text the output from +zabbix_sender+
# @return [Fixnum] the number of data points processed