performance.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 CPUMemory, Traffic, DiskIO, Utils, Rules
  8. __author__ = 'James Iter'
  9. __date__ = '2017/7/2'
  10. __contact__ = 'james.iter.cn@gmail.com'
  11. __copyright__ = '(c) 2017 by James Iter.'
  12. blueprint = Blueprint(
  13. 'api_performance',
  14. __name__,
  15. url_prefix='/api/performance'
  16. )
  17. blueprints = Blueprint(
  18. 'api_performances',
  19. __name__,
  20. url_prefix='/api/performances'
  21. )
  22. cpu_memory = Base(the_class=CPUMemory, the_blueprint=blueprint, the_blueprints=blueprints)
  23. traffic = Base(the_class=Traffic, the_blueprint=blueprint, the_blueprints=blueprints)
  24. disk_io = Base(the_class=DiskIO, the_blueprint=blueprint, the_blueprints=blueprints)
  25. @Utils.dumps2response
  26. def r_cpu_memory_get_by_filter():
  27. return cpu_memory.get_by_filter()
  28. @Utils.dumps2response
  29. def r_traffic_get_by_filter():
  30. return traffic.get_by_filter()
  31. @Utils.dumps2response
  32. def r_disk_io_get_by_filter():
  33. return disk_io.get_by_filter()
  34. def get_performance_data(uuid, uuid_field, the_class=None, granularity='hour'):
  35. args_rules = [
  36. Rules.UUID.value,
  37. ]
  38. try:
  39. ji.Check.previewing(args_rules, {'uuid': uuid})
  40. uuids_str = ':'.join([uuid_field, 'in', uuid])
  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([uuids_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. 'memory_unused': None,
  78. 'rx_packets': None,
  79. 'rx_bytes': None,
  80. 'tx_packets': None,
  81. 'tx_bytes': None,
  82. 'rd_req': None,
  83. 'rd_bytes': 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(uuid):
  104. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='hour')
  105. @Utils.dumps2response
  106. def r_cpu_memory_last_six_hours(uuid):
  107. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='six_hours')
  108. @Utils.dumps2response
  109. def r_cpu_memory_last_day(uuid):
  110. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='day')
  111. @Utils.dumps2response
  112. def r_cpu_memory_last_seven_days(uuid):
  113. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='seven_days')
  114. @Utils.dumps2response
  115. def r_traffic_last_hour(uuid):
  116. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='hour')
  117. @Utils.dumps2response
  118. def r_traffic_last_six_hours(uuid):
  119. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='six_hours')
  120. @Utils.dumps2response
  121. def r_traffic_last_day(uuid):
  122. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='day')
  123. @Utils.dumps2response
  124. def r_traffic_last_seven_days(uuid):
  125. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='seven_days')
  126. @Utils.dumps2response
  127. def r_disk_io_last_hour(uuid):
  128. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='hour')
  129. @Utils.dumps2response
  130. def r_disk_io_last_six_hours(uuid):
  131. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='six_hours')
  132. @Utils.dumps2response
  133. def r_disk_io_last_day(uuid):
  134. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='day')
  135. @Utils.dumps2response
  136. def r_disk_io_last_seven_days(uuid):
  137. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='seven_days')
  138. @Utils.dumps2response
  139. def r_current_top_10():
  140. # JimV 设计的 Guests 容量为 4000 个
  141. volume = 4000
  142. limit = volume
  143. end_ts = ji.Common.ts() - 60
  144. start_ts = end_ts - 60
  145. # 避免落在时间边界上,导致过滤条件的范围落空
  146. if start_ts % 60 == 0:
  147. start_ts -= 1
  148. ret = dict()
  149. ret['state'] = ji.Common.exchange_state(20000)
  150. ret['data'] = {
  151. 'cpu_load': list(),
  152. 'rw_bytes': list(),
  153. 'rw_req': list(),
  154. 'rt_bytes': list(),
  155. 'rt_packets': list()
  156. }
  157. filter_str = ';'.join([':'.join(['timestamp', 'gt', start_ts.__str__()]),
  158. ':'.join(['timestamp', 'lt', end_ts.__str__()])])
  159. rows, _ = CPUMemory.get_by_filter(limit=limit, filter_str=filter_str)
  160. rows.sort(key=lambda k: k['cpu_load'], reverse=True)
  161. ret['data']['cpu_load'] = rows[0:10]
  162. rows, _ = DiskIO.get_by_filter(limit=limit, filter_str=filter_str)
  163. for i in range(rows.__len__()):
  164. rows[i]['rw_bytes'] = rows[i]['rd_bytes'] + rows[i]['wr_bytes']
  165. rows[i]['rw_req'] = rows[i]['rd_req'] + rows[i]['wr_req']
  166. rows.sort(key=lambda k: k['rw_bytes'], reverse=True)
  167. ret['data']['rw_bytes'] = rows[0:10]
  168. rows.sort(key=lambda k: k['rw_req'], reverse=True)
  169. ret['data']['rw_req'] = rows[0:10]
  170. rows, _ = Traffic.get_by_filter(limit=limit, filter_str=filter_str)
  171. for i in range(rows.__len__()):
  172. rows[i]['rt_bytes'] = rows[i]['rx_bytes'] + rows[i]['tx_bytes']
  173. rows[i]['rt_packets'] = rows[i]['rx_packets'] + rows[i]['tx_packets']
  174. rows.sort(key=lambda k: k['rt_bytes'], reverse=True)
  175. ret['data']['rt_bytes'] = rows[0:10]
  176. rows.sort(key=lambda k: k['rt_packets'], reverse=True)
  177. ret['data']['rt_packets'] = rows[0:10]
  178. return ret
  179. @Utils.dumps2response
  180. def r_last_10_minutes_top_10():
  181. _range = 10
  182. volume = 4000
  183. limit = volume * _range
  184. end_ts = ji.Common.ts() - 60
  185. start_ts = end_ts - 60 * _range
  186. # 避免落在时间边界上,导致过滤条件的范围落空
  187. if start_ts % 60 == 0:
  188. start_ts -= 1
  189. ret = dict()
  190. ret['state'] = ji.Common.exchange_state(20000)
  191. ret['data'] = {
  192. 'cpu_load': list(),
  193. 'rw_bytes': list(),
  194. 'rw_req': list(),
  195. 'rt_bytes': list(),
  196. 'rt_packets': list()
  197. }
  198. filter_str = ';'.join([':'.join(['timestamp', 'gt', start_ts.__str__()]),
  199. ':'.join(['timestamp', 'lt', end_ts.__str__()])])
  200. # cpu 负载
  201. guests_uuid_mapping = dict()
  202. rows, _ = CPUMemory.get_by_filter(limit=limit, filter_str=filter_str)
  203. for row in rows:
  204. if row['guest_uuid'] not in guests_uuid_mapping:
  205. guests_uuid_mapping[row['guest_uuid']] = {'cpu_load': 0, 'count': 0}
  206. guests_uuid_mapping[row['guest_uuid']]['cpu_load'] += row['cpu_load']
  207. guests_uuid_mapping[row['guest_uuid']]['count'] += 1
  208. rows = list()
  209. for k, v in guests_uuid_mapping.items():
  210. # 忽略除数为 0 的情况
  211. if v['cpu_load'] == 0:
  212. continue
  213. rows.append({'guest_uuid': k, 'cpu_load': v['cpu_load'] / v['count']})
  214. rows.sort(key=lambda _k: _k['cpu_load'], reverse=True)
  215. ret['data']['cpu_load'] = rows[0:10]
  216. # 磁盘使用统计
  217. guests_uuid_mapping.clear()
  218. rows, _ = DiskIO.get_by_filter(limit=limit, filter_str=filter_str)
  219. for row in rows:
  220. if row['disk_uuid'] not in guests_uuid_mapping:
  221. guests_uuid_mapping[row['disk_uuid']] = {'rw_bytes': 0, 'rw_req': 0}
  222. guests_uuid_mapping[row['disk_uuid']]['rw_bytes'] += row['rd_bytes'] + row['wr_bytes']
  223. guests_uuid_mapping[row['disk_uuid']]['rw_req'] += row['rd_req'] + row['wr_req']
  224. rows = list()
  225. for k, v in guests_uuid_mapping.items():
  226. rows.append({'disk_uuid': k, 'rw_bytes': v['rw_bytes'] * 60 * _range, 'rw_req': v['rw_req'] * 60 * _range})
  227. rows.sort(key=lambda _k: _k['rw_bytes'], reverse=True)
  228. ret['data']['rw_bytes'] = rows[0:10]
  229. rows.sort(key=lambda _k: _k['rw_req'], reverse=True)
  230. ret['data']['rw_req'] = rows[0:10]
  231. # 网络流量
  232. guests_uuid_mapping.clear()
  233. rows, _ = Traffic.get_by_filter(limit=limit, filter_str=filter_str)
  234. for row in rows:
  235. if row['guest_uuid'] not in guests_uuid_mapping:
  236. guests_uuid_mapping[row['guest_uuid']] = {'rt_bytes': 0, 'rt_packets': 0}
  237. guests_uuid_mapping[row['guest_uuid']]['rt_bytes'] += row['rx_bytes'] + row['tx_bytes']
  238. guests_uuid_mapping[row['guest_uuid']]['rt_packets'] += row['rx_packets'] + row['tx_packets']
  239. rows = list()
  240. for k, v in guests_uuid_mapping.items():
  241. rows.append({'guest_uuid': k, 'rt_bytes': v['rt_bytes'] * 60 * _range,
  242. 'rt_packets': v['rt_packets'] * 60 * _range})
  243. rows.sort(key=lambda _k: _k['rt_bytes'], reverse=True)
  244. ret['data']['rt_bytes'] = rows[0:10]
  245. rows.sort(key=lambda _k: _k['rt_packets'], reverse=True)
  246. ret['data']['rt_packets'] = rows[0:10]
  247. return ret