host_performance.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Blueprint
  4. import json
  5. import jimit as ji
  6. import base64
  7. from api.base import Base
  8. from models import HostCPUMemory, HostTraffic, HostDiskUsageIO, Utils, Rules
  9. __author__ = 'James Iter'
  10. __date__ = '2017/8/7'
  11. __contact__ = 'james.iter.cn@gmail.com'
  12. __copyright__ = '(c) 2017 by James Iter.'
  13. blueprint = Blueprint(
  14. 'api_host_performance',
  15. __name__,
  16. url_prefix='/api/host_performance'
  17. )
  18. blueprints = Blueprint(
  19. 'api_host_performances',
  20. __name__,
  21. url_prefix='/api/host_performances'
  22. )
  23. host_cpu_memory = Base(the_class=HostCPUMemory, the_blueprint=blueprint, the_blueprints=blueprints)
  24. host_traffic = Base(the_class=HostTraffic, the_blueprint=blueprint, the_blueprints=blueprints)
  25. host_disk_usage_io = Base(the_class=HostDiskUsageIO, the_blueprint=blueprint, the_blueprints=blueprints)
  26. @Utils.dumps2response
  27. def r_cpu_memory_get_by_filter():
  28. return host_cpu_memory.get_by_filter()
  29. @Utils.dumps2response
  30. def r_traffic_get_by_filter():
  31. return host_traffic.get_by_filter()
  32. @Utils.dumps2response
  33. def r_disk_usage_io_get_by_filter():
  34. return host_disk_usage_io.get_by_filter()
  35. def get_performance_data(node_id, the_class=None, nic_name=None, mountpoint=None, granularity='hour'):
  36. args_rules = [
  37. Rules.NODE_ID.value,
  38. ]
  39. filters = list()
  40. try:
  41. ji.Check.previewing(args_rules, {'node_id': node_id})
  42. node_ids_str = 'node_id:in:' + node_id.__str__()
  43. filters.append(node_ids_str)
  44. if nic_name is not None:
  45. filters.append('name:eq:' + nic_name)
  46. if mountpoint is not None:
  47. filters.append('mountpoint:eq:' + mountpoint)
  48. ret = dict()
  49. ret['state'] = ji.Common.exchange_state(20000)
  50. ret['data'] = list()
  51. max_limit = 10080
  52. ts = ji.Common.ts()
  53. _boundary = ts - 60 * 60
  54. if granularity == 'hour':
  55. _boundary = ts - 60 * 60
  56. elif granularity == 'six_hours':
  57. _boundary = ts - 60 * 60 * 6
  58. elif granularity == 'day':
  59. _boundary = ts - 60 * 60 * 24
  60. elif granularity == 'seven_days':
  61. _boundary = ts - 60 * 60 * 24 * 7
  62. else:
  63. pass
  64. filters.append('timestamp:gt:' + _boundary.__str__())
  65. filter_str = ';'.join(filters)
  66. _rows, _rows_count = the_class.get_by_filter(
  67. offset=0, limit=max_limit, order_by='id', order='asc', filter_str=filter_str)
  68. def smooth_data(boundary=0, interval=60, now_ts=ji.Common.ts(), rows=None):
  69. needs = list()
  70. data = list()
  71. for t in range(boundary + interval, now_ts, interval):
  72. needs.append(t - t % interval)
  73. for row in rows:
  74. if row['timestamp'] % interval != 0:
  75. continue
  76. if needs.__len__() > 0:
  77. t = needs.pop(0)
  78. else:
  79. t = now_ts
  80. while t < row['timestamp']:
  81. data.append({
  82. 'timestamp': t,
  83. 'cpu_load': None,
  84. 'memory_available': None,
  85. 'rx_packets': None,
  86. 'rx_bytes': None,
  87. 'tx_packets': None,
  88. 'tx_bytes': None,
  89. 'rd_req': None,
  90. 'rd_bytes': None,
  91. 'used': None,
  92. 'wr_req': None,
  93. 'wr_bytes': None
  94. })
  95. if needs.__len__() > 0:
  96. t = needs.pop(0)
  97. else:
  98. t = now_ts
  99. data.append(row)
  100. return data
  101. if granularity == 'day':
  102. ret['data'] = smooth_data(boundary=_boundary, interval=600, now_ts=ts, rows=_rows)
  103. if granularity == 'seven_days':
  104. ret['data'] = smooth_data(boundary=_boundary, interval=600, now_ts=ts, rows=_rows)
  105. else:
  106. ret['data'] = smooth_data(boundary=_boundary, interval=60, now_ts=ts, rows=_rows)
  107. return ret
  108. except ji.PreviewingError, e:
  109. return json.loads(e.message)
  110. @Utils.dumps2response
  111. def r_cpu_memory_last_hour(node_id):
  112. return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='hour')
  113. @Utils.dumps2response
  114. def r_cpu_memory_last_six_hours(node_id):
  115. return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='six_hours')
  116. @Utils.dumps2response
  117. def r_cpu_memory_last_day(node_id):
  118. return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='day')
  119. @Utils.dumps2response
  120. def r_cpu_memory_last_seven_days(node_id):
  121. return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='seven_days')
  122. @Utils.dumps2response
  123. def r_traffic_last_hour(node_id, nic_name):
  124. return get_performance_data(node_id=node_id, nic_name=nic_name, the_class=HostTraffic, granularity='hour')
  125. @Utils.dumps2response
  126. def r_traffic_last_six_hours(node_id, nic_name):
  127. return get_performance_data(node_id=node_id, nic_name=nic_name, the_class=HostTraffic, granularity='six_hours')
  128. @Utils.dumps2response
  129. def r_traffic_last_day(node_id, nic_name):
  130. return get_performance_data(node_id=node_id, nic_name=nic_name, the_class=HostTraffic, granularity='day')
  131. @Utils.dumps2response
  132. def r_traffic_last_seven_days(node_id, nic_name):
  133. return get_performance_data(node_id=node_id, nic_name=nic_name, the_class=HostTraffic, granularity='seven_days')
  134. @Utils.dumps2response
  135. def r_disk_usage_io_last_hour(node_id, mountpoint):
  136. mountpoint = base64.b64decode(mountpoint)
  137. return get_performance_data(node_id=node_id, mountpoint=mountpoint, the_class=HostDiskUsageIO, granularity='hour')
  138. @Utils.dumps2response
  139. def r_disk_usage_io_last_six_hours(node_id, mountpoint):
  140. mountpoint = base64.b64decode(mountpoint)
  141. return get_performance_data(node_id=node_id, mountpoint=mountpoint, the_class=HostDiskUsageIO,
  142. granularity='six_hours')
  143. @Utils.dumps2response
  144. def r_disk_usage_io_last_day(node_id, mountpoint):
  145. mountpoint = base64.b64decode(mountpoint)
  146. return get_performance_data(node_id=node_id, mountpoint=mountpoint, the_class=HostDiskUsageIO, granularity='day')
  147. @Utils.dumps2response
  148. def r_disk_usage_io_last_seven_days(node_id, mountpoint):
  149. mountpoint = base64.b64decode(mountpoint)
  150. return get_performance_data(node_id=node_id, mountpoint=mountpoint, the_class=HostDiskUsageIO,
  151. granularity='seven_days')