Skip to content

Commit

Permalink
Merge pull request #2 from alunAlunnnnn/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
alunAlunnnnn authored Oct 22, 2021
2 parents a920fd9 + e46c78d commit cb93c88
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 66 deletions.
54 changes: 43 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,86 @@

​ 当前版本为 v0.1,在 win 10 19042、winserver 2019 中测试可用。并且 linux_py36 分支提供对 linux 的支持,因为适配环境为 centos7.9 的 py36 所以在性能上略有下降

 

​ 目前仅支持写入 sqlite 数据库,后续会添加对 mysql、pgsql 的支持

 

​ 当前的 linux 适配版仅为过渡使用,后续会基于 python 3.9 及 python 3.9 devel 进行完整性能版的适配

​ 目前仅收集 CPU、RAM、DISK I/O 的信息,后续补充网络 I/O
 

​ 目前仅收集 CPU、RAM、DISK I/O 的信息,后续补充网络 I/O


<br>
<br>
<br>
<br>


注意事项:

&nbsp;

1、受限于硬盘和cpu使用率计算逻辑的影响,最小的收集间隔为 3 秒,小于 3 秒的设置不会报错,但是无法生效

2、每次收集的数据插入 sqlite 后会占大约 1.5kb 的空间,所以当长时间监控,并且收集间隔很短的时候,一定要注意空间占用的问题
&nbsp;

2、每次收集的数据插入 sqlite 后会占大约 1.5kb 的空间,所以当长时间监控,并且收集间隔很短的时候,一定要注意空间占用的问题


<br>
<br>
<br>
<br>


使用方法:

1、初始化 sqlite 数据库

> hs_system_monitor.exe init -d D:\systemonitor\example.db
> hs_system_monitor.exe init -d D:/systemonitor/example.db
&nbsp;

2、单次收集数据

>hs_system_monitor.exe run -d D:/systemonitor/example.db.db
>hs_system_monitor.exe run -d D:/systemonitor/example.db
&nbsp;

3、持续收集数据(默认间隔30秒)

> hs_system_monitor.exe run -d D:/systemonitor/example.db.db -l
> hs_system_monitor.exe run -d D:/systemonitor/example.db -l
&nbsp;

4、持续收集数据(自定义间隔 此处为10秒)

> hs_system_monitor.exe run -d D:/systemonitor/example.db.db -l -s 10
> hs_system_monitor.exe run -d D:/systemonitor/example.db -l -s 10
&nbsp;

源码打包:

>pyinstaller -F --distpath ./release --workpath ./release/build -i img\hispatial.ico -n hs_system_monitor sys_monitor_cli.py

<br>
<br>
<br>
<br>


可能出现的问题:

1、若可以成功初始化数据库,但是无法运行,并且提示 Error message [WinError 21] 设备未就绪。: 'Z' 。

出错原因:电脑或服务器不具有 cd/dvd 驱动器,但是操作系统默认添加了一个 cd/dvd 驱动器,导致无法正常收集该设备的相关信息

解决方法:设备管理器 ---> 卸载设备。卸载对应驱动器的设备


源码打包:

>pyinstaller -F --distpath ./release --workpath ./release/build -i img\hispatial.ico -n hs_system_monitor sys_monitor_cli.py
Binary file modified __pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/_get_sys_information.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/_initial_sqlite.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/_update_monitor_value.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/sys_monitor_cli.cpython-39.pyc
Binary file not shown.
37 changes: 30 additions & 7 deletions _get_sys_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def _get_cpu_status(start_time):
cpu_status = {}

cpu_use = psutil.cpu_percent(interval=1, percpu=True)
# get cpu use maximum
cpu_use_max = max(cpu_use)
cpu_use_avg = cpu_use_max / cpu_use.__len__()
# get cpu use average
cpu_use_avg = sum(cpu_use) / cpu_use.__len__()

cpu_use_json = {}
for i, each_cpu_use in enumerate(cpu_use):
Expand Down Expand Up @@ -83,13 +85,15 @@ def _get_mem_status(start_time):


@lru_cache(60)
def _get_disk_status(start_time):
def _get_disk_net_status(start_time):
# print("func _get_disk_status")
disk_status_res = {}

# get disk read/write capacity with dict type
disk_io_origin = psutil.disk_io_counters(perdisk=True)
net_io_origin = psutil.net_io_counters()
time.sleep(1)
net_io = psutil.net_io_counters()
disk_io = psutil.disk_io_counters(perdisk=True)

# calculate disk io per second
Expand All @@ -108,7 +112,26 @@ def _get_disk_status(start_time):
"unit": unit
}

return disk_status_res
# network i/o and package i/o
net_status = _net_io_calculate(net_io_origin, net_io)

return disk_status_res, net_status


def _net_io_calculate(net_io_origin, net_io):
net_send = _byte_convert(net_io.bytes_sent - net_io_origin.bytes_sent, "m")
net_receive = _byte_convert(net_io.bytes_recv - net_io_origin.bytes_recv, "m")
net_package_send = net_io.packets_sent - net_io_origin.packets_sent
net_package_receive = net_io.packets_recv - net_io_origin.packets_recv

net_status = {
"net_send": net_send,
"net_receive": net_receive,
"net_package_send": net_package_send,
"net_package_receive": net_package_receive
}

return net_status


@lru_cache(60)
Expand Down Expand Up @@ -156,18 +179,18 @@ def get_sys_info():
# get system status info and insert into table
cpu_status = _get_cpu_status(start)
mem_status = _get_mem_status(start)
disk_status = _get_disk_status(start)
disk_status, net_status = _get_disk_net_status(start)
# print("cpu status: ", cpu_status)
# print("mem status: ", mem_status)
# print("disk status: ", disk_status)

finish = time.time()
# finish = time.time()
# print("finish: ", finish)

cost = finish - start
# cost = finish - start
# print("cost: ", cost)

return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start)), hard_info, cpu_status, mem_status, disk_status
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start)), hard_info, cpu_status, mem_status, disk_status, net_status


if __name__ == '__main__':
Expand Down
158 changes: 122 additions & 36 deletions _initial_sqlite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,119 @@
from sys_monitor.utils._db_sqlite import SqliteDB

# config = {
# "sqlite": {
# "tables":
# {
# "t_hs_sys_monitor": [
# {
# "name": "time",
# "type": "text"
# },
# {
# "name": "cpu_use_avg",
# "type": "float"
# },
# {
# "name": "cpu_use_max",
# "type": "float"
# },
# {
# "name": "cpu_use_percore",
# "type": "text"
# },
# {
# "name": "mem_total",
# "type": "float"
# },
# {
# "name": "mem_use_size",
# "type": "float"
# },
# {
# "name": "mem_free",
# "type": "float"
# },
# {
# "name": "mem_use",
# "type": "float"
# },
# {
# "name": "swap_total",
# "type": "float"
# },
# {
# "name": "swap_use_size",
# "type": "float"
# },
# {
# "name": "swap_free",
# "type": "float"
# },
# {
# "name": "swap_use",
# "type": "float"
# },
# {
# "name": "unit",
# "type": "text"
# },
# {
# "name": "disk_io",
# "type": "text"
# }
# ],
# "t_hs_hard_info": [
# {
# "name": "time",
# "type": "text"
# },
# {
# "name": "cpu_physical",
# "type": "integer"
# },
# {
# "name": "cpu_logical",
# "type": "integer"
# },
# {
# "name": "mem_total",
# "type": "float"
# },
# {
# "name": "mem_use_size",
# "type": "float"
# },
# {
# "name": "mem_free",
# "type": "float"
# },
# {
# "name": "swap_total",
# "type": "float"
# },
# {
# "name": "swap_use_size",
# "type": "float"
# },
# {
# "name": "swap_free",
# "type": "float"
# },
# {
# "name": "disk_info",
# "type": "text"
# },
# {
# "name": "unit",
# "type": "text"
# }
# ]
# }
# },
# "postgresql": {},
# "mysql": {}
# }

config = {
"sqlite": {
"tables":
Expand Down Expand Up @@ -60,54 +174,25 @@
{
"name": "disk_io",
"type": "text"
}
],
"t_hs_hard_info": [
{
"name": "time",
"type": "text"
},
{
"name": "cpu_physical",
"type": "integer"
},
{
"name": "cpu_logical",
"type": "integer"
},
{
"name": "mem_total",
"name": "net_receive",
"type": "float"
},
{
"name": "mem_use_size",
"name": "net_send",
"type": "float"
},
{
"name": "mem_free",
"name": "net_package_receive",
"type": "float"
},
{
"name": "swap_total",
"name": "net_package_send",
"type": "float"
},
{
"name": "swap_use_size",
"type": "float"
},
{
"name": "swap_free",
"type": "float"
},
{
"name": "disk_info",
"type": "text"
},
{
"name": "unit",
"type": "text"
}
]

],
}
},
"postgresql": {},
Expand All @@ -130,4 +215,5 @@ def init_sqlite(db_file):


if __name__ == '__main__':
init_sqlite("D:/systemonitor/example.db")
# init_sqlite("D:/systemonitor/example.db")
init_sqlite("D:/systemonitor/example_new.db")
Loading

0 comments on commit cb93c88

Please sign in to comment.