host_performance.py 5.8 KB

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