Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit f2a9a44

Browse files
committed
Split output from file parsing
This patch moves all output functionality out of the parse_file method which loads and transforms data. This split makes it easier to control where output is sent without being entangled with the logic that generated the output.
1 parent 8f86a37 commit f2a9a44

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

json2graphite.rb

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,25 @@ def close
6363
end
6464

6565
def parse_file(filename)
66-
begin
67-
data = JSON.parse(File.read(filename))
66+
data = JSON.parse(File.read(filename))
6867

69-
# Newer versions of the log tool insert a timestamp field into the JSON.
70-
if data['timestamp']
71-
timestamp = Time.parse(data.delete('timestamp'))
72-
parent_key = nil
73-
else
74-
timestamp = get_timestamp(filename)
75-
# The only data supported in the older log tool comes from puppetserver.
76-
parent_key = 'servers.' + get_hoststr(filename) + '.puppetserver'
77-
end
68+
# Newer versions of the log tool insert a timestamp field into the JSON.
69+
if data['timestamp']
70+
timestamp = Time.parse(data.delete('timestamp'))
71+
parent_key = nil
72+
else
73+
timestamp = get_timestamp(filename)
74+
# The only data supported in the older log tool comes from puppetserver.
75+
parent_key = 'servers.' + get_hoststr(filename) + '.puppetserver'
76+
end
7877

79-
if $options[:output_format] == 'influxdb'
80-
array = influx_metrics(data, timestamp, parent_key)
81-
$net_output.write(array.join("\n"))
82-
else
83-
array = metrics(data, timestamp, parent_key)
84-
lines = array.map do |item|
85-
item.split('\n')
86-
end.flatten
87-
lines.each do |line|
88-
if $options[:host]
89-
# IS THIS NECESSARY??? I HAVE NO IDEA!!!
90-
#sleep 0.0001
91-
$net_output.write("#{line}\n\r\n")
92-
else
93-
puts(line)
94-
end
95-
end
96-
end
97-
rescue => e
98-
STDERR.puts "ERROR: #{filename}: #{e.message}"
78+
case $options[:output_format]
79+
when 'influxdb'
80+
influx_metrics(data, timestamp, parent_key).join("\n")
81+
else
82+
metrics(data, timestamp, parent_key).map do |item|
83+
item.split('\n')
84+
end.flatten.join("\r\n")
9985
end
10086
end
10187

@@ -327,14 +313,21 @@ def influx_metrics(data, timestamp, parent_key = nil)
327313
$net_output = NetworkOutput.new(url)
328314
end
329315

330-
if $options[:pattern]
331-
Dir.glob($options[:pattern]).each do |filename|
332-
parse_file(filename)
333-
end
334-
end
316+
data_files = ARGV
317+
data_files += Dir.glob($options[:pattern]) if $options[:pattern]
318+
319+
data_files.each do |filename|
320+
begin
321+
converted_data = parse_file(filename)
335322

336-
while filename = ARGV.shift
337-
parse_file(filename)
323+
if $options[:host]
324+
$net_output.write(converted_data)
325+
else
326+
STDOUT.write(converted_data)
327+
end
328+
rescue => e
329+
STDERR.puts "ERROR: #{filename}: #{e.message}"
330+
end
338331
end
339332

340333
$net_output.close if $options[:host]

0 commit comments

Comments
 (0)