@@ -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,7 @@ module SystemMetrics
398399
399400 metrics_json = metrics_to_json ( metrics_data )
400401
402+ STDOUT . puts ( metrics_json ) if @print_output
401403 write_metrics_to_file ( metrics_json )
402404 end
403405 end
@@ -427,12 +429,13 @@ if $PROGRAM_NAME == __FILE__
427429 * process_expression (-e, --process_expression): #{ PROCESS_EXPRESSION_DEFAULT }
428430 * metrics_dir (-m, --metrics_dir): #{ METRICS_DIR_DEFAULT }
429431 * verbose (-v, --verbose): False
432+ * Print to STDOUT (--[no-]print): False
430433 DEFAULTS
431434
432435 options = { }
433436
434437 OptionParser . new { |opts |
435- opts . banner = 'Usage: generate_system_stats.rb [options]'
438+ opts . banner = 'Usage: system_metrics [options]'
436439
437440 opts . on ( '-h' , '--help' , 'Display the help text' ) do
438441 puts DESCRIPTION
@@ -462,6 +465,7 @@ if $PROGRAM_NAME == __FILE__
462465 options [ :metrics_dir ] = metrics_dir
463466 end
464467 opts . on ( '-v' , '--verbose' , String , 'Enable Verbose output' ) { options [ :verbose ] = true }
468+ opts . on ( '--[no-]print' , 'Print to STDOUT' ) { |p | options [ :print ] = p }
465469 } . parse!
466470
467471 if options [ :metric_type ]
@@ -478,6 +482,7 @@ if $PROGRAM_NAME == __FILE__
478482 process_expression = options [ :process_expression ] || PROCESS_EXPRESSION_DEFAULT
479483 metrics_dir = options [ :metrics_dir ] || METRICS_DIR_DEFAULT
480484 verbose = options [ :verbose ] || false
485+ print_output = options [ :print ] || false
481486
482487 if options [ :polling_interval ] || options [ :file_interval ]
483488 options_error = 'Polling interval must be less than file interval'
@@ -493,11 +498,12 @@ if $PROGRAM_NAME == __FILE__
493498 * process_expression #{ process_expression }
494499 * metrics_dir: #{ metrics_dir }
495500 * verbose: #{ verbose }
501+ * print: #{ print_output }
496502 SETTINGS
497503 puts OPTION_SETTINGS
498504 end
499505
500506 obj = SystemMetrics ::GenerateSystemMetrics . new ( polling_interval , file_interval , metric_type ,
501- process_expression , metrics_dir , verbose )
507+ process_expression , metrics_dir , verbose , print_output )
502508 obj . generate_metrics
503509end
0 commit comments