bin/metrics-asg.rb in sensu-plugins-aws-10.0.2 vs bin/metrics-asg.rb in sensu-plugins-aws-10.0.3

- old
+ new

@@ -20,11 +20,11 @@ # USAGE: # # # NOTES: # Returns latency statistics by default. You can specify any valid ASG metric type, see -# http://http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/as-metricscollected.html +# https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/as-metricscollected.html # # LICENSE: # Peter Hoppe <peter.hoppe.extern@bertelsmann.de> # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. @@ -43,12 +43,11 @@ long: '--name ASG_NAME' option :scheme, description: 'Metric naming scheme, text to prepend to metric', short: '-s SCHEME', - long: '--scheme SCHEME', - default: '' + long: '--scheme SCHEME' option :fetch_age, description: 'How long ago to fetch metrics for', short: '-f AGE', long: '--fetch_age', @@ -60,14 +59,14 @@ short: '-m METRIC', long: '--metric', default: 'GroupInServiceInstances' option :statistic, - description: 'Statistics type', + description: 'Statistic type', short: '-t STATISTIC', long: '--statistic', - default: '' + default: 'Sum' option :aws_region, short: '-r AWS_REGION', long: '--aws-region REGION', description: 'AWS Region (defaults to us-east-1).', @@ -99,11 +98,11 @@ cloud_watch.get_metric_statistics( namespace: 'AWS/AutoScaling', metric_name: metric_name, dimensions: [ { - name: 'AutoScaling', + name: 'AutoScalingGroupName', value: asg_name } ], statistics: [value], start_time: config[:end_time] - config[:period], @@ -112,41 +111,42 @@ ) end def print_statistics(asg_name, statistics) result = {} - static_value = {} statistics.each do |key, static| r = cloud_watch_metric(key, static, asg_name) - static_value['AutoScalingGroup.' + asg_name + '.' + key + '.' + static] = static - result['AutoScalingGroup.' + asg_name + '.' + key + '.' + static] = r[:datapoints][0] unless r[:datapoints][0].nil? + keys = + if config[:scheme].nil? + [] + else + [config[:scheme]] + end + keys.concat ['AutoScalingGroup', asg_name.tr(' ', '_'), key, static] + + result[keys.join('.')] = r[:datapoints].first unless r[:datapoints].first.nil? end result.each do |key, value| - output key.downcase.to_s, value[static_value[key].downcase], value[:timestamp].to_i + output key.downcase.to_s, value[config[:statistic].downcase], value[:timestamp].to_i end end def run - if config[:statistic] == '' - default_statistic_per_metric = { - 'GroupMinSize' => 'Sum', - 'GroupMaxSize' => 'Sum', - 'GroupDesiredCapacity' => 'Sum', - 'GroupInServiceInstances' => 'Sum', - 'GroupPendingInstances' => 'Sum', - 'GroupStandbyInstances' => 'Sum', - 'GroupTerminatingInstances' => 'Sum', - 'GroupTotalInstances' => 'Sum' - } - statistic = default_statistic_per_metric - else - statistic = config[:statistic] - end + statistic = { + 'GroupMinSize' => config[:statistic], + 'GroupMaxSize' => config[:statistic], + 'GroupDesiredCapacity' => config[:statistic], + 'GroupInServiceInstances' => config[:statistic], + 'GroupPendingInstances' => config[:statistic], + 'GroupStandbyInstances' => config[:statistic], + 'GroupTerminatingInstances' => config[:statistic], + 'GroupTotalInstances' => config[:statistic] + } begin if config[:asgname].nil? - asg.describe_auto_scaling_groups.auto_scaling_groups.each do |autoascalinggroup| - print_statistics(autoascalinggroup.auto_scaling_group_name, statistic) + asg.describe_auto_scaling_groups.auto_scaling_groups.each do |autoscalinggroup| + print_statistics(autoscalinggroup.auto_scaling_group_name, statistic) end else print_statistics(config[:asgname], statistic) end ok