schedule.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import logging
  4. import threading
  5. from plugins import ipc
  6. global_schedule_config = None
  7. class Schedule:
  8. def __init__(self, config):
  9. global global_schedule_config
  10. self.monitor_config = global_schedule_config
  11. if config is not None and config.__contains__('id'):
  12. self.monitor_config = config
  13. global_schedule_config = config
  14. # register plugins
  15. self.plugins = {
  16. 'ipc': ipc.IPC
  17. }
  18. def deliver(self):
  19. if self.monitor_config is None:
  20. logging.error('[SCHEDULE] deliver get monitor config but None')
  21. return
  22. if not self.monitor_config.__contains__('data'):
  23. logging.error('[SCHEDULE] deliver get config but no field "data"')
  24. return
  25. data = self.monitor_config['data']
  26. if not isinstance(data, list):
  27. logging.error("[SCHEDULE] deliver get config data but type %s, want <class 'list'>", type(data))
  28. return
  29. check_result = self.check_config(data)
  30. enable_data = check_result['enable']
  31. disable_data = check_result['disable']
  32. if len(disable_data) > 0:
  33. logging.error('[SCHEDULE] deliver config.data contain invalid item: %s', disable_data)
  34. for item in enable_data:
  35. plugin_type = item['type']
  36. if not self.plugins.__contains__(plugin_type):
  37. logging.error('[SCHEDULE] deliver parse item but received invalid plugin type: %s', plugin_type)
  38. continue
  39. plugin = self.plugins[plugin_type](**item)
  40. thread = threading.Thread(target=plugin.exec)
  41. thread.setDaemon(True)
  42. thread.start()
  43. @staticmethod
  44. def check_config(data_list):
  45. enable = []
  46. disable = []
  47. for item in data_list:
  48. # filter if item is disable
  49. if not isinstance(item, dict):
  50. disable.append(item)
  51. continue
  52. if not item.__contains__('host'):
  53. disable.append(item)
  54. continue
  55. if not item.__contains__('port'):
  56. disable.append(item)
  57. continue
  58. if not item.__contains__('dev_id'):
  59. disable.append(item)
  60. continue
  61. if not item.__contains__('type'):
  62. disable.append(item)
  63. continue
  64. if not item.__contains__('schema'):
  65. disable.append(item)
  66. continue
  67. if not item.__contains__('username'):
  68. disable.append(item)
  69. continue
  70. if not item.__contains__('pwd'):
  71. disable.append(item)
  72. continue
  73. # set default value if item doesn't contain the key
  74. if not item.__contains__('expr'):
  75. item['expr'] = 0
  76. if not item.__contains__('kpi'):
  77. item['kpi'] = {}
  78. enable.append(item)
  79. return {'enable': enable, 'disable': disable}
  80. def parse_collect_config(self):
  81. pass
  82. def parse_upload_config(self):
  83. pass