diff --git a/README.md b/README.md index 5860716..09f58b1 100644 --- a/README.md +++ b/README.md @@ -64,3 +64,13 @@ When `--netcat` is used with InfluxDB output, data will be sent to port 8086. Th ``` ./json2graphite.rb ~/Downloads/logdump/puppetserver/*.json --convert-to influxdb --netcat localhost --influx-db pe-metrics ``` + +### Pipe data into json2graphite.rb + +You can pipe the output of the metrics script from pe_metric_curl_cron_jobs directly into json2graphite.rb such as: + +``` +/opt/puppetlabs/pe_metric_curl_cron_jobs/scripts/tk_metrics --metrics_type puppetdb | ./json2graphite.rb +``` + +However, you cannot pipe metrics data in and read a file in the same invocation of the script. diff --git a/json2graphite.rb b/json2graphite.rb index bb55f57..2b61a77 100755 --- a/json2graphite.rb +++ b/json2graphite.rb @@ -65,6 +65,10 @@ def close def parse_file(filename) data = JSON.parse(File.read(filename)) + parse_input(data) +end + +def parse_input( data ) # Newer versions of the log tool insert a timestamp field into the JSON. if data['timestamp'] timestamp = Time.parse(data.delete('timestamp')) @@ -316,9 +320,17 @@ def influx_metrics(data, timestamp, parent_key = nil) $net_output = NetworkOutput.new(url) end -data_files = ARGV +data_files = [] data_files += Dir.glob($options[:pattern]) if $options[:pattern] +#http://ruby-doc.org/core-1.9.3/ARGF.html#method-i-filename +while ARGF.filename != '-' + filename = ARGF.filename + data_files += [filename] + ARGF.skip + break if filename == ARGF.filename +end + data_files.each do |filename| begin converted_data = parse_file(filename) @@ -333,4 +345,18 @@ def influx_metrics(data, timestamp, parent_key = nil) end end +if ARGF.filename == '-' + begin + converted_data = parse_input( JSON.parse(ARGF.read) ) + + if $options[:host] + $net_output.write(converted_data) + else + STDOUT.write(converted_data) + end + rescue => e + STDERR.puts "ERROR: During read from STDIN: #{e.message}" + end +end + $net_output.close if $options[:host]