如何了解RDS免费存储 [英] How to know RDS free storage

查看:18
本文介绍了如何了解RDS免费存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

先谢谢你..

解决方案

几种方法

使用 AWS 控制台

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

通过 AWS CLI 使用 API

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

我将展示如何使用 AWS CLI 执行此操作.

这假设您已设置 AWS CLI 凭证.我在环境变量中导出了 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY,但是有 多种配置 CLI(或 SDK)的方法.

REGION="eu-west-1"START="$(date -u -d '5 分钟前''+%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 \--统计平均\--dimensions "Name=DBInstanceIdentifier, Value=${INSTANCE_NAME}"{"Label": "FreeStorageSpace",数据点": [{"时间戳": "2017-11-16T14:01:00Z",平均":95406264320.0,单位":字节"}]}

通过 Java SDK 使用 API

以下是如何通过 Java AWS SDK,使用 Cloudwatch API.

build.gradle 内容

应用插件:'java'应用插件:'应用程序'源兼容性 = 1.8存储库{jcenter()}依赖{编译'com.amazonaws:aws-java-sdk-cloudwatch:1.11.232'}mainClassName = 'GetRDSInfo'

Java 类

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

import java.util.Calendar;导入 java.util.Date;进口 com.amazonaws.regions.Regions;导入 com.amazonaws.services.cloudwatch.AmazonCloudWatch;导入 com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;导入 com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;导入 com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;导入 com.amazonaws.services.cloudwatch.model.StandardUnit;导入 com.amazonaws.services.cloudwatch.model.Dimension;导入 com.amazonaws.services.cloudwatch.model.Datapoint;公共类 GetRDSInfo {公共静态无效主(字符串 [] args){最终长技嘉 = 1024L * 1024L * 1024L;//将我们的 endTime 计算为现在,将 startTime 计算为 5 分钟前.日历校准 = Calendar.getInstance();日期结束时间 = cal.getTime();cal.add(Calendar.MINUTE, -5);日期开始时间 = cal.getTime();String dbIdentifier = "tstirldbopgs001";地区 region = Regions.EU_WEST_1;维度暗淡 = 新维度().withName("DBInstanceIdentifier").withValue(dbIdentifier);最终 AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.standard().withRegion(区域).建造();GetMetricStatisticsRequest req = new GetMetricStatisticsRequest().withNamespace("AWS/RDS").withMetricName("FreeStorageSpace").withStatistics("平均值").withStartTime(startTime).withEndTime(endTime).withDimensions(dim).withPeriod(300);GetMetricStatisticsResult res = cw.getMetricStatistics(req);对于(数据点dp:res.getDatapoints()){//我们只请求了过去 5 分钟的平均可用空间//所以我们只有一个数据点double freespaceGigs = dp.getAverage()/GIGABYTE;System.out.println(String.format("可用空间:%.2f GB", freespaceGigs));}}}

Java 代码执行示例

<代码>>运行>任务:运行可用空间:88.85 GB在 7 秒内构建成功

I've created a RDS postgres instance with size of 65GB initially. Is it possible to get free space available using any postgres query..

if not, how can I achieve the same ??

Thank you in advance..

解决方案

A couple ways to do it

Using the AWS Console

Go to the RDS console and select the region your database is in. Click on the Show Monitoring button and pick your database instance. There will be a graph (like below image) that shows Free Storage Space.

This is documented over at AWS RDS documentation.

Using the API via AWS CLI

Alternatively, you can use the AWS API to get the information from cloudwatch.

I will show how to do this with the AWS CLI.

This assumes you have set up the AWS CLI credentials. I export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in my environment variables, but there are 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"
        }
    ]
}

Using the API via Java SDK

Here's a rudimentary example of how to get the same data via the Java AWS SDK, using the Cloudwatch API.

build.gradle contents

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 class

Again, I rely on the credential chain to get AWS API credentials (I set them in my environment). You can change the call to the builder to change this behavior (see Working with AWS Credentials documentation).

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));
        }
    }
}

Example Java Code Execution

> gradle run

> Task :run
Free Space: 88.85 GB


BUILD SUCCESSFUL in 7s

这篇关于如何了解RDS免费存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆