如何使用 API、CLI 或 Terraform 为 RabbitMQ 代理获取 AmazonMQ 节点

问题描述

我正在尝试为每个 RabbitMQ 代理的 systemcpuUtilizaiton 创建 AWS Cloudwatch 警报 节点通过 terraform。要创建 AWS Cloudwatch alarm,我需要提供 AWS docs 中提到的维度(节点名称和代理)。

因此,我希望从 AWS(通过 CLI、API 或 terraform获取 rabbitMQ 代理节点名称

请注意:我可以在 AWS Cloudwatch 控制台中看到每个代理节点的矩阵,但不能从 API、SDK 或 CLI 中看到。

我浏览了以下链接,但没有得到任何有用的信息https://awscli.amazonaws.com/v2/documentation/api/latest/reference/mq/index.html#cli-aws-mq https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/mq_broker

如果我遗漏了什么,请告诉我。

解决方法

用于 cloudwatch 维度的 AWS MQ 节点名称似乎没有通过 AWS API 公开,但节点名称在知道 IP 地址的情况下是可以预测的。我相信这可以用来为警报构造有效的节点名称。

data "aws_region" "current" {}

resource "aws_mq_broker" "example" {
  ...
}

resource "aws_cloudwatch_metric_alarm" "bat" {
  for_each = toset([
    for instance in aws_mq_broker.example.instances : instance.ip_address
  ])

  alarm_name          = "terraform-test-foobar5"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  metric_name         = "SystemCpuUtilization"
  namespace           = "AWS/AmazonMQ"
  period              = "120"
  statistic           = "Average"
  threshold           = "80"

  dimensions = {
    Broker = aws_mq_broker.example.name
    Node   = "rabbitmq@ip-${replace(each.value,".","-")}.${data.aws_region.current.name}.compute.internal"
  }
}
,

我已将上述问题提交给 AWS 支持,以下是解决方案:

首先来自 AWS 团队的回应,AmazonMQ-RabbitMQ 代理节点由 AWS 内部管理,目前未通过 API 或 SDK 公开。

因此,无法通过 API 或 SDK 获取 Rabbit MQ 代理节点名称因此无法在 Rabbit MQ 代理节点的 systemCpuUtilizaitonas node name are required dimensions for creating the alert 上直接创建 cloudwatch 警报。

有两种替代方案

  1. 查询 RabbitMQ API 以获取节点名称
  2. 使用 prometheus/cloudwatch-exporter,从可用节点名称的 Cloud Watch 获取矩阵详细信息。

我使用了第二种方法,下面的值文件来获取我们感兴趣的矩阵

prometheus-cloudwatch-exporter:
    namespace: monitoring
    enabled: true
    override:
      metrics:
        alb: false
        rds: false
        # ... based on requirement
      alerts:
        ec2: false # based on requirement
      additionalMetrics: |-
        # below configuration will fetch the martics,# containing Rabbit MQ broker node names
        - aws_namespace: AWS/AmazonMQ
          aws_metric_name: SystemCpuUtilization
          aws_dimensions: [Broker,Node]
          aws_statistics: [Average]

如果一切都配置正确,您应该能够aws_amazonmq_system_cpu_utilization_average在 prometheus 中运行,如下所示。现在使用 Prometheus 警报管理器在此矩阵之上创建警报。 enter image description here