utils.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import json
  5. from http.client import responses
  6. import conf
  7. from typing import List
  8. from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
  9. from alibabacloud_tea_openapi import models as open_api_models
  10. from alibabacloud_alidns20150109 import models as alidns_20150109_models
  11. from alibabacloud_tea_util import models as util_models
  12. from alibabacloud_tea_util.client import Client as UtilClient
  13. from alibabacloud_cms20190101.client import Client as Cms20190101Client
  14. from alibabacloud_tea_openapi import models as open_api_models
  15. from alibabacloud_cms20190101 import models as cms_20190101_models
  16. from alibabacloud_tea_util import models as util_models
  17. from alibabacloud_tea_util.client import Client as UtilClient
  18. def update_dns_weights(bandwidth):
  19. '''
  20. @param bandwidth: 带宽值
  21. @return:
  22. '''
  23. if bandwidth > 1800:
  24. alb_weight = round(1800 / bandwidth * 100)
  25. backup_weight = (100 - alb_weight) // 2
  26. print(f"alb_weight: {alb_weight}, backup_weight: {backup_weight}")
  27. return backup_weight
  28. else:
  29. alb_weight = 0
  30. backup_weight = 0
  31. print(f"alb_weight: {alb_weight}, backup_weight: {backup_weight}")
  32. return backup_weight
  33. def client(access_key_id, access_key_secret, endpoint) -> Cms20190101Client:
  34. """
  35. 使用AK&SK初始化账号Client
  36. @return: Client
  37. @throws Exception
  38. """
  39. # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  40. # 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。
  41. config = open_api_models.Config(
  42. # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
  43. access_key_id=os.environ[access_key_id],
  44. # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
  45. access_key_secret=os.environ[access_key_secret]
  46. )
  47. # Endpoint 请参考 https://api.aliyun.com/product/Cms
  48. config.endpoint = endpoint
  49. return Cms20190101Client(config)
  50. def DescribeMetricList(metrics_client, namespace, metric_name, period, dimensions, start_time, end_time):
  51. '''
  52. @param metrics_client: 云监控客户端
  53. @param namespace: 云产品的数据命名空间 共享带宽
  54. @param metric_name: 云产品的监控项名称 入流量
  55. @param period: 监控数据的统计周期 秒
  56. @param dimensions: 指定资源的监控维度 共享带宽
  57. @param start_time: 监控开始时间
  58. @param end_time: 监控结束时间
  59. @return:
  60. '''
  61. metrics_client()
  62. describe_metric_list_request = cms_20190101_models.DescribeMetricListRequest(
  63. namespace=namespace,
  64. metric_name=metric_name,
  65. period=period,
  66. dimensions=dimensions,
  67. start_time=start_time,
  68. end_time=end_time
  69. )
  70. runtime = util_models.RuntimeOptions()
  71. try:
  72. # 复制代码运行请自行打印 API 的返回值
  73. response = metrics_client.describe_metric_list_with_options(describe_metric_list_request, runtime)
  74. datapoints = json.loads(response["Datapoints"])
  75. if datapoints:
  76. value_in_bits = datapoints[0]["Value"]
  77. value_in_megabits = value_in_bits / 1000000
  78. print(f"Value in Megabits: {value_in_megabits:.2f} Mb")
  79. return value_in_megabits
  80. except Exception as error:
  81. exit(error)
  82. def get_records(response):
  83. '''
  84. @param response:
  85. @return:
  86. '''
  87. records = [
  88. {
  89. "Value": record["Value"],
  90. "RecordId": record["RecordId"]
  91. }
  92. for record in response["DomainRecords"]["Record"]
  93. if record["RR"] == "longvideoapi"
  94. ]
  95. return records
  96. def DescribeDomainRecords(dns_client, lang, domain_name):
  97. '''
  98. @param dns_client: dns客户端
  99. @param lang: 语言
  100. @param domain_name: 解析记录的ID 主域名
  101. @return:
  102. '''
  103. dns_client()
  104. describe_domain_records_request = alidns_20150109_models.DescribeDomainRecordsRequest(
  105. lang=lang,
  106. domain_name=domain_name,
  107. page_size=500,
  108. page_number=1
  109. )
  110. runtime = util_models.RuntimeOptions()
  111. try:
  112. response=dns_client.describe_domain_records_with_options(describe_domain_records_request, runtime)
  113. result = get_records(response)
  114. for record in result:
  115. print(record)
  116. except Exception as error:
  117. exit(error)
  118. def update_dnsslbweight(dns_client, lang, record, backup_weight):
  119. '''
  120. @param dns_client: dns客户端
  121. @param lang: 语言
  122. @param record: 解析记录ID
  123. @param backup_weight: 权重
  124. @return:
  125. '''
  126. dns_client()
  127. update_dnsslbweight_request = alidns_20150109_models.UpdateDNSSLBWeightRequest(
  128. lang=lang,
  129. record_id=record,
  130. weight=backup_weight
  131. )
  132. runtime = util_models.RuntimeOptions()
  133. try:
  134. # 复制代码运行请自行打印 API 的返回值
  135. dns_client.update_dnsslbweight_with_options(update_dnsslbweight_request, runtime)
  136. except Exception as error:
  137. exit(error)