rds_monitor.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from datetime import datetime
  2. from resource.enums.region import Region
  3. from resource.service.FeiShuService import FeiShuService
  4. from resource.utils.utils import value_covert, bottom_right, convert_storage_unit
  5. from service.RDSServicee import RDSService
  6. rds_service = RDSService()
  7. fei_shu_service = FeiShuService()
  8. fei_shu_spread_sheet_token = "XzQGsheQzhk74rtknKacClASnTc"
  9. fei_shu_sheet_id = "R50CB5"
  10. tenant_access_token = fei_shu_service.get_tenant_access_token("cli_a89702999f3c900b", "47ewnaxRqJAvHYdUR8idHgfzfeqAu0Pz")
  11. instance_type_map = {
  12. "Primary": "主实例",
  13. "Readonly": "只读实例",
  14. "Guard": "灾备实例",
  15. "Temp": "临时实例"
  16. }
  17. status_map = {
  18. "Creating": "创建中",
  19. "Running": "使用中",
  20. "Deleting": "删除中",
  21. "Rebooting": "重启中",
  22. "Stopping": "暂停中",
  23. "Stopped": "已暂停",
  24. "DBInstanceClassChanging": "升降级中",
  25. "TRANSING": "迁移中",
  26. "EngineVersionUpgrading": "迁移版本中",
  27. "TransingToOthers": "迁移数据到其他RDS中",
  28. "GuardDBInstanceCreating": "生产灾备实例中",
  29. "Restoring": "备份恢复中",
  30. "Importing": "数据导入中",
  31. "ImportingFromOthers": "从其他RDS实例导入数据中",
  32. "DBInstanceNetTypeChanging": "内外网切换中",
  33. "GuardSwitching": "容灾切换中",
  34. "INS_CLONING": "实例克隆中",
  35. "Released": "已释放实例"
  36. }
  37. storage_type_map = {
  38. "local_ssd": "高性能本地盘",
  39. "ephemeral_ssd": "性能本地盘",
  40. "cloud_ssd": "SSD 云盘",
  41. "general_essd": "高性能云盘",
  42. "cloud_essd0": "ESSD PL0 云盘",
  43. "cloud_essd": "ESSD PL1 云盘",
  44. "cloud_essd2": "ESSD PL2 云盘",
  45. "cloud_essd3": "ESSD PL3 云盘"
  46. }
  47. pay_type_map = {
  48. "Postpaid": "按量付费",
  49. "Prepaid": "包年包月",
  50. "SERVERLESS": "Serverless"
  51. }
  52. category_map = {
  53. "Basic": "基础系列",
  54. "HighAvailability": "高可用系列",
  55. "cluster": "MySQL 集群系列",
  56. "AlwaysOn": "SQL Server 集群系列",
  57. "Finance": "三节点企业系列",
  58. "Serverless_basic": "Serverless 基础系列"
  59. }
  60. def main():
  61. region_list = [Region.HANG_ZHOU, Region.HONG_KONG, Region.US_WEST, Region.SOUTHEAST]
  62. for region in region_list:
  63. all_instances = rds_service.get_all_instance(region=region)
  64. for instance in all_instances:
  65. dt = datetime.now().strftime('%Y-%m-%d')
  66. instance_id = instance.dbinstance_id
  67. instance_name = instance.dbinstance_description
  68. category = value_covert(instance.category, category_map)
  69. memory = convert_storage_unit(instance.dbinstance_memory, "mb", "gb")
  70. cpu = instance.dbinstance_cpu
  71. status = value_covert(instance.dbinstance_status, status_map)
  72. instance_type = value_covert(instance.dbinstance_type, instance_type_map)
  73. engine = instance.engine
  74. engine_version = instance.engine_version
  75. zone_id = instance.zone_id
  76. attributes = rds_service.get_instance_detail(region, instance_id)
  77. attribute = attributes[0]
  78. disk_used = convert_storage_unit(attribute.dbinstance_disk_used, "b", "gb")
  79. storage = attribute.dbinstance_storage
  80. storage_type = value_covert(attribute.dbinstance_storage_type, storage_type_map)
  81. pay_type = value_covert(attribute.pay_type, pay_type_map)
  82. values = [[dt, region.desc, zone_id, instance_id, instance_name, category, status, instance_type, pay_type, engine, engine_version, memory, cpu, disk_used, storage, storage_type]]
  83. start_col = "A2"
  84. end_col = bottom_right(start_col, values)
  85. fei_shu_service.spreadsheet_values_prepend(tenant_access_token, fei_shu_spread_sheet_token, f"{fei_shu_sheet_id}!{start_col}:{end_col}", values)
  86. if __name__ == "__main__":
  87. main()