#!/usr/bin/env python # -*- coding:utf-8 -*- import logging import threading from plugins import ipc global_schedule_config = None class Schedule: 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.error('[SCHEDULE] deliver get monitor config but None') return if not self.monitor_config.__contains__('data'): logging.error('[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 ", 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](**item) thread = threading.Thread(target=plugin.exec) thread.setDaemon(True) thread.start() @staticmethod def check_config(data_list): enable = [] disable = [] for item in data_list: # filter if item is disable if not isinstance(item, dict): disable.append(item) continue if not item.__contains__('host'): 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__('username'): 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 def parse_upload_config(self): pass