From dc1fc2b2cb8d804f537c72a6fe4957883fe66b8d Mon Sep 17 00:00:00 2001 From: wunder957 Date: Mon, 18 Sep 2023 14:37:24 +0800 Subject: [PATCH] Add subprocess monitor --- duetector/monitors/subprocess_monitor.py | 54 ++++++++++++++++++++++++ duetector/tracers/__init__.py | 4 +- duetector/tracers/base.py | 4 ++ duetector/tracers/subprocess/__init__.py | 0 tests/test_sp_monitor.py | 0 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 duetector/monitors/subprocess_monitor.py create mode 100644 duetector/tracers/subprocess/__init__.py create mode 100644 tests/test_sp_monitor.py diff --git a/duetector/monitors/subprocess_monitor.py b/duetector/monitors/subprocess_monitor.py new file mode 100644 index 0000000..dc02606 --- /dev/null +++ b/duetector/monitors/subprocess_monitor.py @@ -0,0 +1,54 @@ +from typing import Any, Callable, Dict, List, Optional + +from duetector.collectors.base import Collector +from duetector.log import logger +from duetector.managers.collector import CollectorManager +from duetector.managers.filter import FilterManager +from duetector.managers.tracer import TracerManager +from duetector.monitors import Monitor +from duetector.tracers.base import SubprocessTracer + + +class SubprocessHost: + pass + + +class SubprocessMonitor(Monitor): + default_config = { + **Monitor.default_config, + "auto_init": True, + "timeout": 5, + } + + @property + def auto_init(self): + """ + Auto init tracers when init monitor. + """ + return self.config.auto_init + + @property + def timeout(self): + """ + Timeout for shell command. + """ + return int(self.config.timeout) + + def __init__(self, config: Optional[Dict[str, Any]] = None, *args, **kwargs): + super().__init__(config=config, *args, **kwargs) + if self.disabled: + logger.info("SubprocessMonitor disabled") + self.tracers = [] + self.filters = [] + self.collectors = [] + return + + self.tracers: List[SubprocessTracer] = TracerManager(config).init(tracer_type=SubprocessTracer) # type: ignore + self.filters: List[Callable] = FilterManager(config).init() + self.collectors: List[Collector] = CollectorManager(config).init() + + if self.auto_init: + self.init() + + def init(): + pass diff --git a/duetector/tracers/__init__.py b/duetector/tracers/__init__.py index 5d8875f..8f39e26 100644 --- a/duetector/tracers/__init__.py +++ b/duetector/tracers/__init__.py @@ -1,3 +1,3 @@ -from .base import BccTracer, ShellTracer, Tracer +from .base import BccTracer, ShellTracer, SubprocessTracer, Tracer -__all__ = ["Tracer", "BccTracer", "ShellTracer"] +__all__ = ["Tracer", "BccTracer", "ShellTracer", "SubprocessTracer"] diff --git a/duetector/tracers/base.py b/duetector/tracers/base.py index c462448..edafd9e 100644 --- a/duetector/tracers/base.py +++ b/duetector/tracers/base.py @@ -306,3 +306,7 @@ def set_callback(self, host, callback: Callable[[NamedTuple], None]): Set callback function to host. """ host.set_callback(self, callback) + + +class SubprocessTracer(Tracer): + pass diff --git a/duetector/tracers/subprocess/__init__.py b/duetector/tracers/subprocess/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_sp_monitor.py b/tests/test_sp_monitor.py new file mode 100644 index 0000000..e69de29