1
0

graph_urls.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. import json
  16. import copy
  17. import re
  18. from rrd import config
  19. from rrd.model.tmpgraph import TmpGraph
  20. from rrd.model.endpoint import Endpoint, EndpointCounter
  21. def generate_graph_urls(graph, start, end):
  22. counters = graph.counters or []
  23. if not counters:
  24. return []
  25. endpoint_list = graph.hosts or []
  26. if not endpoint_list:
  27. return []
  28. endpoint_objs = Endpoint.gets_by_endpoint(endpoint_list)
  29. if not endpoint_objs:
  30. return []
  31. endpoint_ids = [x.id for x in endpoint_objs]
  32. counters = []
  33. for c in graph.counters:
  34. if c.find("metric=") == -1:
  35. counters.append(c)
  36. else:
  37. metric=""
  38. tags = []
  39. qs = []
  40. c = c.strip()
  41. for q in c.split():
  42. q = q.strip()
  43. if q.startswith("metric="):
  44. metric = q.replace("metric=", "^", 1)
  45. qs.append(metric)
  46. else:
  47. qs.append(q)
  48. tags.append(q)
  49. counter_objs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids[:], limit=100)
  50. if not counter_objs:
  51. continue
  52. for co in counter_objs:
  53. if not re.search('^%s(/|$)' %metric, co.counter):
  54. continue
  55. matched = True
  56. for tag in tags:
  57. if not re.search('(/|,)%s(,|$)' %tag, co.counter):
  58. matched = False
  59. break
  60. if not matched:
  61. continue
  62. counters.append(co.counter)
  63. if not counters:
  64. return []
  65. counters = sorted(list(set(counters)))
  66. return _generate_graph_urls(graph, counters, endpoint_list, start, end)
  67. def _generate_graph_urls(graph, counters, endpoint_list, start, end):
  68. ret_graphs = []
  69. if graph.graph_type == 'h':
  70. for c in counters:
  71. tmp_graph_id = TmpGraph.add(endpoint_list, [c,])
  72. if not tmp_graph_id:
  73. break
  74. new_g = copy.deepcopy(graph)
  75. new_g.counters = c
  76. if end:
  77. new_g.src = '''/chart/h?id=%s&start=%s&end=%s''' %(tmp_graph_id, start or (0-graph.timespan), end)
  78. else:
  79. new_g.src = '''/chart/h?id=%s&start=%s''' %(tmp_graph_id, start or (0-graph.timespan))
  80. if graph.method == 'SUM':
  81. new_g.src += "&sum=on"
  82. else:
  83. new_g.src += "&cf=%s" %graph.method
  84. ret_graphs.append(new_g)
  85. elif graph.graph_type=='k':
  86. for e in endpoint_list:
  87. tmp_graph_id = TmpGraph.add([e,], counters)
  88. if not tmp_graph_id:
  89. break
  90. new_g = copy.deepcopy(graph)
  91. new_g.hosts = e
  92. if end:
  93. new_g.src = '''/chart/k?id=%s&start=%s&end=%s''' %(tmp_graph_id, start or (0-graph.timespan), end)
  94. else:
  95. new_g.src = '''/chart/k?id=%s&start=%s''' %(tmp_graph_id, start or (0-graph.timespan))
  96. if graph.method == 'SUM':
  97. new_g.src += "&sum=on"
  98. else:
  99. new_g.src += "&cf=%s" %graph.method
  100. ret_graphs.append(new_g)
  101. else:
  102. #组合视角
  103. tmp_graph_id = TmpGraph.add(endpoint_list, counters)
  104. if not tmp_graph_id:
  105. return []
  106. new_g = copy.deepcopy(graph)
  107. if end:
  108. new_g.src = '''/chart/a?id=%s&start=%s&end=%s''' %(tmp_graph_id, start or (0-graph.timespan), end)
  109. else:
  110. new_g.src = '''/chart/a?id=%s&start=%s''' %(tmp_graph_id, start or (0-graph.timespan))
  111. if graph.method == 'SUM':
  112. new_g.src += "&sum=on"
  113. else:
  114. new_g.src += "&cf=%s" %graph.method
  115. ret_graphs.append(new_g)
  116. return ret_graphs