如何知道RDS免费存储

时间:2017-11-16 13:52:53

标签: amazon-web-services rds

我最初创建了一个大小为65GB的RDS postgres实例。 是否可以使用任何postgres查询获得可用空间..

如果没有,我怎么能达到同样的目的?

提前谢谢..

1 个答案:

答案 0 :(得分:9)

有两种方法可以做到这一点

使用AWS控制台

转到RDS控制台并选择数据库所在的区域。单击显示监控按钮并选择您的数据库实例。将有一个图表(如下图所示),显示可用存储空间

这是在AWS RDS documentation记录的。

Example AWS RDS console

通过AWS CLI使用API​​

或者,您可以使用AWS API从cloudwatch获取信息。

我将展示如何使用AWS CLI

执行此操作

这假设您已设置AWS CLI凭据。我在我的环境变量中导出AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY,但有multiple ways to configure the CLI (or SDKS)

REGION="eu-west-1"
START="$(date -u -d '5 minutes ago' '+%Y-%m-%dT%T')"
END="$(date -u '+%Y-%m-%dT%T')"
INSTANCE_NAME="tstirldbopgs001"

AWS_DEFAULT_REGION="$REGION" aws cloudwatch get-metric-statistics \
  --namespace AWS/RDS --metric-name FreeStorageSpace \
  --start-time $START --end-time $END --period 300 \
  --statistics Average \
  --dimensions "Name=DBInstanceIdentifier, Value=${INSTANCE_NAME}"

{
    "Label": "FreeStorageSpace",
    "Datapoints": [
        {
            "Timestamp": "2017-11-16T14:01:00Z",
            "Average": 95406264320.0,
            "Unit": "Bytes"
        }
    ]
}

通过Java SDK使用API​​

以下是如何使用Java AWS SDK通过Cloudwatch API获取相同数据的基本示例。

build.gradle内容

apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.8

repositories {
    jcenter()
}

dependencies {
    compile 'com.amazonaws:aws-java-sdk-cloudwatch:1.11.232'
}

mainClassName = 'GetRDSInfo'

Java类

同样,我依靠凭据链来获取AWS API凭据(我在我的环境中设置它们)。您可以更改对构建器的调用以更改此行为(请参阅Working with AWS Credentials文档)。

import java.util.Calendar;
import java.util.Date;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.Datapoint;

public class GetRDSInfo {
    public static void main(String[] args) {
        final long GIGABYTE = 1024L * 1024L * 1024L;

        // calculate our endTime as now and startTime as 5 minutes ago.
        Calendar cal = Calendar.getInstance();
        Date endTime = cal.getTime();
        cal.add(Calendar.MINUTE, -5);
        Date startTime = cal.getTime();

        String dbIdentifier = "tstirldbopgs001";
        Regions region = Regions.EU_WEST_1;

        Dimension dim = new Dimension()
            .withName("DBInstanceIdentifier")
            .withValue(dbIdentifier);

        final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.standard()
                                        .withRegion(region)
                                        .build();

        GetMetricStatisticsRequest req = new GetMetricStatisticsRequest()
            .withNamespace("AWS/RDS")
            .withMetricName("FreeStorageSpace")
            .withStatistics("Average")
            .withStartTime(startTime)
            .withEndTime(endTime)
            .withDimensions(dim)
            .withPeriod(300);

        GetMetricStatisticsResult res = cw.getMetricStatistics(req);

        for (Datapoint dp : res.getDatapoints()) {
            // We requested only the average free space over the last 5 minutes
            // so we only have one datapoint
            double freespaceGigs = dp.getAverage() / GIGABYTE;
            System.out.println(String.format("Free Space: %.2f GB", freespaceGigs));
        }
    }
}

示例Java代码执行

> gradle run

> Task :run
Free Space: 88.85 GB


BUILD SUCCESSFUL in 7s