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

Commit b877431

Browse files
committed
Allow piping metrics into the conversion script
Prior to this commit, you could not pipe output into our conversion script. The conversion script could only read files. After this commit, you can pipe output into the script OR you can read a file. You cannot pipe output in and read a file in the same invocation of the conversion script.
1 parent a1d9c2c commit b877431

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

json2graphite.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def close
6565
def parse_file(filename)
6666
data = JSON.parse(File.read(filename))
6767

68+
parse_input(data)
69+
end
70+
71+
def parse_input( data )
6872
# Newer versions of the log tool insert a timestamp field into the JSON.
6973
if data['timestamp']
7074
timestamp = Time.parse(data.delete('timestamp'))
@@ -316,9 +320,17 @@ def influx_metrics(data, timestamp, parent_key = nil)
316320
$net_output = NetworkOutput.new(url)
317321
end
318322

319-
data_files = ARGV
323+
data_files = []
320324
data_files += Dir.glob($options[:pattern]) if $options[:pattern]
321325

326+
#http://ruby-doc.org/core-1.9.3/ARGF.html#method-i-filename
327+
while ARGF.filename != '-'
328+
filename = ARGF.filename
329+
data_files += [filename]
330+
ARGF.skip
331+
break if filename == ARGF.filename
332+
end
333+
322334
data_files.each do |filename|
323335
begin
324336
converted_data = parse_file(filename)
@@ -333,4 +345,18 @@ def influx_metrics(data, timestamp, parent_key = nil)
333345
end
334346
end
335347

348+
if ARGF.filename == '-'
349+
begin
350+
converted_data = parse_input( JSON.parse(ARGF.read) )
351+
352+
if $options[:host]
353+
$net_output.write(converted_data)
354+
else
355+
STDOUT.write(converted_data)
356+
end
357+
rescue => e
358+
STDERR.puts "ERROR: During read from STDIN: #{e.message}"
359+
end
360+
end
361+
336362
$net_output.close if $options[:host]

0 commit comments

Comments
 (0)