performance.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. for i in range(10):
  162. if rows[i]['cpu_load'] == 0:
  163. break
  164. ret['data']['cpu_load'].append(rows[i])
  165. rows, _ = DiskIO.get_by_filter(limit=limit, filter_str=filter_str)
  166. for i in range(rows.__len__()):
  167. rows[i]['rw_bytes'] = rows[i]['rd_bytes'] + rows[i]['wr_bytes']
  168. rows[i]['rw_req'] = rows[i]['rd_req'] + rows[i]['wr_req']
  169. rows.sort(key=lambda k: k['rw_bytes'], reverse=True)
  170. for i in range(10):
  171. if rows[i]['rw_req'] == 0:
  172. break
  173. ret['data']['rw_bytes'].append(rows[i])
  174. rows.sort(key=lambda k: k['rw_req'], reverse=True)
  175. for i in range(10):
  176. if rows[i]['rw_req'] == 0:
  177. break
  178. ret['data']['rw_req'].append(rows[i])
  179. rows, _ = Traffic.get_by_filter(limit=limit, filter_str=filter_str)
  180. for i in range(rows.__len__()):
  181. rows[i]['rt_bytes'] = rows[i]['rx_bytes'] + rows[i]['tx_bytes']
  182. rows[i]['rt_packets'] = rows[i]['rx_packets'] + rows[i]['tx_packets']
  183. rows.sort(key=lambda k: k['rt_bytes'], reverse=True)
  184. for i in range(10):
  185. if rows[i]['rt_packets'] == 0:
  186. break
  187. ret['data']['rt_bytes'].append(rows[i])
  188. rows.sort(key=lambda k: k['rt_packets'], reverse=True)
  189. for i in range(10):
  190. if rows[i]['rt_packets'] == 0:
  191. break
  192. ret['data']['rt_packets'].append(rows[i])
  193. return ret
  194. @Utils.dumps2response
  195. def r_last_the_range_minutes_top_10(_range):
  196. volume = 4000
  197. limit = volume * _range
  198. end_ts = ji.Common.ts() - 60
  199. start_ts = end_ts - 60 * _range
  200. # 避免落在时间边界上,导致过滤条件的范围落空
  201. if start_ts % 60 == 0:
  202. start_ts -= 1
  203. ret = dict()
  204. ret['state'] = ji.Common.exchange_state(20000)
  205. ret['data'] = {
  206. 'cpu_load': list(),
  207. 'rw_bytes': list(),
  208. 'rw_req': list(),
  209. 'rt_bytes': list(),
  210. 'rt_packets': list()
  211. }
  212. filter_str = ';'.join([':'.join(['timestamp', 'gt', start_ts.__str__()]),
  213. ':'.join(['timestamp', 'lt', end_ts.__str__()])])
  214. # cpu 负载
  215. guests_uuid_mapping = dict()
  216. rows, _ = CPUMemory.get_by_filter(limit=limit, filter_str=filter_str)
  217. for row in rows:
  218. if row['guest_uuid'] not in guests_uuid_mapping:
  219. guests_uuid_mapping[row['guest_uuid']] = {'cpu_load': 0, 'count': 0}
  220. guests_uuid_mapping[row['guest_uuid']]['cpu_load'] += row['cpu_load']
  221. guests_uuid_mapping[row['guest_uuid']]['count'] += 1.0
  222. rows = list()
  223. for k, v in guests_uuid_mapping.items():
  224. # 忽略除数为 0 的情况
  225. if v['cpu_load'] == 0:
  226. continue
  227. rows.append({'guest_uuid': k, 'cpu_load': v['cpu_load'] / v['count']})
  228. rows.sort(key=lambda _k: _k['cpu_load'], reverse=True)
  229. ret['data']['cpu_load'] = rows[0:10]
  230. # 磁盘使用统计
  231. guests_uuid_mapping.clear()
  232. rows, _ = DiskIO.get_by_filter(limit=limit, filter_str=filter_str)
  233. for row in rows:
  234. if row['disk_uuid'] not in guests_uuid_mapping:
  235. guests_uuid_mapping[row['disk_uuid']] = {'rw_bytes': 0, 'rw_req': 0}
  236. guests_uuid_mapping[row['disk_uuid']]['rw_bytes'] += row['rd_bytes'] + row['wr_bytes']
  237. guests_uuid_mapping[row['disk_uuid']]['rw_req'] += row['rd_req'] + row['wr_req']
  238. rows = list()
  239. for k, v in guests_uuid_mapping.items():
  240. # 过滤掉无操作的数据
  241. if v['rw_req'] == 0:
  242. continue
  243. rows.append({'disk_uuid': k, 'rw_bytes': v['rw_bytes'] * 60 * _range, 'rw_req': v['rw_req'] * 60 * _range})
  244. rows.sort(key=lambda _k: _k['rw_bytes'], reverse=True)
  245. ret['data']['rw_bytes'] = rows[0:10]
  246. rows.sort(key=lambda _k: _k['rw_req'], reverse=True)
  247. ret['data']['rw_req'] = rows[0:10]
  248. # 网络流量
  249. guests_uuid_mapping.clear()
  250. rows, _ = Traffic.get_by_filter(limit=limit, filter_str=filter_str)
  251. for row in rows:
  252. if row['guest_uuid'] not in guests_uuid_mapping:
  253. guests_uuid_mapping[row['guest_uuid']] = {'rt_bytes': 0, 'rt_packets': 0}
  254. guests_uuid_mapping[row['guest_uuid']]['rt_bytes'] += row['rx_bytes'] + row['tx_bytes']
  255. guests_uuid_mapping[row['guest_uuid']]['rt_packets'] += row['rx_packets'] + row['tx_packets']
  256. rows = list()
  257. for k, v in guests_uuid_mapping.items():
  258. # 过滤掉无流量的数据
  259. if v['rt_packets'] == 0:
  260. continue
  261. rows.append({'guest_uuid': k, 'rt_bytes': v['rt_bytes'] * 60 * _range,
  262. 'rt_packets': v['rt_packets'] * 60 * _range})
  263. rows.sort(key=lambda _k: _k['rt_bytes'], reverse=True)
  264. ret['data']['rt_bytes'] = rows[0:10]
  265. rows.sort(key=lambda _k: _k['rt_packets'], reverse=True)
  266. ret['data']['rt_packets'] = rows[0:10]
  267. return ret
  268. @Utils.dumps2response
  269. def r_last_10_minutes_top_10():
  270. return r_last_the_range_minutes_top_10(_range=10)
  271. @Utils.dumps2response
  272. def r_last_hour_top_10():
  273. return r_last_the_range_minutes_top_10(_range=60)
  274. @Utils.dumps2response
  275. def r_last_six_hours_top_10():
  276. return r_last_the_range_minutes_top_10(_range=60 * 6)
  277. @Utils.dumps2response
  278. def r_last_day_top_10():
  279. return r_last_the_range_minutes_top_10(_range=60 * 24)