Files
Smb-Server/app/services/disk_monitor.py
T
bi c790e43cdf feat: SMB文件服务器管理后台 - 完整版本
- 用户管理:增删改、批量导入(Excel模板)、admin保护
- 部门管理:增删改(含回收站恢复机制)、删除保护(有用户不可删)
- 权限管理:部门内角色(admin/readwrite/readonly/none)
- 监控页面:CPU/内存/磁盘实时监控
- 日志页面:操作日志查询
- SMB权限:用户文件夹700(member)/750(admin)、ACL继承
- admin跨目录访问:通过setfacl ACL实现
2026-07-13 23:03:03 +08:00

72 lines
1.9 KiB
Python

"""磁盘监控服务"""
import os
import json
import subprocess
from app.utils.helpers import run_cmd
DATA_DIR = os.environ.get("DATA_DIR", "/data/smb")
def get_disk_usage() -> dict:
"""获取总磁盘使用情况"""
# df -B1 以字节为单位
rc, stdout, _ = run_cmd(["df", "-B1", DATA_DIR or "/data/smb"])
if rc != 0 or not stdout.strip():
return {
"total_space": 0, "used_space": 0, "free_space": 0,
"usage_percent": 0.0
}
lines = stdout.strip().split("\n")
if len(lines) < 2:
return {"total_space": 0, "used_space": 0, "free_space": 0, "usage_percent": 0.0}
fields = lines[1].split()
total = int(fields[1])
used = int(fields[2])
free = int(fields[3])
pct = float(fields[4].replace("%", ""))
return {
"total_space": total,
"used_space": used,
"free_space": free,
"usage_percent": pct
}
def get_dept_usages() -> list:
"""获取各部门磁盘使用"""
usages = []
if not os.path.exists(DATA_DIR):
return usages
for entry in os.scandir(DATA_DIR):
if entry.is_dir() and entry.name.startswith("dept-"):
dept_name = entry.name[4:] # 去掉 "dept-" 前缀
path = entry.path
rc, stdout, _ = run_cmd(["du", "-sb", path], timeout=60)
if rc == 0:
size = int(stdout.split()[0])
else:
size = 0
usages.append({
"dept_name": dept_name,
"used_bytes": size
})
# 加上 exchange 目录
exchange_path = os.path.join(DATA_DIR, "exchange")
if os.path.exists(exchange_path):
rc, stdout, _ = run_cmd(["du", "-sb", exchange_path], timeout=60)
if rc == 0:
size = int(stdout.split()[0])
else:
size = 0
usages.append({
"dept_name": "exchange",
"used_bytes": size
})
return usages