@@ -54,13 +54,14 @@ module SystemMetrics
5454 #
5555 # @return [void]
5656 def initialize ( polling_interval , file_interval , metric_type , process_expression , metrics_dir ,
57- verbose = false )
57+ verbose = false , print_output = false )
5858 @polling_interval = polling_interval
5959 @file_interval = file_interval
6060 @metric_type = metric_type
6161 @process_expression = process_expression
6262 @metrics_dir = metrics_dir
6363 @verbose = verbose
64+ @print_output = print_output
6465
6566 @hostname = `hostname` . strip
6667 puts "Hostname is: #{ @hostname } " if @verbose
@@ -398,6 +399,9 @@ module SystemMetrics
398399
399400 metrics_json = metrics_to_json ( metrics_data )
400401
402+ if @print_output != false then
403+ STDOUT . puts ( metrics_json )
404+ end
401405 write_metrics_to_file ( metrics_json )
402406 end
403407 end
@@ -427,12 +431,13 @@ if $PROGRAM_NAME == __FILE__
427431 * process_expression (-e, --process_expression): #{ PROCESS_EXPRESSION_DEFAULT }
428432 * metrics_dir (-m, --metrics_dir): #{ METRICS_DIR_DEFAULT }
429433 * verbose (-v, --verbose): False
434+ * Print to STDOUT (--[no-]print): False
430435 DEFAULTS
431436
432437 options = { }
433438
434439 OptionParser . new { |opts |
435- opts . banner = ' Usage: generate_system_stats.rb [options]'
440+ opts . banner = " Usage: system_metrics [options]"
436441
437442 opts . on ( '-h' , '--help' , 'Display the help text' ) do
438443 puts DESCRIPTION
@@ -461,7 +466,8 @@ if $PROGRAM_NAME == __FILE__
461466 'The puppet_metrics_collector output directory' ) do |metrics_dir |
462467 options [ :metrics_dir ] = metrics_dir
463468 end
464- opts . on ( '-v' , '--verbose' , String , 'Enable Verbose output' ) { options [ :verbose ] = true }
469+ opts . on ( "-v" , "--verbose" , String , "Enable Verbose output" ) { options [ :verbose ] = true }
470+ opts . on ( '--[no-]print' , 'Print to STDOUT' ) { |p | options [ :print ] = p }
465471 } . parse!
466472
467473 if options [ :metric_type ]
@@ -478,6 +484,7 @@ if $PROGRAM_NAME == __FILE__
478484 process_expression = options [ :process_expression ] || PROCESS_EXPRESSION_DEFAULT
479485 metrics_dir = options [ :metrics_dir ] || METRICS_DIR_DEFAULT
480486 verbose = options [ :verbose ] || false
487+ print_output = options [ :print ] || false
481488
482489 if options [ :polling_interval ] || options [ :file_interval ]
483490 options_error = 'Polling interval must be less than file interval'
@@ -493,11 +500,12 @@ if $PROGRAM_NAME == __FILE__
493500 * process_expression #{ process_expression }
494501 * metrics_dir: #{ metrics_dir }
495502 * verbose: #{ verbose }
503+ * print: #{ print_output }
496504 SETTINGS
497505 puts OPTION_SETTINGS
498506 end
499507
500508 obj = SystemMetrics ::GenerateSystemMetrics . new ( polling_interval , file_interval , metric_type ,
501- process_expression , metrics_dir , verbose )
509+ process_expression , metrics_dir , verbose , print_output )
502510 obj . generate_metrics
503511end
0 commit comments