api.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from rrd import app
  17. from flask import request, jsonify, render_template
  18. from rrd.store import db
  19. from rrd.model.user import User
  20. from rrd.model.team import Team
  21. from rrd.model.portal.template import Template
  22. from rrd.model.portal.action import Action
  23. from rrd.model.portal.host_group import HostGroup
  24. from rrd.model.portal.host import Host
  25. from rrd.model.portal.expression import Expression
  26. from rrd.model.portal.strategy import Strategy
  27. from rrd import corelib, config
  28. from rrd.utils.logger import logging
  29. log = logging.getLogger(__file__)
  30. @app.route("/favicon.ico")
  31. def favicon():
  32. return ""
  33. @app.route('/api/version')
  34. def api_version():
  35. return '2.0.0'
  36. @app.route('/api/health')
  37. def api_health():
  38. return 'ok'
  39. @app.route('/api/user/<int:user_id>/inteams/<team_names>')
  40. def api_user_in_teams(user_id, team_names):
  41. u = User.get_by_id(user_id)
  42. if not u:
  43. return jsonify(data=False)
  44. team_list = team_names.split(",") or []
  45. if u.in_teams(team_list):
  46. return jsonify(data=True)
  47. else:
  48. return jsonify(data=False)
  49. @app.route('/api/uic/group')
  50. def api_query_uic_group():
  51. q = request.args.get('query', '').strip()
  52. limit = int(request.args.get('limit', '10'))
  53. teams = Team.get_teams(q, limit)
  54. log.debug(teams)
  55. r = [x.dict() for x in teams]
  56. return jsonify(data=r)
  57. @app.route('/api/template/query')
  58. def api_template_query():
  59. q = request.args.get('query', '').strip()
  60. limit = int(request.args.get('limit', '10'))
  61. ts, _ = Template.query(1, limit, q)
  62. ts = [t.to_json() for t in ts]
  63. return jsonify(data=ts)
  64. @app.route('/api/template/<tpl_id>')
  65. def api_template_get(tpl_id):
  66. tpl_id = int(tpl_id)
  67. t = Template.get(tpl_id)
  68. if not t:
  69. return jsonify(msg='no such tpl')
  70. return jsonify(msg='', data=t.to_json())
  71. @app.route('/api/action/<action_id>')
  72. def api_action_get(action_id):
  73. action_id = int(action_id)
  74. a = Action.get(action_id)
  75. if not a:
  76. return jsonify(msg="no such action")
  77. return jsonify(msg='', data=a.to_json())
  78. @app.route("/api/expression/<exp_id>")
  79. def api_expression_get(exp_id):
  80. exp_id = int(exp_id)
  81. expression = Expression.get(exp_id)
  82. if not expression:
  83. return jsonify(msg="no such expression")
  84. return jsonify(msg='', data=expression.to_json())
  85. @app.route("/api/strategy/<s_id>")
  86. def api_strategy_get(s_id):
  87. s_id = int(s_id)
  88. s = Strategy.get(s_id)
  89. if not s:
  90. return jsonify(msg="no such strategy")
  91. return jsonify(msg='', data=s.to_json())
  92. @app.route('/api/metric/query')
  93. def api_metric_query():
  94. q = request.args.get('query', '').strip()
  95. limit = int(request.args.get('limit', '10'))
  96. h = {"Content-type": "application/json"}
  97. r = corelib.auth_requests("GET", "%s/metric/default_list" \
  98. %(config.API_ADDR,), headers=h)
  99. if r.status_code != 200:
  100. log.error("%s:%s" %(r.status_code, r.text))
  101. return []
  102. metrics = r.json() or []
  103. matched_metrics = [x for x in metrics if q in x]
  104. ret_data = [q,] + matched_metrics[:limit]
  105. return jsonify(data=[{'name': name} for name in ret_data])
  106. # 给ping监控提供的接口
  107. @app.route('/api/pings')
  108. def api_pings_get():
  109. names = db.query_column("select hostname from host")
  110. return jsonify(hosts=names)
  111. @app.route('/api/debug')
  112. def api_debug():
  113. return render_template('portal/debug/index.html')
  114. @app.route('/api/group/<grp_name>/hosts.json')
  115. def api_group_hosts_json(grp_name):
  116. group = HostGroup.read(where='id = %s', params=[grp_name])
  117. if not group:
  118. group = HostGroup.read(where='grp_name = %s', params=[grp_name])
  119. if not group:
  120. return jsonify(msg='no such group %s' % grp_name)
  121. vs, _ = Host.query(1, 10000000, '', '0', group.id)
  122. names = [v.hostname for v in vs]
  123. return jsonify(msg='', data=names)