使用 Greptime Cloud 监控AWS CloudWatch
使用 Greptime Cloud 监控AWS CloudWatch
使用 Greptime Cloud 监控AWS CloudWatch
- 引言
- CloudWach 是 AWS 上进行监控的一个服务,如果你想把这些监控数据自行保存或进行分析,可以通过 API 接口将其导出并存储。
- 目前主流的监控平台是 Prometheus ,其易用性收到广泛的应用。但是由于其在 HA 和高基数的问题,而维护 Thanos 集群又提升了复杂度。近年来,GreptimeDB 作为个新的时序数据库,其原生支持高可用,并且兼容 Prometheus 协议。
- 本文将通过 Grafana Alloy 将 AWS CloudWatch 的数据抓取,并转化为 Prometheus 格式发送到 GreptimeCloud。
- 核心工具简介
- AWS CloudWatch:用于收集和监控 AWS 服务指标。
- Alloy:专注于数据抓取、转换与传输的工具。
- Greptime Cloud:高效的时序数据存储与分析平台。
- 在 AWS 上创建 IAM 用户并绑定 Alloy 所需 Policy
{ "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1674249227793", "Action": [ "tag:GetResources", "cloudwatch:GetMetricData", "cloudwatch:GetMetricStatistics", "cloudwatch:ListMetrics", "ec2:DescribeTags", "ec2:DescribeInstances", "ec2:DescribeRegions", "iam:ListAccountAliases", "ec2:DescribeTransitGateway*", "apigateway:GET", "dms:DescribeReplicationInstances", "dms:DescribeReplicationTasks" ], "Effect": "Allow", "Resource": "*" } ] }
并将该 Policy 绑从对应的 AWS IAM User 上,将该用户的 Credential 保存到 ~/.aws/crendetails
根据官方 安装文档 下载并安装到本地
新建一个 config.alloy 文件,该文件将会有以下内容
prometheus.exporter.cloudwatch "aws_ec2_metrics" { ...... } prometheus.scrape "awsmonitoring" { ...... } prometheus.remote_write "greptimedb" { ...... }
prometheus.exporter.cloudwatch 将会配置从 CloudWatch 拉取指标
prometheus.exporter.cloudwatch "aws_ec2_metrics" { sts_region = "ap-east-1" discovery { type = "AWS/EC2" regions = ["ap-east-1"] metric { name = "CPUUtilization" statistics = ["Average"] period = "5m" } metric { name = "NetworkPacketsIn" statistics = ["Average"] period = "5m" } } }
该文件配置了从 ap-east-1 区域拉取 EC2 的两个指标:
- CPUUtilization,统计方式采用均值,统计周期 5 分钟
- NetworkPacketsIn,统计方式采用均值,统计周期 5 分钟
prometheus.scrape "awsmonitoring" 配置
prometheus.scrape "awsmonitoring" { targets = prometheus.exporter.cloudwatch.aws_ec2_metrics.targets forward_to = [prometheus.remote_write.greptimedb.receiver] }
对于prometheus.remote_write "greptimedb"的配置请登录 GreptimeCloud 的控制台进行配置
选择Alloy,并将配置内容复制到config.alloy中
运行命令启动Alloy
alloy-linux-amd64 run config.alloy
回到Greptime Cloud 检查,如果出现了以下内容,说明配置成功。
Greptime
使用以下 JSON 文件作为 Dashboard 的配置
{ "kind": "Dashboard", "metadata": { "name": "aws_ec2", "project": "default", "version": 0 }, "spec": { "display": { "name": "aws_ec2" }, "panels": { "CPUUtilization": { "kind": "Panel", "spec": { "display": { "name": "CPUUtilization" }, "plugin": { "kind": "TimeSeriesChart", "spec": { "yAxis": { "show": true, "label": "", "format": { "unit": "percent" } }, "legend": { "position": "bottom" } } }, "queries": [ { "kind": "TimeSeriesQuery", "spec": { "plugin": { "kind": "PrometheusTimeSeriesQuery", "spec": { "query": "aws_ec2_cpuutilization_average", "seriesNameFormat": "{{ dimension_InstanceId}}" } } } } ] } }, "CPUUtilization-1": { "kind": "Panel", "spec": { "display": { "name": "NetworkPacketsIn" }, "plugin": { "kind": "TimeSeriesChart", "spec": { "yAxis": { "show": true, "label": "", "format": { "unit": "bytes" } }, "legend": { "position": "bottom" } } }, "queries": [ { "kind": "TimeSeriesQuery", "spec": { "plugin": { "kind": "PrometheusTimeSeriesQuery", "spec": { "query": "aws_ec2_network_packets_in_average", "seriesNameFormat": "{{ dimension_InstanceId}}" } } } } ] } } }, "layouts": [ { "kind": "Grid", "spec": { "display": { "title": "Panel Group", "collapse": { "open": true } }, "items": [ { "x": 0, "y": 0, "width": 13, "height": 7, "content": { "$ref": "#/spec/panels/CPUUtilization" } }, { "x": 0, "y": 7, "width": 13, "height": 7, "content": { "$ref": "#/spec/panels/CPUUtilization-1" } } ] } } ], "variables": [], "duration": "1h", "refreshInterval": "0s" } }