@@ -63,39 +63,25 @@ def close
63
63
end
64
64
65
65
def parse_file ( filename )
66
- begin
67
- data = JSON . parse ( File . read ( filename ) )
66
+ data = JSON . parse ( File . read ( filename ) )
68
67
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
78
77
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 " )
99
85
end
100
86
end
101
87
@@ -327,14 +313,21 @@ def influx_metrics(data, timestamp, parent_key = nil)
327
313
$net_output = NetworkOutput . new ( url )
328
314
end
329
315
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 )
335
322
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
338
331
end
339
332
340
333
$net_output. close if $options[ :host ]
0 commit comments