xmovsystemmonitor 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ Metadata-Version: 2.1
2
+ Name: xmovsystemmonitor
3
+ Version: 0.1.0
4
+ Summary: A Python library for xmov systemmonitor
5
+ Author-email: jiangbin <07jiangbin@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://git.xmov.ai/jiangbin/xmovsystemmonitor
8
+ Project-URL: Repository, https://git.xmov.ai/jiangbin/xmovsystemmonitor
9
+ Project-URL: Issues, https://git.xmov.ai/jiangbin/xmovsystemmonitor
10
+ Keywords: python,xmov,systemmonitor
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.6
17
+ Classifier: Programming Language :: Python :: 3.7
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Requires-Python: >=3.6
25
+ Description-Content-Type: text/markdown
26
+ Requires-Dist: psutil
27
+ Requires-Dist: fastapi
28
+ Requires-Dist: uvicorn
29
+ Provides-Extra: dev
30
+ Requires-Dist: twine; extra == "dev"
31
+ Requires-Dist: pip-tools; extra == "dev"
32
+ Requires-Dist: build; extra == "dev"
33
+ Requires-Dist: pytest>=6.0; extra == "dev"
34
+ Requires-Dist: pytest-cov>=2.0; extra == "dev"
35
+ Requires-Dist: black>=22.0; extra == "dev"
36
+ Requires-Dist: flake8>=4.0; extra == "dev"
37
+ Requires-Dist: mypy>=0.900; extra == "dev"
38
+
39
+
40
+ # systemmonitor
41
+
42
+ 一个用于systemmonitor的工具,可以读取systemmonitor的日志文件,并按照时间顺序执行systemmonitor请求。
43
+
44
+ ## Features
45
+
46
+
47
+ ## 安装
48
+
49
+ ```bash
50
+ # 本地
51
+ pip install systemmonitor -i https://pypi.org/simple/
52
+ # 阿里云
53
+ pip install systemmonitor -i https://pypi.org/simple/
54
+ ```
55
+
56
+ ## 用法
57
+
58
+ ```bash
59
+ python -m systemmonitor <log_file>
60
+ ```
61
+
62
+
63
+ ### 开发配置
64
+
65
+ ```bash
66
+ # 克隆仓库
67
+ git clone git@github.com:atanx/systemmonitor.git
68
+ cd systemmonitor
69
+
70
+ # 安装开发依赖
71
+ pip install -e ".[dev]"
72
+
73
+ # 手动修改修改__init__.py中的__version__, 然后打包
74
+ make build
75
+
76
+ # 上传到xmov-pypi, 需要安装twine, 配置~/.pypirc
77
+ make upload
78
+ ```
79
+
80
+
@@ -0,0 +1,42 @@
1
+
2
+ # systemmonitor
3
+
4
+ 一个用于systemmonitor的工具,可以读取systemmonitor的日志文件,并按照时间顺序执行systemmonitor请求。
5
+
6
+ ## Features
7
+
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ # 本地
13
+ pip install systemmonitor -i https://pypi.org/simple/
14
+ # 阿里云
15
+ pip install systemmonitor -i https://pypi.org/simple/
16
+ ```
17
+
18
+ ## 用法
19
+
20
+ ```bash
21
+ python -m systemmonitor <log_file>
22
+ ```
23
+
24
+
25
+ ### 开发配置
26
+
27
+ ```bash
28
+ # 克隆仓库
29
+ git clone git@github.com:atanx/systemmonitor.git
30
+ cd systemmonitor
31
+
32
+ # 安装开发依赖
33
+ pip install -e ".[dev]"
34
+
35
+ # 手动修改修改__init__.py中的__version__, 然后打包
36
+ make build
37
+
38
+ # 上传到xmov-pypi, 需要安装twine, 配置~/.pypirc
39
+ make upload
40
+ ```
41
+
42
+
@@ -0,0 +1,70 @@
1
+
2
+ [build-system]
3
+ requires = ["setuptools>=45", "wheel"]
4
+ build-backend = "setuptools.build_meta"
5
+
6
+ [project]
7
+ name = "xmovsystemmonitor"
8
+ dynamic = ["version"]
9
+ description = "A Python library for xmov systemmonitor"
10
+ readme = "README.md"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "jiangbin", email = "07jiangbin@gmail.com" }]
13
+ keywords = ["python", "xmov", "systemmonitor"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.6",
21
+ "Programming Language :: Python :: 3.7",
22
+ "Programming Language :: Python :: 3.8",
23
+ "Programming Language :: Python :: 3.9",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Topic :: Software Development :: Libraries :: Python Modules",
28
+ ]
29
+ requires-python = ">=3.6"
30
+ dependencies = ["psutil", "fastapi", "uvicorn"]
31
+
32
+ [project.optional-dependencies]
33
+ dev = [
34
+ "twine",
35
+ "pip-tools",
36
+ "build",
37
+ "pytest>=6.0",
38
+ "pytest-cov>=2.0",
39
+ "black>=22.0",
40
+ "flake8>=4.0",
41
+ "mypy>=0.900",
42
+ ]
43
+
44
+ [project.urls]
45
+ Homepage = "https://git.xmov.ai/jiangbin/xmovsystemmonitor"
46
+ Repository = "https://git.xmov.ai/jiangbin/xmovsystemmonitor"
47
+ Issues = "https://git.xmov.ai/jiangbin/xmovsystemmonitor"
48
+
49
+ [tool.setuptools.packages.find]
50
+ where = ["."]
51
+ include = ["xmovsystemmonitor*"]
52
+
53
+ [tool.black]
54
+ line-length = 88
55
+ target-version = ['py38']
56
+
57
+ [tool.pytest.ini_options]
58
+ testpaths = ["tests"]
59
+ python_files = ["test_*.py"]
60
+ python_classes = ["Test*"]
61
+ python_functions = ["test_*"]
62
+
63
+ [tool.mypy]
64
+ python_version = "3.8"
65
+ warn_return_any = true
66
+ warn_unused_configs = true
67
+ disallow_untyped_defs = true
68
+
69
+ [tool.setuptools.dynamic]
70
+ version = { attr = "xmovsystemmonitor.__version__" }
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,31 @@
1
+ import pytest
2
+ import requests
3
+
4
+ def test_get_system_info():
5
+ response = requests.get("http://localhost:8000/")
6
+ print(response.json())
7
+ assert response.status_code == 200
8
+ assert "cpu_percent" in response.json()
9
+ assert "memory_percent" in response.json()
10
+
11
+ def test_start_monitoring():
12
+ response = requests.get("http://localhost:8000/start-monitoring")
13
+ print(response.json())
14
+ assert response.status_code == 200
15
+ assert "message" in response.json()
16
+ assert response.json()["message"] == "监控已启动"
17
+
18
+ def test_stop_monitoring():
19
+ response = requests.get("http://localhost:8000/stop-monitoring")
20
+ print(response.json())
21
+ assert response.status_code == 200
22
+ assert "message" in response.json()
23
+ assert response.json()["message"] == "监控已停止"
24
+
25
+ def test_statistic():
26
+ response = requests.get("http://localhost:8000/statistic")
27
+ print(response.json())
28
+ assert response.status_code == 200
29
+ assert "cpu_percent" in response.json()
30
+ assert "memory_percent" in response.json()
31
+ assert "data_list" in response.json()
@@ -0,0 +1,5 @@
1
+ """系统监控工具 - 获取CPU和内存使用率"""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ from .system_info import get_system_info
@@ -0,0 +1,45 @@
1
+ """命令行入口 - 显示系统监控信息"""
2
+
3
+ from fastapi import FastAPI
4
+ from fastapi.responses import JSONResponse
5
+ import uvicorn
6
+ import argparse
7
+ from systemmonitor.system_info_monitor_thread import SystemInfoMonitorThread
8
+ from systemmonitor import get_system_info
9
+
10
+ from systemmonitor.logger import logger
11
+
12
+
13
+ parser = argparse.ArgumentParser()
14
+ parser.add_argument("--host", type=str, default="0.0.0.0")
15
+ parser.add_argument("--port", type=int, default=8000)
16
+ args = parser.parse_args()
17
+
18
+ app = FastAPI()
19
+ background_thread = SystemInfoMonitorThread(interval=1.0)
20
+ background_thread.start()
21
+
22
+ @app.get("/")
23
+ def read_root():
24
+ info = get_system_info()
25
+ return JSONResponse(content=info)
26
+
27
+ @app.get("/start-monitoring")
28
+ def start_monitoring():
29
+ """启动监控"""
30
+ background_thread.start_to_monitor()
31
+ return JSONResponse(content={"message": "监控已启动"})
32
+
33
+ @app.get("/stop-monitoring")
34
+ def stop_monitoring():
35
+ """停止监控"""
36
+ background_thread.stop_to_monitor()
37
+ return JSONResponse(content={"message": "监控已停止"})
38
+
39
+ @app.get("/statistic")
40
+ def statistic():
41
+ """获取监控统计信息"""
42
+ return JSONResponse(content=background_thread.statistic())
43
+
44
+ if __name__ == "__main__":
45
+ uvicorn.run(app, host="0.0.0.0", port=8000)
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+
3
+ import logging
4
+
5
+ def get_logger(name: str, level: int = logging.INFO, filename: str = "systemmonitor.log"):
6
+ logger = logging.getLogger(name)
7
+ logger.setLevel(level)
8
+ console_handler = logging.StreamHandler()
9
+ file_handler = logging.FileHandler(filename) if filename != "" else None
10
+ console_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
11
+ file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
12
+ logger.addHandler(console_handler)
13
+ logger.addHandler(file_handler)
14
+ return logger
15
+
16
+
17
+ logger = get_logger(__name__, level=logging.INFO, filename="systemmonitor.log")
@@ -0,0 +1,38 @@
1
+
2
+ import psutil
3
+
4
+
5
+ def get_cpu_percent(interval: float = 1.0) -> float:
6
+ """
7
+ 获取CPU使用率百分比
8
+
9
+ Args:
10
+ interval: 采样间隔时间(秒),默认1秒
11
+
12
+ Returns:
13
+ CPU使用率百分比(0-100)
14
+ """
15
+ return psutil.cpu_percent(interval=interval)
16
+
17
+
18
+ def get_memory_percent() -> float:
19
+ """
20
+ 获取内存使用率百分比
21
+
22
+ Returns:
23
+ 内存使用率百分比(0-100)
24
+ """
25
+ return psutil.virtual_memory().percent
26
+
27
+
28
+ def get_system_info() -> dict:
29
+ """
30
+ 获取系统信息(CPU和内存使用率)
31
+
32
+ Returns:
33
+ 包含CPU和内存使用率的字典
34
+ """
35
+ return {
36
+ "cpu_percent": get_cpu_percent(),
37
+ "memory_percent": get_memory_percent(),
38
+ }
@@ -0,0 +1,82 @@
1
+ # coding: utf-8
2
+
3
+ import threading
4
+ import time
5
+ import logging
6
+ from dataclasses import dataclass
7
+
8
+ from starlette.background import P
9
+ from .system_info import get_cpu_percent, get_memory_percent
10
+ from systemmonitor.logger import logger
11
+
12
+ @dataclass
13
+ class SystemInfo:
14
+ cpu_percent: float
15
+ memory_percent: float
16
+ timestamp: float
17
+
18
+ def to_dict(self):
19
+ return {
20
+ "cpu_percent": self.cpu_percent,
21
+ "memory_percent": self.memory_percent,
22
+ "timestamp": self.timestamp,
23
+ }
24
+
25
+
26
+ class SystemInfoCollector:
27
+ def __init__(self):
28
+ self.system_info_list =[]
29
+
30
+ def collect(self):
31
+ self.system_info_list.append(SystemInfo(get_cpu_percent(), get_memory_percent(), time.time()))
32
+
33
+ def get_system_info_list(self):
34
+ return self.system_info_list
35
+
36
+ def reset_system_info_list(self):
37
+ self.system_info_list.clear()
38
+
39
+ def statistic(self):
40
+ return {
41
+ "cpu_percent": {
42
+ "min": min(system_info.cpu_percent for system_info in self.system_info_list),
43
+ "max": max(system_info.cpu_percent for system_info in self.system_info_list),
44
+ "avg": sum(system_info.cpu_percent for system_info in self.system_info_list) / len(self.system_info_list),
45
+ },
46
+ "memory_percent": {
47
+ "min": min(system_info.memory_percent for system_info in self.system_info_list),
48
+ "max": max(system_info.memory_percent for system_info in self.system_info_list),
49
+ "avg": sum(system_info.memory_percent for system_info in self.system_info_list) / len(self.system_info_list),
50
+ },
51
+ "data_list": [system_info.to_dict() for system_info in self.system_info_list],
52
+ }
53
+
54
+
55
+ class SystemInfoMonitorThread(threading.Thread):
56
+ def __init__(self, interval: float = 1.0):
57
+ super().__init__()
58
+ self.running = False
59
+ self.interval = interval
60
+ self.system_info_collector = SystemInfoCollector()
61
+
62
+ def run(self):
63
+ while True:
64
+ if not self.running:
65
+ time.sleep(self.interval)
66
+ logger.info("监控暂停中...")
67
+ continue
68
+ self.system_info_collector.collect()
69
+ latest_system_info: SystemInfo = self.system_info_collector.get_system_info_list()[-1]
70
+ logger.info(f"监控中, 系统信息: {latest_system_info.to_dict()}")
71
+ time.sleep(self.interval)
72
+
73
+ def start_to_monitor(self):
74
+ self.running = True
75
+ logger.info("监控已重置,开始监控")
76
+
77
+ def stop_to_monitor(self):
78
+ self.running = False
79
+ logger.info("监控已停止")
80
+
81
+ def statistic(self):
82
+ return self.system_info_collector.statistic()
@@ -0,0 +1,80 @@
1
+ Metadata-Version: 2.1
2
+ Name: xmovsystemmonitor
3
+ Version: 0.1.0
4
+ Summary: A Python library for xmov systemmonitor
5
+ Author-email: jiangbin <07jiangbin@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://git.xmov.ai/jiangbin/xmovsystemmonitor
8
+ Project-URL: Repository, https://git.xmov.ai/jiangbin/xmovsystemmonitor
9
+ Project-URL: Issues, https://git.xmov.ai/jiangbin/xmovsystemmonitor
10
+ Keywords: python,xmov,systemmonitor
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.6
17
+ Classifier: Programming Language :: Python :: 3.7
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Requires-Python: >=3.6
25
+ Description-Content-Type: text/markdown
26
+ Requires-Dist: psutil
27
+ Requires-Dist: fastapi
28
+ Requires-Dist: uvicorn
29
+ Provides-Extra: dev
30
+ Requires-Dist: twine; extra == "dev"
31
+ Requires-Dist: pip-tools; extra == "dev"
32
+ Requires-Dist: build; extra == "dev"
33
+ Requires-Dist: pytest>=6.0; extra == "dev"
34
+ Requires-Dist: pytest-cov>=2.0; extra == "dev"
35
+ Requires-Dist: black>=22.0; extra == "dev"
36
+ Requires-Dist: flake8>=4.0; extra == "dev"
37
+ Requires-Dist: mypy>=0.900; extra == "dev"
38
+
39
+
40
+ # systemmonitor
41
+
42
+ 一个用于systemmonitor的工具,可以读取systemmonitor的日志文件,并按照时间顺序执行systemmonitor请求。
43
+
44
+ ## Features
45
+
46
+
47
+ ## 安装
48
+
49
+ ```bash
50
+ # 本地
51
+ pip install systemmonitor -i https://pypi.org/simple/
52
+ # 阿里云
53
+ pip install systemmonitor -i https://pypi.org/simple/
54
+ ```
55
+
56
+ ## 用法
57
+
58
+ ```bash
59
+ python -m systemmonitor <log_file>
60
+ ```
61
+
62
+
63
+ ### 开发配置
64
+
65
+ ```bash
66
+ # 克隆仓库
67
+ git clone git@github.com:atanx/systemmonitor.git
68
+ cd systemmonitor
69
+
70
+ # 安装开发依赖
71
+ pip install -e ".[dev]"
72
+
73
+ # 手动修改修改__init__.py中的__version__, 然后打包
74
+ make build
75
+
76
+ # 上传到xmov-pypi, 需要安装twine, 配置~/.pypirc
77
+ make upload
78
+ ```
79
+
80
+
@@ -0,0 +1,13 @@
1
+ README.md
2
+ pyproject.toml
3
+ tests/test_api.py
4
+ xmovsystemmonitor/__init__.py
5
+ xmovsystemmonitor/__main__.py
6
+ xmovsystemmonitor/logger.py
7
+ xmovsystemmonitor/system_info.py
8
+ xmovsystemmonitor/system_info_monitor_thread.py
9
+ xmovsystemmonitor.egg-info/PKG-INFO
10
+ xmovsystemmonitor.egg-info/SOURCES.txt
11
+ xmovsystemmonitor.egg-info/dependency_links.txt
12
+ xmovsystemmonitor.egg-info/requires.txt
13
+ xmovsystemmonitor.egg-info/top_level.txt
@@ -0,0 +1,13 @@
1
+ psutil
2
+ fastapi
3
+ uvicorn
4
+
5
+ [dev]
6
+ twine
7
+ pip-tools
8
+ build
9
+ pytest>=6.0
10
+ pytest-cov>=2.0
11
+ black>=22.0
12
+ flake8>=4.0
13
+ mypy>=0.900
@@ -0,0 +1 @@
1
+ xmovsystemmonitor