| 548 | |
| 549 | |
| 550 | class LogServerManager(object): |
| 551 | _CONFIG_FILE = "{}/data/logserver_config.json".format(public.get_panel_path()) |
| 552 | _SOURCE_MAP = { |
| 553 | "site": SiteLogSource, |
| 554 | "syslog": SysLogSource, |
| 555 | } |
| 556 | _LOG_SERVER_MAP: Dict[str, BaseLogServer] = { |
| 557 | "bt_log_server": BTLogServer, |
| 558 | } |
| 559 | |
| 560 | def __init__(self): |
| 561 | self._config: Optional[Dict[str, List[dict]]] = None |
| 562 | self.last_server = None |
| 563 | |
| 564 | @property |
| 565 | def config(self) -> Dict[str, List[dict]]: |
| 566 | if self._config is not None: |
| 567 | return self._config |
| 568 | default_conf = {} |
| 569 | try: |
| 570 | self._config = json.loads(public.readFile(self._CONFIG_FILE)) |
| 571 | except (json.JSONDecodeError, TypeError): |
| 572 | pass |
| 573 | if isinstance(self._config, dict): |
| 574 | return self._config |
| 575 | |
| 576 | self._config = default_conf |
| 577 | return self._config |
| 578 | |
| 579 | def save_conf(self): |
| 580 | if self._config: |
| 581 | public.writeFile(self._CONFIG_FILE, json.dumps(self._config)) |
| 582 | |
| 583 | def add_logserver(self, server_type: str, data: dict) -> Tuple[bool, str]: |
| 584 | if server_type not in self._LOG_SERVER_MAP: |
| 585 | return False, "不支持的日志服务系统" |
| 586 | |
| 587 | try: |
| 588 | s = self._LOG_SERVER_MAP[server_type].new(data) |
| 589 | except: |
| 590 | return False, "配置信息错误" |
| 591 | else: |
| 592 | if isinstance(s, str): |
| 593 | return False, s |
| 594 | if s.check_exist(self) is True: |
| 595 | return False, "该配置已存在,请勿重新添加" |
| 596 | |
| 597 | data["id"] = str(int(time.time())) |
| 598 | data["server_type"] = server_type |
| 599 | if "servers" not in self.config: |
| 600 | self.config["servers"] = [] |
| 601 | |
| 602 | self.config["servers"].append(data) |
| 603 | self.save_conf() |
| 604 | self.last_server = data |
| 605 | return True, "配置添加成功" |
| 606 | |
| 607 | def modify_logserver(self, server_id: str, data: dict) -> Tuple[bool, str]: |
no test coverage detected