|
4 | 4 | # puppetlabs-puppet-metrics-viewer/json2graphite.rb |
5 | 5 | # puppetlabs-puppet_metrics_collector/files/json2timeseriesdb |
6 | 6 |
|
| 7 | +require 'fcntl' |
7 | 8 | require 'json' |
8 | 9 | require 'time' |
9 | 10 | require 'optparse' |
@@ -349,44 +350,43 @@ end |
349 | 350 | data_files = [] |
350 | 351 | data_files += Dir.glob($options[:pattern]) if $options[:pattern] |
351 | 352 |
|
352 | | -# Collect JSON files to process from ARGF. |
353 | | -# http://ruby-doc.org/core-1.9.3/ARGF.html#method-i-filename |
| 353 | +# Collect JSON files to process from ARGV. |
354 | 354 |
|
355 | | -while ARGF.filename != '-' |
356 | | - filename = ARGF.filename |
357 | | - data_files += [filename] |
358 | | - ARGF.skip |
359 | | - break if filename == ARGF.filename |
| 355 | +data_files += ARGV |
| 356 | + |
| 357 | +# Validate STDIN. |
| 358 | + |
| 359 | +data_files_includes_stdin = data_files.include?('-') |
| 360 | +stdin_provided = STDIN.fcntl(Fcntl::F_GETFL, 0) == 0 |
| 361 | + |
| 362 | +# Guard against waiting indefinately for non-existant STDIN. |
| 363 | +if data_files_includes_stdin && !stdin_provided |
| 364 | + STDERR.puts "ERROR: STDIN specified via '-' but STDIN not provided" |
| 365 | + exit 1 |
360 | 366 | end |
361 | 367 |
|
362 | | -# Process collected JSON files. |
| 368 | +# Guard against not reading STDIN. |
| 369 | +if !data_files_includes_stdin && stdin_provided |
| 370 | + STDERR.puts "WARNING: STDIN provided but not specified via '-'" |
| 371 | + data_files += ['-'] |
| 372 | +end |
363 | 373 |
|
| 374 | +# Process collected JSON files (including '-' to process data from STDIN). |
| 375 | + |
364 | 376 | data_files.each do |filename| |
365 | 377 | begin |
366 | | - converted_data = parse_file(filename) |
367 | | - if $options[:host] |
368 | | - $net_output.write(converted_data) |
| 378 | + if filename == '-' |
| 379 | + converted_data = STDIN.each_line.map { |l| parse_input( JSON.parse(l) )}.flatten.join("\n") |
369 | 380 | else |
370 | | - STDOUT.puts(converted_data) |
| 381 | + converted_data = parse_file(filename) |
371 | 382 | end |
372 | | - rescue => e |
373 | | - STDERR.puts "ERROR: #{filename}: #{e.message}" |
374 | | - end |
375 | | -end |
376 | | - |
377 | | -# Process JSON data from STDIN. |
378 | | - |
379 | | -if ARGF.filename == '-' |
380 | | - begin |
381 | | - input = ARGF.read |
382 | | - converted_data = input.lines.map { |l| parse_input( JSON.parse(l) )} |
383 | 383 | if $options[:host] |
384 | | - $net_output.write(converted_data.flatten.join("\n")) |
| 384 | + $net_output.write(converted_data) |
385 | 385 | else |
386 | 386 | STDOUT.puts(converted_data) |
387 | 387 | end |
388 | 388 | rescue => e |
389 | | - STDERR.puts "ERROR: During read from STDIN: #{e.message}" |
| 389 | + STDERR.puts "ERROR: #{filename}: #{e.message}" |
390 | 390 | end |
391 | 391 | end |
392 | 392 |
|
|
0 commit comments