monitor.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- encoding:utf-8 -*-
  2. from kscore.session import get_session
  3. import json
  4. if __name__ == "__main__":
  5. s = get_session()
  6. client = s.create_client("monitor", "cn-beijing-6", use_ssl=True)
  7. clientv2 = s.create_client("monitorv2", "cn-beijing-6", use_ssl=True)
  8. '''
  9. 通用产品线,不包含容器(docker)
  10. '''
  11. #ListMetrics
  12. m = client.list_metrics(InstanceID="293bbbc1-6c27-4567-89fc-xxxxx",
  13. Namespace="kec",
  14. PageIndex="1",
  15. PageSize="10")
  16. print(json.dumps(m, sort_keys=True, indent=4))
  17. #GetMetricStatistics
  18. #m = client.get_metric_statistics(
  19. # InstanceID="ef6eaa98-8e2b-4629-98e0-xxxxx",
  20. # Namespace="eip",
  21. # MetricName="eip.bps.in",
  22. # StartTime="2021-09-15T10:09:00Z",
  23. # EndTime="2021-09-15T10:19:00Z",
  24. # Period="60",
  25. # Aggregate="Average,Max,Min")
  26. #print(json.dumps(m, sort_keys=True, indent=4))
  27. #GetMetricStatisticsBatch version=2018-11-14
  28. param = {
  29. "Namespace":
  30. "kec",
  31. "StartTime":
  32. "2021-09-15T10:00:00Z",
  33. "EndTime":
  34. "2021-09-15T10:09:00Z",
  35. "Period":
  36. "180",
  37. "Aggregate": ["Max", "Min", "Avg"],
  38. "Metrics": [{
  39. "InstanceID": "293bbbc1-6c27-4567-89fc-xxxxx",
  40. "MetricName": "net.if.in"
  41. }, {
  42. "InstanceID": "293bbbc1-6c27-4567-89fc-xxxxx",
  43. "MetricName": "cpu.utilizition.total"
  44. }, {
  45. "InstanceID": "6a725f27-1c7e-4704-95c8-xxxxx",
  46. "MetricName": "net.if.out"
  47. }]
  48. }
  49. #m = client.get_metric_statistics_batch_v2(**param)
  50. #print(json.dumps(m, sort_keys=True, indent=4))
  51. '''
  52. 只支持容器docker(kce),其余产品线不支持。
  53. '''
  54. #ListMetrics
  55. paraml = {
  56. "Action": "ListMetrics",
  57. "Version": "2019-08-12",
  58. "Namespace": "kce",
  59. "PageIndex": "1",
  60. "PageSize": "10",
  61. "Dimensions.0.Name": "ClusterId",
  62. "Dimensions.0.Value": "807a4149-b7e2-4e05-8a35-xxxxx",
  63. "Dimensions.1.Name": "NamespaceName",
  64. "Dimensions.1.Value": "xxxxx",
  65. "Dimensions.2.Name": "WorkloadType",
  66. "Dimensions.2.Value": "deployment",
  67. "Dimensions.3.Name": "WorkloadName",
  68. "Dimensions.3.Value": "xxxxx",
  69. "Dimensions.4.Name": "PodName",
  70. "Dimensions.4.Value": "xxxxx-xxxxx-xxxxx",
  71. # "Dimensions.5.Name":"ContainerName",
  72. # "Dimensions.5.Value":"xxxxx"
  73. }
  74. #m = client.list_metrics_v3(**paraml)
  75. #print(json.dumps(m, sort_keys=True, indent=4))
  76. #GetMetricStatistics
  77. paramg = {
  78. "Action": "GetMetricStatistics",
  79. "Version": "2019-08-12",
  80. "Namespace": "kce",
  81. "MetricName": "pod.network.rx",
  82. "StartTime": "2021-09-15T10:09:00Z",
  83. "EndTime": "2021-09-15T10:19:00Z",
  84. "Period": "60",
  85. "Aggregate": "Average,Max,Min",
  86. "Dimensions.0.Name": "ClusterId",
  87. "Dimensions.0.Value": "807a4149-b7e2-4e05-8a35-xxxxx",
  88. "Dimensions.1.Name": "NamespaceName",
  89. "Dimensions.1.Value": "xxxxx",
  90. "Dimensions.2.Name": "WorkloadType",
  91. "Dimensions.2.Value": "deployment",
  92. "Dimensions.3.Name": "WorkloadName",
  93. "Dimensions.3.Value": "xxxxx",
  94. "Dimensions.4.Name": "PodName",
  95. "Dimensions.4.Value": "xxxxx",
  96. # "Dimensions.5.Name":"ContainerName",
  97. # "Dimensions.5.Value":"xxxxx"
  98. }
  99. #m = client.get_metric_statistics_v3(**paramg)
  100. #print(json.dumps(m, sort_keys=True, indent=4))
  101. #ListAlarmPolicy
  102. #m = clientv2.list_alarm_policy(PageIndex=1, PageSize=10)
  103. #print(json.dumps(m, sort_keys=True, indent=4))
  104. #DescribeAlarmPolicy
  105. #m = clientv2.describe_alarm_policy(PolicyId=25232)
  106. #print(json.dumps(m, sort_keys=True, indent=4))
  107. #DescribePolicyObject
  108. #m = clientv2.describe_policy_object(PolicyId=25232, PageIndex=1, PageSize=10)
  109. #print(json.dumps(m, sort_keys=True, indent=4))
  110. #DescribeAlarmReceives
  111. #m = clientv2.describe_alarm_receives(PolicyId=25232)
  112. #print(json.dumps(m, sort_keys=True, indent=4))
  113. #AddAlarmReceives
  114. paraml = {
  115. "PolicyId": 25232,
  116. "ContactFlag": 2,
  117. "ContactWay": 3,
  118. "ContactId": [1985, 3607],
  119. }
  120. #m = clientv2.add_alarm_receives(**paraml)
  121. #print(json.dumps(m, sort_keys=True, indent=4))
  122. #DeleteAlarmReceives
  123. paraml = {
  124. "PolicyId": 25232,
  125. "ContactFlag": 2,
  126. "ContactId": [1985, 3607],
  127. }
  128. #m = clientv2.delete_alarm_receives(**paraml)
  129. #print(json.dumps(m, sort_keys=True, indent=4))
  130. #GetUserGroup
  131. #m = clientv2.get_user_group()
  132. #print(json.dumps(m, sort_keys=True, indent=4))
  133. #GetAlertUser
  134. #m = clientv2.get_alert_user(UserGrpId=[879, 1484])
  135. #print(json.dumps(m, sort_keys=True, indent=4))
  136. #UpdateAlertUserStatus
  137. paraml = {
  138. "UserId": [1985, 3607],
  139. "UserStatus": 1,
  140. }
  141. #m = clientv2.update_alert_user_status(**paraml)
  142. #print(json.dumps(m, sort_keys=True, indent=4))