graph.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #-*- coding:utf-8 -*-
  2. import json
  3. from rrd.config import API_ADDR
  4. from rrd import corelib
  5. class DashboardGraph(object):
  6. def __init__(self, id, title, hosts, counters, screen_id,
  7. timespan=3600, graph_type='h', method='', position=0):
  8. self.id = str(id)
  9. self.title = title
  10. self.hosts = hosts or []
  11. self.counters = counters or []
  12. self.screen_id = str(screen_id)
  13. self.timespan = timespan
  14. self.graph_type = graph_type
  15. self.method = method.upper() # method can be ["", "sum", "average", "max", "min", "last"]
  16. self.position = position or self.id # init as self.id
  17. def __repr__(self):
  18. return "<DashboardGraph id=%s, title=%s, screen_id=%s>" %(self.id, self.title, self.screen_id)
  19. __str__ = __repr__
  20. @classmethod
  21. def gets_by_screen_id(cls, screen_id):
  22. h = {"Content-type": "application/json"}
  23. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/graphs/screen/%s" %(screen_id,), headers=h)
  24. if r.status_code != 200:
  25. raise Exception(r.text)
  26. j = r.json()
  27. return [cls(*[x["graph_id"], x["title"], x["endpoints"], x["counters"], \
  28. x["screen_id"], x["timespan"], x["graph_type"], x["method"], x["position"]]) for x in j]
  29. @classmethod
  30. def get(cls, id):
  31. h = {"Content-type": "application/json"}
  32. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/graph/%s" %(id,), headers=h)
  33. if r.status_code != 200:
  34. raise Exception(r.text)
  35. x = r.json()
  36. return x and cls(*[x["graph_id"], x["title"], x["endpoints"], x["counters"], \
  37. x["screen_id"], x["timespan"], x["graph_type"], x["method"], x["position"]])
  38. @classmethod
  39. def add(cls, title, hosts, counters, screen_id,
  40. timespan=3600, graph_type='h', method='', position=0):
  41. d = {
  42. "screen_id": int(screen_id),
  43. "title": title,
  44. "endpoints": hosts,
  45. "counters": counters,
  46. "timespan": int(timespan),
  47. "graph_type": graph_type,
  48. "method": method,
  49. "position": int(position),
  50. "falcon_tags": "",
  51. }
  52. h = {"Content-type": "application/json"}
  53. r = corelib.auth_requests("POST", API_ADDR + "/dashboard/graph", data = json.dumps(d), headers =h )
  54. if r.status_code != 200:
  55. raise Exception(r.text)
  56. j = r.json()
  57. graph_id = j and j.get("id")
  58. return graph_id and cls.get(graph_id)
  59. @classmethod
  60. def remove(cls, id):
  61. h = {"Content-type": "application/json"}
  62. r = corelib.auth_requests("DELETE", API_ADDR + "/dashboard/graph/%s" %(id,), headers=h)
  63. if r.status_code != 200:
  64. raise Exception(r.text)
  65. return r.json()
  66. def update(self, title=None, hosts=None, counters=None, screen_id=None,
  67. timespan=None, graph_type=None, method=None, position=None):
  68. title = self.title if title is None else title
  69. hosts = self.hosts if hosts is None else hosts
  70. counters = self.counters if counters is None else counters
  71. screen_id = screen_id or self.screen_id
  72. timespan = timespan or self.timespan
  73. graph_type = graph_type or self.graph_type
  74. method = method if method is not None else self.method
  75. position = position or self.position
  76. d = {
  77. "screen_id": int(screen_id),
  78. "title": title,
  79. "endpoints": hosts,
  80. "counters": counters,
  81. "timespan": int(timespan),
  82. "graph_type": graph_type,
  83. "method": method,
  84. "position": int(position),
  85. "falcon_tags": "",
  86. }
  87. h = {"Content-type": "application/json"}
  88. r = corelib.auth_requests("PUT", API_ADDR + "/dashboard/graph/%s" %(self.id,), data = json.dumps(d), headers =h )
  89. if r.status_code != 200:
  90. raise Exception(r.text)
  91. j = r.json()
  92. graph_id = j and j.get("id")
  93. return graph_id and cls.get(graph_id)
  94. @classmethod
  95. def update_multi(cls, rows):
  96. for x in rows:
  97. id = x["id"]
  98. hosts = x["hosts"] or []
  99. counters = x["counters"] or []
  100. grh = cls.get(id)
  101. grh and grh.update(hosts=hosts, counters=counters)