CloudWatch Anwendungsbeispiele SDK für Ruby - AWS SDKfür Ruby

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

CloudWatch Anwendungsbeispiele SDK für Ruby

Die folgenden Codebeispiele zeigen Ihnen, wie Sie mithilfe von AWS SDK for Ruby with Aktionen ausführen und allgemeine Szenarien implementieren CloudWatch.

Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Aktionen zeigen Ihnen zwar, wie Sie einzelne Servicefunktionen aufrufen, aber Sie können Aktionen im Kontext der zugehörigen Szenarien sehen.

Jedes Beispiel enthält einen Link zum vollständigen Quellcode, in dem Sie Anweisungen zum Einrichten und Ausführen des Codes im Kontext finden.

Themen

Aktionen

Das folgende Codebeispiel zeigt die VerwendungDescribeAlarms.

SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

require 'aws-sdk-cloudwatch' # Lists the names of available Amazon CloudWatch alarms. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @example # list_alarms(Aws::CloudWatch::Client.new(region: 'us-east-1')) def list_alarms(cloudwatch_client) response = cloudwatch_client.describe_alarms if response.metric_alarms.count.positive? response.metric_alarms.each do |alarm| puts alarm.alarm_name end else puts 'No alarms found.' end rescue StandardError => e puts "Error getting information about alarms: #{e.message}" end
  • APIEinzelheiten finden Sie DescribeAlarmsunter AWS SDK for Ruby APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungDescribeAlarmsForMetric.

SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

# # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @example # describe_metric_alarms(Aws::CloudWatch::Client.new(region: 'us-east-1')) def describe_metric_alarms(cloudwatch_client) response = cloudwatch_client.describe_alarms if response.metric_alarms.count.positive? response.metric_alarms.each do |alarm| puts '-' * 16 puts "Name: #{alarm.alarm_name}" puts "State value: #{alarm.state_value}" puts "State reason: #{alarm.state_reason}" puts "Metric: #{alarm.metric_name}" puts "Namespace: #{alarm.namespace}" puts "Statistic: #{alarm.statistic}" puts "Period: #{alarm.period}" puts "Unit: #{alarm.unit}" puts "Eval. periods: #{alarm.evaluation_periods}" puts "Threshold: #{alarm.threshold}" puts "Comp. operator: #{alarm.comparison_operator}" if alarm.key?(:ok_actions) && alarm.ok_actions.count.positive? puts 'OK actions:' alarm.ok_actions.each do |a| puts " #{a}" end end if alarm.key?(:alarm_actions) && alarm.alarm_actions.count.positive? puts 'Alarm actions:' alarm.alarm_actions.each do |a| puts " #{a}" end end if alarm.key?(:insufficient_data_actions) && alarm.insufficient_data_actions.count.positive? puts 'Insufficient data actions:' alarm.insufficient_data_actions.each do |a| puts " #{a}" end end puts 'Dimensions:' if alarm.key?(:dimensions) && alarm.dimensions.count.positive? alarm.dimensions.each do |d| puts " Name: #{d.name}, Value: #{d.value}" end else puts ' None for this alarm.' end end else puts 'No alarms found.' end rescue StandardError => e puts "Error getting information about alarms: #{e.message}" end # Example usage: def run_me region = '' # Print usage information and then stop. if ARGV[0] == '--help' || ARGV[0] == '-h' puts 'Usage: ruby cw-ruby-example-show-alarms.rb REGION' puts 'Example: ruby cw-ruby-example-show-alarms.rb us-east-1' exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? region = 'us-east-1' # Otherwise, use the values as specified at the command prompt. else region = ARGV[0] end cloudwatch_client = Aws::CloudWatch::Client.new(region: region) puts 'Available alarms:' describe_metric_alarms(cloudwatch_client) end run_me if $PROGRAM_NAME == __FILE__

Das folgende Codebeispiel zeigt die VerwendungDisableAlarmActions.

SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

# Disables an alarm in Amazon CloudWatch. # # Prerequisites. # # - The alarm to disable. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @param alarm_name [String] The name of the alarm to disable. # @return [Boolean] true if the alarm was disabled; otherwise, false. # @example # exit 1 unless alarm_actions_disabled?( # Aws::CloudWatch::Client.new(region: 'us-east-1'), # 'ObjectsInBucket' # ) def alarm_actions_disabled?(cloudwatch_client, alarm_name) cloudwatch_client.disable_alarm_actions(alarm_names: [alarm_name]) true rescue StandardError => e puts "Error disabling alarm actions: #{e.message}" false end # Example usage: def run_me alarm_name = 'ObjectsInBucket' alarm_description = 'Objects exist in this bucket for more than 1 day.' metric_name = 'NumberOfObjects' # Notify this Amazon Simple Notification Service (Amazon SNS) topic when # the alarm transitions to the ALARM state. alarm_actions = ['arn:aws:sns:us-east-1:111111111111:Default_CloudWatch_Alarms_Topic'] namespace = 'AWS/S3' statistic = 'Average' dimensions = [ { name: "BucketName", value: "amzn-s3-demo-bucket" }, { name: 'StorageType', value: 'AllStorageTypes' } ] period = 86_400 # Daily (24 hours * 60 minutes * 60 seconds = 86400 seconds). unit = 'Count' evaluation_periods = 1 # More than one day. threshold = 1 # One object. comparison_operator = 'GreaterThanThreshold' # More than one object. # Replace us-west-2 with the AWS Region you're using for Amazon CloudWatch. region = 'us-east-1' cloudwatch_client = Aws::CloudWatch::Client.new(region: region) if alarm_created_or_updated?( cloudwatch_client, alarm_name, alarm_description, metric_name, alarm_actions, namespace, statistic, dimensions, period, unit, evaluation_periods, threshold, comparison_operator ) puts "Alarm '#{alarm_name}' created or updated." else puts "Could not create or update alarm '#{alarm_name}'." end if alarm_actions_disabled?(cloudwatch_client, alarm_name) puts "Alarm '#{alarm_name}' disabled." else puts "Could not disable alarm '#{alarm_name}'." end end run_me if $PROGRAM_NAME == __FILE__

Das folgende Codebeispiel zeigt die VerwendungListMetrics.

SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

# Lists available metrics for a metric namespace in Amazon CloudWatch. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @param metric_namespace [String] The namespace of the metric. # @example # list_metrics_for_namespace( # Aws::CloudWatch::Client.new(region: 'us-east-1'), # 'SITE/TRAFFIC' # ) def list_metrics_for_namespace(cloudwatch_client, metric_namespace) response = cloudwatch_client.list_metrics(namespace: metric_namespace) if response.metrics.count.positive? response.metrics.each do |metric| puts " Metric name: #{metric.metric_name}" if metric.dimensions.count.positive? puts ' Dimensions:' metric.dimensions.each do |dimension| puts " Name: #{dimension.name}, Value: #{dimension.value}" end else puts 'No dimensions found.' end end else puts "No metrics found for namespace '#{metric_namespace}'. " \ 'Note that it could take up to 15 minutes for recently-added metrics ' \ 'to become available.' end end # Example usage: def run_me metric_namespace = 'SITE/TRAFFIC' # Replace us-west-2 with the AWS Region you're using for Amazon CloudWatch. region = 'us-east-1' cloudwatch_client = Aws::CloudWatch::Client.new(region: region) # Add three datapoints. puts 'Continuing...' unless datapoint_added_to_metric?( cloudwatch_client, metric_namespace, 'UniqueVisitors', 'SiteName', 'example.com', 5_885.0, 'Count' ) puts 'Continuing...' unless datapoint_added_to_metric?( cloudwatch_client, metric_namespace, 'UniqueVisits', 'SiteName', 'example.com', 8_628.0, 'Count' ) puts 'Continuing...' unless datapoint_added_to_metric?( cloudwatch_client, metric_namespace, 'PageViews', 'PageURL', 'example.html', 18_057.0, 'Count' ) puts "Metrics for namespace '#{metric_namespace}':" list_metrics_for_namespace(cloudwatch_client, metric_namespace) end run_me if $PROGRAM_NAME == __FILE__
  • APIEinzelheiten finden Sie ListMetricsunter AWS SDK for Ruby APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungPutMetricAlarm.

SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

# Creates or updates an alarm in Amazon CloudWatch. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @param alarm_name [String] The name of the alarm. # @param alarm_description [String] A description about the alarm. # @param metric_name [String] The name of the metric associated with the alarm. # @param alarm_actions [Array] A list of Strings representing the # Amazon Resource Names (ARNs) to execute when the alarm transitions to the # ALARM state. # @param namespace [String] The namespace for the metric to alarm on. # @param statistic [String] The statistic for the metric. # @param dimensions [Array] A list of dimensions for the metric, specified as # Aws::CloudWatch::Types::Dimension. # @param period [Integer] The number of seconds before re-evaluating the metric. # @param unit [String] The unit of measure for the statistic. # @param evaluation_periods [Integer] The number of periods over which data is # compared to the specified threshold. # @param theshold [Float] The value against which the specified statistic is compared. # @param comparison_operator [String] The arithmetic operation to use when # comparing the specified statistic and threshold. # @return [Boolean] true if the alarm was created or updated; otherwise, false. # @example # exit 1 unless alarm_created_or_updated?( # Aws::CloudWatch::Client.new(region: 'us-east-1'), # 'ObjectsInBucket', # 'Objects exist in this bucket for more than 1 day.', # 'NumberOfObjects', # ['arn:aws:sns:us-east-1:111111111111:Default_CloudWatch_Alarms_Topic'], # 'AWS/S3', # 'Average', # [ # { # name: 'BucketName', # value: 'amzn-s3-demo-bucket' # }, # { # name: 'StorageType', # value: 'AllStorageTypes' # } # ], # 86_400, # 'Count', # 1, # 1, # 'GreaterThanThreshold' # ) def alarm_created_or_updated?( cloudwatch_client, alarm_name, alarm_description, metric_name, alarm_actions, namespace, statistic, dimensions, period, unit, evaluation_periods, threshold, comparison_operator ) cloudwatch_client.put_metric_alarm( alarm_name: alarm_name, alarm_description: alarm_description, metric_name: metric_name, alarm_actions: alarm_actions, namespace: namespace, statistic: statistic, dimensions: dimensions, period: period, unit: unit, evaluation_periods: evaluation_periods, threshold: threshold, comparison_operator: comparison_operator ) true rescue StandardError => e puts "Error creating alarm: #{e.message}" false end
  • APIEinzelheiten finden Sie PutMetricAlarmunter AWS SDK for Ruby APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungPutMetricData.

SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

require 'aws-sdk-cloudwatch' # Adds a datapoint to a metric in Amazon CloudWatch. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @param metric_namespace [String] The namespace of the metric to add the # datapoint to. # @param metric_name [String] The name of the metric to add the datapoint to. # @param dimension_name [String] The name of the dimension to add the # datapoint to. # @param dimension_value [String] The value of the dimension to add the # datapoint to. # @param metric_value [Float] The value of the datapoint. # @param metric_unit [String] The unit of measurement for the datapoint. # @return [Boolean] # @example # exit 1 unless datapoint_added_to_metric?( # Aws::CloudWatch::Client.new(region: 'us-east-1'), # 'SITE/TRAFFIC', # 'UniqueVisitors', # 'SiteName', # 'example.com', # 5_885.0, # 'Count' # ) def datapoint_added_to_metric?( cloudwatch_client, metric_namespace, metric_name, dimension_name, dimension_value, metric_value, metric_unit ) cloudwatch_client.put_metric_data( namespace: metric_namespace, metric_data: [ { metric_name: metric_name, dimensions: [ { name: dimension_name, value: dimension_value } ], value: metric_value, unit: metric_unit } ] ) puts "Added data about '#{metric_name}' to namespace " \ "'#{metric_namespace}'." true rescue StandardError => e puts "Error adding data about '#{metric_name}' to namespace " \ "'#{metric_namespace}': #{e.message}" false end
  • APIEinzelheiten finden Sie PutMetricDataunter AWS SDK for Ruby APIReferenz.