Browse Source

complement the schedule, not complete.

tangs 6 years ago
parent
commit
1f98dfd022
1 changed files with 87 additions and 2 deletions
  1. 87 2
      schedule.py

+ 87 - 2
schedule.py

@@ -2,15 +2,100 @@
 # -*- coding:utf-8 -*-
 
 
+import logging
+import threading
+
+from plugins import ipc
+
+global_schedule_config = {}
+
 
 class Schedule:
-    def __init__(self):
-        self.monitor_config = {}
+    def __init__(self, config):
+        global global_schedule_config
+
+        self.monitor_config = global_schedule_config
+        if config is not None and config.__contains__('id'):
+            self.monitor_config = config
+            global_schedule_config = config
+
+        # register plugins
+        self.plugins = {
+            'ipc': ipc.IPC
+        }
 
     def deliver(self):
         if self.monitor_config is None:
+            logging.debug('[SCHEDULE] deliver get monitor config but None')
+            return
+
+        if not self.monitor_config.__contains__('data'):
+            logging.debug('[SCHEDULE] deliver get config but no field "data"')
+            return
+
+        data = self.monitor_config['data']
+        if not isinstance(data, list):
+            logging.error("[SCHEDULE] deliver get config data but type %s, want <class 'list'>", type(data))
             return
 
+        check_result = self.check_config(data)
+        enable_data = check_result['enable']
+        disable_data = check_result['disable']
+        if len(disable_data) > 0:
+            logging.error('[SCHEDULE] deliver config.data contain invalid item: %s', disable_data)
+
+        for item in enable_data:
+            plugin_type = item['type']
+            if not self.plugins.__contains__(plugin_type):
+                logging.error('[SCHEDULE] deliver parse item but received invalid plugin type: %s', plugin_type)
+                continue
+
+            plugin = self.plugins[plugin_type]()
+            thread = threading.Thread(target=plugin.exec)
+            thread.setDaemon(True)
+            thread.start()
+
+    @staticmethod
+    def check_config(self, data_list):
+        enable = []
+        disable = []
+        for item in data_list:
+            # filter if item is disable
+            if not isinstance(item, object):
+                disable.append(item)
+                continue
+            if not item.__contains__('dev_ip'):
+                disable.append(item)
+                continue
+            if not item.__contains__('port'):
+                disable.append(item)
+                continue
+            if not item.__contains__('dev_id'):
+                disable.append(item)
+                continue
+            if not item.__contains__('type'):
+                disable.append(item)
+                continue
+            if not item.__contains__('schema'):
+                disable.append(item)
+                continue
+            if not item.__contains__('login_name'):
+                disable.append(item)
+                continue
+            if not item.__contains__('pwd'):
+                disable.append(item)
+                continue
+
+            # set default value if item doesn't contain the key
+            if not item.__contains__('expr'):
+                item.expr = 0
+            if not item.__contains__('kpi'):
+                item.kpi = {}
+
+            enable.append(item)
+
+        return {'enable': enable, 'disable': disable}
+
     def parse_collect_config(self):
         pass