api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding:utf-8 -*-
  2. __author__ = 'Ulric Qin'
  3. from rrd import app
  4. from flask import request, jsonify, render_template
  5. from rrd.store import db
  6. from rrd.model.user import User
  7. from rrd.model.team import Team
  8. from rrd.model.portal.template import Template
  9. from rrd.model.portal.action import Action
  10. from rrd.model.portal.host_group import HostGroup
  11. from rrd.model.portal.host import Host
  12. from rrd.model.portal.expression import Expression
  13. from rrd.model.portal.strategy import Strategy
  14. from rrd import corelib, config
  15. from rrd.utils.logger import logging
  16. log = logging.getLogger(__file__)
  17. @app.route("/favicon.ico")
  18. def favicon():
  19. return ""
  20. @app.route('/api/version')
  21. def api_version():
  22. return '2.0.0'
  23. @app.route('/api/health')
  24. def api_health():
  25. return 'ok'
  26. @app.route('/api/user/<int:user_id>/inteams/<team_names>')
  27. def api_user_in_teams(user_id, team_names):
  28. u = User.get_by_id(user_id)
  29. if not u:
  30. return jsonify(data=False)
  31. team_list = team_names.split(",") or []
  32. if u.in_teams(team_list):
  33. return jsonify(data=True)
  34. else:
  35. return jsonify(data=False)
  36. @app.route('/api/uic/group')
  37. def api_query_uic_group():
  38. q = request.args.get('query', '').strip()
  39. limit = int(request.args.get('limit', '10'))
  40. teams = Team.get_teams(q, limit)
  41. log.debug(teams)
  42. r = [x.dict() for x in teams]
  43. return jsonify(data=r)
  44. @app.route('/api/template/query')
  45. def api_template_query():
  46. q = request.args.get('query', '').strip()
  47. limit = int(request.args.get('limit', '10'))
  48. ts, _ = Template.query(1, limit, q)
  49. ts = [t.to_json() for t in ts]
  50. return jsonify(data=ts)
  51. @app.route('/api/template/<tpl_id>')
  52. def api_template_get(tpl_id):
  53. tpl_id = int(tpl_id)
  54. t = Template.get(tpl_id)
  55. if not t:
  56. return jsonify(msg='no such tpl')
  57. return jsonify(msg='', data=t.to_json())
  58. @app.route('/api/action/<action_id>')
  59. def api_action_get(action_id):
  60. action_id = int(action_id)
  61. a = Action.get(action_id)
  62. if not a:
  63. return jsonify(msg="no such action")
  64. return jsonify(msg='', data=a.to_json())
  65. @app.route("/api/expression/<exp_id>")
  66. def api_expression_get(exp_id):
  67. exp_id = int(exp_id)
  68. expression = Expression.get(exp_id)
  69. if not expression:
  70. return jsonify(msg="no such expression")
  71. return jsonify(msg='', data=expression.to_json())
  72. @app.route("/api/strategy/<s_id>")
  73. def api_strategy_get(s_id):
  74. s_id = int(s_id)
  75. s = Strategy.get(s_id)
  76. if not s:
  77. return jsonify(msg="no such strategy")
  78. return jsonify(msg='', data=s.to_json())
  79. @app.route('/api/metric/query')
  80. def api_metric_query():
  81. q = request.args.get('query', '').strip()
  82. limit = int(request.args.get('limit', '10'))
  83. h = {"Content-type": "application/json"}
  84. r = corelib.auth_requests("GET", "%s/metric/default_list" \
  85. %(config.API_ADDR,), headers=h)
  86. if r.status_code != 200:
  87. log.error("%s:%s" %(r.status_code, r.text))
  88. return []
  89. metrics = r.json() or []
  90. metrics = [q,] + metrics
  91. return jsonify(data=[{'name': name} for name in metrics])
  92. # 给ping监控提供的接口
  93. @app.route('/api/pings')
  94. def api_pings_get():
  95. names = db.query_column("select hostname from host")
  96. return jsonify(hosts=names)
  97. @app.route('/api/debug')
  98. def api_debug():
  99. return render_template('portal/debug/index.html')
  100. @app.route('/api/group/<grp_name>/hosts.json')
  101. def api_group_hosts_json(grp_name):
  102. group = HostGroup.read(where='id = %s', params=[grp_name])
  103. if not group:
  104. group = HostGroup.read(where='grp_name = %s', params=[grp_name])
  105. if not group:
  106. return jsonify(msg='no such group %s' % grp_name)
  107. vs, _ = Host.query(1, 10000000, '', '0', group.id)
  108. names = [v.hostname for v in vs]
  109. return jsonify(msg='', data=names)