aws-cli监控告警:使用CLI设置和管理CloudWatch告警
概述
Amazon CloudWatch是AWS的核心监控服务,而aws-cli提供了强大的命令行接口来管理CloudWatch告警。通过CLI,您可以自动化监控告警的创建、管理和维护,实现基础设施监控的DevOps化。
本文将深入介绍如何使用aws-cli设置和管理CloudWatch告警,涵盖从基础配置到高级用法的完整流程。
环境准备与配置
安装aws-cli
# 使用pip安装aws-cli
pip install awscli
# 验证安装
aws --version
配置AWS凭证
# 基本配置
aws configure
AWS Access Key ID: YOUR_ACCESS_KEY
AWS Secret Access Key: YOUR_SECRET_KEY
Default region name: us-east-1
Default output format: json
验证配置
# 测试配置是否正确
aws sts get-caller-identity
CloudWatch告警核心概念
告警组件解析
关键参数说明
| 参数 | 描述 | 示例值 |
|---|---|---|
--metric-name | 监控的指标名称 | CPUUtilization |
--namespace | 指标命名空间 | AWS/EC2 |
--statistic | 统计方法 | Average, Maximum, Minimum |
--period | 数据点周期(秒) | 300 (5分钟) |
--threshold | 告警阈值 | 70.0 |
--comparison-operator | 比较运算符 | GreaterThanThreshold |
--evaluation-periods | 评估周期数 | 2 |
创建CloudWatch告警
基础告警创建
# 创建CPU使用率告警
aws cloudwatch put-metric-alarm \
--alarm-name "high-cpu-usage" \
--alarm-description "警报当CPU使用率超过70%" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 70.0 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=InstanceId,Value=i-1234567890abcdef0 \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:MyTopic \
--unit Percent
多维度告警配置
# 包含多个维度的告警
aws cloudwatch put-metric-alarm \
--alarm-name "multi-dimension-alarm" \
--metric-name RequestCount \
--namespace AWS/ApplicationELB \
--statistic Sum \
--period 60 \
--threshold 1000 \
--comparison-operator GreaterThanThreshold \
--dimensions \
Name=LoadBalancer,Value=app/my-loadbalancer/1234567890abcdef \
Name=TargetGroup,Value=targetgroup/my-targets/1234567890abcdef \
--evaluation-periods 1
复合告警创建
# 创建复合告警(需要多个指标条件)
aws cloudwatch put-composite-alarm \
--alarm-name "composite-cpu-memory" \
--alarm-description "CPU和内存同时异常告警" \
--alarm-rule "ALARM(high-cpu-usage) OR ALARM(high-memory-usage)" \
--actions-enabled \
--alarm-actions arn:aws:sns:us-east-1:123456789012:MyTopic
告警管理操作
查询告警信息
# 列出所有告警
aws cloudwatch describe-alarms
# 查询特定告警
aws cloudwatch describe-alarms --alarm-names "high-cpu-usage"
# 根据状态筛选告警
aws cloudwatch describe-alarms --state-value ALARM
# 查询告警历史
aws cloudwatch describe-alarm-history \
--alarm-name "high-cpu-usage" \
--history-item-type StateUpdate \
--start-date 2024-01-01T00:00:00Z \
--end-date 2024-01-02T00:00:00Z
告警状态管理
# 启用告警动作
aws cloudwatch enable-alarm-actions --alarm-names "high-cpu-usage"
# 禁用告警动作
aws cloudwatch disable-alarm-actions --alarm-names "high-cpu-usage"
# 手动设置告警状态(用于测试)
aws cloudwatch set-alarm-state \
--alarm-name "high-cpu-usage" \
--state-value ALARM \
--state-reason "Manual testing of alarm"
告警删除操作
# 删除单个告警
aws cloudwatch delete-alarms --alarm-names "high-cpu-usage"
# 批量删除告警
aws cloudwatch delete-alarms --alarm-names "alarm1" "alarm2" "alarm3"
高级告警配置
动态阈值告警
# 使用异常检测创建动态阈值告警
aws cloudwatch put-metric-alarm \
--alarm-name "anomaly-detection-alarm" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--comparison-operator GreaterThanUpperThreshold \
--threshold-metric-id "m1" \
--evaluation-periods 2 \
--datapoints-to-alarm 1 \
--treat-missing-data notBreaching
多动作告警
# 配置多个告警动作
aws cloudwatch put-metric-alarm \
--alarm-name "multi-action-alarm" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 80.0 \
--comparison-operator GreaterThanThreshold \
--alarm-actions \
arn:aws:sns:us-east-1:123456789012:HighPriorityTopic \
arn:aws:automate:us-east-1:ec2:reboot \
--ok-actions arn:aws:sns:us-east-1:123456789012:RecoveryTopic \
--insufficient-data-actions arn:aws:sns:us-east-1:123456789012:DataIssueTopic
实战案例:完整的监控解决方案
EC2实例监控模板
#!/bin/bash
# ec2-monitoring-setup.sh
INSTANCE_ID="i-1234567890abcdef0"
ALARM_NAME_PREFIX="ec2-${INSTANCE_ID}"
# CPU使用率告警
aws cloudwatch put-metric-alarm \
--alarm-name "${ALARM_NAME_PREFIX}-cpu-high" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 80.0 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=InstanceId,Value=${INSTANCE_ID} \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:EC2-Alerts
# 内存使用告警(需要自定义指标)
aws cloudwatch put-metric-alarm \
--alarm-name "${ALARM_NAME_PREFIX}-memory-high" \
--metric-name MemoryUtilization \
--namespace System/Linux \
--statistic Average \
--period 300 \
--threshold 90.0 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=InstanceId,Value=${INSTANCE_ID} \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:EC2-Alerts
# 磁盘空间告警
aws cloudwatch put-metric-alarm \
--alarm-name "${ALARM_NAME_PREFIX}-disk-high" \
--metric-name DiskSpaceUtilization \
--namespace System/Linux \
--statistic Average \
--period 300 \
--threshold 85.0 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=InstanceId,Value=${INSTANCE_ID} Name=MountPath,Value=/ \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:EC2-Alerts
RDS数据库监控
# RDS CPU使用率告警
aws cloudwatch put-metric-alarm \
--alarm-name "rds-production-cpu-high" \
--metric-name CPUUtilization \
--namespace AWS/RDS \
--statistic Average \
--period 300 \
--threshold 75.0 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=DBInstanceIdentifier,Value=production-db \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:DBA-Alerts
# RDS连接数告警
aws cloudwatch put-metric-alarm \
--alarm-name "rds-production-connections-high" \
--metric-name DatabaseConnections \
--namespace AWS/RDS \
--statistic Average \
--period 300 \
--threshold 500 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=DBInstanceIdentifier,Value=production-db \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:DBA-Alerts
告警管理最佳实践
命名规范策略
监控策略配置表
| 资源类型 | 关键指标 | 建议阈值 | 评估周期 | 严重级别 |
|---|---|---|---|---|
| EC2实例 | CPUUtilization | 80% | 2个周期 | critical |
| EC2实例 | MemoryUtilization | 90% | 2个周期 | warning |
| RDS数据库 | CPUUtilization | 75% | 3个周期 | critical |
| RDS数据库 | FreeStorageSpace | <5GB | 1个周期 | critical |
| ELB负载均衡器 | HTTPCode_ELB_5XX_Count | >10 | 1个周期 | critical |
自动化维护脚本
#!/bin/bash
# alarm-maintenance.sh
# 定期清理无效告警
INVALID_ALARMS=$(aws cloudwatch describe-alarms --query 'MetricAlarms[?StateValue==`INSUFFICIENT_DATA`].AlarmName' --output text)
for alarm in $INVALID_ALARMS; do
echo "清理无效告警: $alarm"
aws cloudwatch delete-alarms --alarm-names "$alarm"
done
# 批量禁用非工作时间告警
if [[ $(date +%H) -ge 18 ]] || [[ $(date +%H) -lt 9 ]]; then
aws cloudwatch disable-alarm-actions --alarm-names $(aws cloudwatch describe-alarms --query 'MetricAlarms[?StateValue!=`ALARM`].AlarmName' --output text)
fi
故障排查与调试
常见问题解决
# 检查权限问题
aws cloudwatch describe-alarms --debug 2>&1 | grep -i "access denied"
# 验证指标数据是否存在
aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-1234567890abcdef0 \
--start-time $(date -u -d "1 hour ago" +%Y-%m-%dT%H:%M:%SZ) \
--end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
--period 300 \
--statistics Average
# 测试SNS通知配置
aws sns publish \
--topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic \
--message "测试告警通知" \
--subject "CloudWatch告警测试"
监控脚本调试技巧
# 使用dry-run模式测试
aws cloudwatch put-metric-alarm \
--alarm-name "test-alarm" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 70.0 \
--comparison-operator GreaterThanThreshold \
--dry-run
# 验证JSON格式
aws cloudwatch put-metric-alarm \
--cli-input-json file://alarm-config.json \
--generate-cli-skeleton
总结
通过aws-cli管理CloudWatch告警提供了强大的自动化和脚本化能力。本文涵盖了从基础配置到高级用法的完整流程,包括:
- 环境配置 - 安装和配置aws-cli
- 告警创建 - 各种类型的告警配置方法
- 告警管理 - 查询、修改、删除操作
- 高级功能 - 复合告警、动态阈值等
- 实战案例 - 完整的监控解决方案模板
- 最佳实践 - 命名规范和维护策略
- 故障排查 - 常见问题解决方法
掌握这些技能后,您可以构建出健壮、自动化的监控体系,确保云基础设施的稳定运行。记得定期审查和优化告警配置,避免告警疲劳并确保监控的有效性。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



