检查AWS RDS数据库详细信息
#aws #devops #database #monitoring

此博客介绍了一个脚本以获取AWS RDS数据库的详细信息。

AWS CLI为Amazon Web服务提供了统一的命令行接口。该脚本使用了AWS CLI的2种方法:

  • AWS EC2描述区域
  • aws rds描述db-instances

脚本扫描所有AWS区域,并打印有关AWS RDS DB实例的详细信息。

输出包括

  • DB引擎
  • DB版本
  • DB标识符
  • DB机器类
  • multiaz(启用/禁用)
  • 备份保留期
  • 分配存储
  • 最大分配存储

该脚本具有内部参数nstorageallocationwatermarkpctg,以标记最大分配存储和分配存储之间距离的DB实例小于NStorageAllocationWaterMarkmarkPctg Percents。在这种情况下,DB实例被强调为“坏”。这表明应增加最大分配存储。

如果在没有存储AutoScale属性的情况下定义了DB实例,它也将被标记为“坏”。我建议AWS RDS中的任何生产级DB实例用于使用存储自动属性定义。

也可以在Multiaz禁用的DB实例中标记为“ BAD”。

脚本

# Check_AWS_RDS_DB_Instances_Details

now=$(date)

echo ".................................................................. "
echo "Check AWS RDS DB Instances Details"
echo "Date: $now "

echo ".................................................................. "

tot_val=0

nStorageAllocationWatermarkPctg="25"

for region in `aws ec2 describe-regions --query 'Regions[].RegionName' --output text`
do
    printf "Region: %15s\n" $region

    db_counter_in_reg_val=0

    for db_ident in `aws rds describe-db-instances --region ${region} --query 'DBInstances[*].[DBInstanceIdentifier]' --output text`
    do

     a=`aws rds describe-db-instances --region ${region} --db-instance-identifier ${db_ident} --query 'DBInstances[*].[Engine,DBInstanceIdentifier,EngineVersion,MultiAZ,AllocatedStorage,MaxAllocatedStorage,DBInstanceClass,Status,BackupRetentionPeriod]' --output text`

     strEngine=$(echo $a | cut -d' ' -f1)
     strDBIdent=$(echo $a | cut -d' ' -f2)
     strEngineVersion=$(echo $a | cut -d' ' -f3)
     strMultiAZ=$(echo $a | cut -d' ' -f4)
     strAllocatedStorage=$(echo $a | cut -d' ' -f5)
     strMaxAllocatedStorage=$(echo $a | cut -d' ' -f6)
     strDBInstanceClass=$(echo $a | cut -d' ' -f7)
     strBackupRetentionPeriod=$(echo $a | cut -d' ' -f9)

     strAttention1="Ok"
     strAttention2="Ok"

     storage_left_pct="N/A"

     if [ "$strMaxAllocatedStorage" = "None" ]; then
        strAttention1="Bad"
     else
        storage_left="$(($strMaxAllocatedStorage-$strAllocatedStorage))"
        storage_left_pct="$(( 100*($strMaxAllocatedStorage-$strAllocatedStorage)/$strMaxAllocatedStorage ))"

        if [ "$storage_left_pct" -lt "$nStorageAllocationWatermarkPctg" ]; then
           strAttention1="Bad"
        fi
     fi

     if [ "$strMultiAZ" = "False" ]; then
        strAttention2="Bad"
     fi

     printf "#%-3s | eng: %-8s | ver: %-8s | ident: %-50s | class: %-15s | mAZ: %-6s %-3s | backupRetPeriod: %-3d | allocStorage: %-8s (left %-3s pct) | maxAllocStorage: %-8s %-3s \n" $((db_counter_in_reg_val + 1)) ${strEngine} ${strEngineVersion} ${strDBIdent} ${strDBInstanceClass} ${strMultiAZ} ${strAttention2} ${strBackupRetentionPeriod} ${strAllocatedStorage} ${storage_left_pct} ${strMaxAllocatedStorage} ${strAttention1}

     db_counter_in_reg_val=$((db_counter_in_reg_val + 1))

     tot_val=$((tot_val + 1))

    done
done

echo ".................................................................. "

printf "TOTAL:                                                  %10s\n" $tot_val

echo ".................................................................. "

输出的示例

…
.................................................................. 
Check AWS RDS DB Instances Details
Date: Tue Dec  6 06:48:09 IST 2022 
.................................................................. 

...
Region:       us-east-1
#1   | eng: postgres | ver: 13.3     | ident: my-db-1         | class: db.r5.xlarge    | mAZ: True   Ok  | backupRetPeriod: 35  | allocStorage: 2000     (left 20  pct) | maxAllocStorage: 2500     Bad
#2   | eng: postgres | ver: 13.3     | ident: my-db-2         | class: db.r5.xlarge    | mAZ: True   Ok  | backupRetPeriod: 35  | allocStorage: 1500     (left 20  pct) | maxAllocStorage: 1875     Bad
#3   | eng: mysql    | ver: 8.0.28    | ident: my-db-3        | class: db.m6g.large    | mAZ: False  Bad | backupRetPeriod: 7   | allocStorage: 49       (left 83  pct) | maxAllocStorage: 297      Ok  
#4   | eng: postgres | ver: 13.3     | ident: my-db-4         | class: db.t3.small     | mAZ: True   Ok  | backupRetPeriod: 7   | allocStorage: 10       (left 66  pct) | maxAllocStorage: 30       Ok  
#5   | eng: mysql    | ver: 5.7.48   | ident: my-db-5         | class: db.m5.2xlarge   | mAZ: True   Ok  | backupRetPeriod: 14  | allocStorage: 100      (left 50  pct) | maxAllocStorage: 200      Ok 
...

.................................................................. 
TOTAL:                                                         123
..................................................................
…

结论

在此博客中,我介绍了一个脚本,该脚本迭代了所有AWS区域,并打印了有关AWS RDS DB实例的详细信息。该脚本可用于监视您的AWS RDS数据库有关开发,分期和生产环境的实例。