You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何在AWS中创建自定义指标告警监控实例DB连接

Setting Up AWS CloudWatch Custom Metric Alert for PostgreSQL Connection Count

Got it, let's walk through how to build this custom monitoring setup step by step—this is a common use case for keeping tabs on database connection limits, so I’ll break it down clearly.

Step 1: Install and Configure the CloudWatch Agent on Your EC2 Instance

First, you need the CloudWatch Agent running on your EC2 instance to execute your netstat command and send the data to CloudWatch as a custom metric.

1.1 Attach the Required IAM Role to Your EC2 Instance

Make sure your instance has an IAM role with the CloudWatchAgentServerPolicy managed policy attached. This lets the agent send metrics to CloudWatch without hardcoding credentials.

1.2 Install the CloudWatch Agent

  • For Amazon Linux 2:
    sudo yum install amazon-cloudwatch-agent -y
    
  • For Ubuntu/Debian:
    sudo apt update && sudo apt install amazon-cloudwatch-agent -y
    

1.3 Create the Agent Configuration File

Create a config file (e.g., /opt/aws/amazon-cloudwatch-agent/etc/custom_postgres_config.json) with the following content. This config runs your netstat command (modified to count active connections) and reports it as a custom metric:

{
  "metrics": {
    "namespace": "Custom/PostgresConnections",
    "metrics_collected": {
      "custom": {
        "active_postgres_connections": {
          "command": "/bin/netstat -an | grep ESTABLISHED | grep :5432 | wc -l",
          "metrics": [
            {
              "name": "ActiveConnections",
              "unit": "Count"
            }
          ],
          "append_dimensions": {
            "InstanceId": "${aws:InstanceId}"
          }
        }
      }
    },
    "append_dimensions": {
      "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
      "ImageId": "${aws:ImageId}",
      "InstanceId": "${aws:InstanceId}",
      "InstanceType": "${aws:InstanceType}"
    }
  }
}
  • Note: I added ESTABLISHED to the grep to exclude the listening port (since you probably care about active connections, not just the open port). Adjust the command if you want to include other connection states.
  • Double-check the path to netstat on your instance (run which netstat to confirm) and update it in the config if needed.

1.4 Start and Enable the Agent

sudo amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/custom_postgres_config.json -s
sudo systemctl enable amazon-cloudwatch-agent

Step 2: Verify the Custom Metric in CloudWatch

Wait 5-10 minutes, then head to the CloudWatch console:

  1. Go to Metrics > All metrics
  2. Look for the Custom/PostgresConnections namespace
  3. Select the metric linked to your InstanceId and named ActiveConnections—you should see data points if everything works as expected.

Step 3: Create the CloudWatch Alert

Now set up an alert to notify you when connections hit your threshold:

  1. In CloudWatch, go to Alarms > All alarms > Create alarm
  2. Select your custom metric (Custom/PostgresConnections > ActiveConnections)
  3. Configure the threshold:
    • For example, set "Whenever ActiveConnections is > 100" (adjust this number based on your database's connection limit)
    • Choose the evaluation period (e.g., 1 out of 1 periods)
  4. Configure notification:
    • Select an existing SNS topic (or create a new one) to send emails, SMS, or trigger other actions (like Lambda functions)
  5. Name your alarm (e.g., "High Postgres Connection Count on EC2 Instance") and save it.

Pro Tips

  • If you want to monitor connections from multiple instances, the append_dimensions in the config will include InstanceId, so you can create a dashboard to view all instances at once.
  • Test the netstat command manually on your instance first to make sure it returns the correct count before setting up the agent.
  • If you don’t see metrics in CloudWatch, check the agent logs at /var/log/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log for errors.

内容的提问来源于stack exchange,提问作者thakral

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:50:20