1
0

alarm.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. from .bean import Bean
  16. from rrd.store import alarm_db
  17. class Event(Bean):
  18. _db = alarm_db
  19. _tbl = 'events'
  20. _cols = 'id, event_caseId, step, cond, status, timestamp'
  21. def __init__(self, id, event_caseId, step, cond, status, timestamp):
  22. self.id = id
  23. self.event_caseId = event_caseId
  24. self.step = step
  25. self.cond = cond
  26. self.status = status
  27. self.timestamp = timestamp
  28. @classmethod
  29. def query(cls, page, limit, event_caseId):
  30. where = 'event_caseId = %s'
  31. params = [event_caseId]
  32. vs = cls.select_vs(where=where, params=params, page=page, limit=limit, order='timestamp desc')
  33. total = cls.total(where, params)
  34. return vs, total
  35. class EventCase(Bean):
  36. _db = alarm_db
  37. _tbl = 'event_cases'
  38. _cols = 'id, endpoint, metric, func, cond, note, max_step, current_step, priority, status, timestamp, update_at, closed_at, closed_note, user_modified, tpl_creator, expression_id, strategy_id, template_id, process_note, process_status'
  39. def __init__(self, id, endpoint, metric, func, cond, note, max_step, current_step, priority,\
  40. status, timestamp, update_at, closed_at, closed_note, user_modified, tpl_creator, \
  41. expression_id, strategy_id, template_id, process_note, process_status):
  42. self.id = id
  43. self.endpoint = endpoint
  44. self.metric = metric
  45. self.func = func
  46. self.cond = cond
  47. self.note = note
  48. self.max_step = max_step
  49. self.current_step = current_step
  50. self.priority = priority
  51. self.status = status
  52. self.timestamp = timestamp
  53. self.update_at = update_at
  54. self.closed_at = closed_at
  55. self.closed_note = closed_note
  56. self.user_modified = user_modified
  57. self.tpl_creator = tpl_creator
  58. self.expression_id = expression_id
  59. self.strategy_id = strategy_id
  60. self.template_id = template_id
  61. self.process_note = process_note
  62. self.process_status = process_status
  63. @classmethod
  64. def query(cls, page, limit, endpoint_query, metric_query, status):
  65. where = '1=1'
  66. params = []
  67. if status == "PROBLEM" or status == "OK":
  68. where = 'status = %s'
  69. params = [status]
  70. if endpoint_query != "":
  71. where += ' and endpoint like %s'
  72. params.append('%' + endpoint_query + '%')
  73. if metric_query != "":
  74. where += ' and metric like %s'
  75. params.append('%' + metric_query + '%')
  76. vs = cls.select_vs(where=where, params=params, page=page, limit=limit, order='update_at desc')
  77. total = cls.total(where, params)
  78. return vs, total
  79. class EventNote(Bean):
  80. _db = alarm_db
  81. _tbl = 'event_note'
  82. _cols = 'id, event_caseId, note, case_id, status, timestamp, user_id'
  83. def __init__(self, id, event_caseId, note, case_id, status, timestamp, user_id):
  84. self.id = id
  85. self.event_caseId = event_caseId
  86. self.note = note
  87. self.case_id = case_id
  88. self.status = status
  89. self.timestamp = timestamp
  90. self.user_id = user_id
  91. #desc events;
  92. #+--------------+------------------+------+-----+-------------------+-----------------------------+
  93. #| Field | Type | Null | Key | Default | Extra |
  94. #+--------------+------------------+------+-----+-------------------+-----------------------------+
  95. #| id | mediumint(9) | NO | PRI | NULL | auto_increment |
  96. #| event_caseId | varchar(50) | YES | MUL | NULL | |
  97. #| step | int(10) unsigned | YES | | NULL | |
  98. #| cond | varchar(200) | NO | | NULL | |
  99. #| status | int(3) unsigned | YES | | 0 | |
  100. #| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  101. #+--------------+------------------+------+-----+-------------------+-----------------------------+
  102. #
  103. #desc event_note;
  104. #+--------------+------------------+------+-----+-------------------+-----------------------------+
  105. #| Field | Type | Null | Key | Default | Extra |
  106. #+--------------+------------------+------+-----+-------------------+-----------------------------+
  107. #| id | mediumint(9) | NO | PRI | NULL | auto_increment |
  108. #| event_caseId | varchar(50) | YES | MUL | NULL | |
  109. #| note | varchar(300) | YES | | NULL | |
  110. #| case_id | varchar(20) | YES | | NULL | |
  111. #| status | varchar(15) | YES | | NULL | |
  112. #| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  113. #| user_id | int(10) unsigned | YES | MUL | NULL | |
  114. #+--------------+------------------+------+-----+-------------------+-----------------------------+
  115. #
  116. #desc event_cases;
  117. #+----------------+------------------+------+-----+-------------------+-----------------------------+
  118. #| Field | Type | Null | Key | Default | Extra |
  119. #+----------------+------------------+------+-----+-------------------+-----------------------------+
  120. #| id | varchar(50) | NO | PRI | NULL | |
  121. #| endpoint | varchar(100) | NO | MUL | NULL | |
  122. #| metric | varchar(200) | NO | | NULL | |
  123. #| func | varchar(50) | YES | | NULL | |
  124. #| cond | varchar(200) | NO | | NULL | |
  125. #| note | varchar(500) | YES | | NULL | |
  126. #| max_step | int(10) unsigned | YES | | NULL | |
  127. #| current_step | int(10) unsigned | YES | | NULL | |
  128. #| priority | int(6) | NO | | NULL | |
  129. #| status | varchar(20) | NO | | NULL | |
  130. #| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  131. #| update_at | timestamp | YES | | NULL | |
  132. #| closed_at | timestamp | YES | | NULL | |
  133. #| closed_note | varchar(250) | YES | | NULL | |
  134. #| user_modified | int(10) unsigned | YES | | NULL | |
  135. #| tpl_creator | varchar(64) | YES | | NULL | |
  136. #| expression_id | int(10) unsigned | YES | | NULL | |
  137. #| strategy_id | int(10) unsigned | YES | | NULL | |
  138. #| template_id | int(10) unsigned | YES | | NULL | |
  139. #| process_note | mediumint(9) | YES | | NULL | |
  140. #| process_status | varchar(20) | YES | | unresolved | |
  141. #+----------------+------------------+------+-----+-------------------+-----------------------------+